Skip to content
This repository was archived by the owner on Dec 8, 2023. It is now read-only.

Commit 8a4a1ab

Browse files
committed
Add client tests.
1 parent 65f8581 commit 8a4a1ab

2 files changed

Lines changed: 70 additions & 4 deletions

File tree

alma/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ def __init__(self, api_key, **options):
2828
ApiModes.LIVE: options["api_root"],
2929
}
3030
elif type(options["api_root"]) is not dict:
31-
raise TypeError("`api_root` option must be a dict or a string")
31+
raise ValueError("`api_root` option must be a dict or a string")
3232

3333
if options["mode"] not in (ApiModes.LIVE, ApiModes.TEST):
3434
raise ValueError(
3535
"`mode` option must be one of ({LIVE}, {TEST})".format(
36-
LIVE=ApiModes.LIVE, TEST=ApiModes.TEST
36+
LIVE=ApiModes.LIVE.value, TEST=ApiModes.TEST.value
3737
)
3838
)
3939

@@ -47,7 +47,7 @@ def add_user_agent_component(self, component, version):
4747

4848
def init_user_agent(self):
4949
self.add_user_agent_component("Python", platform.python_version())
50-
self.add_user_agent_component("Alma for Python", alma_version)
50+
self.add_user_agent_component("alma-client", alma_version)
5151

5252
def _endpoint(self, endpoint_name):
5353
endpoint = self._endpoints.get(endpoint_name)

tests/test_client.py

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import platform
12
from unittest import TestCase
23

34
import pytest
4-
from alma import Client
5+
from alma import ApiModes, Client
6+
from alma.version import __version__ as alma_version
57

68

79
class ClientTest(TestCase):
@@ -18,3 +20,67 @@ def test_client_raises_with_empty_api_key_value(self):
1820
def test_client_raises_with_empty_api_key_set_to_None(self):
1921
with pytest.raises(ValueError, match=r"An API key is required"):
2022
Client(api_key=None)
23+
24+
def test_client_detects_mode_with_regards_to_the_suggested_key(self):
25+
alma_client = Client(api_key="sk_live_3geNR4OVI0gQGCAKgcYqQs2I")
26+
assert alma_client.context.options["mode"] == ApiModes.LIVE
27+
alma_client = Client(api_key="sk_test_l6C92m3mqmS0aWcscEw62Xk4")
28+
assert alma_client.context.options["mode"] == ApiModes.TEST
29+
30+
def test_client_can_override_api_root_with_a_string(self):
31+
api_root = "http://localhost"
32+
alma_client = Client(api_key="sk_live_3geNR4OVI0gQGCAKgcYqQs2I", api_root=api_root)
33+
assert alma_client.context.options["api_root"] == {
34+
ApiModes.LIVE: api_root,
35+
ApiModes.TEST: api_root,
36+
}
37+
38+
def test_client_can_override_api_root_with_a_dict(self):
39+
api_root = "http://localhost"
40+
alma_client = Client(
41+
api_key="sk_live_3geNR4OVI0gQGCAKgcYqQs2I", api_root={ApiModes.LIVE: api_root}
42+
)
43+
assert alma_client.context.options["api_root"] == {
44+
ApiModes.LIVE: api_root,
45+
}
46+
47+
def test_client_cannot_override_api_root_with_something_else(self):
48+
with pytest.raises(ValueError, match=r"`api_root` option must be a dict or a string"):
49+
Client(api_key="sk_live_3geNR4OVI0gQGCAKgcYqQs2I", api_root=12)
50+
51+
def test_client_cannot_override_mode_with_something_else(self):
52+
with pytest.raises(ValueError, match=r"`mode` option must be one of \(live, test\)"):
53+
Client(api_key="sk_live_3geNR4OVI0gQGCAKgcYqQs2I", mode="blah")
54+
55+
def test_client_expose_endpoints(self):
56+
alma_client = Client(api_key="sk_live_3geNR4OVI0gQGCAKgcYqQs2I")
57+
assert alma_client.payments.fetch_all
58+
assert alma_client.orders.fetch_all
59+
assert alma_client.exports.fetch_all
60+
61+
# Exposed but empty
62+
assert alma_client.merchants
63+
64+
65+
class ContextTest(TestCase):
66+
def setUp(self):
67+
client = Client(api_key="sk_live_3geNR4OVI0gQGCAKgcYqQs2I")
68+
self.context = client.context
69+
70+
def test_context_expose_logger(self):
71+
assert self.context.logger.name == "alma-python-client"
72+
assert self.context.logger.level == 0
73+
74+
def test_context_expose_api_root_and_mode(self):
75+
assert self.context.api_root == self.context.options["api_root"]
76+
assert self.context.mode == self.context.options["mode"]
77+
78+
def test_context_expose_url_for(self):
79+
assert self.context.url_for("/path/test") == "https://api.getalma.eu/path/test"
80+
81+
def test_context_expose_user_agent_string(self):
82+
assert self.context.user_agent_string().startswith(
83+
"alma-client/{alma_version}; Python/{python_version}".format(
84+
alma_version=alma_version, python_version=platform.python_version()
85+
)
86+
)

0 commit comments

Comments
 (0)