Skip to content

Commit 5520499

Browse files
committed
generator: fix import flag for count when _one label presents
fixes #1139 The `has_persons_count_label` flag should be set if either there is a `{{count}}` in the label or if there is a `_one` label present for a widget. Add unit tests for the `get_widgets_file_import_flags` function
1 parent 4070818 commit 5520499

File tree

2 files changed

+259
-3
lines changed

2 files changed

+259
-3
lines changed

packages/evolution-generator/src/scripts/generate_widgets.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,20 +1070,27 @@ def get_widgets_file_import_flags(section_rows) -> ImportFlags:
10701070
):
10711071
# Check to see if the conditional is not empty and does not finish with 'CustomConditional'
10721072
import_flags.has_conditionals_import = True
1073-
if row["inputRange"]:
1073+
if row.get("inputRange"):
10741074
import_flags.has_input_range_import = True
10751075
if row["inputType"] == "Custom":
10761076
import_flags.has_custom_widgets_import = True
1077-
if row["help_popup"] or row["confirm_popup"]:
1077+
if row.get("help_popup") or row.get("confirm_popup"):
10781078
import_flags.has_help_popup_import = True
10791079

10801080
# Check all rows for label context
10811081
label_fr = row.get("label::fr", "")
10821082
label_en = row.get("label::en", "")
1083+
label_one_en = row.get("label_one::en", "")
1084+
label_one_fr = row.get("label_one::fr", "")
10831085
# Check for {{nickname}}, {{count}}, and {{genderedSuffix:...}} in labels
10841086
if "{{nickname}}" in label_fr or "{{nickname}}" in label_en:
10851087
import_flags.has_nickname_label = True
1086-
if "{{count}}" in label_fr or "{{count}}" in label_en:
1088+
if (
1089+
(label_one_en.strip())
1090+
or (label_one_fr.strip())
1091+
or "{{count}}" in label_fr
1092+
or "{{count}}" in label_en
1093+
):
10871094
import_flags.has_persons_count_label = True
10881095
if "{{genderedSuffix" in label_fr or "{{genderedSuffix" in label_en:
10891096
import_flags.has_gendered_suffix_label = True

packages/evolution-generator/src/tests/test_generate_widgets.py

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
get_string_parameters,
1818
generate_path,
1919
generate_import_statements,
20+
get_widgets_file_import_flags,
2021
)
2122

2223

