Skip to content

Commit

Permalink
cleanup tests a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
sissbruecker committed Oct 2, 2024
1 parent 1b5cce6 commit 4fcea39
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 54 deletions.
2 changes: 1 addition & 1 deletion bookmarks/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def setup_bookmark(
if added is None:
added = timezone.now()
if modified is None:
modified = timezone.now()
modified = timezone.now()
bookmark = Bookmark(
url=url,
title=title,
Expand Down
60 changes: 32 additions & 28 deletions bookmarks/tests/test_exporter.py
Original file line number Diff line number Diff line change
@@ -1,89 +1,93 @@
from datetime import datetime, timezone

from django.test import TestCase
from django.utils import timezone

from bookmarks.services import exporter
from bookmarks.tests.helpers import BookmarkFactoryMixin


class ExporterTestCase(TestCase, BookmarkFactoryMixin):
def test_export_bookmarks(self):
added = timezone.now()
timestamp_added = int(added.timestamp())
modified = timezone.now()
timestamp_modified = int(modified.timestamp())

bookmarks = [
self.setup_bookmark(
url="https://example.com/1",
title="Title 1",
added=added,
modified=modified,
added=datetime.fromtimestamp(1, timezone.utc),
modified=datetime.fromtimestamp(11, timezone.utc),
description="Example description",
),
self.setup_bookmark(
url="https://example.com/2",
title="Title 2",
added=added,
modified=modified,
added=datetime.fromtimestamp(2, timezone.utc),
modified=datetime.fromtimestamp(22, timezone.utc),
tags=[
self.setup_tag(name="tag1"),
self.setup_tag(name="tag2"),
self.setup_tag(name="tag3"),
],
),
self.setup_bookmark(
url="https://example.com/3", title="Title 3", added=added, modified=modified, unread=True
url="https://example.com/3",
title="Title 3",
added=datetime.fromtimestamp(3, timezone.utc),
modified=datetime.fromtimestamp(33, timezone.utc),
unread=True,
),
self.setup_bookmark(
url="https://example.com/4", title="Title 4", added=added, modified=modified, shared=True
url="https://example.com/4",
title="Title 4",
added=datetime.fromtimestamp(4, timezone.utc),
modified=datetime.fromtimestamp(44, timezone.utc),
shared=True,
),
self.setup_bookmark(
url="https://example.com/5",
title="Title 5",
added=added,
modified=modified,
added=datetime.fromtimestamp(5, timezone.utc),
modified=datetime.fromtimestamp(55, timezone.utc),
shared=True,
description="Example description",
notes="Example notes",
),
self.setup_bookmark(
url="https://example.com/6",
title="Title 6",
added=added,
modified=modified,
added=datetime.fromtimestamp(6, timezone.utc),
modified=datetime.fromtimestamp(66, timezone.utc),
shared=True,
notes="Example notes",
),
self.setup_bookmark(
url="https://example.com/7",
title="Title 7",
added=added,
modified=modified,
added=datetime.fromtimestamp(7, timezone.utc),
modified=datetime.fromtimestamp(77, timezone.utc),
is_archived=True,
),
self.setup_bookmark(
url="https://example.com/8",
title="Title 8",
added=added,
modified=modified,
added=datetime.fromtimestamp(8, timezone.utc),
modified=datetime.fromtimestamp(88, timezone.utc),
tags=[self.setup_tag(name="tag4"), self.setup_tag(name="tag5")],
is_archived=True,
),
]
html = exporter.export_netscape_html(bookmarks)

lines = [
f'<DT><A HREF="https://example.com/1" ADD_DATE="{timestamp_added}" LAST_MODIFIED="{timestamp_modified}" PRIVATE="1" TOREAD="0" TAGS="">Title 1</A>',
'<DT><A HREF="https://example.com/1" ADD_DATE="1" LAST_MODIFIED="11" PRIVATE="1" TOREAD="0" TAGS="">Title 1</A>',
"<DD>Example description",
f'<DT><A HREF="https://example.com/2" ADD_DATE="{timestamp_added}" LAST_MODIFIED="{timestamp_modified}" PRIVATE="1" TOREAD="0" TAGS="tag1,tag2,tag3">Title 2</A>',
f'<DT><A HREF="https://example.com/3" ADD_DATE="{timestamp_added}" LAST_MODIFIED="{timestamp_modified}" PRIVATE="1" TOREAD="1" TAGS="">Title 3</A>',
f'<DT><A HREF="https://example.com/4" ADD_DATE="{timestamp_added}" LAST_MODIFIED="{timestamp_modified}" PRIVATE="0" TOREAD="0" TAGS="">Title 4</A>',
f'<DT><A HREF="https://example.com/5" ADD_DATE="{timestamp_added}" LAST_MODIFIED="{timestamp_modified}" PRIVATE="0" TOREAD="0" TAGS="">Title 5</A>',
'<DT><A HREF="https://example.com/2" ADD_DATE="2" LAST_MODIFIED="22" PRIVATE="1" TOREAD="0" TAGS="tag1,tag2,tag3">Title 2</A>',
'<DT><A HREF="https://example.com/3" ADD_DATE="3" LAST_MODIFIED="33" PRIVATE="1" TOREAD="1" TAGS="">Title 3</A>',
'<DT><A HREF="https://example.com/4" ADD_DATE="4" LAST_MODIFIED="44" PRIVATE="0" TOREAD="0" TAGS="">Title 4</A>',
'<DT><A HREF="https://example.com/5" ADD_DATE="5" LAST_MODIFIED="55" PRIVATE="0" TOREAD="0" TAGS="">Title 5</A>',
"<DD>Example description[linkding-notes]Example notes[/linkding-notes]",
f'<DT><A HREF="https://example.com/6" ADD_DATE="{timestamp_added}" LAST_MODIFIED="{timestamp_modified}" PRIVATE="0" TOREAD="0" TAGS="">Title 6</A>',
'<DT><A HREF="https://example.com/6" ADD_DATE="6" LAST_MODIFIED="66" PRIVATE="0" TOREAD="0" TAGS="">Title 6</A>',
"<DD>[linkding-notes]Example notes[/linkding-notes]",
f'<DT><A HREF="https://example.com/7" ADD_DATE="{timestamp_added}" LAST_MODIFIED="{timestamp_modified}" PRIVATE="1" TOREAD="0" TAGS="linkding:archived">Title 7</A>',
f'<DT><A HREF="https://example.com/8" ADD_DATE="{timestamp_added}" LAST_MODIFIED="{timestamp_modified}" PRIVATE="1" TOREAD="0" TAGS="tag4,tag5,linkding:archived">Title 8</A>',
'<DT><A HREF="https://example.com/7" ADD_DATE="7" LAST_MODIFIED="77" PRIVATE="1" TOREAD="0" TAGS="linkding:archived">Title 7</A>',
'<DT><A HREF="https://example.com/8" ADD_DATE="8" LAST_MODIFIED="88" PRIVATE="1" TOREAD="0" TAGS="tag4,tag5,linkding:archived">Title 8</A>',
]
self.assertIn("\n\r".join(lines), html)

Expand Down
44 changes: 24 additions & 20 deletions bookmarks/tests/test_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
disable_logging,
)
from bookmarks.utils import parse_timestamp
from datetime import datetime


class ImporterTestCase(TestCase, BookmarkFactoryMixin, ImportTestMixin):
Expand All @@ -27,7 +26,9 @@ def assertBookmarksImported(self, html_tags: List[BookmarkHtmlTag]):
self.assertEqual(bookmark.title, html_tag.title)
self.assertEqual(bookmark.description, html_tag.description)
self.assertEqual(bookmark.date_added, parse_timestamp(html_tag.add_date))
self.assertEqual(bookmark.date_modified, parse_timestamp(html_tag.last_modified))
self.assertEqual(
bookmark.date_modified, parse_timestamp(html_tag.last_modified)
)
self.assertEqual(bookmark.unread, html_tag.to_read)
self.assertEqual(bookmark.shared, not html_tag.private)

Expand All @@ -47,31 +48,31 @@ def test_import(self):
title="Example title",
description="Example description",
add_date="1",
last_modified="1",
last_modified="11",
tags="example-tag",
),
BookmarkHtmlTag(
href="https://example.com/foo",
title="Foo title",
description="",
add_date="2",
last_modified="2",
last_modified="22",
tags="",
),
BookmarkHtmlTag(
href="https://example.com/bar",
title="Bar title",
description="Bar description",
add_date="3",
last_modified="3",
last_modified="33",
tags="bar-tag, other-tag",
),
BookmarkHtmlTag(
href="https://example.com/baz",
title="Baz title",
description="Baz description",
add_date="4",
last_modified="4",
last_modified="44",
to_read=True,
),
]
Expand All @@ -96,39 +97,39 @@ def test_synchronize(self):
title="Example title",
description="Example description",
add_date="1",
last_modified="1",
last_modified="11",
tags="example-tag",
),
BookmarkHtmlTag(
href="https://example.com/foo",
title="Foo title",
description="",
add_date="2",
last_modified="2",
last_modified="22",
tags="",
),
BookmarkHtmlTag(
href="https://example.com/bar",
title="Bar title",
description="Bar description",
add_date="3",
last_modified="3",
last_modified="33",
tags="bar-tag, other-tag",
),
BookmarkHtmlTag(
href="https://example.com/unread",
title="Unread title",
description="Unread description",
add_date="3",
last_modified="3",
add_date="4",
last_modified="44",
to_read=True,
),
BookmarkHtmlTag(
href="https://example.com/private",
title="Private title",
description="Private description",
add_date="4",
last_modified="4",
add_date="5",
last_modified="55",
private=True,
),
]
Expand All @@ -147,23 +148,23 @@ def test_synchronize(self):
title="Updated Example title",
description="Updated Example description",
add_date="111",
last_modified="111",
last_modified="1111",
tags="updated-example-tag",
),
BookmarkHtmlTag(
href="https://example.com/foo",
title="Updated Foo title",
description="Updated Foo description",
add_date="222",
last_modified="222",
last_modified="2222",
tags="new-tag",
),
BookmarkHtmlTag(
href="https://example.com/bar",
title="Updated Bar title",
description="Updated Bar description",
add_date="333",
last_modified="333",
last_modified="3333",
tags="updated-bar-tag, updated-other-tag",
),
BookmarkHtmlTag(
Expand All @@ -182,7 +183,12 @@ def test_synchronize(self):
last_modified="4",
private=False,
),
BookmarkHtmlTag(href="https://baz.com", add_date="444", last_modified="444", tags="baz-tag"),
BookmarkHtmlTag(
href="https://baz.com",
add_date="444",
last_modified="4444",
tags="baz-tag",
),
]

