Skip to content

Commit d98a41a

Browse files
Add unittest for formula
1 parent c1edbc2 commit d98a41a

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

causal_testing/json_front/json_class.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ def generate_tests(self, effects: dict, mutates: dict, estimators: dict, f_flag:
120120
def _execute_tests(self, concrete_tests, estimators, test, f_flag):
121121
failures = 0
122122
test["estimator"] = estimators[test["estimator"]]
123+
if "formula" in test:
124+
self._append_to_file(f"Estimator formula used for test: {test['formula']}")
123125
for concrete_test in concrete_tests:
124126
failed = self._execute_test_case(concrete_test, test, f_flag)
125127
if failed:
@@ -234,7 +236,7 @@ def _append_to_file(self, line: str, log_level: int = None):
234236
"""
235237
with open(self.output_path, "a", encoding="utf-8") as f:
236238
f.write(
237-
line + "\n",
239+
line
238240
)
239241
if log_level:
240242
logger.log(level=log_level, msg=line)

tests/json_front_tests/test_json_class.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import json
77

88
from causal_testing.testing.estimators import LinearRegressionEstimator
9-
from causal_testing.testing.causal_test_outcome import NoEffect
9+
from causal_testing.testing.causal_test_outcome import NoEffect, Positive
1010
from tests.test_helpers import create_temp_dir_if_non_existent, remove_temp_dir_if_existent
1111
from causal_testing.json_front.json_class import JsonUtility, CausalVariables
1212
from causal_testing.specification.variable import Input, Output, Meta
@@ -127,6 +127,34 @@ def test_generate_tests_from_json(self):
127127
temp_out = reader.readlines()
128128
self.assertIn("failed", temp_out[-1])
129129

130+
def test_formula_in_json_test(self):
131+
example_test = {
132+
"tests": [
133+
{
134+
"name": "test1",
135+
"mutations": {"test_input": "Increase"},
136+
"estimator": "LinearRegressionEstimator",
137+
"estimate_type": "ate",
138+
"effect_modifiers": [],
139+
"expectedEffect": {"test_output": "Positive"},
140+
"skip": False,
141+
"formula": "test_output ~ test_input"
142+
}
143+
]
144+
}
145+
self.json_class.test_plan = example_test
146+
effects = {"Positive": Positive()}
147+
mutates = {
148+
"Increase": lambda x: self.json_class.scenario.treatment_variables[x].z3
149+
> self.json_class.scenario.variables[x].z3
150+
}
151+
estimators = {"LinearRegressionEstimator": LinearRegressionEstimator}
152+
153+
self.json_class.generate_tests(effects, mutates, estimators, False)
154+
with open("temp_out.txt", 'r') as reader:
155+
temp_out = reader.readlines()
156+
self.assertIn("test_output ~ test_input", ''.join(temp_out))
157+
130158
def tearDown(self) -> None:
131159
pass
132160
# remove_temp_dir_if_existent()

0 commit comments

Comments
 (0)