Skip to content

Commit 8b447b2

Browse files
First version of all test cases for score definition
1 parent fece92c commit 8b447b2

File tree

2 files changed

+114
-128
lines changed

2 files changed

+114
-128
lines changed

src/sasctl/_services/score_definitions.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ class ScoreDefinitions(Service):
2828
_cas_management = CASManagement()
2929
_model_respository = ModelRepository()
3030

31-
list_definitions, get_definition, update_definition, delete_definition = (
32-
Service._crud_funcs("/definitions", "definition")
33-
)
31+
(
32+
list_definitions,
33+
get_definition,
34+
update_definition,
35+
delete_definition,
36+
) = Service._crud_funcs("/definitions", "definition")
3437

3538
@classmethod
3639
def create_score_definition(
@@ -72,10 +75,12 @@ def create_score_definition(
7275
"""
7376

7477
model = cls._model_respository.get_model(model_id)
75-
78+
7679
if model.status_code >= 400:
77-
raise HTTPError({
78-
f"This model may not exist in a project or the model may not exist at all. See error: {model.json()}"}
80+
raise HTTPError(
81+
{
82+
f"This model may not exist in a project or the model may not exist at all. See error: {model.json()}"
83+
}
7984
)
8085
model_project_id = model.get("projectId")
8186
model_project_version_id = model.get("projectVersionId")
@@ -98,21 +103,25 @@ def create_score_definition(
98103

99104
table = cls._cas_management.get_table(server_name, library_name, table_name)
100105
if table.status_code >= 400 and not table_file:
101-
raise HTTPError(f"This table may not exist in CAS. Please include the `table_file` argument in the function call if it doesn't exist. See error {table.json()}")
102-
elif table.status_code >= 400 and table_file:
106+
raise HTTPError(
107+
f"This table may not exist in CAS. Please include the `table_file` argument in the function call if it doesn't exist. See error {table.json()}"
108+
)
109+
elif table.status_code >= 400 and table_file:
103110
cls._cas_management.upload_file(
104111
str(table_file), table_name
105112
) # do I need to add a check if the file doesn't exist or does upload_file take care of that?
106113
table = cls._cas_management.get_table(server_name, library_name, table_name)
107114
if table.status_code >= 400:
108-
raise HTTPError(f"The file failed to upload properly or another error occurred. See the error: {table.json()}")
115+
raise HTTPError(
116+
f"The file failed to upload properly or another error occurred. See the error: {table.json()}"
117+
)
109118
# Checks if the inputted table exists, and if not, uploads a file to create a new table
110119

111120
save_score_def = {
112121
"name": score_def_name,
113122
"description": description,
114123
"objectDescriptor": {
115-
"uri": f"/modelManagement/models/{model_id}",
124+
"uri": f"/modelRepository/models/{model_id}",
116125
"name": f"{model_name}({model_version})",
117126
"type": "sas.models.model",
118127
},
@@ -136,9 +145,7 @@ def create_score_definition(
136145

137146
headers_score_def = {"Content-Type": "application/json"}
138147

139-
new_score_def = cls.post(
148+
return cls.post(
140149
"/definitions", data=json.dumps(save_score_def), headers=headers_score_def
141150
)
142-
143-
return new_score_def
144-
# The response information of the score definition can be seen as a JSON as well as the RestOBJ
151+
# The response information of the score definition can be seen as a JSON as well as a RestOBJ

tests/unit/test_score_definitions.py

Lines changed: 93 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -23,132 +23,111 @@
2323
from sasctl.core import RestObj, VersionInfo, request
2424
from sasctl._services.score_definitions import ScoreDefinitions as sd
2525

26+
2627
def test_create_score_definition():
2728
"""
2829
Test Cases:
29-
- Valid model id?
30-
- yes
31-
- no
32-
- Valid input mapping?
33-
- yes
34-
- no
35-
-Valid table name?
36-
-yes
37-
-no
38-
-Valid file?
39-
-yes
40-
-no
30+
- Valid model id with input mapping and valid table name
31+
- Invalid model id
32+
- Valid table name without input mapping
33+
- Invalid table name with invalid file
34+
- Invalid table name with valid file and without input mapping
35+
4136
"""
42-
37+
with mock.patch("sasctl.core.Session._get_authorization_token"):
38+
current_session("example.com", "username", "password")
39+
4340
with mock.patch(
4441
"sasctl._services.model_repository.ModelRepository.get_model"
4542
) as get_model:
46-
with mock.patch(
43+
with mock.patch(
4744
"sasctl._services.cas_management.CASManagement.get_table"
4845
) as get_table:
4946
with mock.patch(
5047
"sasctl._services.cas_management.CASManagement.upload_file"
5148
) as upload_file:
52-
get_model.return_value.status_code = 404
53-
with pytest.raises(HTTPError):
54-
sd.create_score_definition(
55-
score_def_name = "test_create_sd",
56-
model_id = "12345",
57-
table_name = "test_table"
49+
with mock.patch(
50+
"sasctl._services.score_definitions.ScoreDefinitions.post"
51+
) as post:
52+
get_model.return_value.status_code = 404
53+
with pytest.raises(HTTPError):
54+
sd.create_score_definition(
55+
score_def_name="test_create_sd",
56+
model_id="12345",
57+
table_name="test_table",
58+
)
59+
60+
get_model.return_value.status_code = 200
61+
get_model.return_value.json.return_value = {
62+
"id": "12345",
63+
"projectId": "54321",
64+
"projectVersionId": "67890",
65+
"name": "test_model",
66+
}
67+
get_table.return_value.status_code = 404
68+
with pytest.raises(HTTPError):
69+
sd.create_score_definition(
70+
score_def_name="test_create_sd",
71+
model_id="12345",
72+
table_name="test_table",
73+
)
74+
get_table.return_value.status_code = 404
75+
upload_file.return_value = None
76+
get_table.return_value.status_code = 404
77+
with pytest.raises(HTTPError):
78+
sd.create_score_definition(
79+
score_def_name="test_create_sd",
80+
model_id="12345",
81+
table_name="test_table",
82+
table_file="test_path",
83+
)
84+
85+
get_table.return_value.status_code = 404
86+
upload_file.return_value = RestObj
87+
get_table.return_value.status_code = 200
88+
get_table.return_value.json.return_value = {
89+
"tableName": "test_table"
90+
}
91+
response = sd.create_score_definition(
92+
score_def_name="test_create_sd",
93+
model_id="12345",
94+
table_name="test_table",
95+
table_file="test_path",
5896
)
59-
# get_model.return_value.status_code = 200
60-
# get_model.return_value.json.return_value = {
61-
# "id": "12345",
62-
# "projectId": "54321",
63-
# "projectVersionId": "67890",
64-
# "name": "test_model"
65-
# "inputVariables": {"A", "B", "C"} #look at syntax combine square brackets and curly bracket
66-
# }
67-
# sd.create_score_definition(
68-
# score_def_name = "test_create_sd",
69-
# model_id = "12345",
70-
# table_name = "test_table",
71-
# mappings = {
72-
# {"mappingVariable": "A", "datasource": "datasource", "variableName": "A"},
73-
# {"mappingVariable": "B", "datasource": "datasource", "variableName": "B"},
74-
# {"mappingVariable": "C", "datasource": "datasource", "variableName": "C"}
75-
# }
76-
# )
77-
78-
get_model.return_value.status_code = 200
79-
get_model.return_value.json.return_value = {
80-
"id": "12345",
81-
"projectId": "54321",
82-
"projectVersionId": "67890",
83-
"name": "test_model",
84-
}
85-
get_table.return_value.status_code = 404
86-
with pytest.raises(HTTPError):
87-
sd.create_score_definition(
88-
score_def_name = "test_create_sd",
89-
model_id = "12345",
90-
table_name = "test_table"
97+
assert response
98+
99+
get_table.return_value.status_code = 200
100+
get_table.return_value.json.return_value = {
101+
"tableName": "test_table"
102+
}
103+
response = sd.create_score_definition(
104+
score_def_name="test_create_sd",
105+
model_id="12345",
106+
table_name="test_table",
107+
table_file="test_path",
91108
)
92-
get_table.return_value.status_code = 404
93-
upload_file.return_value = None
94-
get_table.return_value.status_code = 404
95-
with pytest.raises(HTTPError):
96-
sd.create_score_definition(
97-
score_def_name = "test_create_sd",
98-
model_id = "12345",
99-
table_name = "test_table",
100-
table_file = "test_path"
101-
)
102-
103-
get_table.return_value.status_code = 404
104-
upload_file.return_value = RestObj
105-
get_table.return_value.status_code = 200
106-
response = sd.create_score_definition(
107-
score_def_name = "test_create_sd",
108-
model_id = "12345",
109-
table_name = "test_table",
110-
table_file = "test_path"
111-
)
112-
assert response
113-
114-
115-
116-
117-
# get_table.return_value.status_code = 200
118-
# get_table.return_value.json.return_value = {
119-
# "tableName": "test_table"
120-
# }
121-
# sd.create_score_definition(
122-
# score_def_name = "test_create_sd",
123-
# model_id = "12345",
124-
# table_name = "test_table"
125-
# )
109+
assert response
126110

127-
128-
129-
130-
131-
132-
# # Test for valid model id, no input mapping, get_table succeeded, and post succeeds
133-
134-
# get_table.return_value = RestObj()
135-
# raise_for_status.return_value = None
136-
137-
138-
139-
# assert response
140-
141-
# Test for invalid model id
142-
#get_model.return_value = RestObj()
143-
#with pytest.raises(
144-
#HTTPError
145-
#): #test tget_Table since we don't know what the return value is restobj
146-
#sd.create_score_definition(
147-
#score_def_name = "test_create_sd",
148-
#model_id = "12345",
149-
#table_name = "test_table",
150-
#)
151-
#Test for valid and invalid for each
152-
#Start with testing invalid and realize you can work from the last call to the first because the last call working means the first call must have worked
153-
154-
111+
get_model.return_value.status_code = 200
112+
get_model.return_value.json.return_value = {
113+
"id": "12345",
114+
"projectId": "54321",
115+
"projectVersionId": "67890",
116+
"name": "test_model",
117+
"inputVariables": [
118+
{"name": "first"},
119+
{"name": "second"},
120+
{"name": "third"},
121+
],
122+
}
123+
get_table.return_value.status_code = 200
124+
get_table.return_value.json.return_value = {
125+
"tableName": "test_table"
126+
}
127+
response = sd.create_score_definition(
128+
score_def_name="test_create_sd",
129+
model_id="12345",
130+
table_name="test_table",
131+
)
132+
assert response
133+
assert post.call_count == 3

0 commit comments

Comments
 (0)