@@ -935,3 +936,251 @@ def test_parse_parameters_multiple_line_breaks(self):
935936
assert params["max"] == "7"
936937
assert params["overmaxallowed"] is True
937938
assert len(params) == 3 # make sure no empty or extra keys are created
939+
940+
941+
class TestGetWidgetsFileImportFlags:
942+
"""Tests for get_widgets_file_import_flags function"""
943+
944+
def test_empty_section_rows(self):
945+
"""Test that empty section_rows returns default ImportFlags"""
946+
section_rows = []
947+
import_flags = get_widgets_file_import_flags(section_rows)
948+
949+
# Only validations should be True by default
950+
assert import_flags.has_validations_import is True
951+
952+
# All other flags should be False
953+
assert import_flags.has_choices_import is False
954+
assert import_flags.has_custom_choices_import is False
955+
assert import_flags.has_conditionals_import is False
956+
assert import_flags.has_input_range_import is False
957+
assert import_flags.has_custom_widgets_import is False
958+
assert import_flags.has_custom_validations_import is False
959+
assert import_flags.has_custom_conditionals_import is False
960+
assert import_flags.has_help_popup_import is False
961+
assert import_flags.has_helper_import is False
962+
assert import_flags.has_formatter_import is False
963+
assert import_flags.has_custom_formatter_import is False
964+
assert import_flags.has_nickname_label is False
965+
assert import_flags.has_persons_count_label is False
966+
assert import_flags.has_gendered_suffix_label is False
967+
968+
def test_choices_imports(self):
969+
"""Test that choices columns are correctly detected"""
970+
# Standard choices
971+
section_rows = [
972+
{
973+
"choices": "yesNo",
974+
"inputType": "Radio",
975+
"validation": "",
976+
"conditional": "",
977+
}
978+
]
979+
import_flags = get_widgets_file_import_flags(section_rows)
980+
assert import_flags.has_choices_import is True
981+
assert import_flags.has_custom_choices_import is False
982+
983+
# Custom choices
984+
section_rows = [
985+
{
986+
"choices": "myCustomChoices",
987+
"inputType": "Radio",
988+
"validation": "",
989+
"conditional": "",
990+
}
991+
]
992+
import_flags = get_widgets_file_import_flags(section_rows)
993+
assert import_flags.has_choices_import is False
994+
assert import_flags.has_custom_choices_import is True
995+
996+
# Explicitly custom choices with CustomChoices suffix
997+
section_rows = [
998+
{
999+
"choices": "myCustomChoices_customChoices",
1000+
"inputType": "Radio",
1001+
"validation": "",
1002+
"conditional": "",
1003+
}
1004+
]
1005+
import_flags = get_widgets_file_import_flags(section_rows)
1006+
assert import_flags.has_choices_import is False
1007+
assert import_flags.has_custom_choices_import is True
1008+
1009+
def test_validation_imports(self):
1010+
"""Test that validation columns are correctly detected"""
1011+
# Default validations
1012+
section_rows = [
1013+
{"inputType": "Radio", "choices": "", "validation": "", "conditional": ""}
1014+
]
1015+
import_flags = get_widgets_file_import_flags(section_rows)
1016+
assert import_flags.has_validations_import is True
1017+
assert import_flags.has_custom_validations_import is False
1018+
1019+
# Standard validation
1020+
section_rows = [
1021+
{
1022+
"inputType": "Radio",
1023+
"choices": "",
1024+
"validation": "phoneNumberValidation",
1025+
"conditional": "",
1026+
}
1027+
]
1028+
import_flags = get_widgets_file_import_flags(section_rows)
1029+
assert import_flags.has_validations_import is True
1030+
assert import_flags.has_custom_validations_import is False
1031+
1032+
# Explicitly custom validation with CustomValidation suffix
1033+
section_rows = [
1034+
{
1035+
"inputType": "Radio",
1036+
"choices": "",
1037+
"validation": "specialCustomValidation",
1038+
"conditional": "",
1039+
}
1040+
]
1041+
import_flags = get_widgets_file_import_flags(section_rows)
1042+
print(import_flags)
1043+
# has_validation_import is always true because of default validations, should it be?
1044+
assert import_flags.has_validations_import is True
1045+
assert import_flags.has_custom_validations_import is True
1046+
1047+
def test_conditional_imports(self):
1048+
"""Test that conditional columns are correctly detected"""
1049+
# Default conditionals
1050+
section_rows = [
1051+
{"inputType": "Radio", "choices": "", "validation": "", "conditional": ""}
1052+
]
1053+
import_flags = get_widgets_file_import_flags(section_rows)
1054+
assert import_flags.has_conditionals_import is False
1055+
assert import_flags.has_custom_conditionals_import is False
1056+
1057+
# Standard conditional
1058+
section_rows = [
1059+
{
1060+
"inputType": "Radio",
1061+
"choices": "",
1062+
"validation": "",
1063+
"conditional": "someConditional",
1064+
}
1065+
]
1066+
import_flags = get_widgets_file_import_flags(section_rows)
1067+
assert import_flags.has_conditionals_import is True
1068+
assert import_flags.has_custom_conditionals_import is False
1069+
1070+
# Custom conditional with CustomConditional suffix
1071+
section_rows = [
1072+
{
1073+
"inputType": "Radio",
1074+
"choices": "",
1075+
"validation": "",
1076+
"conditional": "myCustomConditional",
1077+
}
1078+
]
1079+
import_flags = get_widgets_file_import_flags(section_rows)
1080+
assert import_flags.has_conditionals_import is False
1081+
assert import_flags.has_custom_conditionals_import is True
1082+
1083+
def test_input_range_imports(self):
1084+
"""Test that inputRange columns are correctly detected"""
1085+
section_rows = [
1086+
{
1087+
"inputType": "Radio",
1088+
"choices": "",
1089+
"validation": "",
1090+
"conditional": "",
1091+
"inputRange": "satisfaction",
1092+
}
1093+
]
1094+
import_flags = get_widgets_file_import_flags(section_rows)
1095+
assert import_flags.has_input_range_import is True
1096+
1097+
def test_custom_widgets_imports(self):
1098+
"""Test that Custom inputType is correctly detected"""
1099+
section_rows = [
1100+
{"inputType": "Custom", "choices": "", "validation": "", "conditional": ""}
1101+
]
1102+
import_flags = get_widgets_file_import_flags(section_rows)
1103+
assert import_flags.has_custom_widgets_import is True
1104+
1105+
def test_help_popup_imports(self):
1106+
"""Test that help_popup columns are correctly detected"""
1107+
# Help popup
1108+
section_rows = [
1109+
{
1110+
"inputType": "Radio",
1111+
"choices": "",
1112+
"validation": "",
1113+
"conditional": "",
1114+
"help_popup": "someHelpPopup",
1115+
}
1116+
]
1117+
import_flags = get_widgets_file_import_flags(section_rows)
1118+
assert import_flags.has_help_popup_import is True
1119+
1120+
# Confirm popup
1121+
section_rows = [
1122+
{
1123+
"inputType": "Radio",
1124+
"choices": "",
1125+
"validation": "",
1126+
"conditional": "",
1127+
"confirm_popup": "someConfirmPopup",
1128+
}
1129+
]
1130+
import_flags = get_widgets_file_import_flags(section_rows)
1131+
assert import_flags.has_help_popup_import is True
1132+
1133+
def test_label_context_imports(self):
1134+
"""Test that label context imports are correctly detected"""
1135+
# Nickname label
1136+
section_rows = [
1137+
{
1138+
"inputType": "Radio",
1139+
"choices": "",
1140+
"validation": "",
1141+
"conditional": "",
1142+
"label::en": "Hello {{nickname}}!",
1143+
}
1144+
]
1145+
import_flags = get_widgets_file_import_flags(section_rows)
1146+
assert import_flags.has_nickname_label is True
1147+
1148+
# Persons count via count in label
1149+
section_rows = [
1150+
{
1151+
"inputType": "Radio",
1152+
"choices": "",
1153+
"validation": "",
1154+
"conditional": "",
1155+
"label::en": "There are {{count}} people",
1156+
}
1157+
]
1158+
import_flags = get_widgets_file_import_flags(section_rows)
1159+
assert import_flags.has_persons_count_label is True
1160+
1161+
# Persons count via label_one
1162+
section_rows = [
1163+
{
1164+
"inputType": "Radio",
1165+
"choices": "",
1166+
"validation": "",
1167+
"conditional": "",
1168+
"label::en": "Regular label",
1169+
"label_one::en": "Single person label",
1170+
}
1171+
]
1172+
import_flags = get_widgets_file_import_flags(section_rows)
1173+
assert import_flags.has_persons_count_label is True
1174+
1175+
# Gendered suffix
1176+
section_rows = [
1177+
{
1178+
"inputType": "Radio",
1179+
"choices": "",
1180+
"validation": "",
1181+
"conditional": "",
1182+
"label::en": "{{genderedSuffix:he/she}} is happy",
1183+
}
1184+
]
1185+
import_flags = get_widgets_file_import_flags(section_rows)
1186+
assert import_flags.has_gendered_suffix_label is True

0 commit comments

Comments
 (0)