Skip to content

Commit 707facc

Browse files
committed
CDD-3175: tests
1 parent 843f340 commit 707facc

File tree

25 files changed

+1864
-74
lines changed

25 files changed

+1864
-74
lines changed

cms/metrics_interface/field_choices_callables.py

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ def get_all_theme_names_and_ids() -> LIST_OF_TWO_STRING_ITEM_TUPLES:
314314
[(1, "immunisation"), ...]
315315
"""
316316
metrics_interface = MetricsAPIInterface()
317+
317318
return _build_id_name_tuple_choices(
318319
choices=metrics_interface.get_all_theme_names_and_ids()
319320
)
@@ -383,32 +384,6 @@ def get_all_sub_theme_names_and_ids() -> LIST_OF_TWO_STRING_ITEM_TUPLES:
383384
)
384385

385386

386-
def get_filtered_unique_sub_theme_names_for_parent_theme(
387-
parent_theme_id,
388-
) -> LIST_OF_TWO_STRING_ITEM_TUPLES:
389-
"""Callable for the `choices` on the `theme` fields of the CMS blocks.
390-
391-
Notes:
392-
This callable wraps the `MetricsAPIInterface`
393-
and is passed to a migration for the CMS blocks.
394-
This means that we don't need to create a new migration
395-
whenever a new chart type is added.
396-
Instead, the 1-off migration is pointed at this callable.
397-
So Wagtail will pull the choices by invoking this function.
398-
399-
Returns:
400-
A list of 2-item tuples of theme names.
401-
Examples:
402-
[("Infectious_disease", "Infectious_disease"), ...]
403-
"""
404-
metrics_interface = MetricsAPIInterface()
405-
return _build_id_name_tuple_choices(
406-
choices=metrics_interface.get_filtered_unique_sub_theme_names_for_parent_theme(
407-
parent_theme_id=parent_theme_id
408-
),
409-
)
410-
411-
412387
def get_all_topic_names() -> LIST_OF_TWO_STRING_ITEM_TUPLES:
413388
"""Callable for the `choices` on the `topic` fields of the CMS blocks.
414389
@@ -462,6 +437,7 @@ def get_all_topic_names_and_ids() -> LIST_OF_TWO_STRING_ITEM_TUPLES:
462437
[(1, "6-in-1"), ...]
463438
"""
464439
metrics_interface = MetricsAPIInterface()
440+
465441
return _build_id_name_tuple_choices(
466442
choices=metrics_interface.get_all_topic_names_and_ids()
467443
)

cms/metrics_interface/interface.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -253,22 +253,6 @@ def get_all_unique_sub_theme_names(self) -> QuerySet:
253253
"""
254254
return self.sub_theme_manager.get_all_unique_names()
255255

