def test_create_document_unknown_parent(self, mock_new): """Verify documents can be created for import with unknown parents.""" importer.create_document(self.prefix, self.path, parent=self.parent) mock_new.assert_called_once_with(self.mock_tree, self.path, self.root, self.prefix, parent=self.parent)
def test_create_document_explicit_tree(self, mock_new, mock_get_tree): """Verify a new document can be created for import (explicit tree).""" mock_document = Mock() mock_document.root = None tree = Tree(document=mock_document) importer.create_document(self.prefix, self.path, tree=tree) self.assertFalse(mock_get_tree.called) mock_new.assert_called_once_with(self.path, self.prefix, parent=None) self.assertIn(mock_document, tree)
def run_import(args, cwd, error, catch=True, _tree=None): """Process arguments and run the `doorstop import` subcommand. :param args: Namespace of CLI arguments :param cwd: current working directory :param error: function to call for CLI errors :param catch: catch and log :class:`~doorstop.common.DoorstopError` """ document = item = None attrs = utilities.literal_eval(args.attrs, error) mapping = utilities.literal_eval(args.map, error) if args.path: if not args.prefix: error("when [path] specified, [prefix] is also required") elif args.document: error("'--document' cannot be used with [path] [prefix]") elif args.item: error("'--item' cannot be used with [path] [prefix]") ext = utilities.get_ext(args, error, None, None) elif not (args.document or args.item): error("specify [path], '--document', or '--item' to import") with utilities.capture(catch=catch) as success: if args.path: # get the document request_next_number = _request_next_number(args) tree = _tree or _get_tree(args, cwd, request_next_number=request_next_number) document = tree.find_document(args.prefix) # import items into it msg = "importing '{}' into document {}...".format(args.path, document) utilities.show(msg, flush=True) importer.import_file(args.path, document, ext, mapping=mapping) elif args.document: prefix, path = args.document document = importer.create_document(prefix, path, parent=args.parent) elif args.item: prefix, uid = args.item request_next_number = _request_next_number(args) item = importer.add_item(prefix, uid, attrs=attrs, request_next_number=request_next_number) if not success: return False if document: utilities.show("imported document: {} ({})".format(document.prefix, document.relpath)) else: assert item utilities.show("imported item: {} ({})".format(item.uid, item.relpath)) return True
def test_create_document_with_parent(self, mock_new): """Verify a new document can be created for import with a parent.""" importer.create_document(self.prefix, self.path, parent=self.parent) mock_new.assert_called_once_with(self.path, self.prefix, parent=self.parent)
def run_import(args, cwd, error, catch=True, _tree=None): """Process arguments and run the `doorstop import` subcommand. :param args: Namespace of CLI arguments :param cwd: current working directory :param error: function to call for CLI errors :param catch: catch and log :class:`~doorstop.common.DoorstopError` """ document = item = None attrs = utilities.literal_eval(args.attrs, error) mapping = utilities.literal_eval(args.map, error) if args.path: if not args.prefix: error("when [path] specified, [prefix] is also required") elif args.document: error("'--document' cannot be used with [path] [prefix]") elif args.item: error("'--item' cannot be used with [path] [prefix]") ext = utilities.get_ext(args, error, None, None) elif not (args.document or args.item): error("specify [path], '--document', or '--item' to import") with utilities.capture(catch=catch) as success: if args.path: # get the document request_next_number = _request_next_number(args) tree = _tree or _get_tree( args, cwd, request_next_number=request_next_number) document = tree.find_document(args.prefix) # import items into it msg = "importing '{}' into document {}...".format( args.path, document) utilities.show(msg, flush=True) importer.import_file(args.path, document, ext, mapping=mapping) elif args.document: prefix, path = args.document document = importer.create_document(prefix, path, parent=args.parent) elif args.item: prefix, uid = args.item request_next_number = _request_next_number(args) item = importer.add_item(prefix, uid, attrs=attrs, request_next_number=request_next_number) if not success: return False if document: utilities.show("imported document: {} ({})".format( document.prefix, document.relpath)) else: assert item utilities.show("imported item: {} ({})".format(item.uid, item.relpath)) return True