Skip to content

Commit 291fd26

Browse files
create temp files
1 parent 4c02210 commit 291fd26

File tree

1 file changed

+58
-10
lines changed

1 file changed

+58
-10
lines changed

tests/json_front_tests/test_json_class.py

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import unittest
22
from pathlib import Path
33
import os
4-
4+
import json
55
import scipy
6-
6+
import csv
77
from tests.test_helpers import create_temp_dir_if_non_existent, remove_temp_dir_if_existent
88
from causal_testing.json_front.json_class import JsonUtility
99
from causal_testing.specification.variable import Input, Output, Meta
10+
from causal_testing.specification.scenario import Scenario
11+
from causal_testing.specification.causal_specification import CausalSpecification
1012

1113

1214
class TestJsonClass(unittest.TestCase):
@@ -17,22 +19,25 @@ class TestJsonClass(unittest.TestCase):
1719
"""
1820

1921
def setUp(self) -> None:
20-
temp_dir_path = create_temp_dir_if_non_existent()
22+
temp_dir_path = Path(create_temp_dir_if_non_existent())
2123
json_file_name = "tests.json"
2224
dag_file_name = "dag.dot"
2325
data_file_name = "data.csv"
24-
self.json_path = os.path.join(temp_dir_path, json_file_name)
25-
self.dag_path = os.path.join(temp_dir_path, json_file_name)
26-
self.data_path = os.path.join(temp_dir_path, json_file_name)
26+
self.json_path = temp_dir_path / json_file_name
27+
self.dag_path = temp_dir_path / dag_file_name
28+
self.data_path = temp_dir_path / data_file_name
29+
self.setup_data_file()
30+
self.setup_json_file()
31+
self.setup_dag_file()
2732
self.json_class = JsonUtility("logs.log")
2833
self.example_distribution = scipy.stats.uniform(0, 10)
2934
self.input_dict_list = [{"name": "test_input", "type": float, "distribution": self.example_distribution}]
3035
self.output_dict_list = [{"name": "test_output", "type": float}]
3136
self.meta_dict_list = [{"name": "test_meta", "type": float, "populate": populate_example}]
32-
self.json_class.set_variables(self.input_dict_list, self.output_dict_list, self.meta_dict_list)
37+
self.json_class.set_variables(self.input_dict_list, self.output_dict_list, None)
38+
self.json_class.set_path(self.json_path, self.dag_path, self.data_path)
3339

3440
def test_setting_paths(self):
35-
self.json_class.set_path(self.json_path, self.dag_path, self.data_path)
3641
self.assertEqual(self.json_class.json_path, Path(self.json_path))
3742
self.assertEqual(self.json_class.dag_path, Path(self.dag_path))
3843
self.assertEqual(self.json_class.data_path, Path(self.data_path))
@@ -49,6 +54,7 @@ def test_set_outputs(self):
4954
self.assertEqual(ctf_output[0].datatype, self.json_class.outputs[0].datatype)
5055

5156
def test_set_metas(self):
57+
self.json_class.set_variables(self.input_dict_list, self.output_dict_list, self.meta_dict_list)
5258
ctf_meta = [Meta("test_meta", float, populate_example)]
5359
self.assertEqual(ctf_meta[0].name, self.json_class.metas[0].name)
5460
self.assertEqual(ctf_meta[0].datatype, self.json_class.metas[0].datatype)
@@ -59,8 +65,50 @@ def test_argparse(self):
5965
self.assertTrue(args.dag_path)
6066
self.assertTrue(args.json_path)
6167

68+
def test_setup_modelling_scenario(self):
69+
self.json_class.setup()
70+
print(type(self.json_class.modelling_scenario))
71+
print(self.json_class.modelling_scenario)
72+
self.assertIsInstance(self.json_class.modelling_scenario, Scenario)
73+
74+
def test_setup_causal_specification(self):
75+
self.json_class.setup()
76+
77+
self.assertIsInstance(self.json_class.causal_specification, CausalSpecification)
78+
79+
def test_concrete_tests_generated(self):
80+
self.setup_json_file()
81+
self.setup_data_file()
82+
self.setup_dag_file()
83+
6284
def tearDown(self) -> None:
6385
remove_temp_dir_if_existent()
6486

65-
def populate_example():
66-
pass
87+
def setup_json_file(self):
88+
json_test = {
89+
"tests": [
90+
{"name": "test1", "mutations": {}, "estimator": None, "estimate_type": None, "effect_modifiers": [],
91+
"expectedEffect": {}, "skip": False}]
92+
}
93+
json_object = json.dumps(json_test)
94+
with open(self.json_path, "w") as f:
95+
f.write(json_object)
96+
f.close()
97+
98+
def setup_data_file(self):
99+
header = ['test_input', 'test_output']
100+
data = [1, 2]
101+
with open(self.data_path, "w") as f:
102+
writer = csv.writer(f)
103+
writer.writerow(header)
104+
writer.writerow(data)
105+
106+
def setup_dag_file(self):
107+
dag_dot = (
108+
"digraph G {A->B}"
109+
)
110+
with open(self.dag_path, "w") as f:
111+
f.write(dag_dot)
112+
113+
def populate_example(*args, **kwargs):
114+
pass

0 commit comments

Comments
 (0)