Skip to content

Commit 5fb0027

Browse files
committed
Fixup tests
1 parent 5df50c0 commit 5fb0027

2 files changed

Lines changed: 73 additions & 93 deletions

File tree

scripts/_cache.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ function _cache_help() {
99
echo
1010
echo " flush-redis - flush and re-fill the redis (private api) cache"
1111
echo " flush-redis-reserved-namespace - blue-green update the reserved namespace in the redis (private api) cache"
12+
echo " flush-search - flush and re-build the wagtail search index"
1213

1314
return 0
1415
}
@@ -20,6 +21,7 @@ function _cache() {
2021
case $verb in
2122
"flush-redis") _cache_flush_redis $args ;;
2223
"flush-redis-reserved-namespace") _cache_flush_redis_reserved_namespace $args ;;
24+
"flush-search") _flush_search $args ;;
2325

2426
*) _cache_help ;;
2527
esac
@@ -34,3 +36,8 @@ function _cache_flush_redis_reserved_namespace() {
3436
uhd venv activate
3537
python manage.py hydrate_private_api_cache_reserved_namespace
3638
}
39+
40+
function _flush_search() {
41+
uhd venv activate
42+
python manage.py wagtail_update_index
43+
}

tests/integration/public_api/v2/views/test_search.py

Lines changed: 66 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -10,62 +10,12 @@
1010
from tests.factories.cms.page import CommonPageFactory
1111

1212

13-
@pytest.fixture
14-
def topics(db) -> [TopicPage]:
15-
for i in range(5):
16-
TopicPageFactory.create(
17-
path=f"topic-{i}",
18-
depth=1,
19-
title=f"title topic {i}",
20-
slug=f"slug-{i}",
21-
seo_title=f"seo_title {i}",
22-
body=f"body text {i}",
23-
)
24-
return TopicPage.objects.all()
25-
26-
27-
# @pytest.fixture
28-
# def rare_topics(db) -> [TopicPage]:
29-
# for i in range(2):
30-
# TopicPageFactory.create(
31-
# path=f"topic-rare-{i}",
32-
# depth=1,
33-
# title=f"title rare {i}",
34-
# slug=f"slug-rare-{i}",
35-
# seo_title=f"seo_title-rare {i}",
36-
# body=f"body text rare {i}",
37-
# )
38-
39-
40-
@pytest.fixture
41-
def other_pages(db) -> [UKHSAPage]:
42-
for i in range(5):
43-
CommonPageFactory.create(
44-
path=f"page-{i}",
45-
depth=1,
46-
title=f"title rare {i}",
47-
slug=f"slug-page{i}",
48-
seo_title=f"seo_title {i}",
49-
body=f"body text {i}",
50-
)
51-
return TopicPage.objects.all()
52-
53-
54-
# @pytest.fixture
55-
# def rare_pages(db) -> [UKHSAPage]:
56-
# for i in range(5):
57-
# UKHSAPage.create(
58-
# path=f"page-rare-{i}",
59-
# depth=1,
60-
# title=f"title rare {i}",
61-
# slug=f"slug-rare-page-{i}",
62-
# seo_title=f"seo_title rare {i}",
63-
# body=f"body text rare {i}",
64-
# )
65-
# return TopicPage.objects.all()
13+
@pytest.mark.django_db(True)
14+
class TestSearchAPIView:
6615

16+
_topics = []
17+
_pages = []
6718

68-
class TestSearchAPIView:
6919
@property
7020
def path(self) -> str:
7121
return "/api/public/timeseries/v2/search"
@@ -74,10 +24,44 @@ def path(self) -> str:
7424
def target_domain(self) -> str:
7525
return os.environ.get("PUBLIC_API_TEST_DOMAIN", "http://testserver")
7626

