Skip to content

Commit 26fe616

Browse files
samyarpotlapallismlindauer
authored andcommitted
Corrected status code comparisons
1 parent 172dddd commit 26fe616

File tree

4 files changed

+55
-64
lines changed

4 files changed

+55
-64
lines changed

src/sasctl/_services/score_definitions.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import json
77
from typing import Union
88

9-
from ..core import current_session, delete, get, sasctl_command
9+
from ..core import current_session, delete, get, sasctl_command, RestObj
1010
from .cas_management import CASManagement
1111
from .model_repository import ModelRepository
1212
from .service import Service
@@ -76,10 +76,10 @@ def create_score_definition(
7676

7777
model = cls._model_respository.get_model(model_id)
7878

79-
if model.status_code >= 400:
79+
if not model:
8080
raise HTTPError(
8181
{
82-
f"This model may not exist in a project or the model may not exist at all. See error: {model.json()}"
82+
f"This model may not exist in a project or the model may not exist at all."
8383
}
8484
)
8585
model_project_id = model.get("projectId")
@@ -103,18 +103,18 @@ def create_score_definition(
103103
# Optional mapping - Maps the variables in the data to the variables of the score object. It's not necessary to create a score definition.
104104

105105
table = cls._cas_management.get_table(server_name, library_name, table_name)
106-
if table.status_code >= 400 and not table_file:
106+
if not table and not table_file:
107107
raise HTTPError(
108-
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+
f"This table may not exist in CAS. Please include the `table_file` argument in the function call if it doesn't exist."
109109
)
110-
elif table.status_code >= 400 and table_file:
110+
elif not table and table_file:
111111
cls._cas_management.upload_file(
112112
str(table_file), table_name
113113
) # do I need to add a check if the file doesn't exist or does upload_file take care of that?
114114
table = cls._cas_management.get_table(server_name, library_name, table_name)
115-
if table.status_code >= 400:
115+
if not table:
116116
raise HTTPError(
117-
f"The file failed to upload properly or another error occurred. See the error: {table.json()}"
117+
f"The file failed to upload properly or another error occurred."
118118
)
119119
# Checks if the inputted table exists, and if not, uploads a file to create a new table
120120

src/sasctl/_services/score_execution.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ def create_score_execution(
5959

6060
# Gets information about the scoring object from the score definition and raises an exception if the score definition does not exist
6161
score_definition = cls._score_definitions.get_definition(score_definition_id)
62-
if score_definition.status_code >= 400:
63-
raise HTTPError(score_definition.json())
62+
if not score_definition:
63+
raise HTTPError
6464
score_exec_name = score_definition.get("name")
6565
model_uri = score_definition.get("objectDescriptor", "uri")
6666
model_name = score_definition.get("objectDescriptor", "name")
@@ -76,20 +76,16 @@ def create_score_execution(
7676
score_execution = cls.list_executions(
7777
filter=f"eq(scoreDefinitionId, '{score_definition_id}')"
7878
)
79-
if score_execution.status_code >= 400:
80-
raise HTTPError(
81-
f"Something went wrong in the LIST_EXECUTIONS statement. See error: {score_execution.json()}"
82-
)
79+
if not score_execution:
80+
raise HTTPError(f"Something went wrong in the LIST_EXECUTIONS statement.")
8381

8482
# Checking the count of the execution list to see if there are any score executions for this score_definition_id already running
8583
execution_count = score_execution.get("count") # Exception catch location
8684
if execution_count == 1:
8785
execution_id = score_execution.get("items", 0, "id")
8886
deleted_execution = cls.delete_execution(execution_id)
8987
if deleted_execution.status_code >= 400:
90-
raise HTTPError(
91-
f"Something went wrong in the DELETE statement. See error: {deleted_execution.json()}"
92-
)
88+
raise HTTPError(f"Something went wrong in the DELETE statement.")
9389

9490
headers_score_exec = {"Content-Type": "application/json"}
9591

tests/unit/test_score_definitions.py

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626

2727

2828
class CustomMock:
29-
def __init__(self, status_code, json_info):
30-
self.status_code = status_code
29+
def __init__(self, json_info):
3130
self.json_info = json_info
3231

3332
def get(self, key1, key2=None, key3=None):
@@ -66,7 +65,7 @@ def test_create_score_definition():
6665
"sasctl._services.score_definitions.ScoreDefinitions.post"
6766
) as post:
6867
# Invalid model id test case
69-
get_model.return_value.status_code = 404
68+
get_model.return_value = None
7069
with pytest.raises(HTTPError):
7170
sd.create_score_definition(
7271
score_def_name="test_create_sd",
@@ -75,14 +74,16 @@ def test_create_score_definition():
7574
)
7675

7776
# Valid model id but invalid table name with no table_file argument test case
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
77+
get_model_mock = CustomMock(
78+
json_info={
79+
"id": "12345",
80+
"projectId": "54321",
81+
"projectVersionId": "67890",
82+
"name": "test_model",
83+
},
84+
)
85+
get_model.return_value = get_model_mock
86+
get_table.return_value = None
8687
with pytest.raises(HTTPError):
8788
sd.create_score_definition(
8889
score_def_name="test_create_sd",
@@ -91,9 +92,9 @@ def test_create_score_definition():
9192
)
9293

9394
# Invalid table name with a table_file argument that doesn't work test case
94-
get_table.return_value.status_code = 404
95+
get_table.return_value = None
9596
upload_file.return_value = None
96-
get_table.return_value.status_code = 404
97+
get_table.return_value = None
9798
with pytest.raises(HTTPError):
9899
sd.create_score_definition(
99100
score_def_name="test_create_sd",
@@ -103,12 +104,12 @@ def test_create_score_definition():
103104
)
104105

105106
# Valid table_file argument that successfully creates a table test case
106-
get_table.return_value.status_code = 404
107+
get_table.return_value = None
107108
upload_file.return_value = RestObj
108-
get_table.return_value.status_code = 200
109-
get_table.return_value.json.return_value = {
110-
"tableName": "test_table"
111-
}
109+
get_table_mock = CustomMock(
110+
json_info={"tableName": "test_table"},
111+
)
112+
get_table.return_value = get_table_mock
112113
response = sd.create_score_definition(
113114
score_def_name="test_create_sd",
114115
model_id="12345",
@@ -118,10 +119,7 @@ def test_create_score_definition():
118119
assert response
119120

120121
# Valid table_name argument test case
121-
get_table.return_value.status_code = 200
122-
get_table.return_value.json.return_value = {
123-
"tableName": "test_table"
124-
}
122+
get_table.return_value = get_table_mock
125123
response = sd.create_score_definition(
126124
score_def_name="test_create_sd",
127125
model_id="12345",
@@ -131,8 +129,7 @@ def test_create_score_definition():
131129
assert response
132130

133131
# Checking response with inputVariables in model elements
134-
model_mock_execution = CustomMock(
135-
status_code=200,
132+
get_model_mock = CustomMock(
136133
json_info={
137134
"id": "12345",
138135
"projectId": "54321",
@@ -145,11 +142,8 @@ def test_create_score_definition():
145142
],
146143
},
147144
)
148-
get_model.return_value = model_mock_execution
149-
get_table.return_value.status_code = 200
150-
get_table.return_value.json.return_value = {
151-
"tableName": "test_table"
152-
}
145+
get_model.return_value = get_model_mock
146+
get_table.return_value = get_table_mock
153147
response = sd.create_score_definition(
154148
score_def_name="test_create_sd",
155149
model_id="12345",

tests/unit/test_score_execution.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@
2929

3030
# Creating a CustomMock for list_executions to avoid a TypeError when comparing status code from mock with >= 400 in score_execution
3131
class CustomMock:
32-
def __init__(self, status_code, json_info):
33-
self.status_code = status_code
32+
def __init__(self, json_info):
3433
self.json_info = json_info
3534

3635
def get(self, key1, key2=None, key3=None):
3736
if key2 is None and key3 is None:
3837
return self.json_info[key1]
38+
elif key2 and not key3:
39+
return self.json_info[key1][key2]
3940
else:
4041
return self.json_info[key1][key2][key3]
4142

@@ -74,31 +75,32 @@ def test_create_score_execution():
7475
"sasctl._services.score_execution.ScoreExecution.post"
7576
) as post:
7677
# Invalid score definition id test case
77-
get_definition.return_value.status_code = 404
78+
get_definition.return_value = None
7879
with pytest.raises(HTTPError):
7980
se.create_score_execution(score_definition_id="12345")
8081

8182
# Valid score definition id and invalid list_executions argument test case
82-
get_definition.return_value.status_code = 200
83-
get_definition.return_value.json.return_value = {
84-
"inputData": {
85-
"libraryName": "cas-shared-default",
86-
"tableName": "test_table",
83+
get_definition_mock = CustomMock(
84+
json_info={
85+
"inputData": {
86+
"libraryName": "cas-shared-default",
87+
"tableName": "test_table",
88+
},
89+
"name": "score_def_name",
90+
"objectDescriptor": {
91+
"name": "test_model",
92+
"type": "sas.publish.example",
93+
"uri": "/modelPublish/models/example",
94+
},
8795
},
88-
"name": "score_def_name",
89-
"objectDescriptor": {
90-
"name": "test_model",
91-
"type": "sas.publish.example",
92-
"uri": "/modelPublish/models/example",
93-
},
94-
}
95-
list_executions.return_value.status_code = 404
96+
)
97+
get_definition.return_value = get_definition_mock
98+
list_executions.return_value = None
9699
with pytest.raises(HTTPError):
97100
se.create_score_execution(score_definition_id="12345")
98101

99102
# Valid list_executions argument with execution already running but invalid delete_execution argument test case
100103
list_mock_execution = CustomMock(
101-
status_code=200,
102104
json_info={"count": 1, "items": [{"id": "1234"}]},
103105
)
104106
list_executions.return_value = list_mock_execution
@@ -114,7 +116,6 @@ def test_create_score_execution():
114116

115117
# Valid list_executions argument without execution already running test case
116118
list_mock_execution_diff_count = CustomMock(
117-
status_code=200,
118119
json_info={"count": 0, "items": [{"id": "1234"}]},
119120
)
120121
list_executions.return_value = list_mock_execution_diff_count

0 commit comments

Comments
 (0)