9
9
10
10
import unittest
11
11
import Algorithmia
12
+ from uuid import uuid4
12
13
13
14
if sys .version_info .major == 3 :
14
15
unicode = str
15
16
16
17
class ClientTest (unittest .TestCase ):
17
18
seed (datetime .now ().microsecond )
18
-
19
- username = "a_Mrtest"
20
- org_name = "a_myOrg"
19
+ # due to legacy reasons, regular client tests are tested against api.algorithmia.com, whereas admin api tests are run
20
+ # against test.algorithmia.com.
21
+ admin_username = "a_Mrtest"
22
+ admin_org_name = "a_myOrg"
23
+ environment_name = "Python 3.9"
21
24
22
25
def setUp (self ):
23
26
self .admin_api_key = unicode (os .environ .get ('ALGORITHMIA_A_KEY' ))
27
+ self .regular_api_key = unicode (os .environ .get ('ALGORITHMIA_API_KEY' ))
28
+
29
+ self .admin_username = self .admin_username + str (int (random () * 10000 ))
30
+ self .admin_org_name = self .admin_org_name + str (int (random () * 10000 ))
31
+ self .admin_client = Algorithmia .client (api_address = "https://test.algorithmia.com" ,
32
+ api_key = self .admin_api_key )
33
+ self .regular_client = Algorithmia .client (api_address = 'https://api.algorithmia.com' ,
34
+ api_key = self .regular_api_key )
24
35
25
- self . username = self .username + str ( int ( random () * 10000 ) )
26
- self . org_name = self . org_name + str ( int ( random () * 10000 ))
27
- self . c = Algorithmia . client ( api_address = "https://test.algorithmia.com" ,
28
- api_key = self .admin_api_key )
36
+ environments = self .regular_client . get_environment ( "python3" )
37
+ for environment in environments [ 'environments' ]:
38
+ if environment [ 'display_name' ] == self . environment_name :
39
+ self .environment_id = environment [ 'id' ]
29
40
30
41
def test_create_user (self ):
31
- response = self .c .create_user (
32
- {"username" : self .username , "email" : self .username + "@algo.com" , "passwordHash" : "" ,
42
+ response = self .admin_client .create_user (
43
+ {"username" : self .admin_username , "email" : self .admin_username + "@algo.com" , "passwordHash" : "" ,
33
44
"shouldCreateHello" : False })
34
45
35
46
if type (response ) is dict :
36
- self .assertEqual (self .username , response ['username' ])
47
+ self .assertEqual (self .admin_username , response ['username' ])
37
48
else :
38
49
self .assertIsNotNone (response )
39
50
40
51
def test_get_org_types (self ):
41
- response = self .c .get_org_types ()
52
+ response = self .admin_client .get_org_types ()
42
53
self .assertTrue (len (response ) > 0 )
43
54
44
55
def test_create_org (self ):
45
- response = self .c .create_org (
46
- {"org_name" : self .org_name , "org_label" : "some label" , "org_contact_name" : "Some owner" ,
47
- "org_email" : self .org_name + "@algo.com" , "type_id" : "basic" })
56
+ response = self .admin_client .create_org (
57
+ {"org_name" : self .admin_org_name , "org_label" : "some label" , "org_contact_name" : "Some owner" ,
58
+ "org_email" : self .admin_org_name + "@algo.com" , "type_id" : "basic" })
48
59
49
- self .assertEqual (self .org_name , response [u'org_name' ])
60
+ self .assertEqual (self .admin_org_name , response [u'org_name' ])
50
61
51
62
def test_get_org (self ):
52
- response = self .c .get_org ("a_myOrg84" )
63
+ response = self .admin_client .get_org ("a_myOrg84" )
53
64
self .assertEqual ("a_myOrg84" , response ['org_name' ])
54
65
55
66
def test_get_environment (self ):
56
- client = Algorithmia .client (api_key = unicode (os .environ .get ('ALGORITHMIA_API_KEY' )))
57
- response = client .get_environment ("python2" )
67
+ response = self .admin_client .get_environment ("python2" )
58
68
59
69
if u'error' not in response :
60
70
self .assertTrue (response is not None and u'environments' in response )
61
71
62
72
def test_get_build_logs (self ):
63
- client = Algorithmia .client (api_address = 'https://api.algorithmia.com' ,
64
- api_key = unicode (os .environ .get ('ALGORITHMIA_API_KEY' )))
65
73
user = unicode (os .environ .get ('ALGO_USER_NAME' ))
66
74
algo = unicode ('echo' )
67
75
algo_path = u'%s/%s' % (user , algo )
68
- result = client .algo (algo_path ).build_logs ()
76
+ result = self . regular_client .algo (algo_path ).build_logs ()
69
77
70
78
if u'error' in result :
71
79
print (result )
@@ -74,7 +82,7 @@ def test_get_build_logs(self):
74
82
75
83
def test_get_build_logs_no_ssl (self ):
76
84
client = Algorithmia .client (api_address = 'https://api.algorithmia.com' ,
77
- api_key = unicode ( os . environ . get ( 'ALGORITHMIA_API_KEY' )) , ca_cert = False )
85
+ api_key = self . regular_api_key , ca_cert = False )
78
86
user = unicode (os .environ .get ('ALGO_USER_NAME' ))
79
87
algo = u'Echo'
80
88
result = client .algo (user + '/' + algo ).build_logs ()
@@ -97,15 +105,15 @@ def test_edit_org(self):
97
105
"resource_type" : "organization"
98
106
}
99
107
100
- response = self .c .edit_org (org_name , obj )
108
+ response = self .admin_client .edit_org (org_name , obj )
101
109
if type (response ) is dict :
102
110
print (response )
103
111
else :
104
112
self .assertEqual (204 , response .status_code )
105
113
106
114
def test_get_template (self ):
107
115
filename = "./temptest"
108
- response = self .c .get_template ("36fd467e-fbfe-4ea6-aa66-df3f403b7132" , filename )
116
+ response = self .admin_client .get_template ("36fd467e-fbfe-4ea6-aa66-df3f403b7132" , filename )
109
117
110
118
if type (response ) is dict :
111
119
self .assertTrue (u'error' in response or u'message' in response )
@@ -117,7 +125,7 @@ def test_get_template(self):
117
125
print (e )
118
126
119
127
def test_get_supported_languages (self ):
120
- response = self .c .get_supported_languages ()
128
+ response = self .admin_client .get_supported_languages ()
121
129
self .assertTrue (response is not None )
122
130
123
131
if type (response ) is not list :
@@ -127,7 +135,7 @@ def test_get_supported_languages(self):
127
135
self .assertTrue (response is not None and language_found )
128
136
129
137
def test_invite_to_org (self ):
130
- response = self .c .invite_to_org ("a_myOrg38" , "a_Mrtest4" )
138
+ response = self .admin_client .invite_to_org ("a_myOrg38" , "a_Mrtest4" )
131
139
if type (response ) is dict :
132
140
self .assertTrue (u'error' in response )
133
141
else :
@@ -136,20 +144,20 @@ def test_invite_to_org(self):
136
144
# This test will require updating after the /v1/organizations/{org_name}/errors endpoint has been
137
145
# deployed to the remote environment.
138
146
def test_get_organization_errors (self ):
139
- response = self .c .get_organization_errors (self .org_name )
147
+ response = self .admin_client .get_organization_errors (self .admin_org_name )
140
148
self .assertTrue (response is not None )
141
149
142
150
if type (response ) is list :
143
151
self .assertEqual (0 , len (response ), 'Received unexpected result, should have been 0.' )
144
152
145
153
def test_get_user_errors (self ):
146
- response = self .c .get_user_errors (self .username )
154
+ response = self .admin_client .get_user_errors (self .admin_username )
147
155
148
156
self .assertTrue (response is not None )
149
157
self .assertEqual (0 , len (response ))
150
158
151
159
def test_get_algorithm_errors (self ):
152
- response = self .c .get_algorithm_errors ('hello' )
160
+ response = self .admin_client .get_algorithm_errors ('hello' )
153
161
self .assertTrue (response is not None )
154
162
155
163
if type (response ) is dict :
@@ -158,5 +166,61 @@ def test_get_algorithm_errors(self):
158
166
self .assertEqual (404 , response .status_code )
159
167
160
168
169
+ def test_algorithm_programmatic_create_process (self ):
170
+ algorithm_name = "algo_" + str (uuid4 ()).split ("-" )[- 1 ]
171
+ payload = "John"
172
+ expected_response = "hello John"
173
+ full_path = self .regular_client .username () + "/" + algorithm_name
174
+ details = {
175
+ "summary" : "Example Summary" ,
176
+ "label" : "QA" ,
177
+ "tagline" : "Example Tagline"
178
+ }
179
+ settings = {
180
+ "source_visibility" : "open" ,
181
+ "algorithm_environment" : self .environment_id ,
182
+ "license" : "apl" ,
183
+ "network_access" : "isolated" ,
184
+ "pipeline_enabled" : False
185
+ }
186
+ created_algo = self .regular_client .algo (full_path )
187
+ response = created_algo .create (details = details ,settings = settings )
188
+ self .assertEqual (response .name , algorithm_name , "algorithm creation failed" )
189
+
190
+ # --- Creation complete, compiling
191
+
192
+ response = created_algo .compile ()
193
+ git_hash = response .version_info .git_hash
194
+ algo_with_build = self .regular_client .algo (full_path + "/" + git_hash )
195
+ self .assertEqual (response .name , created_algo .algoname )
196
+
197
+ # --- compiling complete, now testing algorithm request
198
+ response = algo_with_build .pipe (payload ).result
199
+ self .assertEqual (response , expected_response , "compiling failed" )
200
+
201
+ # --- testing complete, now publishing new release.
202
+
203
+ pub_settings = {"algorithm_callability" : "private" }
204
+ pub_version_info = {
205
+ "release_notes" : "created programmatically" ,
206
+ "sample_input" : payload ,
207
+ "version_type" : "minor"
208
+ }
209
+ pub_details = {"label" : "testing123" }
210
+
211
+ response = algo_with_build .publish (
212
+ details = pub_details ,
213
+ settings = pub_settings ,
214
+ version_info = pub_version_info
215
+ )
216
+ self .assertEqual (response .version_info .semantic_version , "0.1.0" , "Publishing failed, semantic version is not correct." )
217
+
218
+ # --- publishing complete, getting additional information
219
+
220
+ response = created_algo .info (git_hash )
221
+
222
+ self .assertEqual (response .version_info .semantic_version , "0.1.0" , "information is incorrect" )
223
+
224
+
161
225
if __name__ == '__main__' :
162
226
unittest .main ()
0 commit comments