def test_add_item(self, mock_new): """Verify an item can be imported into an existing document.""" importer.add_item(self.prefix, self.uid) mock_new.assert_called_once_with(self.mock_tree, self.mock_document, self.path, self.root, self.uid, auto=False)
def test_add_item_explicit_document(self, mock_new, mock_get_tree): """Verify an item can be imported into an explicit document.""" mock_document = self.mock_document mock_tree = mock_document.tree mock_document.tree._item_cache = MagicMock() # pylint:disable=W0212 importer.add_item(self.prefix, self.uid, document=mock_document) self.assertFalse(mock_get_tree.called) mock_new.assert_called_once_with(mock_tree, mock_document, self.path, self.root, self.uid, auto=False)
def test_add_item_explicit_document(self, mock_new, mock_get_tree): """Verify an item can be imported into an explicit document.""" mock_document = self.mock_document mock_tree = mock_document.tree mock_document.tree._item_cache = MagicMock() importer.add_item(self.prefix, self.uid, document=mock_document) self.assertFalse(mock_get_tree.called) mock_new.assert_called_once_with( mock_tree, mock_document, self.path, self.root, self.uid, auto=False )
def test_add_item_with_attrs(self): """Verify an item can be imported with attributes.""" attrs = {'text': "The item text.", 'ext': "External attrubte."} item = importer.add_item(self.prefix, self.uid, attrs=attrs) self.assertEqual(self.uid, item.uid) self.assertEqual(attrs['text'], item.text) self.assertEqual(attrs['ext'], item.get('ext'))
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 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