"""Tests for payee handlers.""" from unittest.mock import MagicMock import pytest from royalty_common.marshalling.custom_fields import ma from royalty_common.test_utils.helpers import get_json_body from royalty_common.views.list_view import ListView class Xenomorph: """Xenomorph fake model.""" def __init__(self, name): """Init.""" self.name = name CUSTOM_ORDER = '' SOME_XENOMORPHS = [ Xenomorph(name=name) for name in ('Jenna', 'Jenn', 'John', 'Jay', 'JP', 'Jacob') ] @pytest.fixture() def mock_pagination_helpers(): """Mock pagination helpers.""" XenomorphList.get_page = MagicMock(return_value=SOME_XENOMORPHS) Xenomorph.count = MagicMock(return_value=1023) class XenomorphSchema(ma.Schema): """Fake schema.""" name = ma.String() class XenomorphList(ListView): """A view for listing Xenomorphs.""" model_class = Xenomorph list_entry_schema = XenomorphSchema() def custom_order(self): """Pecking order.""" return CUSTOM_ORDER @pytest.fixture(scope='module') def register_endpoint(test_app): """Add a test endpoint for listing Xenomorphs.""" test_app.add_url_rule( '/xenomorphsfortesting', view_func=XenomorphList.as_view('list_xenomorphs') ) expected_result = { 'items': XenomorphSchema().dump(SOME_XENOMORPHS, many=True), 'total_count': 1023 } def test_with_good_parameters( mock_pagination_helpers, register_endpoint, fixture_client): """Retrieve a page of Xenomorphs.""" res = fixture_client.get( '/xenomorphsfortesting?offset=2&limit=17&query=os' ) assert res.status_code == 200 assert get_json_body(res) == expected_result XenomorphList.get_page.assert_called_once_with( offset=2, limit=17, query='os', custom_order=CUSTOM_ORDER ) Xenomorph.count.assert_called_once() def test_with_bad_parameters( mock_pagination_helpers, register_endpoint, fixture_client): """Retrieve a page of Xenomorphs.""" res = fixture_client.get( '/xenomorphsfortesting?offset=blah&limit=-2&query=os' ) assert res.status_code == 200 assert get_json_body(res) == expected_result XenomorphList.get_page.assert_called_once_with( offset=0, limit=1, query='os', custom_order=CUSTOM_ORDER ) Xenomorph.count.assert_called_once()