Skip to content

Commit

Permalink
Corrected status code comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
samyarpotlapalli committed Aug 2, 2024
1 parent f1bc3b7 commit 67cb185
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 64 deletions.
16 changes: 8 additions & 8 deletions src/sasctl/_services/score_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import json
from typing import Union

from ..core import current_session, delete, get, sasctl_command
from ..core import current_session, delete, get, sasctl_command, RestObj
from .cas_management import CASManagement
from .model_repository import ModelRepository
from .service import Service
Expand Down Expand Up @@ -76,10 +76,10 @@ def create_score_definition(

model = cls._model_respository.get_model(model_id)

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

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

Expand Down
14 changes: 5 additions & 9 deletions src/sasctl/_services/score_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ def create_score_execution(

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

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

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

Expand Down
52 changes: 23 additions & 29 deletions tests/unit/test_score_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@


class CustomMock:
def __init__(self, status_code, json_info):
self.status_code = status_code
def __init__(self, json_info):
self.json_info = json_info

def get(self, key1, key2=None, key3=None):
Expand Down Expand Up @@ -66,7 +65,7 @@ def test_create_score_definition():
"sasctl._services.score_definitions.ScoreDefinitions.post"
) as post:
# Invalid model id test case
get_model.return_value.status_code = 404
get_model.return_value = None
with pytest.raises(HTTPError):
sd.create_score_definition(
score_def_name="test_create_sd",
Expand All @@ -75,14 +74,16 @@ def test_create_score_definition():
)

# Valid model id but invalid table name with no table_file argument test case
get_model.return_value.status_code = 200
get_model.return_value.json.return_value = {
"id": "12345",
"projectId": "54321",
"projectVersionId": "67890",
"name": "test_model",
}
get_table.return_value.status_code = 404
get_model_mock = CustomMock(
json_info={
"id": "12345",
"projectId": "54321",
"projectVersionId": "67890",
"name": "test_model",
},
)
get_model.return_value = get_model_mock
get_table.return_value = None
with pytest.raises(HTTPError):
sd.create_score_definition(
score_def_name="test_create_sd",
Expand All @@ -91,9 +92,9 @@ def test_create_score_definition():
)

# Invalid table name with a table_file argument that doesn't work test case
get_table.return_value.status_code = 404
get_table.return_value = None
upload_file.return_value = None
get_table.return_value.status_code = 404
get_table.return_value = None
with pytest.raises(HTTPError):
sd.create_score_definition(
score_def_name="test_create_sd",
Expand All @@ -103,12 +104,12 @@ def test_create_score_definition():
)

# Valid table_file argument that successfully creates a table test case
get_table.return_value.status_code = 404
get_table.return_value = None
upload_file.return_value = RestObj
get_table.return_value.status_code = 200
get_table.return_value.json.return_value = {
"tableName": "test_table"
}
get_table_mock = CustomMock(
json_info={"tableName": "test_table"},
)
get_table.return_value = get_table_mock
response = sd.create_score_definition(
score_def_name="test_create_sd",
model_id="12345",
Expand All @@ -118,10 +119,7 @@ def test_create_score_definition():
assert response

# Valid table_name argument test case
get_table.return_value.status_code = 200
get_table.return_value.json.return_value = {
"tableName": "test_table"
}
get_table.return_value = get_table_mock
response = sd.create_score_definition(
score_def_name="test_create_sd",
model_id="12345",
Expand All @@ -131,8 +129,7 @@ def test_create_score_definition():
assert response

# Checking response with inputVariables in model elements
model_mock_execution = CustomMock(
status_code=200,
get_model_mock = CustomMock(
json_info={
"id": "12345",
"projectId": "54321",
Expand All @@ -145,11 +142,8 @@ def test_create_score_definition():
],
},
)
get_model.return_value = model_mock_execution
get_table.return_value.status_code = 200
get_table.return_value.json.return_value = {
"tableName": "test_table"
}
get_model.return_value = get_model_mock
get_table.return_value = get_table_mock
response = sd.create_score_definition(
score_def_name="test_create_sd",
model_id="12345",
Expand Down
37 changes: 19 additions & 18 deletions tests/unit/test_score_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@

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

def get(self, key1, key2=None, key3=None):
if key2 is None and key3 is None:
return self.json_info[key1]
elif key2 and not key3:
return self.json_info[key1][key2]
else:
return self.json_info[key1][key2][key3]

Expand Down Expand Up @@ -74,31 +75,32 @@ def test_create_score_execution():
"sasctl._services.score_execution.ScoreExecution.post"
) as post:
# Invalid score definition id test case
get_definition.return_value.status_code = 404
get_definition.return_value = None
with pytest.raises(HTTPError):
se.create_score_execution(score_definition_id="12345")

# Valid score definition id and invalid list_executions argument test case
get_definition.return_value.status_code = 200
get_definition.return_value.json.return_value = {
"inputData": {
"libraryName": "cas-shared-default",
"tableName": "test_table",
get_definition_mock = CustomMock(
json_info={
"inputData": {
"libraryName": "cas-shared-default",
"tableName": "test_table",
},
"name": "score_def_name",
"objectDescriptor": {
"name": "test_model",
"type": "sas.publish.example",
"uri": "/modelPublish/models/example",
},
},
"name": "score_def_name",
"objectDescriptor": {
"name": "test_model",
"type": "sas.publish.example",
"uri": "/modelPublish/models/example",
},
}
list_executions.return_value.status_code = 404
)
get_definition.return_value = get_definition_mock
list_executions.return_value = None
with pytest.raises(HTTPError):
se.create_score_execution(score_definition_id="12345")

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

# Valid list_executions argument without execution already running test case
list_mock_execution_diff_count = CustomMock(
status_code=200,
json_info={"count": 0, "items": [{"id": "1234"}]},
)
list_executions.return_value = list_mock_execution_diff_count
Expand Down

0 comments on commit 67cb185

Please sign in to comment.