Skip to content

Commit fece92c

Browse files
Adding fail cases for get_table and upload_file
1 parent fa56437 commit fece92c

File tree

3 files changed

+69
-12
lines changed

3 files changed

+69
-12
lines changed

src/sasctl/_services/score_definitions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ def create_score_definition(
7777
raise HTTPError({
7878
f"This model may not exist in a project or the model may not exist at all. See error: {model.json()}"}
7979
)
80-
model_project_id = model.json()["projectId"]
81-
model_project_version_id = model.json()["projectVersionId"]
82-
model_name = model.json()["name"]
80+
model_project_id = model.get("projectId")
81+
model_project_version_id = model.get("projectVersionId")
82+
model_name = model.get("name")
8383
# Checking if the model exists and if it's in a project
8484

8585
try:
8686
inputMapping = []
87-
for input_item in model.json()["inputVariables"]:
87+
for input_item in model.get("inputVariables"):
8888
var = {
8989
"mappingValue": input_item["name"],
9090
"mappingType": "datasource",

src/sasctl/_services/score_execution.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ def create_score_execution(
5757
score_definition = sd.get_definition(score_definition_id)
5858
if score_definition.status_code >= 400:
5959
raise HTTPError(score_definition.json())
60-
score_exec_name = score_definition.json()["name"]
61-
model_uri = score_definition.json()["objectDescriptor"]["uri"]
62-
model_name = score_definition.json()["objectDescriptor"]["name"]
63-
model_input_library = score_definition.json()["inputData"]["libraryName"]
64-
model_table_name = score_definition.json()["inputData"]["tableName"]
60+
score_exec_name = score_definition.get("name")
61+
model_uri = score_definition.get("objectDescriptor", ["uri"])
62+
model_name = score_definition.get("objectDescriptor", ["name"])
63+
model_input_library = score_definition.get("inputData", ["libraryName"])
64+
model_table_name = score_definition.get("inputData", ["tableName"])
6565

6666
# Defining a default output table name if none is provided
6767
if not output_table_name:
@@ -72,11 +72,11 @@ def create_score_execution(
7272
score_execution = cls.get(
7373
f"/executions?filter=eq(scoreExecutionRequest.scoreDefinitionId,%27{score_definition_id}%27)"
7474
)
75-
execution_count = score_execution.json()[
75+
execution_count = score_execution.get(
7676
"count"
77-
] # Exception catch location
77+
) # Exception catch location
7878
if execution_count == 1:
79-
execution_id = score_execution.json()["items"][0]["id"]
79+
execution_id = score_execution.get("items", [0], ["id"])
8080
deleted_score_execution = cls.delete_execution(execution_id)
8181
print(deleted_score_execution)
8282
except KeyError:

tests/unit/test_score_definitions.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,24 @@ def test_create_score_definition():
5656
model_id = "12345",
5757
table_name = "test_table"
5858
)
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+
# )
5977

6078
get_model.return_value.status_code = 200
6179
get_model.return_value.json.return_value = {
@@ -71,6 +89,45 @@ def test_create_score_definition():
7189
model_id = "12345",
7290
table_name = "test_table"
7391
)
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+
# )
126+
127+
128+
129+
130+
74131

75132
# # Test for valid model id, no input mapping, get_table succeeded, and post succeeds
76133

0 commit comments

Comments
 (0)