Skip to content

Commit 0f0cba5

Browse files
committed
fix testing by copy ReproNim#86 and change isVis logic
1 parent af55533 commit 0f0cba5

File tree

1 file changed

+54
-93
lines changed

1 file changed

+54
-93
lines changed

reproschema/tests/test_rs2redcap_redcap2rs.py

Lines changed: 54 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from pathlib import Path
44
from shutil import copytree, rmtree
55

6-
import pandas as pd
76
import pytest
87
from click.testing import CliRunner
98

@@ -100,17 +99,16 @@ def errors_check(cat, atr, orig, final):
10099
print(f"Attribute {atr} is missing in the original {cat}")
101100
elif orig != final:
102101
print(f"Attribute {atr} is different in the final {cat}")
103-
# print(f"Original: {orig}")
104-
# print(f"Final: {final}")
102+
print(f"Original: {orig}")
103+
print(f"Final: {final}")
105104
error_shortmsg = f"{cat}: {atr} is different"
106105
return error_shortmsg
107106

108107

109108
def print_return_msg(error_msg):
110-
# print(error_msg)
109+
print(error_msg)
111110
return error_msg
112111

113-
114112
def compare_protocols(prot_tree_orig, prot_tree_final):
115113
# compare the two dictionaries
116114
errors_list = []
@@ -221,57 +219,49 @@ def compare_protocols(prot_tree_orig, prot_tree_final):
221219
)
222220
)
223221
else:
224-
print(
225-
f"Activity {act_name}: addProperties have different elements"
226-
)
227222
errors_list.append(
228-
f"Activity {act_name}: addProperties have different elements"
223+
print_return_msg(
224+
f"Activity {act_name}: addProperties have different elements, orig: {act_props_orig} and final: {act_props_final}"
225+
)
229226
)
230227
else:
231228
for nm, el in act_props_final.items():
232229
for key in ["isVis", "valueRequired"]:
233230
error = False
234-
orig_value = getattr(act_props_orig[nm], key)
235-
final_value = getattr(el, key)
236-
237-
if key == "valueRequired":
238-
# Debug print
239-
print(f"\nDebug - Activity: {act_name}, Item: {nm}")
240-
print(
241-
f"Original valueRequired: {orig_value}, type: {type(orig_value)}"
242-
)
243-
print(
244-
f"Final valueRequired: {final_value}, type: {type(final_value)}"
245-
)
246-
247-
# Compare only True values
248-
if orig_value is True:
249-
if final_value is not True:
231+
orig_val = getattr(act_props_orig[nm], key)
232+
final_val = getattr(el, key)
233+
234+
if key == "isVis":
235+
orig_norm = normalize_condition(orig_val) if orig_val is not None else None
236+
final_norm = normalize_condition(final_val) if final_val is not None else None
237+
238+
# Case 1: original is True - final can be None or True
239+
if orig_norm is True:
240+
if not (final_norm is None or final_norm is True):
250241
error = True
251-
print(
252-
f"Error case 1: orig=True, final={final_value}"
253-
)
254-
elif final_value is True:
255-
if orig_value is not True:
242+
# Case 2: original is False - final must be False
243+
elif orig_norm is False:
244+
if final_norm is not False:
256245
error = True
257-
print(
258-
f"Error case 2: orig={orig_value}, final=True"
259-
)
260-
261-
elif key == "isVis":
262-
if orig_value is True:
263-
# If original is True and final is None/missing, don't count as error
264-
pass
265-
elif orig_value is not None:
266-
if normalize_condition(orig_value) != normalize_condition(final_value):
246+
# Case 3: original is something else - must match exactly
247+
elif orig_norm is not None:
248+
if orig_norm != final_norm:
267249
error = True
268-
elif final_value is not None and final_value is not True:
250+
else: # handle valueRequired
251+
if (orig_val is not None) and (
252+
normalize_condition(final_val)
253+
!= normalize_condition(orig_val)
254+
):
269255
error = True
270-
256+
elif final_val and orig_val is None:
257+
if normalize_condition(final_val) != False:
258+
error = True
259+
271260
if error:
272261
errors_list.append(
273-
print(
274-
f"Activity {act_name}: addProperties {nm} have different {key}"
262+
print_return_msg(
263+
f"Activity {act_name}: addProperties {nm} have different {key}, "
264+
f"orig: {orig_val}, final: {normalize_condition(final_val)}"
275265
)
276266
)
277267
# check compute
@@ -285,9 +275,10 @@ def compare_protocols(prot_tree_orig, prot_tree_final):
285275
)
286276
)
287277
else:
288-
print(f"Activity {act_name}: compute have different elements")
289278
errors_list.append(
290-
f"Activity {act_name}: compute have different elements"
279+
print_return_msg(
280+
f"Activity {act_name}: compute have different elements, orig: {act_comp_orig}, final: {act_comp_final}"
281+
)
291282
)
292283
else:
293284
for nm, el in act_comp_final.items():
@@ -297,7 +288,7 @@ def compare_protocols(prot_tree_orig, prot_tree_final):
297288
getattr(act_comp_orig[nm], "jsExpression")
298289
):
299290
errors_list.append(
300-
print(
291+
print_return_msg(
301292
f"Activity {act_name}: compute {nm} have different jsExpression"
302293
)
303294
)
@@ -313,7 +304,7 @@ def compare_protocols(prot_tree_orig, prot_tree_final):
313304
else:
314305
errors_list.append(
315306
print_return_msg(
316-
f"Activity {act_name}: items have different elements"
307+
f"Activity {act_name}: items have different elements, orig: {act_items_orig}, final: {act_items_final}"
317308
)
318309
)
319310
else:
@@ -334,45 +325,20 @@ def compare_protocols(prot_tree_orig, prot_tree_final):
334325
) != normalize_condition(
335326
act_items_orig[nm]["obj"].question.get("en", "")
336327
):
337-
# Handle cases where one might be NaN/None and the other empty string
338-
orig_q = act_items_orig[nm]["obj"].question.get("en", "")
339-
final_q = el["obj"].question.get("en", "")
340-
341-
print(
342-
f"\nDebug - Comparing questions for {act_name}/{nm}:"
343-
)
344-
print(f"Original question: {repr(orig_q)}")
345-
print(f"Final question: {repr(final_q)}")
346-
print(
347-
f"Original normalized: {repr(normalize_condition(orig_q))}"
348-
)
349-
print(
350-
f"Final normalized: {repr(normalize_condition(final_q))}"
351-
)
352-
353-
# Convert None/NaN to empty string for comparison
354-
orig_q = (
355-
"" if pd.isna(orig_q) or orig_q is None else orig_q
356-
)
357-
final_q = (
358-
"" if pd.isna(final_q) or final_q is None else final_q
359-
)
360-
361-
if normalize_condition(orig_q) != normalize_condition(
362-
final_q
328+
if "<br><br>" in normalize_condition(
329+
act_items_orig[nm]["obj"].question.get("en", "")
363330
):
364-
if "<br><br>" in normalize_condition(orig_q):
365-
warnings_list.append(
366-
print_return_msg(
367-
f"Activity {act_name}: items {nm} have different question, FIX normalized function!!!"
368-
)
331+
warnings_list.append(
332+
print_return_msg(
333+
f"Activity {act_name}: items {nm} have different question, FIX normalized function!!!"
369334
)
370-
else:
371-
errors_list.append(
372-
print_return_msg(
373-
f"Activity {act_name}: items {nm} have different question"
374-
)
335+
)
336+
else:
337+
errors_list.append(
338+
print_return_msg(
339+
f"Activity {act_name}: items {nm} have different question"
375340
)
341+
)
376342
elif (
377343
el["obj"].ui.inputType
378344
!= act_items_orig[nm]["obj"].ui.inputType
@@ -420,7 +386,7 @@ def test_rs2redcap_redcap2rs(tmpdir):
420386
"output_nimh.csv",
421387
],
422388
)
423-
# # print("\n results of reproschema2redcap", result1.output)
389+
print("\n results of reproschema2redcap", result1.output)
424390

425391
result2 = runner.invoke(
426392
main,
@@ -433,7 +399,7 @@ def test_rs2redcap_redcap2rs(tmpdir):
433399
],
434400
)
435401

436-
# print("\n results of redcap2reproschema", result2.output)
402+
print("\n results of redcap2reproschema", result2.output)
437403

438404
protocol_schema_orig = (
439405
"nimh_minimal_orig/nimh_minimal/nimh_minimal/nimh_minimal_schema"
@@ -462,10 +428,5 @@ def test_rs2redcap_redcap2rs(tmpdir):
462428
prot_tree_orig, prot_tree_final
463429
)
464430

465-
# More informative assertion
466-
real_errors = [err for err in errors_list if err is not None]
467-
if real_errors:
468-
print("\nDetailed errors:")
469-
for err in real_errors:
470-
print(f"- {err}")
471-
assert not real_errors, f"Found {len(real_errors)} errors"
431+
assert not errors_list, f"Errors: {errors_list}"
432+
print("No errors, but found warnings: ", warnings_list)

0 commit comments

Comments
 (0)