Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dpl 3280 service layer tests #63

Merged
merged 3 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 47 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@

import pytest
from data_platform_catalogue.client import BaseCatalogueClient
from data_platform_catalogue.search_types import (FacetOption, ResultType,
SearchFacets, SearchResponse,
SearchResult)
from data_platform_catalogue.search_types import (
FacetOption,
ResultType,
SearchFacets,
SearchResponse,
SearchResult,
)
from django.test import Client
from faker import Faker
from home.service.search import SearchService
from home.service.details import DetailsService
from home.forms.search import SearchForm

fake = Faker()

Expand Down Expand Up @@ -59,7 +66,8 @@ def mock_catalogue():
mock_catalogue = MagicMock(spec=BaseCatalogueClient)
mock_fn.return_value = mock_catalogue
mock_search_response(
mock_catalogue, page_results=generate_page(), total_results=100)
mock_catalogue, page_results=generate_page(), total_results=100
)
mock_search_facets_response(mock_catalogue, domains=generate_options())

yield mock_catalogue
Expand All @@ -69,10 +77,44 @@ def mock_catalogue():

def mock_search_response(mock_catalogue, total_results=0, page_results=()):
search_response = SearchResponse(
total_results=total_results, page_results=page_results)
total_results=total_results, page_results=page_results
)
mock_catalogue.search.return_value = search_response


def mock_search_facets_response(mock_catalogue, domains):
mock_catalogue.search_facets.return_value = SearchFacets(
{"domains": domains})


@pytest.fixture
def valid_form():
valid_form = SearchForm(
data={
"query": "test",
"domains": ["urn:li:domain:HMCTS"],
"sort": "ascending",
"clear_filter": False,
"clear_label": False,
}
)
assert valid_form.is_valid()

return valid_form


@pytest.fixture
def search_context(valid_form):
search_service = SearchService(form=valid_form, page="1")
context = search_service._get_context()
return context


@pytest.fixture
def detail_context(mock_catalogue):
mock_catalogue.search.return_value = SearchResponse(
total_results=1, page_results=generate_page(page_size=1)
)
details_service = DetailsService(urn="urn:li:dataProduct:test")
context = details_service._get_context()
return context
39 changes: 14 additions & 25 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
from home.forms.search import SearchForm
import pytest


@pytest.fixture
def valid_form():
valid_form = SearchForm(
data={
"query": "test",
"domains": ["urn:li:domain:HMCTS"],
"sort": "ascending",
"clear_filter": False,
"clear_label": False,
}
)
assert valid_form.is_valid()

return valid_form


class TestSearchForm:
Expand All @@ -33,15 +16,21 @@ def test_all_fields_nullable(self):
assert SearchForm(data={}).is_valid()

def test_form_encode_without_filter_for_one_filter(self, valid_form):
assert (valid_form.encode_without_filter("urn:li:domain:HMCTS") ==
"?query=test&sort=ascending&clear_filter=False&clear_label=False")
assert (
valid_form.encode_without_filter("urn:li:domain:HMCTS")
== "?query=test&sort=ascending&clear_filter=False&clear_label=False"
)

def test_form_encode_without_filter_for_two_filters(self):
two_filter_form = SearchForm(data={
"query": "test",
"domains": ["urn:li:domain:HMCTS", "urn:li:domain:HMPPS"]
})
two_filter_form = SearchForm(
data={
"query": "test",
"domains": ["urn:li:domain:HMCTS", "urn:li:domain:HMPPS"],
}
)
two_filter_form.is_valid()

assert (two_filter_form.encode_without_filter("urn:li:domain:HMCTS") ==
"?query=test&domains=urn%3Ali%3Adomain%3AHMPPS&sort=&clear_filter=False&clear_label=False")
assert (
two_filter_form.encode_without_filter("urn:li:domain:HMCTS")
== "?query=test&domains=urn%3Ali%3Adomain%3AHMPPS&sort=&clear_filter=False&clear_label=False"
)
42 changes: 42 additions & 0 deletions tests/test_services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from types import GeneratorType
from data_platform_catalogue.search_types import ResultType


class TestSearchService:
def test_get_context_form(self, valid_form, search_context):
assert search_context["form"] == valid_form

def test_get_context_search_result(self, mock_catalogue, search_context):
assert search_context["results"] == mock_catalogue.search(
).page_results
assert search_context["total_results"] == 100

def test_get_context_paginator(self, search_context):
assert search_context["page_obj"].number == 1
assert isinstance(search_context["page_range"], GeneratorType)
assert search_context["paginator"].num_pages == 5

def test_get_context_page_title(self, search_context):
assert search_context["page_title"] == 'Search for "test" - Data catalogue'

def test_get_context_label_clear_href(self, search_context):
assert search_context["label_clear_href"] == {
"HMCTS": "?query=test&sort=ascending&clear_filter=False&clear_label=False"
}


class TestDetailsService:
def test_get_context(self, detail_context, mock_catalogue):
assert detail_context["result"] == mock_catalogue.search(
).page_results[0]
result_type = (
"Data product"
if mock_catalogue.search().page_results[0].result_type
== ResultType.DATA_PRODUCT
else "Table"
)
assert detail_context["result_type"] == result_type
assert (
detail_context["page_title"]
== f"{mock_catalogue.search().page_results[0].name} - Data catalogue"
)
Loading