Skip to content

Commit 341ecda

Browse files
committed
Update write_json_files test cases.
1 parent 68991f5 commit 341ecda

File tree

1 file changed

+47
-18
lines changed

1 file changed

+47
-18
lines changed

tests/unit/test_write_json_files.py

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
# Copyright © 2023, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
55
# SPDX-License-Identifier: Apache-2.0
66

7+
import io
78
import json
89
import os
910
import pickle
1011
import random
12+
import sys
1113
import tempfile
1214
import unittest
1315
import warnings
1416
from pathlib import Path
17+
from unittest.mock import patch
1518

1619
import numpy as np
1720
import pandas as pd
@@ -32,11 +35,7 @@
3235
{"name": "NINQ", "type": "double"},
3336
{"name": "CLNO", "type": "double"},
3437
{"name": "DEBTINC", "type": "double"},
35-
{"name": "JOB_Office", "type": "integer"},
36-
{"name": "JOB_Other", "type": "integer"},
37-
{"name": "JOB_ProfExe", "type": "integer"},
38-
{"name": "JOB_Sales", "type": "integer"},
39-
{"name": "JOB_Self", "type": "integer"},
38+
{"name": "JOB", "type": "string"},
4039
{"name": "REASON_HomeImp", "type": "integer"},
4140
]
4241

@@ -117,7 +116,16 @@ def test_generate_mlflow_variable_properties():
117116
- Return expected number of variables from mlflow_model.py output
118117
"""
119118
dict_list = jf.generate_mlflow_variable_properties(input_dict)
120-
assert len(dict_list) == 16
119+
assert len(dict_list) == 12
120+
121+
tensor_dict = [
122+
{"type": "string"},
123+
{"type": "double"},
124+
{"type": "tensor", "tensor-spec": {"dtype": "string"}},
125+
{"type": "tensor", "tensor-spec": {"dtype": "double"}},
126+
]
127+
dict_list = jf.generate_mlflow_variable_properties(tensor_dict)
128+
assert len(dict_list) == 4
121129

122130

123131
def test_write_var_json(hmeq_dataset):
@@ -133,6 +141,12 @@ def test_write_var_json(hmeq_dataset):
133141
assert (Path(tmp_dir) / "inputVar.json").exists()
134142
jf.write_var_json(df, False, Path(tmp_dir))
135143
assert (Path(tmp_dir) / "outputVar.json").exists()
144+
with patch.object(jf, "notebook_output", True):
145+
capture_output = io.StringIO()
146+
sys.stdout = capture_output
147+
_ = jf.write_var_json(df, False, Path(tmp_dir))
148+
sys.stdout = sys.__stdout__
149+
assert "was successfully written and saved to " in capture_output.getvalue()
136150

137151
var_dict = jf.write_var_json(df, False)
138152
assert "outputVar.json" in var_dict
@@ -188,13 +202,18 @@ def test_write_model_properties_json():
188202
- Truncate custom property that is too long
189203
"""
190204
with tempfile.TemporaryDirectory() as tmp_dir:
191-
jf.write_model_properties_json(
192-
model_name="Test_Model",
193-
target_variable="BAD",
194-
target_values=[1, 0],
195-
json_path=Path(tmp_dir),
196-
)
197-
assert (Path(tmp_dir) / "ModelProperties.json").exists()
205+
with patch.object(jf, "notebook_output", True):
206+
capture_output = io.StringIO()
207+
sys.stdout = capture_output
208+
jf.write_model_properties_json(
209+
model_name="Test_Model",
210+
target_variable="BAD",
211+
target_values=[1, 0],
212+
json_path=Path(tmp_dir),
213+
)
214+
sys.stdout = sys.__stdout__
215+
assert (Path(tmp_dir) / "ModelProperties.json").exists()
216+
assert "was successfully written and saved to " in capture_output.getvalue()
198217

199218
prop_dict = jf.write_model_properties_json(
200219
model_name="Test_Model",
@@ -250,8 +269,13 @@ def test_write_file_metadata_json():
250269
- Proper score resource name for H2O.ai model
251270
"""
252271
with tempfile.TemporaryDirectory() as tmp_dir:
253-
jf.write_file_metadata_json(model_prefix="Test_Model", json_path=Path(tmp_dir))
254-
assert (Path(tmp_dir) / "fileMetadata.json").exists()
272+
with patch.object(jf, "notebook_output", True):
273+
capture_output = io.StringIO()
274+
sys.stdout = capture_output
275+
jf.write_file_metadata_json(model_prefix="Test_Model", json_path=Path(tmp_dir))
276+
assert (Path(tmp_dir) / "fileMetadata.json").exists()
277+
sys.stdout = sys.__stdout__
278+
assert "was successfully written and saved to " in capture_output.getvalue()
255279

256280
meta_dict = jf.write_file_metadata_json(model_prefix="Test_Model")
257281
assert "fileMetadata.json" in meta_dict
@@ -396,8 +420,13 @@ def test_input_fit_statistics(monkeypatch):
396420
"""
397421
tuple_list = [("RASE", 10, 1), ("_NObs_", 33, "TEST")]
398422
with tempfile.TemporaryDirectory() as tmp_dir:
399-
jf.input_fit_statistics(tuple_list=tuple_list, json_path=Path(tmp_dir))
400-
assert (Path(tmp_dir) / "dmcas_fitstat.json").exists()
423+
with patch.object(jf, "notebook_output", True):
424+
capture_output = io.StringIO()
425+
sys.stdout = capture_output
426+
jf.input_fit_statistics(tuple_list=tuple_list, json_path=Path(tmp_dir))
427+
assert (Path(tmp_dir) / "dmcas_fitstat.json").exists()
428+
sys.stdout = sys.__stdout__
429+
assert "was successfully written and saved to " in capture_output.getvalue()
401430

402431
fitstat_dict = jf.input_fit_statistics(tuple_list=tuple_list)
403432
assert "dmcas_fitstat.json" in fitstat_dict
@@ -607,4 +636,4 @@ def test_create_requirements_json(change_dir):
607636
unittest.TestCase.maxDiff = None
608637
unittest.TestCase().assertCountEqual(
609638
json.loads(json_dict["requirements.json"]), expected
610-
)
639+
)

0 commit comments

Comments
 (0)