4
4
# Copyright © 2023, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
5
5
# SPDX-License-Identifier: Apache-2.0
6
6
7
+ import io
7
8
import json
8
9
import os
9
10
import pickle
10
11
import random
12
+ import sys
11
13
import tempfile
12
14
import unittest
13
15
import warnings
14
16
from pathlib import Path
17
+ from unittest .mock import patch
15
18
16
19
import numpy as np
17
20
import pandas as pd
32
35
{"name" : "NINQ" , "type" : "double" },
33
36
{"name" : "CLNO" , "type" : "double" },
34
37
{"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" },
40
39
{"name" : "REASON_HomeImp" , "type" : "integer" },
41
40
]
42
41
@@ -117,7 +116,16 @@ def test_generate_mlflow_variable_properties():
117
116
- Return expected number of variables from mlflow_model.py output
118
117
"""
119
118
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
121
129
122
130
123
131
def test_write_var_json (hmeq_dataset ):
@@ -133,6 +141,12 @@ def test_write_var_json(hmeq_dataset):
133
141
assert (Path (tmp_dir ) / "inputVar.json" ).exists ()
134
142
jf .write_var_json (df , False , Path (tmp_dir ))
135
143
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 ()
136
150
137
151
var_dict = jf .write_var_json (df , False )
138
152
assert "outputVar.json" in var_dict
@@ -188,13 +202,18 @@ def test_write_model_properties_json():
188
202
- Truncate custom property that is too long
189
203
"""
190
204
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 ()
198
217
199
218
prop_dict = jf .write_model_properties_json (
200
219
model_name = "Test_Model" ,
@@ -250,8 +269,13 @@ def test_write_file_metadata_json():
250
269
- Proper score resource name for H2O.ai model
251
270
"""
252
271
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 ()
255
279
256
280
meta_dict = jf .write_file_metadata_json (model_prefix = "Test_Model" )
257
281
assert "fileMetadata.json" in meta_dict
@@ -396,8 +420,13 @@ def test_input_fit_statistics(monkeypatch):
396
420
"""
397
421
tuple_list = [("RASE" , 10 , 1 ), ("_NObs_" , 33 , "TEST" )]
398
422
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 ()
401
430
402
431
fitstat_dict = jf .input_fit_statistics (tuple_list = tuple_list )
403
432
assert "dmcas_fitstat.json" in fitstat_dict
@@ -607,4 +636,4 @@ def test_create_requirements_json(change_dir):
607
636
unittest .TestCase .maxDiff = None
608
637
unittest .TestCase ().assertCountEqual (
609
638
json .loads (json_dict ["requirements.json" ]), expected
610
- )
639
+ )
0 commit comments