1010from 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