async def test_get_all_bookmarks(aresponses): """Test getting recent bookmarks.""" aresponses.add( "api.pinboard.in", "/v1/posts/all", "get", aresponses.Response(text=load_fixture("posts_all_response.xml"), status=200), ) async with ClientSession() as session: api = API(TEST_API_TOKEN, session=session) bookmarks = await api.bookmark.async_get_all_bookmarks( tags=["tag1"], start=2, results=1, from_dt=datetime.now(), to_dt=datetime.now() + timedelta(days=2), ) assert len(bookmarks) == 1 assert bookmarks[0] == Bookmark( "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "https://mylink.com", "A really neat website!", "I saved this bookmark to Pinboard", pytz.utc.localize(datetime(2020, 9, 2, 3, 59, 55)), tags=["tag1", "tag2"], unread=True, shared=False, )
async def test_add_bookmark(aresponses): """Test deleting a bookmark.""" aresponses.add( "api.pinboard.in", "/v1/posts/add", "get", aresponses.Response(text=load_fixture("posts_add_response.xml"), status=200), ) async with ClientSession() as session: api = API(TEST_API_TOKEN, session=session) # A unsuccessful request will throw an exception, so if no exception is thrown, # we can count this as a successful test: await api.bookmark.async_add_bookmark( "http://test.url", "My Test Bookmark", description="I like this bookmark", tags=["tag1", "tag2"], created_datetime=datetime.now(), replace=True, shared=True, toread=True, )
async def main() -> None: """Create the aiohttp session and run the example.""" logging.basicConfig(level=logging.INFO) api = API("<PINBOARD_API_TOKEN>") try: last_change_dt = await api.async_get_last_change_datetime() _LOGGER.info(last_change_dt) except RequestError as err: _LOGGER.info(err)
async def test_http_error(aresponses): """Test that an HTTP error is handled properly.""" aresponses.add( "api.pinboard.in", "/v1/posts/delete", "get", aresponses.Response(text=None, status=500), ) async with ClientSession() as session: api = API(TEST_API_TOKEN, session=session) with pytest.raises(RequestError): await api.bookmark.async_delete_bookmark("http://test.url")
async def test_data_error(aresponses): """Test that a Pinboard data error is handled properly.""" aresponses.add( "api.pinboard.in", "/v1/posts/delete", "get", aresponses.Response(text=load_fixture("error_response.xml"), status=200), ) async with ClientSession() as session: api = API(TEST_API_TOKEN, session=session) with pytest.raises(RequestError) as err: await api.bookmark.async_delete_bookmark("http://test.url") assert str(err.value) == "item not found"
async def test_get_tags(aresponses): """Test getting tags.""" aresponses.add( "api.pinboard.in", "/v1/tags/get", "get", aresponses.Response(text=load_fixture("tags_get_response.xml"), status=200), ) async with ClientSession() as session: api = API(TEST_API_TOKEN, session=session) tags = await api.tag.async_get_tags() assert tags == {"tag1": 3, "tag2": 1, "tag3": 2}
async def test_rename_tag(aresponses): """Test renaming a tag.""" aresponses.add( "api.pinboard.in", "/v1/tags/rename", "get", aresponses.Response(text=load_fixture("tags_rename_response.xml"), status=200), ) async with ClientSession() as session: api = API(TEST_API_TOKEN, session=session) # A unsuccessful request will throw an exception, so if no exception is thrown, # we can count this as a successful test: await api.tag.async_rename_tag("tag1", "new-tag1")
async def test_delete_bookmark(aresponses): """Test deleting a bookmark.""" aresponses.add( "api.pinboard.in", "/v1/posts/delete", "get", aresponses.Response(text=load_fixture("posts_delete_response.xml"), status=200), ) async with ClientSession() as session: api = API(TEST_API_TOKEN, session=session) # A unsuccessful request will throw an exception, so if no exception is thrown, # we can count this as a successful test: await api.bookmark.async_delete_bookmark("http://test.url")
async def test_get_last_change_datetime(aresponses): """Test getting the last time a bookmark was altered.""" aresponses.add( "api.pinboard.in", "/v1/posts/update", "get", aresponses.Response(text=load_fixture("posts_update_response.xml"), status=200), ) async with ClientSession() as session: api = API(TEST_API_TOKEN, session=session) most_recent_dt = await api.bookmark.async_get_last_change_datetime() assert most_recent_dt == pytz.utc.localize( datetime(2020, 9, 3, 13, 7, 19))
async def test_get_suggested_tags(aresponses): """Test getting recent bookmarks.""" aresponses.add( "api.pinboard.in", "/v1/posts/suggest", "get", aresponses.Response(text=load_fixture("posts_suggest_response.xml"), status=200), ) async with ClientSession() as session: api = API(TEST_API_TOKEN, session=session) tags = await api.bookmark.async_get_suggested_tags("https://mylink.com" ) assert tags == { "popular": ["security"], "recommended": ["ssh", "linux"], }
async def test_get_dates(aresponses): """Test getting bookmarks by date.""" aresponses.add( "api.pinboard.in", "/v1/posts/dates", "get", aresponses.Response(text=load_fixture("posts_dates_response.xml"), status=200), ) async with ClientSession() as session: api = API(TEST_API_TOKEN, session=session) dates = await api.bookmark.async_get_dates(tags=["tag1", "tag2"]) assert dates == { maya.parse("2020-09-05").datetime().date(): 1, maya.parse("2020-09-04").datetime().date(): 1, maya.parse("2020-09-03").datetime().date(): 3, }
async def test_get_bookmarks_by_date(aresponses): """Test getting bookmarks by date.""" aresponses.add( "api.pinboard.in", "/v1/posts/get", "get", aresponses.Response(text=load_fixture("posts_get_response.xml"), status=200), ) aresponses.add( "api.pinboard.in", "/v1/posts/get", "get", aresponses.Response(text=load_fixture("posts_get_empty_response.xml"), status=200), ) async with ClientSession() as session: api = API(TEST_API_TOKEN, session=session) bookmarks = await api.bookmark.async_get_bookmarks_by_date( pytz.utc.localize(datetime(2020, 9, 3, 13, 7, 19))) assert len(bookmarks) == 1 assert bookmarks[0] == Bookmark( "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "https://mylink.com", "A really neat website!", "I saved this bookmark to Pinboard", pytz.utc.localize(datetime(2020, 9, 2, 3, 59, 55)), tags=["tag1", "tag2"], unread=True, shared=False, ) bookmarks = await api.bookmark.async_get_bookmarks_by_date( pytz.utc.localize(datetime(2020, 9, 3, 13, 7, 19)), tags=["non-tag1"]) assert not bookmarks
async def test_get_notes(aresponses): """Test getting notes.""" aresponses.add( "api.pinboard.in", "/v1/notes/list", "get", aresponses.Response(text=load_fixture("notes_get_response.xml"), status=200), ) async with ClientSession() as session: api = API(TEST_API_TOKEN, session=session) notes = await api.note.async_get_notes() assert len(notes) == 1 assert notes[0] == Note( "xxxxxxxxxxxxxxxxxxxx", "Test", "xxxxxxxxxxxxxxxxxxxx", pytz.utc.localize(datetime(2020, 9, 6, 5, 59, 47)), pytz.utc.localize(datetime(2020, 9, 6, 5, 59, 47)), 14, )