From 396d4ac11fb7a09743fc080047454468a0800375 Mon Sep 17 00:00:00 2001 From: Martin Kudlej Date: Tue, 7 May 2024 14:16:31 +0200 Subject: [PATCH] add backend methods with integration tests --- tests/integration/conftest.py | 12 +++++ .../test_integration_backend_methods.py | 51 +++++++++++++++++++ threescale_api/resources.py | 20 ++++++++ 3 files changed, 83 insertions(+) create mode 100644 tests/integration/test_integration_backend_methods.py diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 75f9066..abb3849 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -223,6 +223,11 @@ def hits_metric(service): return service.metrics.read_by(system_name='hits') +@pytest.fixture(scope='module') +def backend_hits_metric(backend): + return backend.metrics.read_by(system_name=('hits.' + str(backend['id']))) + + @pytest.fixture(scope='module') def method_params(service): suffix = get_suffix() @@ -248,6 +253,13 @@ def method(hits_metric, method_params): cleanup(resource) +@pytest.fixture(scope='module') +def backend_method(backend_hits_metric, method_params): + resource = backend_hits_metric.methods.create(params=method_params) + yield resource + cleanup(resource) + + def get_mapping_rule_pattern(): suffix = get_suffix() pattern = f'test-{suffix}'.replace('_', '-') diff --git a/tests/integration/test_integration_backend_methods.py b/tests/integration/test_integration_backend_methods.py new file mode 100644 index 0000000..44013ba --- /dev/null +++ b/tests/integration/test_integration_backend_methods.py @@ -0,0 +1,51 @@ +import pytest + +from threescale_api.errors import ApiClientError + +from tests.integration import asserts + +def test_list_methods(backend_hits_metric, backend_method): + assert len(backend_hits_metric.methods.list()) >= 1 + +def test_should_create_method(backend_method, method_params): + asserts.assert_resource(backend_method) + asserts.assert_resource_params(backend_method, method_params) + + +def test_should_not_create_method_for_custom_metric(backend_hits_metric, method_params): + resource = backend_hits_metric.methods.create(params=method_params, throws=False) + asserts.assert_errors_contains(resource, ['system_name']) + + +def test_should_friendly_name_be_required(backend_hits_metric): + resource = backend_hits_metric.methods.create(params={}, throws=False) + asserts.assert_errors_contains(resource, ['friendly_name']) + + +def test_should_raise_api_exception(backend_hits_metric): + with pytest.raises(ApiClientError): + backend_hits_metric.methods.create(params={}) + + +def test_should_read_method(backend_method, method_params): + resource = backend_method.read() + asserts.assert_resource(resource) + asserts.assert_resource_params(resource, method_params) + + +def test_should_update_method(backend_method, updated_method_params): + resource = backend_method.update(params=updated_method_params) + asserts.assert_resource(resource) + asserts.assert_resource_params(resource, updated_method_params) + + +def test_should_delete_method(backend_hits_metric, updated_method_params): + resource = backend_hits_metric.methods.create(params=updated_method_params) + assert resource.exists() + resource.delete() + assert not resource.exists() + + +def test_should_list_methods(backend_hits_metric): + resources = backend_hits_metric.methods.list() + assert len(resources) == 1 diff --git a/threescale_api/resources.py b/threescale_api/resources.py index 8f19e8d..a4a2b0a 100644 --- a/threescale_api/resources.py +++ b/threescale_api/resources.py @@ -114,6 +114,13 @@ def url(self) -> str: return self.parent.url + '/methods' +class BackendMethods(Methods): + def __init__(self, *args, entity_name='method', entity_collection='methods', per_page=None, + **kwargs): + super().__init__(*args, entity_name=entity_name, + entity_collection=entity_collection, per_page=per_page, **kwargs) + + class ApplicationPlans(DefaultPlanClient): def __init__(self, *args, entity_name='application_plan', entity_collection='plans', **kwargs): super().__init__(*args, entity_name=entity_name, @@ -1104,6 +1111,19 @@ def service(self) -> 'Service': return self.metric.parent +class BackendMethod(Method): + def __init__(self, entity_name='system_name', **kwargs): + super().__init__(entity_name=entity_name, **kwargs) + + @property + def service(self) -> 'Service': + raise AttributeError("'BackendMethod' object has no attribute 'service'") + + @property + def backend(self) -> 'Backend': + return self.metric.parent + + class Metric(DefaultResource): def __init__(self, entity_name='system_name', **kwargs): super().__init__(entity_name=entity_name, **kwargs)