2023-09-01 20:48:21 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
from bookmarks.models import BookmarkSearch, BookmarkSearchForm
|
|
|
|
from bookmarks.tests.helpers import BookmarkFactoryMixin
|
|
|
|
|
|
|
|
|
|
|
|
class BookmarkSearchFormTest(TestCase, BookmarkFactoryMixin):
|
|
|
|
def test_initial_values(self):
|
|
|
|
# no params
|
|
|
|
search = BookmarkSearch()
|
|
|
|
form = BookmarkSearchForm(search)
|
2024-01-27 10:29:16 +00:00
|
|
|
self.assertEqual(form["q"].initial, "")
|
|
|
|
self.assertEqual(form["user"].initial, "")
|
|
|
|
self.assertEqual(form["sort"].initial, BookmarkSearch.SORT_ADDED_DESC)
|
|
|
|
self.assertEqual(form["shared"].initial, BookmarkSearch.FILTER_SHARED_OFF)
|
|
|
|
self.assertEqual(form["unread"].initial, BookmarkSearch.FILTER_UNREAD_OFF)
|
2023-09-01 20:48:21 +00:00
|
|
|
|
|
|
|
# with params
|
2024-01-27 10:29:16 +00:00
|
|
|
search = BookmarkSearch(
|
|
|
|
q="search query",
|
|
|
|
sort=BookmarkSearch.SORT_ADDED_ASC,
|
|
|
|
user="user123",
|
|
|
|
shared=BookmarkSearch.FILTER_SHARED_SHARED,
|
|
|
|
unread=BookmarkSearch.FILTER_UNREAD_YES,
|
|
|
|
)
|
2023-09-01 20:48:21 +00:00
|
|
|
form = BookmarkSearchForm(search)
|
2024-01-27 10:29:16 +00:00
|
|
|
self.assertEqual(form["q"].initial, "search query")
|
|
|
|
self.assertEqual(form["user"].initial, "user123")
|
|
|
|
self.assertEqual(form["sort"].initial, BookmarkSearch.SORT_ADDED_ASC)
|
|
|
|
self.assertEqual(form["shared"].initial, BookmarkSearch.FILTER_SHARED_SHARED)
|
|
|
|
self.assertEqual(form["unread"].initial, BookmarkSearch.FILTER_UNREAD_YES)
|
2023-09-01 20:48:21 +00:00
|
|
|
|
|
|
|
def test_user_options(self):
|
|
|
|
users = [
|
2024-01-27 10:29:16 +00:00
|
|
|
self.setup_user("user1"),
|
|
|
|
self.setup_user("user2"),
|
|
|
|
self.setup_user("user3"),
|
2023-09-01 20:48:21 +00:00
|
|
|
]
|
|
|
|
search = BookmarkSearch()
|
|
|
|
form = BookmarkSearchForm(search, users=users)
|
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
self.assertCountEqual(
|
|
|
|
form["user"].field.choices,
|
|
|
|
[
|
|
|
|
("", "Everyone"),
|
|
|
|
("user1", "user1"),
|
|
|
|
("user2", "user2"),
|
|
|
|
("user3", "user3"),
|
|
|
|
],
|
|
|
|
)
|
2023-09-01 20:48:21 +00:00
|
|
|
|
|
|
|
def test_hidden_fields(self):
|
|
|
|
# no modified params
|
|
|
|
search = BookmarkSearch()
|
|
|
|
form = BookmarkSearchForm(search)
|
|
|
|
self.assertEqual(len(form.hidden_fields()), 0)
|
|
|
|
|
|
|
|
# some modified params
|
2024-01-27 10:29:16 +00:00
|
|
|
search = BookmarkSearch(q="search query", sort=BookmarkSearch.SORT_ADDED_ASC)
|
2023-09-01 20:48:21 +00:00
|
|
|
form = BookmarkSearchForm(search)
|
2024-01-27 10:29:16 +00:00
|
|
|
self.assertCountEqual(form.hidden_fields(), [form["q"], form["sort"]])
|
2023-09-01 20:48:21 +00:00
|
|
|
|
|
|
|
# all modified params
|
2024-01-27 10:29:16 +00:00
|
|
|
search = BookmarkSearch(
|
|
|
|
q="search query",
|
|
|
|
sort=BookmarkSearch.SORT_ADDED_ASC,
|
|
|
|
user="user123",
|
|
|
|
shared=BookmarkSearch.FILTER_SHARED_SHARED,
|
|
|
|
unread=BookmarkSearch.FILTER_UNREAD_YES,
|
|
|
|
)
|
2023-09-01 20:48:21 +00:00
|
|
|
form = BookmarkSearchForm(search)
|
2024-01-27 10:29:16 +00:00
|
|
|
self.assertCountEqual(
|
|
|
|
form.hidden_fields(),
|
|
|
|
[form["q"], form["sort"], form["user"], form["shared"], form["unread"]],
|
|
|
|
)
|
2023-09-01 20:48:21 +00:00
|
|
|
|
|
|
|
# some modified params are editable fields
|
2024-01-27 10:29:16 +00:00
|
|
|
search = BookmarkSearch(
|
|
|
|
q="search query", sort=BookmarkSearch.SORT_ADDED_ASC, user="user123"
|
|
|
|
)
|
|
|
|
form = BookmarkSearchForm(search, editable_fields=["q", "user"])
|
|
|
|
self.assertCountEqual(form.hidden_fields(), [form["sort"]])
|