2023-10-01 19:22:44 +00:00
|
|
|
from django.http import QueryDict
|
2023-09-01 20:48:21 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
|
2023-10-01 19:22:44 +00:00
|
|
|
from bookmarks.models import BookmarkSearch
|
|
|
|
|
2023-09-01 20:48:21 +00:00
|
|
|
|
|
|
|
class BookmarkSearchModelTest(TestCase):
|
|
|
|
def test_from_request(self):
|
|
|
|
# no params
|
2023-10-01 19:22:44 +00:00
|
|
|
query_dict = QueryDict()
|
2023-09-01 20:48:21 +00:00
|
|
|
|
2023-10-01 19:22:44 +00:00
|
|
|
search = BookmarkSearch.from_request(query_dict)
|
2023-09-01 20:48:21 +00:00
|
|
|
self.assertEqual(search.q, '')
|
|
|
|
self.assertEqual(search.user, '')
|
2023-10-01 19:22:44 +00:00
|
|
|
self.assertEqual(search.sort, BookmarkSearch.SORT_ADDED_DESC)
|
|
|
|
self.assertEqual(search.shared, BookmarkSearch.FILTER_SHARED_OFF)
|
|
|
|
self.assertEqual(search.unread, BookmarkSearch.FILTER_UNREAD_OFF)
|
2023-09-01 20:48:21 +00:00
|
|
|
|
|
|
|
# some params
|
2023-10-01 19:22:44 +00:00
|
|
|
query_dict = QueryDict('q=search query&user=user123')
|
2023-09-01 20:48:21 +00:00
|
|
|
|
2023-10-01 19:22:44 +00:00
|
|
|
bookmark_search = BookmarkSearch.from_request(query_dict)
|
2023-09-01 20:48:21 +00:00
|
|
|
self.assertEqual(bookmark_search.q, 'search query')
|
|
|
|
self.assertEqual(bookmark_search.user, 'user123')
|
2023-10-01 19:22:44 +00:00
|
|
|
self.assertEqual(bookmark_search.sort, BookmarkSearch.SORT_ADDED_DESC)
|
|
|
|
self.assertEqual(search.shared, BookmarkSearch.FILTER_SHARED_OFF)
|
|
|
|
self.assertEqual(search.unread, BookmarkSearch.FILTER_UNREAD_OFF)
|
2023-09-01 20:48:21 +00:00
|
|
|
|
|
|
|
# all params
|
2023-10-01 19:22:44 +00:00
|
|
|
query_dict = QueryDict('q=search query&sort=title_asc&user=user123&shared=yes&unread=yes')
|
|
|
|
|
|
|
|
search = BookmarkSearch.from_request(query_dict)
|
|
|
|
self.assertEqual(search.q, 'search query')
|
|
|
|
self.assertEqual(search.user, 'user123')
|
|
|
|
self.assertEqual(search.sort, BookmarkSearch.SORT_TITLE_ASC)
|
|
|
|
self.assertEqual(search.shared, BookmarkSearch.FILTER_SHARED_SHARED)
|
|
|
|
self.assertEqual(search.unread, BookmarkSearch.FILTER_UNREAD_YES)
|
|
|
|
|
|
|
|
# respects preferences
|
|
|
|
preferences = {
|
2023-09-10 20:14:07 +00:00
|
|
|
'sort': BookmarkSearch.SORT_TITLE_ASC,
|
2023-09-16 08:39:27 +00:00
|
|
|
'unread': BookmarkSearch.FILTER_UNREAD_YES,
|
2023-09-01 20:48:21 +00:00
|
|
|
}
|
2023-10-01 19:22:44 +00:00
|
|
|
query_dict = QueryDict('q=search query')
|
2023-09-01 20:48:21 +00:00
|
|
|
|
2023-10-01 19:22:44 +00:00
|
|
|
search = BookmarkSearch.from_request(query_dict, preferences)
|
2023-09-01 20:48:21 +00:00
|
|
|
self.assertEqual(search.q, 'search query')
|
2023-10-01 19:22:44 +00:00
|
|
|
self.assertEqual(search.user, '')
|
2023-09-01 20:48:21 +00:00
|
|
|
self.assertEqual(search.sort, BookmarkSearch.SORT_TITLE_ASC)
|
2023-10-01 19:22:44 +00:00
|
|
|
self.assertEqual(search.shared, BookmarkSearch.FILTER_SHARED_OFF)
|
2023-09-16 08:39:27 +00:00
|
|
|
self.assertEqual(search.unread, BookmarkSearch.FILTER_UNREAD_YES)
|
2023-09-01 20:48:21 +00:00
|
|
|
|
2023-10-01 19:22:44 +00:00
|
|
|
# query overrides preferences
|
|
|
|
preferences = {
|
|
|
|
'sort': BookmarkSearch.SORT_TITLE_ASC,
|
|
|
|
'shared': BookmarkSearch.FILTER_SHARED_SHARED,
|
|
|
|
'unread': BookmarkSearch.FILTER_UNREAD_YES,
|
|
|
|
}
|
|
|
|
query_dict = QueryDict('sort=title_desc&shared=no&unread=off')
|
|
|
|
|
|
|
|
search = BookmarkSearch.from_request(query_dict, preferences)
|
|
|
|
self.assertEqual(search.q, '')
|
|
|
|
self.assertEqual(search.user, '')
|
|
|
|
self.assertEqual(search.sort, BookmarkSearch.SORT_TITLE_DESC)
|
|
|
|
self.assertEqual(search.shared, BookmarkSearch.FILTER_SHARED_UNSHARED)
|
|
|
|
self.assertEqual(search.unread, BookmarkSearch.FILTER_UNREAD_OFF)
|
|
|
|
|
2023-09-01 20:48:21 +00:00
|
|
|
def test_modified_params(self):
|
|
|
|
# no params
|
|
|
|
bookmark_search = BookmarkSearch()
|
|
|
|
modified_params = bookmark_search.modified_params
|
|
|
|
self.assertEqual(len(modified_params), 0)
|
|
|
|
|
|
|
|
# params are default values
|
2023-09-10 20:14:07 +00:00
|
|
|
bookmark_search = BookmarkSearch(q='', sort=BookmarkSearch.SORT_ADDED_DESC, user='', shared='')
|
2023-09-01 20:48:21 +00:00
|
|
|
modified_params = bookmark_search.modified_params
|
|
|
|
self.assertEqual(len(modified_params), 0)
|
|
|
|
|
|
|
|
# some modified params
|
|
|
|
bookmark_search = BookmarkSearch(q='search query', sort=BookmarkSearch.SORT_ADDED_ASC)
|
|
|
|
modified_params = bookmark_search.modified_params
|
|
|
|
self.assertCountEqual(modified_params, ['q', 'sort'])
|
|
|
|
|
|
|
|
# all modified params
|
2023-09-16 08:39:27 +00:00
|
|
|
bookmark_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
|
|
|
modified_params = bookmark_search.modified_params
|
2023-09-16 08:39:27 +00:00
|
|
|
self.assertCountEqual(modified_params, ['q', 'sort', 'user', 'shared', 'unread'])
|
2023-10-01 19:22:44 +00:00
|
|
|
|
|
|
|
# preferences are not modified params
|
|
|
|
preferences = {
|
|
|
|
'sort': BookmarkSearch.SORT_TITLE_ASC,
|
|
|
|
'unread': BookmarkSearch.FILTER_UNREAD_YES,
|
|
|
|
}
|
|
|
|
bookmark_search = BookmarkSearch(preferences=preferences)
|
|
|
|
modified_params = bookmark_search.modified_params
|
|
|
|
self.assertEqual(len(modified_params), 0)
|
|
|
|
|
|
|
|
# param is not modified if it matches the preference
|
|
|
|
preferences = {
|
|
|
|
'sort': BookmarkSearch.SORT_TITLE_ASC,
|
|
|
|
'unread': BookmarkSearch.FILTER_UNREAD_YES,
|
|
|
|
}
|
|
|
|
bookmark_search = BookmarkSearch(sort=BookmarkSearch.SORT_TITLE_ASC,
|
|
|
|
unread=BookmarkSearch.FILTER_UNREAD_YES,
|
|
|
|
preferences=preferences)
|
|
|
|
modified_params = bookmark_search.modified_params
|
|
|
|
self.assertEqual(len(modified_params), 0)
|
|
|
|
|
|
|
|
# overriding preferences is a modified param
|
|
|
|
preferences = {
|
|
|
|
'sort': BookmarkSearch.SORT_TITLE_ASC,
|
|
|
|
'shared': BookmarkSearch.FILTER_SHARED_SHARED,
|
|
|
|
'unread': BookmarkSearch.FILTER_UNREAD_YES,
|
|
|
|
}
|
|
|
|
bookmark_search = BookmarkSearch(sort=BookmarkSearch.SORT_TITLE_DESC,
|
|
|
|
shared=BookmarkSearch.FILTER_SHARED_UNSHARED,
|
|
|
|
unread=BookmarkSearch.FILTER_UNREAD_OFF,
|
|
|
|
preferences=preferences)
|
|
|
|
modified_params = bookmark_search.modified_params
|
|
|
|
self.assertCountEqual(modified_params, ['sort', 'shared', 'unread'])
|
|
|
|
|
|
|
|
def test_has_modifications(self):
|
|
|
|
# no params
|
|
|
|
bookmark_search = BookmarkSearch()
|
|
|
|
self.assertFalse(bookmark_search.has_modifications)
|
|
|
|
|
|
|
|
# params are default values
|
|
|
|
bookmark_search = BookmarkSearch(q='', sort=BookmarkSearch.SORT_ADDED_DESC, user='', shared='')
|
|
|
|
self.assertFalse(bookmark_search.has_modifications)
|
|
|
|
|
|
|
|
# modified params
|
|
|
|
bookmark_search = BookmarkSearch(q='search query', sort=BookmarkSearch.SORT_ADDED_ASC)
|
|
|
|
self.assertTrue(bookmark_search.has_modifications)
|
|
|
|
|
|
|
|
def test_preferences_dict(self):
|
|
|
|
# no params
|
|
|
|
bookmark_search = BookmarkSearch()
|
|
|
|
self.assertEqual(bookmark_search.preferences_dict, {
|
|
|
|
'sort': BookmarkSearch.SORT_ADDED_DESC,
|
|
|
|
'shared': BookmarkSearch.FILTER_SHARED_OFF,
|
|
|
|
'unread': BookmarkSearch.FILTER_UNREAD_OFF,
|
|
|
|
})
|
|
|
|
|
|
|
|
# with params
|
|
|
|
bookmark_search = BookmarkSearch(sort=BookmarkSearch.SORT_TITLE_DESC, unread=BookmarkSearch.FILTER_UNREAD_YES)
|
|
|
|
self.assertEqual(bookmark_search.preferences_dict, {
|
|
|
|
'sort': BookmarkSearch.SORT_TITLE_DESC,
|
|
|
|
'shared': BookmarkSearch.FILTER_SHARED_OFF,
|
|
|
|
'unread': BookmarkSearch.FILTER_UNREAD_YES,
|
|
|
|
})
|
|
|
|
|
|
|
|
# only returns preferences
|
|
|
|
bookmark_search = BookmarkSearch(q='search query', user='user123')
|
|
|
|
self.assertEqual(bookmark_search.preferences_dict, {
|
|
|
|
'sort': BookmarkSearch.SORT_ADDED_DESC,
|
|
|
|
'shared': BookmarkSearch.FILTER_SHARED_OFF,
|
|
|
|
'unread': BookmarkSearch.FILTER_UNREAD_OFF,
|
|
|
|
})
|