Skip to content
Snippets Groups Projects
Commit 7cc31562 authored by rha104's avatar rha104
Browse files

Adding a few more tests

parent 18c2e717
No related branches found
No related tags found
1 merge request!4Added lots of tests. And fixed some issues with the client, discovered while testing
Pipeline #51037 failed
......@@ -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
```
"""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.
......
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
......
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'}
......@@ -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/"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment