def test_remove_tag_from_entry(self, monkeypatch): confirm_runned = False def success(self): return Response(200, None) def getentry_request(self): return Response( 200, ( '{"id": 1, "title": "title",' '"content": "<h1>head</h1>content", "url": "url",' '"is_archived": 0, "is_starred": 1,' '"tags": [' '{"id":7,"label":"tag","slug":"tag"},' '{"id":13,"label":"tag2","slug":"tag2"}]}')) def confirm(msg): nonlocal confirm_runned confirm_runned = True assert msg == ( f'{Back.RED}You are going to remove tag ' f'{Fore.BLUE}tag{Fore.RESET} from entry:' f'{Back.RESET}\n\n\ttitle\n\nContinue?') monkeypatch.setattr(GetEntry, 'request', getentry_request) monkeypatch.setattr(DeleteTagFromEntry, 'request', success) monkeypatch.setattr(click, 'confirm', confirm) params = TagsCommandParams(tags='tag', entry_id=1) params.configure(TagsSubcommand.REMOVE) result = TagsCommand(self.config, params).execute() assert confirm_runned assert result[0]
def test_add_tags_to_entry(self, monkeypatch): tag_to_entry_runned = False def request(self): nonlocal tag_to_entry_runned tag_to_entry_runned = True return Response(200, """[]""") monkeypatch.setattr(AddTagToEntry, 'request', request) params = TagsCommandParams(entry_id=10, tags='tag1') params.command = TagsSubcommand.ADD result, msg = TagsCommand(self.config, params).execute() assert tag_to_entry_runned assert result assert msg == 'Tags successfully added'
def test_remove_tag_from_entry_not_found(self, monkeypatch): def getentry_request(self): return Response( 200, ( '{"id": 1, "title": "title",' '"content": "<h1>head</h1>content", "url": "url",' '"is_archived": 0, "is_starred": 1,' '"tags": [' '{"id":7,"label":"tag1","slug":"tag1"},' '{"id":13,"label":"tag2","slug":"tag2"}]}')) monkeypatch.setattr(GetEntry, 'request', getentry_request) params = TagsCommandParams(tags='tag', entry_id=1) params.configure(TagsSubcommand.REMOVE) result = TagsCommand(self.config, params).execute() assert not result[0] assert result[1] == 'Tag "tag" not found in entry:\n\n\ttitle\n'
def test_entry_not_found(self, monkeypatch): def request(self): response = Response(404, None) raise RequestException( response.error_text, response.error_description) monkeypatch.setattr(GetTagsForEntry, 'request', request) params = TagsCommandParams(entry_id=10) result, msg = TagsCommand(self.config, params).execute() assert not result
def test_tags_param(self, monkeypatch, tags): make_request_runned = False def request(self): nonlocal make_request_runned make_request_runned = True return Response(200, """[]""") monkeypatch.setattr(AddTagToEntry, 'request', request) params = TagsCommandParams(entry_id=10, tags=tags[0]) params.configure(TagsSubcommand.ADD) result = TagsCommand(self.config, params).execute() if tags[0]: assert make_request_runned assert result[0] assert result[1] == "Tags successfully added" else: assert not make_request_runned assert not result[0] assert result[1] == "tags value is empty"
def test_remove_tag_by_label(self, monkeypatch, current_tag): confirm_runned = False def success(self): return Response(200, None) def getlist_request(self): return Response( 200, ( '{"_embedded": {"items": [{"id": 1, "title": "title",' '"content": "<h1>head</h1>content", "url": "url",' '"is_archived": 0, "is_starred": 1,' '"tags": [' '{"id":7,"label":"tag","slug":"tag"},' '{"id":13,"label":"tag2","slug":"tag2"}]},' '{"id": 2, "title": "title 2",' '"content": "<h1>head</h1>content", "url": "url",' '"is_archived": 0, "is_starred": 1,' '"tags": [' '{"id":7,"label":"tag","slug":"tag"},' '{"id":13,"label":"tag2","slug":"tag2"}]}]}}')) def confirm(msg): nonlocal confirm_runned confirm_runned = True assert msg == ( f'{Back.RED}You are going to remove tag ' f'{Fore.BLUE}{current_tag}{Fore.RESET} from this entries:' f'{Back.RESET}\n\n\ttitle\n\ttitle 2\n\nContinue?') return True monkeypatch.setattr(GetListEntries, 'request', getlist_request) monkeypatch.setattr(DeleteTagsByLabel, 'request', success) monkeypatch.setattr(click, 'confirm', confirm) params = TagsCommandParams(tags=current_tag) params.configure(TagsSubcommand.REMOVE) result = TagsCommand(self.config, params).execute() assert confirm_runned assert result[0]
def tags(ctx, command, entry_id, tags, tag_id): """ Tag manipulation command. list (default) command: Retrieve and print tags in format: \b {id}. {slug} {id}. {slug} {id}. {slug} If ENTRY_ID specified, make action related to this entry. The ENTRY_ID can be found with `list` command. add command: Add tags to entry. ENTRY_ID and TAGS should be specified. remove command: Remove tag from entry. (ENTRY_ID and TAGS) or TAG_ID should be specified. """ params = TagsCommandParams(entry_id=entry_id, tags=tags, tag_id=tag_id) params.configure(TagsSubcommand.get(command)) run_command(TagsCommand(ctx.obj, params))
def test_list_entry_tags(self, monkeypatch): def request(self): return Response(200, """[ {"id": 3, "label": "book", "slug": "book"}, {"id": 8, "label": "gut", "slug": "gut"}, {"id": 7, "label": "security", "slug": "security"}] """) monkeypatch.setattr(GetTagsForEntry, 'request', request) params = TagsCommandParams(entry_id=10) result, msg = TagsCommand(self.config, params).execute() assert result assert msg == '3. book\n7. security\n8. gut'
def test_remove_tag_by_id(self, monkeypatch, tag_id): confirm_runned = False def success(self): return Response(200, None) def confirm(msg): nonlocal confirm_runned confirm_runned = True assert msg == ( f'{Back.RED}You are going to remove tag with id: ' f'{Fore.BLUE}{int(tag_id)}{Fore.RESET}{Back.RESET}' '\n\nContinue?') return True monkeypatch.setattr(DeleteTagsById, 'request', success) monkeypatch.setattr(click, 'confirm', confirm) params = TagsCommandParams(tag_id=tag_id) params.configure(TagsSubcommand.REMOVE) result = TagsCommand(self.config, params).execute() assert confirm_runned assert result[0]
def test_tags_to_entry_empty_id(self): params = TagsCommandParams(tags='tags') params.configure(TagsSubcommand.ADD) result = TagsCommand(self.config, params).execute() assert not result[0] assert result[1] == 'Entry id not specified'