1+ import platform
12from unittest import TestCase
23
34import pytest
4- from alma import Client
5+ from alma import ApiModes , Client
6+ from alma .version import __version__ as alma_version
57
68
79class 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