1
1
import unittest
2
2
from pathlib import Path
3
3
import os
4
-
4
+ import json
5
5
import scipy
6
-
6
+ import csv
7
7
from tests .test_helpers import create_temp_dir_if_non_existent , remove_temp_dir_if_existent
8
8
from causal_testing .json_front .json_class import JsonUtility
9
9
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
10
12
11
13
12
14
class TestJsonClass (unittest .TestCase ):
@@ -17,22 +19,25 @@ class TestJsonClass(unittest.TestCase):
17
19
"""
18
20
19
21
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 () )
21
23
json_file_name = "tests.json"
22
24
dag_file_name = "dag.dot"
23
25
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 ()
27
32
self .json_class = JsonUtility ("logs.log" )
28
33
self .example_distribution = scipy .stats .uniform (0 , 10 )
29
34
self .input_dict_list = [{"name" : "test_input" , "type" : float , "distribution" : self .example_distribution }]
30
35
self .output_dict_list = [{"name" : "test_output" , "type" : float }]
31
36
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 )
33
39
34
40
def test_setting_paths (self ):
35
- self .json_class .set_path (self .json_path , self .dag_path , self .data_path )
36
41
self .assertEqual (self .json_class .json_path , Path (self .json_path ))
37
42
self .assertEqual (self .json_class .dag_path , Path (self .dag_path ))
38
43
self .assertEqual (self .json_class .data_path , Path (self .data_path ))
@@ -49,6 +54,7 @@ def test_set_outputs(self):
49
54
self .assertEqual (ctf_output [0 ].datatype , self .json_class .outputs [0 ].datatype )
50
55
51
56
def test_set_metas (self ):
57
+ self .json_class .set_variables (self .input_dict_list , self .output_dict_list , self .meta_dict_list )
52
58
ctf_meta = [Meta ("test_meta" , float , populate_example )]
53
59
self .assertEqual (ctf_meta [0 ].name , self .json_class .metas [0 ].name )
54
60
self .assertEqual (ctf_meta [0 ].datatype , self .json_class .metas [0 ].datatype )
@@ -59,8 +65,50 @@ def test_argparse(self):
59
65
self .assertTrue (args .dag_path )
60
66
self .assertTrue (args .json_path )
61
67
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
+
62
84
def tearDown (self ) -> None :
63
85
remove_temp_dir_if_existent ()
64
86
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