Skip to content

Commit d69af1c

Browse files
committed
Add tests for helper functions
1 parent a4710f0 commit d69af1c

12 files changed

Lines changed: 114 additions & 13 deletions

File tree

splitapiclient/main/sync_apiclient.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __init__(self, config):
2727
self._base_url = config['base_url']
2828
self._apikey = config['apikey']
2929
else:
30-
missing = [i not in config for i in ['base_url', 'apikey']]
30+
missing = [i for i in ['base_url', 'apikey'] if i not in config]
3131
raise InsufficientConfigArgumentsException(
3232
'The following keys must be present in the config dict: %s'
3333
% ','.join(missing)

splitapiclient/resources/attribute.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ class Attribute(BaseResource):
1616
'isSearchable': 'bool',
1717
}
1818

19-
def __init__(self, data, client=None):
19+
def __init__(self, data=None, client=None):
2020
'''
2121
'''
22+
if not data:
23+
data = {}
2224
BaseResource.__init__(self, data.get('id'), client)
2325
self._traffic_type_id = data.get('trafficTypeId')
2426
self._display_name = data.get('displayName')

splitapiclient/resources/environment.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ class Environment(BaseResource):
1212
'name': 'string',
1313
}
1414

15-
def __init__(self, data, client=None):
15+
def __init__(self, data=None, client=None):
1616
'''
1717
'''
18+
if not data:
19+
data = {}
1820
BaseResource.__init__(self, data.get('id'), client)
1921
self._name = data.get('name')
2022

splitapiclient/resources/identity.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ class Identity(BaseResource):
1515
'organizationId': 'string',
1616
}
1717

18-
def __init__(self, data, client=None):
18+
def __init__(self, data=None, client=None):
1919
'''
2020
'''
21+
if not data:
22+
data = {}
2123
BaseResource.__init__(self, None, client)
2224
self._traffic_type_id = data.get('trafficTypeId')
2325
self._key = data.get('key')

splitapiclient/resources/traffic_type.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ class TrafficType(BaseResource):
1212
'displayAttributeId': 'string'
1313
}
1414

15-
def __init__(self, data, client=None):
15+
def __init__(self, data=None, client=None):
1616
'''
1717
Constructor
1818
'''
19+
if not data:
20+
data = {}
1921
BaseResource.__init__(self, data.get('id'), client)
2022
self._name = data.get('name')
2123
self._display_attribute_id = data.get('displayAttributeId')

splitapiclient/tests/resources/test_attribute.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def test_constructor(self, mocker):
4141
def test_getters_and_setters(self):
4242
'''
4343
'''
44-
attr1 = Attribute({})
44+
attr1 = Attribute()
4545
attr1.id = 'a'
4646
attr1.traffic_type_id = 'b'
4747
attr1.display_name = 'c'

splitapiclient/tests/resources/test_environment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_constructor(self, mocker):
3535
def test_getters_and_setters(self):
3636
'''
3737
'''
38-
env1 = Environment({})
38+
env1 = Environment()
3939
env1.id = 'a'
4040
env1.name = 'b'
4141

splitapiclient/tests/resources/test_identity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_constructor(self, mocker):
3535
def test_getters_and_setters(self):
3636
'''
3737
'''
38-
identity1 = Identity({})
38+
identity1 = Identity()
3939
identity1.id = 'a'
4040
identity1.key = 'b'
4141
identity1.traffic_type_id = 'c'

splitapiclient/tests/resources/test_traffic_type.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_constructor(self, mocker):
3838
def test_getters_and_setters(self):
3939
'''
4040
'''
41-
tt1 = TrafficType({})
41+
tt1 = TrafficType()
4242
tt1.id = 'a'
4343
tt1.name = 'b'
4444
tt1.display_attribute_id = 'c'
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
from __future__ import absolute_import, division, print_function, \
2+
unicode_literals
3+
4+
from splitapiclient.util.exceptions import InvalidArgumentException, \
5+
ClientRequiredError, InvalidModelException
6+
from splitapiclient.resources.base_resource import BaseResource
7+
from splitapiclient.resources import Attribute, Environment, Identity, TrafficType
8+
from splitapiclient.main.apiclient import BaseApiClient
9+
from splitapiclient.http_clients.base_client import BaseHttpClient
10+
from splitapiclient.microclients.attribute_microclient import AttributeMicroClient
11+
from splitapiclient.microclients.environment_microclient import EnvironmentMicroClient
12+
from splitapiclient.microclients.identity_microclient import IdentityMicroClient
13+
from splitapiclient.microclients.traffic_type_microclient import TrafficTypeMicroClient
14+
from splitapiclient.main.sync_apiclient import SyncApiClient
15+
from splitapiclient.util import helpers
16+
import pytest
17+
18+
19+
class TestHelperFuntions:
20+
'''
21+
'''
22+
23+
def test_require_client(self, mocker):
24+
'''
25+
'''
26+
httpclient = mocker.Mock(spec=BaseHttpClient)
27+
apiclient = SyncApiClient({'base_url': 'http://test', 'apikey': '123'})
28+
29+
models = ['Attribute', 'Environment', 'Identity', 'TrafficType']
30+
microclients = [
31+
AttributeMicroClient,
32+
EnvironmentMicroClient,
33+
IdentityMicroClient,
34+
TrafficTypeMicroClient,
35+
]
36+
# Test an exception is thrown if no client is passed
37+
for m in models:
38+
with pytest.raises(ClientRequiredError):
39+
helpers.require_client(m, None, None)
40+
41+
# Test that an invalid model raises an exception
42+
with pytest.raises(InvalidModelException):
43+
helpers.require_client('invalidModel', None, None)
44+
45+
# Test that passing httpClient and no apiclient works
46+
for i, m in enumerate(models):
47+
c = helpers.require_client(m, httpclient, None)
48+
assert isinstance(c, microclients[i])
49+
50+
# Test that passing ApiClient and no httpclient works
51+
for i, m in enumerate(models):
52+
c = helpers.require_client(m, None, apiclient)
53+
assert isinstance(c, microclients[i])
54+
55+
# Test that passing both clients works
56+
for i, m in enumerate(models):
57+
c = helpers.require_client(m, httpclient, apiclient)
58+
assert isinstance(c, microclients[i])
59+
60+
def test_as_dict(self, mocker):
61+
'''
62+
'''
63+
model_instances = [
64+
Attribute(),
65+
Identity(),
66+
Environment(),
67+
TrafficType()
68+
]
69+
70+
# Test it works for all subclasses of BaseResource
71+
for instance in model_instances:
72+
assert isinstance(helpers.as_dict(instance), dict)
73+
74+
# Test it works for dictionaries
75+
assert isinstance(helpers.as_dict(dict()), dict)
76+
77+
# Test if fails for anything other thatn those 2 cases
78+
with pytest.raises(InvalidArgumentException):
79+
helpers.as_dict('a string')

0 commit comments

Comments
 (0)