|
1 | | -import csv |
2 | | - |
3 | 1 | import pytest |
4 | 2 |
|
5 | | -from ..redcap2reproschema import process_field_properties |
| 3 | +from ..redcap2reproschema import process_row |
| 4 | +from ..redcap_mappings import REDCAP_COLUMN_MAP |
6 | 5 |
|
7 | 6 |
|
8 | | -def test_process_field_properties_calctext(): |
9 | | - """Test different CALCTEXT annotations with realistic examples""" |
10 | | - test_cases = [ |
11 | | - # Simple CALCTEXT |
12 | | - { |
13 | | - "input": { |
14 | | - "Variable / Field Name": "test_var", |
15 | | - "Required Field?": "", |
16 | | - "Field Annotation": "@CALCTEXT", |
17 | | - "Branching Logic (Show field only if...)": "", |
| 7 | +def update_keys(data): |
| 8 | + """Update keys in the dictionary to match the expected keys in the reproschema""" |
| 9 | + # Add default value for "Field Label" if it is not present |
| 10 | + if "Field Label" not in data: |
| 11 | + data["Field Label"] = "question" |
| 12 | + # Update keys to match the expected keys in the reproschema |
| 13 | + updated_data = {} |
| 14 | + for key, value in data.items(): |
| 15 | + updated_data[REDCAP_COLUMN_MAP[key]] = value |
| 16 | + return updated_data |
| 17 | + |
| 18 | + |
| 19 | +@pytest.mark.parametrize( |
| 20 | + "field_data,expected", |
| 21 | + [ |
| 22 | + # Test case 1: No branching logic or annotations |
| 23 | + ({"Variable / Field Name": "test_field"}, True), |
| 24 | + # Test case 2: With branching logic |
| 25 | + ( |
| 26 | + { |
| 27 | + "Variable / Field Name": "test_field", |
| 28 | + "Branching Logic (Show field only if...)": "[age] > 18", |
18 | 29 | }, |
19 | | - "expected": { |
20 | | - "variableName": "test_var", |
21 | | - "isAbout": "items/test_var", |
22 | | - "isVis": False, |
| 30 | + "age > 18", |
| 31 | + ), |
| 32 | + # Test case 3: With @HIDDEN annotation |
| 33 | + ( |
| 34 | + { |
| 35 | + "Variable / Field Name": "test_field", |
| 36 | + "Field Annotation": "@HIDDEN", |
23 | 37 | }, |
24 | | - }, |
25 | | - # Complex CALCTEXT with conditional logic |
26 | | - { |
27 | | - "input": { |
| 38 | + False, |
| 39 | + ), |
| 40 | + # Test case 4: With both branching logic and @HIDDEN |
| 41 | + ( |
| 42 | + { |
| 43 | + "Variable / Field Name": "test_field", |
| 44 | + "Branching Logic (Show field only if...)": "[age] > 18", |
| 45 | + "Field Annotation": "@HIDDEN", |
| 46 | + }, |
| 47 | + False, |
| 48 | + ), |
| 49 | + ], |
| 50 | +) |
| 51 | +def test_process_field_properties_visibility(field_data, expected): |
| 52 | + # Test case 1: No branching logic or annotations |
| 53 | + _, _, _, add_prop = process_row(update_keys(field_data)) |
| 54 | + if expected is True: |
| 55 | + assert add_prop.get("isVis", True) is True # defaults is True |
| 56 | + else: |
| 57 | + assert add_prop.get("isVis") == expected |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.parametrize( |
| 61 | + "input,expected", |
| 62 | + [ |
| 63 | + # CALCTEXT with conditional logic |
| 64 | + ( |
| 65 | + { |
28 | 66 | "Variable / Field Name": "parkinsons_diagnosis", |
29 | 67 | "Required Field?": "", |
30 | 68 | "Field Annotation": "@CALCTEXT(if(([diagnosis_parkinsons_gsd_category_1(bradykinesia)] && ([diagnosis_parkinsons_gsd_category_1(tremor)] || [diagnosis_parkinsons_gsd_category_1(rigidity)])), 'Yes', 'No'))", |
31 | 69 | "Branching Logic (Show field only if...)": "[some_other_condition] = 1", |
32 | 70 | }, |
33 | | - "expected": { |
| 71 | + { |
34 | 72 | "variableName": "parkinsons_diagnosis", |
35 | 73 | "isAbout": "items/parkinsons_diagnosis", |
36 | 74 | "isVis": False, |
37 | 75 | }, |
38 | | - }, |
| 76 | + ), |
39 | 77 | # CALCTEXT with numerical operations |
40 | | - { |
41 | | - "input": { |
| 78 | + ( |
| 79 | + { |
42 | 80 | "Variable / Field Name": "bmi", |
43 | 81 | "Required Field?": "", |
44 | 82 | "Field Annotation": "@CALCTEXT([weight]/([height]*[height]))", |
45 | 83 | "Branching Logic (Show field only if...)": "[weight] > 0 and [height] > 0", |
46 | 84 | }, |
47 | | - "expected": { |
| 85 | + { |
48 | 86 | "variableName": "bmi", |
49 | 87 | "isAbout": "items/bmi", |
50 | 88 | "isVis": False, |
51 | 89 | }, |
52 | | - }, |
| 90 | + ), |
53 | 91 | # CALCTEXT with multiple nested conditions |
54 | | - { |
55 | | - "input": { |
| 92 | + ( |
| 93 | + { |
56 | 94 | "Variable / Field Name": "complex_score", |
57 | 95 | "Required Field?": "", |
58 | 96 | "Field Annotation": "@CALCTEXT(if([score1] > 10 && [score2] < 5, 'High', if([score1] > 5, 'Medium', 'Low')))", |
59 | 97 | "Branching Logic (Show field only if...)": "", |
60 | 98 | }, |
61 | | - "expected": { |
| 99 | + { |
62 | 100 | "variableName": "complex_score", |
63 | 101 | "isAbout": "items/complex_score", |
64 | 102 | "isVis": False, |
65 | 103 | }, |
66 | | - }, |
67 | | - ] |
68 | | - |
69 | | - for test_case in test_cases: |
70 | | - result = process_field_properties(test_case["input"]) |
71 | | - for key, expected_value in test_case["expected"].items(): |
72 | | - assert ( |
73 | | - result[key] == expected_value |
74 | | - ), f"Failed for {key} in test case with annotation: {test_case['input']['Field Annotation']}" |
| 104 | + ), |
| 105 | + ], |
| 106 | +) |
| 107 | +def test_process_field_properties_calctext(input, expected): |
| 108 | + """Test different CALCTEXT annotations with realistic examples""" |
| 109 | + _, _, _, add_prop = process_row(update_keys(input)) |
| 110 | + for key, expected_value in expected.items(): |
| 111 | + assert ( |
| 112 | + add_prop[key] == expected_value |
| 113 | + ), f"Failed for {key} in test case with annotation: {input['Field Annotation']}" |
75 | 114 |
|
76 | 115 |
|
77 | | -def test_process_field_properties_mixed_annotations(): |
78 | | - """Test fields with multiple annotations""" |
79 | | - test_cases = [ |
| 116 | +@pytest.mark.parametrize( |
| 117 | + "input,expected", |
| 118 | + [ |
80 | 119 | # CALCTEXT with READONLY |
81 | | - { |
82 | | - "input": { |
| 120 | + ( |
| 121 | + { |
83 | 122 | "Variable / Field Name": "test_var", |
84 | 123 | "Required Field?": "", |
85 | 124 | "Field Annotation": "@CALCTEXT @READONLY", |
86 | 125 | "Branching Logic (Show field only if...)": "", |
87 | 126 | }, |
88 | | - "expected": {"isVis": False}, |
89 | | - }, |
| 127 | + {"isVis": False}, |
| 128 | + ), |
90 | 129 | # CALCTEXT with HIDDEN |
91 | | - { |
92 | | - "input": { |
| 130 | + ( |
| 131 | + { |
93 | 132 | "Variable / Field Name": "test_var", |
94 | 133 | "Required Field?": "", |
95 | 134 | "Field Annotation": "@HIDDEN @CALCTEXT(if([var1] > 0, 1, 0))", |
96 | 135 | "Branching Logic (Show field only if...)": "", |
97 | 136 | }, |
98 | | - "expected": {"isVis": False}, |
99 | | - }, |
| 137 | + {"isVis": False}, |
| 138 | + ), |
100 | 139 | # Complex CALCTEXT with other annotations |
101 | | - { |
102 | | - "input": { |
| 140 | + ( |
| 141 | + { |
103 | 142 | "Variable / Field Name": "test_var", |
104 | 143 | "Required Field?": "", |
105 | 144 | "Field Annotation": "@CALCTEXT(if(([var1] && [var2]), 'Yes', 'No')) @READONLY @HIDDEN-SURVEY", |
106 | 145 | "Branching Logic (Show field only if...)": "[condition] = 1", |
107 | 146 | }, |
108 | | - "expected": {"isVis": False}, |
109 | | - }, |
110 | | - ] |
111 | | - |
112 | | - for test_case in test_cases: |
113 | | - result = process_field_properties(test_case["input"]) |
114 | | - for key, expected_value in test_case["expected"].items(): |
115 | | - assert ( |
116 | | - result[key] == expected_value |
117 | | - ), f"Failed for {key} in test case with annotation: {test_case['input']['Field Annotation']}" |
| 147 | + {"isVis": False}, |
| 148 | + ), |
| 149 | + ], |
| 150 | +) |
| 151 | +def test_process_field_properties_mixed_annotations(input, expected): |
| 152 | + """Test fields with multiple annotations""" |
| 153 | + _, _, _, add_prop = process_row(update_keys(input)) |
| 154 | + for key, expected_value in expected.items(): |
| 155 | + assert ( |
| 156 | + add_prop[key] == expected_value |
| 157 | + ), f"Failed for {key} in test case with annotation: {input['Field Annotation']}" |
0 commit comments