Skip to content

Commit a51b66d

Browse files
committed
codecov
1 parent a4f1ae7 commit a51b66d

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

tests/main_tests/test_main.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,22 @@ def test_parse_args_batches(self):
338338
main()
339339
self.assertTrue((self.output_path.parent / "main_batch.json").exists())
340340

341+
def test_parse_args_generation(self):
342+
with tempfile.TemporaryDirectory() as tmp:
343+
with unittest.mock.patch(
344+
"sys.argv",
345+
[
346+
"causal_testing",
347+
"--generate",
348+
"--dag_path",
349+
str(self.dag_path),
350+
"--output",
351+
os.path.join(tmp, "tests.json"),
352+
],
353+
):
354+
main()
355+
self.assertTrue(os.path.exists(os.path.join(tmp, "tests.json")))
356+
341357
def tearDown(self):
342358
if self.output_path.parent.exists():
343359
shutil.rmtree(self.output_path.parent)

tests/testing_tests/test_metamorphic_relations.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import shutil, tempfile
44
import pandas as pd
55
from itertools import combinations
6+
import tempfile
7+
import json
68

79
from causal_testing.specification.causal_dag import CausalDAG
810
from causal_testing.specification.causal_specification import Scenario
@@ -11,6 +13,7 @@
1113
ShouldNotCause,
1214
generate_metamorphic_relations,
1315
generate_metamorphic_relation,
16+
generate_causal_tests,
1417
)
1518
from causal_testing.specification.variable import Input, Output
1619
from causal_testing.testing.base_test_case import BaseTestCase
@@ -177,8 +180,8 @@ def test_all_metamorphic_relations_implied_by_dag_parallel(self):
177180
self.assertEqual(missing_snc_relations, [])
178181

179182
def test_all_metamorphic_relations_implied_by_dag_ignore_cycles(self):
180-
dag = CausalDAG(self.dcg_dot_path, ignore_cycles=True)
181-
metamorphic_relations = generate_metamorphic_relations(dag, threads=2, nodes_to_ignore=set(dag.cycle_nodes()))
183+
dcg = CausalDAG(self.dcg_dot_path, ignore_cycles=True)
184+
metamorphic_relations = generate_metamorphic_relations(dcg, threads=2, nodes_to_ignore=set(dcg.cycle_nodes()))
182185
should_cause_relations = [mr for mr in metamorphic_relations if isinstance(mr, ShouldCause)]
183186
should_not_cause_relations = [mr for mr in metamorphic_relations if isinstance(mr, ShouldNotCause)]
184187

@@ -203,6 +206,46 @@ def test_generate_metamorphic_relation_(self):
203206
ShouldCause(BaseTestCase("X1", "Z"), []),
204207
)
205208

209+
def test_generate_causal_tests_ignore_cycles(self):
210+
dcg = CausalDAG(self.dcg_dot_path, ignore_cycles=True)
211+
relations = generate_metamorphic_relations(dcg, nodes_to_ignore=set(dcg.cycle_nodes()))
212+
with tempfile.TemporaryDirectory() as tmp:
213+
tests_file = os.path.join(tmp, "causal_tests.json")
214+
generate_causal_tests(self.dcg_dot_path, tests_file, ignore_cycles=True)
215+
with open(tests_file, encoding="utf8") as f:
216+
tests = json.load(f)
217+
expected = list(
218+
map(
219+
lambda x: x.to_json_stub(skip=False),
220+
filter(
221+
lambda relation: len(list(dcg.graph.predecessors(relation.base_test_case.outcome_variable)))
222+
> 0,
223+
relations,
224+
),
225+
)
226+
)
227+
self.assertEqual(tests["tests"], expected)
228+
229+
def test_generate_causal_tests(self):
230+
dag = CausalDAG(self.dag_dot_path)
231+
relations = generate_metamorphic_relations(dag)
232+
with tempfile.TemporaryDirectory() as tmp:
233+
tests_file = os.path.join(tmp, "causal_tests.json")
234+
generate_causal_tests(self.dag_dot_path, tests_file)
235+
with open(tests_file, encoding="utf8") as f:
236+
tests = json.load(f)
237+
expected = list(
238+
map(
239+
lambda x: x.to_json_stub(skip=False),
240+
filter(
241+
lambda relation: len(list(dag.graph.predecessors(relation.base_test_case.outcome_variable)))
242+
> 0,
243+
relations,
244+
),
245+
)
246+
)
247+
self.assertEqual(tests["tests"], expected)
248+
206249
def test_shoud_cause_string(self):
207250
sc_mr = ShouldCause(BaseTestCase("X", "Y"), ["A", "B", "C"])
208251
self.assertEqual(str(sc_mr), "X --> Y | ['A', 'B', 'C']")

0 commit comments

Comments
 (0)