# Import updated data
Expand Down Expand Up @@ -318,9 +324,7 @@ def test_use_add_date_when_no_last_modified(self):
import_netscape_html(test_html, self.get_or_create_test_user())

self.assertEqual(Bookmark.objects.count(), 1)
self.assertEqual(
Bookmark.objects.all()[0].date_modified, parse_timestamp("1")
)
self.assertEqual(Bookmark.objects.all()[0].date_modified, parse_timestamp("1"))

def test_keep_title_if_imported_bookmark_has_empty_title(self):
test_html = self.render_html(
Expand Down
16 changes: 11 additions & 5 deletions bookmarks/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,30 @@ def test_parse_bookmarks(self):
title="Example title",
description="Example description",
add_date="1",
last_modified="1",
last_modified="11",
tags="example-tag",
),
BookmarkHtmlTag(
href="https://example.com/foo",
title="Foo title",
description="",
add_date="2",
last_modified="2",
last_modified="22",
tags="",
),
BookmarkHtmlTag(
href="https://example.com/bar",
title="Bar title",
description="Bar description",
add_date="3",
last_modified="3",
last_modified="33",
tags="bar-tag, other-tag",
),
BookmarkHtmlTag(
href="https://example.com/baz",
title="Baz title",
description="Baz description",
add_date="4",
last_modified="4",
to_read=True,
),
]
Expand All @@ -80,7 +79,14 @@ def test_reset_properties_after_adding_bookmark(self):
last_modified="1",
tags="example-tag",
),
BookmarkHtmlTag(href="", title="", description="", add_date="", last_modified="", tags=""),
BookmarkHtmlTag(
href="",
title="",
description="",
add_date="",
last_modified="",
tags="",
),
]
html = self.render_html(html_tags)
bookmarks = parse(html)
Expand Down

0 comments on commit 4fcea39

Please sign in to comment.