From 7cc3156235892e3e49990f0b185f94b9db70f704 Mon Sep 17 00:00:00 2001 From: rha104 <rha104@uib.no> Date: Wed, 20 Jan 2021 08:25:07 +0100 Subject: [PATCH] Adding a few more tests --- README.md | 12 +++++++++ setra_client/client.py | 11 -------- tests/conftest.py | 19 +++++++++++--- tests/test_client.py | 58 ++++++++++++++++++++++++++++++++++++++--- tests/test_endpoints.py | 28 ++++++++++++++++++++ 5 files changed, 111 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index b50d483..b89ff94 100644 --- a/README.md +++ b/README.md @@ -47,3 +47,15 @@ transactions = [Transaction.from_dict({ multi = Multi(batch=batch, vouchers=vouchers, transactions=transactions) response = c.post_multi(multi) ``` + +Running tests +------------- + + +``` +pytest +``` +or +``` +python3 setup.py test +``` diff --git a/setra_client/client.py b/setra_client/client.py index cf3dc85..da18b3d 100644 --- a/setra_client/client.py +++ b/setra_client/client.py @@ -1,7 +1,6 @@ """Client for connecting to SETRA API""" import json import logging -import os import urllib.parse from typing import Tuple, Union, List @@ -12,16 +11,6 @@ from setra_client.models import Multi logger = logging.getLogger(__name__) -def load_json_file(name): - """Load json file from the fixtures directory""" - here = os.path.realpath( - os.path.join(os.getcwd(), - os.path.dirname(__file__).rsplit('/', 1)[0])) - with open(os.path.join(here, 'tests/fixtures', name)) as f: - data = json.load(f) - return data - - def merge_dicts(*dicts): """ Combine a series of dicts without mutating any of them. diff --git a/tests/conftest.py b/tests/conftest.py index 94ea78e..6ea2b15 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,14 +1,27 @@ import json import pytest -import requests_mock +import requests +import os + +from setra_client.client import SetraClient, SetraEndpoints + + + +def load_json_file(name): + """Load json file from the fixtures directory""" + here = os.path.realpath( + os.path.join(os.getcwd(), + os.path.dirname(__file__).rsplit('/', 1)[0])) + with open(os.path.join(here, 'tests/fixtures', name)) as f: + data = json.load(f) + return data -from setra_client.client import SetraClient, SetraEndpoints, load_json_file @pytest.fixture def baseurl(): - return 'https://localhost' + return 'http://localhost' @pytest.fixture diff --git a/tests/test_client.py b/tests/test_client.py index cd667a9..fc24d1c 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,6 +1,9 @@ import pytest +import requests +import requests_mock from requests import HTTPError +from setra_client.client import SetraEndpoints from setra_client.client import SetraClient @@ -41,10 +44,59 @@ def test_init_modify_defaults(client_cls, baseurl, header_name): # Check that we don't do this by mutating default_headers assert client.default_headers[header_name] != headers[header_name] -# -# def test_get_update_schema(client, requests_mock): + +# def test_get_call(client, requests_mock): # """Ensure getting update schema works""" # requests_mock.get('https://localhost/_webservices/?ws=contacts/upsert/1.0', # json={'foo': 'bar'}) -# response = client.get_update_schema() +# response = client.call(method_name='GET', url=) # assert response == {'foo': 'bar'} + +def test_get_batch_call(client, requests_mock, baseurl): + """A working GET call should return HTTP 200, with json content""" + requests_mock.get('http://localhost/api/batch/3', + json={'foo': 'bar'}) + response = client.call(method_name='GET', url=SetraEndpoints(baseurl).batch(batch_id='3')) + assert response.status_code == 200 + assert response.json() == {'foo': 'bar'} + + + +def test_get_batch_call4(client, baseurl): + """A working GET call should return HTTP 200, with json content""" + session = requests.Session() + adapter = requests_mock.Adapter() + session.mount('http://', adapter) + adapter.register_uri('GET', 'http://test.com/1', json={'a': 'b'}, status_code=200) + + resp = session.get('http://test.com/1') + assert resp.status_code == 200 + assert resp.json() == {'a': 'b'} + +def test_get_batch_call5(client, baseurl): + """A failing GET call with 404 should return HTTP 200, and the request with json content""" + session = requests.Session() + adapter = requests_mock.Adapter() + session.mount('http://', adapter) + adapter.register_uri('GET', 'http://localhost/api/batch', json={'a': 'b'}, status_code=404) + + resp = client.call(method_name='GET', url='http://localhost/api/batch') + assert resp.status_code == 200 + assert resp.text == "test" + # assert resp.json() == {'a': 'b'} + +def test_get_batch_call6(client, baseurl): + """A failing GET call with 401 should return custom HTTP status""" + session = requests.Session() + adapter = requests_mock.Adapter() + session.mount('http://', adapter) + adapter.register_uri('GET', 'http://test.com/1', text="", status_code=401) + + resp = client.call(method_name='GET', url='http://test.com/1') + assert resp.status_code == 200 + print("TExt: ------------") + print(resp.text) + print("JSON: ------------") + print(resp.json()) + assert resp.json() == {'a': 'b'} + diff --git a/tests/test_endpoints.py b/tests/test_endpoints.py index fcf3743..8d30d7f 100644 --- a/tests/test_endpoints.py +++ b/tests/test_endpoints.py @@ -6,3 +6,31 @@ def test_init(baseurl): assert endpoints.baseurl == baseurl +def test_init_batchurl(baseurl): + endpoints = SetraEndpoints(baseurl) + assert endpoints.batch_url == "api/batch/" + + +# Test urls: +def test_init_batch_with_value(baseurl): + endpoints = SetraEndpoints(baseurl) + assert endpoints.batch(batch_id="5") == baseurl + "/api/batch/5" + +def test_init_batch_with_empty_value(baseurl): + endpoints = SetraEndpoints(baseurl) + assert endpoints.batch(batch_id="") == baseurl + "/api/batch/" + + +def test_init_batch_without_value(baseurl): + endpoints = SetraEndpoints(baseurl) + assert endpoints.batch() == baseurl + "/api/batch/" + + +def test_init_transaction_with_value(baseurl): + endpoints = SetraEndpoints(baseurl) + assert endpoints.transaction(trans_id="5") == baseurl + "/api/transaction/5" + +def test_init_transaction_without_value(baseurl): + endpoints = SetraEndpoints(baseurl) + assert endpoints.transaction() == baseurl + "/api/transaction/" + -- GitLab