Skip to content

Commit c01ca35

Browse files
author
AndrewC19
committed
Added test for single input variable MT case.
1 parent 8bcf068 commit c01ca35

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

tests/specification_tests/test_metamorphic_relations.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616
from causal_testing.specification.variable import Input, Output
1717

1818

19+
def single_input_program_under_test(X1, Z=None, M=None, Y=None):
20+
if Z is None:
21+
Z = 2 * X1 + -3
22+
if M is None:
23+
M = 3 * Z
24+
if Y is None:
25+
Y = M / 2
26+
return {"X1": X1, "Z": Z, "M": M, "Y": Y}
27+
28+
1929
def program_under_test(X1, X2, X3, Z=None, M=None, Y=None):
2030
if Z is None:
2131
Z = 2 * X1 + -3 * X2 + 10
@@ -36,6 +46,13 @@ def buggy_program_under_test(X1, X2, X3, Z=None, M=None, Y=None):
3646
return {"X1": X1, "X2": X2, "X3": X3, "Z": Z, "M": M, "Y": Y}
3747

3848

49+
class SingleInputProgramUnderTestEDC(ExperimentalDataCollector):
50+
def run_system_with_input_configuration(self, input_configuration: dict) -> pd.DataFrame:
51+
results_dict = single_input_program_under_test(**input_configuration)
52+
results_df = pd.DataFrame(results_dict, index=[0])
53+
return results_df
54+
55+
3956
class ProgramUnderTestEDC(ExperimentalDataCollector):
4057
def run_system_with_input_configuration(self, input_configuration: dict) -> pd.DataFrame:
4158
results_dict = program_under_test(**input_configuration)
@@ -82,6 +99,20 @@ def test_should_cause_metamorphic_relations_correct_spec(self):
8299
test_results = should_cause_MR.execute_tests(self.data_collector)
83100
should_cause_MR.test_oracle(test_results)
84101

102+
def test_should_cause_metamorphic_relations_correct_spec_one_input(self):
103+
"""Test if the ShouldCause MR passes all metamorphic tests where the DAG perfectly represents the program
104+
and there is only a single input."""
105+
causal_dag = CausalDAG(self.dag_dot_path)
106+
self.data_collector = SingleInputProgramUnderTestEDC(
107+
self.scenario, self.default_control_input_config, self.default_treatment_input_config
108+
)
109+
causal_dag.graph.remove_nodes_from(['X2', 'X3'])
110+
adj_set = list(causal_dag.direct_effect_adjustment_sets(['X1'], ['Z'])[0])
111+
should_cause_MR = ShouldCause('X1', 'Z', adj_set, causal_dag)
112+
should_cause_MR.generate_follow_up(10, -10.0, 10.0, 1)
113+
test_results = should_cause_MR.execute_tests(self.data_collector)
114+
should_cause_MR.test_oracle(test_results)
115+
85116
def test_should_not_cause_metamorphic_relations_correct_spec(self):
86117
"""Test if the ShouldNotCause MR passes all metamorphic tests where the DAG perfectly represents the program."""
87118
causal_dag = CausalDAG(self.dag_dot_path)

0 commit comments

Comments
 (0)