77-
@pytest.mark.django_db(True)
78-
def test_search_finds_topics_only(
79-
self, topics: [TopicPage], other_pages: [UKHSAPage]
80-
):
27+
@pytest.fixture
28+
def topics(self) -> [TopicPage]:
29+
topics = []
30+
for i in range(5):
31+
title = f"title topic {i}"
32+
if i % 2 == 0:
33+
title += " rare"
34+
topics = topics + [
35+
TopicPageFactory.create(
36+
path=f"topic-{i}",
37+
depth=1,
38+
title=title,
39+
slug=f"slug-{i}",
40+
seo_title=f"seo_title {i}",
41+
body=f"body text {i}",
42+
)
43+
]
44+
return topics
45+
46+
@pytest.fixture
47+
def other_pages(self) -> [UKHSAPage]:
48+
if not (len(self._pages)):
49+
pages = []
50+
for i in range(5):
51+
self._pages = self._pages + [
52+
CommonPageFactory.create(
53+
path=f"page-{i}",
54+
depth=1,
55+
title=f"title other {i}",
56+
slug=f"slug-page{i}",
57+
seo_title=f"seo_title {i}",
58+
body=f"body text {i}",
59+
)
60+
]
61+
62+
return pages
63+
64+
def test_search_finds_topics_only(self, topics, other_pages):
8165
"""
8266
Given a string that matches all topic pages
8367
When the GET /api/public/v2/search API is called with that query and a limit of 5
@@ -102,38 +86,27 @@ def test_search_finds_topics_only(
10286
target = {"title": page.title, "slug": page.slug}
10387
assert response_data.index(target) > -1
10488

105-
# def test_search_finds_topics_and_others_if_not_enough(
106-
# self, topics: [TopicPage], rare_topics: [TopicPage], rare_pages: [UKHSAPage]
107-
# ):
108-
# # """
109-
# # Given a string that matches 2 topic page
110-
# # When GET /api/public/v2/search is called with that query and no limit
111-
# # Then the results will include the matching topic pages and others with the topics first
112-
# # """
113-
114-
# search = "rare" # All topics have a title
115-
# query = f"search={search}&limit={limit}"
116-
# expected_results = rare_topics + rare_pages
117-
118-
# # When
119-
# client = RequestsClient()
120-
# url = f"{self.target_domain}{self.path}?{query}"
121-
# response: Response = client.get(url)
122-
123-
# # Then
124-
# assert response.status_code == HTTPStatus.OK
125-
# response_data: list[dict] = response.json()
126-
# assert len(expected_results) == len(response_data)
127-
# for page in expected_results:
128-
# target = {"title": page.title, "slug": page.slug}
129-
# assert response_data.index(target) > -1
130-
131-
# pytest.fail("Unimplemented")
132-
133-
# def test_search_doesnt_find_unpublished_content(self):
134-
# """
135-
# Given a string that matches pages with unpublished content
136-
# When the GET /api/public/v2/search API is called with that query and no limit
137-
# Then the results will not include either the unpublished or embargoed content
138-
# """
139-
# pytest.fail("Unimplemented")
89+
def test_search_finds_topics_and_pages(self, topics, other_pages):
90+
"""
91+
Given a string that matches 2 topic page
92+
When GET /api/public/v2/search is called with that query and no limit
93+
Then the results will include the matching topic pages and others with the topics first
94+
"""
95+
96+
# Given
97+
search = "rare" # All topics have a title, only even ones have rare in it
98+
query = f"search={search}"
99+
expected_results = [t for t in topics if "rare" in t.title]
100+
101+
# When
102+
client = RequestsClient()
103+
url = f"{self.target_domain}{self.path}?{query}"
104+
response: Response = client.get(url)
105+
106+
# Then
107+
assert response.status_code == HTTPStatus.OK
108+
response_data: list[dict] = response.json()
109+
assert len(expected_results) == len(response_data)
110+
for page in expected_results:
111+
target = {"title": page.title, "slug": page.slug}
112+
assert response_data.index(target) > -1

0 commit comments

Comments
 (0)