Skip to content

Commit 3c53068

Browse files
authored
ALGO-XXX adding better test coverage (#108)
* added new test to ensure we have create, compile, and publish coverage. Minor bugfixes * added dyanmic environment gathering, added .info() test coverage
1 parent ab51d4c commit 3c53068

File tree

3 files changed

+97
-32
lines changed

3 files changed

+97
-32
lines changed

Algorithmia/algorithm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def versions(self, limit=None, marker=None, published=None, callable=None):
166166
def compile(self):
167167
try:
168168
# Compile algorithm
169-
api_response = self.client.manageApi.algorithms_username_algoname_compile_post(self.username, self.algoname)
169+
api_response = self.client.manageApi.compile_algorithm(self.username, self.algoname)
170170
return api_response
171171
except ApiException as e:
172172
error_message = json.loads(e.body)

Algorithmia/datafile.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,10 @@ def __init__(self, client, dataUrl, cleanup=True):
250250

251251
def __del__(self):
252252
if self.local_file:
253+
filepath = self.local_file.name
253254
self.local_file.close()
254255
if self.cleanup:
255-
os.remove(self.local_file)
256+
os.remove(filepath)
256257

257258
def readable(self):
258259
return True
@@ -267,7 +268,7 @@ def read(self, __size=None):
267268
if not self.local_file:
268269
self.local_file = self.getFile()
269270
output = self.local_file.read()
270-
if __size:
271+
elif __size:
271272
output = self.local_file.read(__size)
272273
else:
273274
output = self.local_file.read()

Test/client_test.py

Lines changed: 93 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,63 +9,71 @@
99

1010
import unittest
1111
import Algorithmia
12+
from uuid import uuid4
1213

1314
if sys.version_info.major == 3:
1415
unicode = str
1516

1617
class ClientTest(unittest.TestCase):
1718
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"
2124

2225
def setUp(self):
2326
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)
2435

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']
2940

3041
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": "",
3344
"shouldCreateHello": False})
3445

3546
if type(response) is dict:
36-
self.assertEqual(self.username, response['username'])
47+
self.assertEqual(self.admin_username, response['username'])
3748
else:
3849
self.assertIsNotNone(response)
3950

4051
def test_get_org_types(self):
41-
response = self.c.get_org_types()
52+
response = self.admin_client.get_org_types()
4253
self.assertTrue(len(response) > 0)
4354

4455
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"})
4859

49-
self.assertEqual(self.org_name, response[u'org_name'])
60+
self.assertEqual(self.admin_org_name, response[u'org_name'])
5061

5162
def test_get_org(self):
52-
response = self.c.get_org("a_myOrg84")
63+
response = self.admin_client.get_org("a_myOrg84")
5364
self.assertEqual("a_myOrg84", response['org_name'])
5465

5566
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")
5868

5969
if u'error' not in response:
6070
self.assertTrue(response is not None and u'environments' in response)
6171

6272
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')))
6573
user = unicode(os.environ.get('ALGO_USER_NAME'))
6674
algo = unicode('echo')
6775
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()
6977

7078
if u'error' in result:
7179
print(result)
@@ -74,7 +82,7 @@ def test_get_build_logs(self):
7482

7583
def test_get_build_logs_no_ssl(self):
7684
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)
7886
user = unicode(os.environ.get('ALGO_USER_NAME'))
7987
algo = u'Echo'
8088
result = client.algo(user + '/' + algo).build_logs()
@@ -97,15 +105,15 @@ def test_edit_org(self):
97105
"resource_type": "organization"
98106
}
99107

100-
response = self.c.edit_org(org_name, obj)
108+
response = self.admin_client.edit_org(org_name, obj)
101109
if type(response) is dict:
102110
print(response)
103111
else:
104112
self.assertEqual(204, response.status_code)
105113

106114
def test_get_template(self):
107115
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)
109117

110118
if type(response) is dict:
111119
self.assertTrue(u'error' in response or u'message' in response)
@@ -117,7 +125,7 @@ def test_get_template(self):
117125
print(e)
118126

119127
def test_get_supported_languages(self):
120-
response = self.c.get_supported_languages()
128+
response = self.admin_client.get_supported_languages()
121129
self.assertTrue(response is not None)
122130

123131
if type(response) is not list:
@@ -127,7 +135,7 @@ def test_get_supported_languages(self):
127135
self.assertTrue(response is not None and language_found)
128136

129137
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")
131139
if type(response) is dict:
132140
self.assertTrue(u'error' in response)
133141
else:
@@ -136,20 +144,20 @@ def test_invite_to_org(self):
136144
# This test will require updating after the /v1/organizations/{org_name}/errors endpoint has been
137145
# deployed to the remote environment.
138146
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)
140148
self.assertTrue(response is not None)
141149

142150
if type(response) is list:
143151
self.assertEqual(0, len(response), 'Received unexpected result, should have been 0.')
144152

145153
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)
147155

148156
self.assertTrue(response is not None)
149157
self.assertEqual(0, len(response))
150158

151159
def test_get_algorithm_errors(self):
152-
response = self.c.get_algorithm_errors('hello')
160+
response = self.admin_client.get_algorithm_errors('hello')
153161
self.assertTrue(response is not None)
154162

155163
if type(response) is dict:
@@ -158,5 +166,61 @@ def test_get_algorithm_errors(self):
158166
self.assertEqual(404, response.status_code)
159167

160168

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+
161225
if __name__ == '__main__':
162226
unittest.main()

0 commit comments

Comments
 (0)