256-
def get_filtered_unique_sub_theme_names_for_parent_theme(
257-
self, parent_theme_id
258-
) -> QuerySet:
259-
"""Get all unique sub_theme names as a flat list queryset.
260-
Note this is achieved by delegating the call to the `SubThemeManager` from the Metrics API
261-
262-
Returns:
263-
QuerySet: A queryset of the individual sub_theme names.
264-
Examples:
265-
`<SubThemeQuerySet ['respiratory', ...]>
266-
267-
"""
268-
return self.sub_theme_manager.get_filtered_unique_names_related_to_theme(
269-
parent_theme_id=parent_theme_id
270-
)
271-
272256
def get_all_topic_names(self) -> QuerySet:
273257
"""Gets all available topic names as a flat list queryset.
274258
Note this is achieved by delegating the call to the `TopicManager` from the Metrics API

tests/factories/metrics/metric.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import factory
22

3-
from metrics.data.models.core_models import Metric
3+
from metrics.data.models.core_models import Metric, Topic
44

55

66
class MetricFactory(factory.django.DjangoModelFactory):
@@ -10,3 +10,14 @@ class MetricFactory(factory.django.DjangoModelFactory):
1010

1111
class Meta:
1212
model = Metric
13+
14+
@classmethod
15+
def create_with_topic(
16+
cls, name: str, topic: str
17+
):
18+
topic, _ = Topic.objects.get_or_create(name=topic)
19+
20+
return cls.create(
21+
name=name,
22+
topic=topic,
23+
)

tests/factories/metrics/sub_theme.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import factory
22

3-
from metrics.data.models.core_models import SubTheme
3+
from metrics.data.models.core_models import SubTheme, Theme
44

55

66
class SubThemeFactory(factory.django.DjangoModelFactory):
@@ -10,3 +10,14 @@ class SubThemeFactory(factory.django.DjangoModelFactory):
1010

1111
class Meta:
1212
model = SubTheme
13+
14+
@classmethod
15+
def create_with_theme(
16+
cls, name: str, theme: str
17+
):
18+
theme, _ = Theme.objects.get_or_create(name=theme)
19+
20+
return cls.create(
21+
name=name,
22+
theme=theme,
23+
)

tests/factories/metrics/theme.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import factory
2+
3+
from metrics.data.models.core_models import Theme, SubTheme
4+
5+
6+
class ThemeFactory(factory.django.DjangoModelFactory):
7+
"""
8+
Factory for creating `Theme` instances for tests
9+
"""
10+
11+
class Meta:
12+
model = Theme

tests/factories/metrics/topic.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import factory
22

3-
from metrics.data.models.core_models import Topic
3+
from metrics.data.models.core_models import Topic, SubTheme
44

55

66
class TopicFactory(factory.django.DjangoModelFactory):
@@ -10,3 +10,14 @@ class TopicFactory(factory.django.DjangoModelFactory):
1010

1111
class Meta:
1212
model = Topic
13+
14+
@classmethod
15+
def create_with_sub_theme(
16+
cls, name: str, sub_theme: str
17+
):
18+
sub_theme, _ = SubTheme.objects.get_or_create(name=sub_theme)
19+
20+
return cls.create(
21+
name=name,
22+
sub_theme=sub_theme,
23+
)

tests/integration/metrics/api/views/test_geographies.py

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ def test_get_returns_correct_results_for_topic(self):
171171

172172
# When
173173
query_params = {"topic": topic}
174-
response: Response = client.get(path=self.path, query_params=query_params)
174+
response: Response = client.get(
175+
path=self.path, query_params=query_params)
175176

176177
# Then
177178
# Geographies are returned in descending alphabetical order
@@ -244,7 +245,8 @@ def test_get_returns_correct_results_for_geography_type(self):
244245

245246
# When
246247
query_params = {"geography_type": ltla}
247-
response: Response = client.get(path=self.path, query_params=query_params)
248+
response: Response = client.get(
249+
path=self.path, query_params=query_params)
248250

249251
# Then
250252
# Geographies are returned in descending alphabetical order
@@ -265,3 +267,104 @@ def test_get_returns_correct_results_for_geography_type(self):
265267
assert result["geographies"][2]["geography_code"] == hackney.geography_code
266268

267269
assert len(result["geographies"]) == 3
270+
271+
272+
class TestGeographiesByGeographyTypeView:
273+
@property
274+
def path(self) -> str:
275+
return "/api/permission-set/geographies"
276+
277+
@pytest.mark.django_db
278+
def test_get_geographies_by_geography_type_id_should_return_geographies(self):
279+
280+
client = APIClient()
281+
ltla = "Lower Tier Local Authority"
282+
283+
bexley = GeographyFactory.create_with_geography_type(
284+
name="Bexley", geography_code="E09000004", geography_type=ltla
285+
)
286+
arun = GeographyFactory.create_with_geography_type(
287+
name="Arun", geography_code="E07000224", geography_type=ltla
288+
)
289+
hackney = GeographyFactory.create_with_geography_type(
290+
name="Hackney", geography_code="E09000012", geography_type=ltla
291+
)
292+
GeographyFactory.create_with_geography_type(
293+
name="England", geography_code="E92000001", geography_type="Nation"
294+
)
295+
296+
geographyTypeId = 1
297+
path = f"{self.path}/{geographyTypeId}"
298+
response: Response = client.get(path=path)
299+
result = response.data
300+
assert len(response.data["choices"]) == 3
301+
assert result["choices"][0][0] == arun.geography_code
302+
assert result["choices"][0][1] == arun.name
303+
304+
assert result["choices"][1][0] == bexley.geography_code
305+
assert result["choices"][1][1] == bexley.name
306+
307+
assert result["choices"][2][0] == hackney.geography_code
308+
assert result["choices"][2][1] == hackney.name
309+
310+
@pytest.mark.django_db
311+
def test_get_geographies_by_geography_type_id_should_return_wildcard(self):
312+
313+
client = APIClient()
314+
ltla = "Lower Tier Local Authority"
315+
316+
bexley = GeographyFactory.create_with_geography_type(
317+
name="Bexley", geography_code="E09000004", geography_type=ltla
318+
)
319+
arun = GeographyFactory.create_with_geography_type(
320+
name="Arun", geography_code="E07000224", geography_type=ltla
321+
)
322+
hackney = GeographyFactory.create_with_geography_type(
323+
name="Hackney", geography_code="E09000012", geography_type=ltla
324+
)
325+
GeographyFactory.create_with_geography_type(
326+
name="England", geography_code="E92000001", geography_type="Nation"
327+
)
328+
329+
geographyTypeId = -1
330+
path = f"{self.path}/{geographyTypeId}"
331+
response: Response = client.get(path=path)
332+
result = response.data
333+
334+
# Choices length should only contain wildcard option
335+
assert len(response.data["choices"]) == 1
336+
337+
# Should return a wildcard choice
338+
assert result["choices"][0][0] == "-1"
339+
assert result["choices"][0][1] == "* (All geographies)"
340+
341+
@pytest.mark.django_db
342+
def test_get_geographies_by_geography_type_id_should_return_an_error(self):
343+
344+
client = APIClient()
345+
ltla = "Lower Tier Local Authority"
346+
347+
bexley = GeographyFactory.create_with_geography_type(
348+
name="Bexley", geography_code="E09000004", geography_type=ltla
349+
)
350+
arun = GeographyFactory.create_with_geography_type(
351+
name="Arun", geography_code="E07000224", geography_type=ltla
352+
)
353+
hackney = GeographyFactory.create_with_geography_type(
354+
name="Hackney", geography_code="E09000012", geography_type=ltla
355+
)
356+
GeographyFactory.create_with_geography_type(
357+
name="England", geography_code="E92000001", geography_type="Nation"
358+
)
359+
360+
geographyTypeId = "string"
361+
path = f"{self.path}/{geographyTypeId}"
362+
response: Response = client.get(path=path)
363+
result = response.data
364+
365+
assert response.status_code == HTTPStatus.BAD_REQUEST
366+
367+
# data should contain error
368+
369+
assert str(result["geography_type_id"][0]
370+
) == "Geography Type must be a number or '-1'"

0 commit comments

Comments
 (0)