Skip to content

Commit 14a54f0

Browse files
Added docstrings and removed list_executions unit tests in test_score_execution.py
1 parent b956ad6 commit 14a54f0

File tree

2 files changed

+92
-98
lines changed

2 files changed

+92
-98
lines changed

src/sasctl/_services/score_execution.py

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,12 @@ def create_score_execution(
6666
6767
"""
6868

69-
# Gets information about the scoring object from the score definition and raises an exception if the score definition does not exist
69+
# Gets information about the scoring object from the score definition
7070
score_definition = cls._score_definitions.get_definition(score_definition_id)
7171
if not score_definition:
7272
raise HTTPError
7373
score_exec_name = score_definition.get("name")
74-
# NEEDS modelManagement not modelRepository
75-
model_uuid = score_definition.get("objectDescriptor").get("uri").split('/')[-1]
74+
model_uuid = score_definition.get("objectDescriptor").get("uri").split("/")[-1]
7675
model_uri = f"/modelManagement/models/{model_uuid}"
7776
model_name = score_definition.get("objectDescriptor").get("name")
7877
model_input_library = score_definition.get("inputData").get("libraryName")
@@ -111,10 +110,22 @@ def create_score_execution(
111110

112111
@classmethod
113112
def poll_score_execution_state(
114-
cls,
115-
score_execution: Union[dict, str],
116-
timeout: int = 300
113+
cls, score_execution: Union[dict, str], timeout: int = 300
117114
):
115+
"""Checks the state of the score execution.
116+
117+
Parameters
118+
--------
119+
score_execution: str or dict
120+
A running score_execution.
121+
timeout: int
122+
Time limit for checking the score_execution state.
123+
124+
Returns
125+
-------
126+
String
127+
128+
"""
118129
if type(score_execution) is str:
119130
exec_id = score_execution
120131
else:
@@ -139,6 +150,18 @@ def get_score_execution_results(
139150
cls,
140151
score_execution: Union[dict, str],
141152
):
153+
"""Generates an output table for the score_execution results.
154+
155+
Parameters
156+
--------
157+
score_execution: str or dict
158+
A running score_execution.
159+
160+
Returns
161+
-------
162+
Table reference
163+
164+
"""
142165
try:
143166
import swat
144167
except ImportError:
@@ -154,9 +177,7 @@ def get_score_execution_results(
154177
# If swat is not available, then
155178
if not swat:
156179
output_table = cls._no_gateway_get_results(
157-
server_name,
158-
library_name,
159-
table_name
180+
server_name, library_name, table_name
160181
)
161182
return output_table
162183
else:
@@ -165,9 +186,7 @@ def get_score_execution_results(
165186
response = cas.loadActionSet("gateway")
166187
if not response:
167188
output_table = cls._no_gateway_get_results(
168-
server_name,
169-
library_name,
170-
table_name
189+
server_name, library_name, table_name
171190
)
172191
return output_table
173192
else:
@@ -180,20 +199,27 @@ def get_score_execution_results(
180199
gateway.return_table("Execution Results", df = table, label = "label", title = "title")"""
181200

182201
output_table = cas.gateway.runlang(
183-
code=gateway_code,
184-
single=True,
185-
timeout_millis=10000
202+
code=gateway_code, single=True, timeout_millis=10000
186203
)
187204
output_table = pd.DataFrame(output_table["Execution Results"])
188205
return output_table
189206

190207
@classmethod
191-
def _no_gateway_get_results(
192-
cls,
193-
server_name,
194-
library_name,
195-
table_name
196-
):
208+
def _no_gateway_get_results(cls, server_name, library_name, table_name):
209+
"""Helper method that builds the output table.
210+
211+
Parameters
212+
--------
213+
server_name: str
214+
CAS server where original table is stored.
215+
library_name: CAS library where original table is stored.
216+
table_name: Table that contains row and columns information to build the output table
217+
218+
Returns
219+
-------
220+
Pandas Dataframe
221+
222+
"""
197223
if pd.__version__ >= StrictVersion("1.0.3"):
198224
from pandas import json_normalize
199225
else:
@@ -219,6 +245,6 @@ def _no_gateway_get_results(
219245
)
220246
output_table = pd.DataFrame(
221247
json_normalize(output_rows.json()["items"])["cells"].to_list(),
222-
columns=column_names
248+
columns=column_names,
223249
)
224250
return output_table

tests/unit/test_score_execution.py

Lines changed: 44 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -65,79 +65,47 @@ def test_create_score_execution():
6565
with mock.patch(
6666
"sasctl._services.score_definitions.ScoreDefinitions.get_definition"
6767
) as get_definition:
68-
with mock.patch(
69-
"sasctl._services.score_execution.ScoreExecution.list_executions"
70-
) as list_executions:
71-
with mock.patch(
72-
"sasctl._services.score_execution.ScoreExecution.delete_execution"
73-
) as delete_execution:
74-
with mock.patch(
75-
"sasctl._services.score_execution.ScoreExecution.post"
76-
) as post:
77-
# Invalid score definition id test case
78-
get_definition.return_value = None
79-
with pytest.raises(HTTPError):
80-
se.create_score_execution(score_definition_id="12345")
81-
82-
# Valid score definition id and invalid list_executions argument test case
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-
},
95-
},
96-
)
97-
get_definition.return_value = get_definition_mock
98-
list_executions.return_value = None
99-
with pytest.raises(HTTPError):
100-
se.create_score_execution(score_definition_id="12345")
101-
102-
# Valid list_executions argument with execution already running but invalid delete_execution argument test case
103-
list_mock_execution = CustomMock(
104-
json_info={"count": 1, "items": [{"id": "1234"}]},
105-
)
106-
list_executions.return_value = list_mock_execution
107-
delete_execution.return_value.status_code = 404
108-
with pytest.raises(HTTPError):
109-
se.create_score_execution(score_definition_id="12345")
110-
111-
# Valid list_executions argument with execution already running but valid delete_execution argument test case
112-
list_executions.return_value = list_mock_execution
113-
delete_execution.return_value.status_code = 200
114-
response = se.create_score_execution(score_definition_id="3456")
115-
assert response
116-
117-
# Valid list_executions argument without execution already running test case
118-
list_mock_execution_diff_count = CustomMock(
119-
json_info={"count": 0, "items": [{"id": "1234"}]},
120-
)
121-
list_executions.return_value = list_mock_execution_diff_count
122-
response = se.create_score_execution(score_definition_id="12345")
123-
assert response
124-
125-
# Checking whether the output table name remained the default empty string or the default changed as writted in score_execution
126-
data = post.call_args
127-
json_data = json.loads(data.kwargs["data"])
128-
assert json_data["outputTable"]["tableName"] != ""
129-
130-
# With output table specified within create_score_execution arguments test case
131-
response = se.create_score_execution(
132-
score_definition_id="12345", output_table_name="example_table"
133-
)
134-
assert response
135-
assert post.call_count == 3
136-
137-
# Checking whether specified output table name or the default output table name is in the response
138-
data = post.call_args
139-
json_data = json.loads(data.kwargs["data"])
140-
assert (
141-
target["outputTable"]["tableName"]
142-
== json_data["outputTable"]["tableName"]
143-
)
68+
with mock.patch("sasctl._services.score_execution.ScoreExecution.post") as post:
69+
# Invalid score definition id test case
70+
get_definition.return_value = None
71+
with pytest.raises(HTTPError):
72+
se.create_score_execution(score_definition_id="12345")
73+
74+
# Valid score definition id and invalid list_executions argument test case
75+
get_definition_mock = CustomMock(
76+
json_info={
77+
"inputData": {
78+
"libraryName": "cas-shared-default",
79+
"tableName": "test_table",
80+
},
81+
"name": "score_def_name",
82+
"objectDescriptor": {
83+
"name": "test_model",
84+
"type": "sas.publish.example",
85+
"uri": "/modelPublish/models/example",
86+
},
87+
},
88+
)
89+
get_definition.return_value = get_definition_mock
90+
response = se.create_score_execution(score_definition_id="3456")
91+
assert response
92+
93+
# Checking whether the output table name remained the default empty string or the default changed as writted in score_execution
94+
data = post.call_args
95+
json_data = json.loads(data.kwargs["data"])
96+
assert json_data["outputTable"]["tableName"] != ""
97+
98+
# With output table specified within create_score_execution arguments test case
99+
response = se.create_score_execution(
100+
score_definition_id="12345", output_table_name="example_table"
101+
)
102+
assert response
103+
assert post.call_count == 2
104+
105+
# Checking whether specified output table name or the default output table name is in the response
106+
data = post.call_args
107+
json_data = json.loads(data.kwargs["data"])
108+
assert (
109+
target["outputTable"]["tableName"]
110+
== json_data["outputTable"]["tableName"]
111+
)

0 commit comments

Comments
 (0)