Skip to content

Commit a833285

Browse files
committed
test(test_model): added auto-detect csv file with identifier to check if match with slug format
1 parent d623f3b commit a833285

1 file changed

Lines changed: 29 additions & 108 deletions

File tree

pokemon_v2/test_models.py

Lines changed: 29 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -28,61 +28,6 @@ class CSVResourceNameValidationTestCase(TestCase):
2828
# Pattern for valid resource identifiers: lowercase letters, numbers, and hyphens only
2929
VALID_IDENTIFIER_PATTERN = re.compile(r"^[a-z0-9-]+$")
3030

31-
# CSV files with 'identifier' column to validate
32-
CSV_FILES_TO_VALIDATE = [
33-
"abilities.csv",
34-
"berry_firmness.csv",
35-
"conquest_episodes.csv",
36-
"conquest_kingdoms.csv",
37-
"conquest_move_displacements.csv",
38-
"conquest_move_ranges.csv",
39-
"conquest_stats.csv",
40-
"conquest_warrior_archetypes.csv",
41-
"conquest_warrior_skills.csv",
42-
"conquest_warrior_stats.csv",
43-
"conquest_warriors.csv",
44-
"contest_types.csv",
45-
"egg_groups.csv",
46-
"encounter_conditions.csv",
47-
"encounter_condition_values.csv",
48-
"encounter_methods.csv",
49-
"evolution_triggers.csv",
50-
"genders.csv",
51-
"generations.csv",
52-
"growth_rates.csv",
53-
"items.csv",
54-
"item_categories.csv",
55-
"item_flags.csv",
56-
"item_fling_effects.csv",
57-
"item_pockets.csv",
58-
"languages.csv",
59-
"locations.csv",
60-
"location_areas.csv",
61-
"moves.csv",
62-
"move_battle_styles.csv",
63-
"move_damage_classes.csv",
64-
"move_flags.csv",
65-
"move_meta_ailments.csv",
66-
"move_meta_categories.csv",
67-
"move_targets.csv",
68-
"natures.csv",
69-
"pal_park_areas.csv",
70-
"pokeathlon_stats.csv",
71-
"pokedexes.csv",
72-
"pokemon.csv",
73-
"pokemon_colors.csv",
74-
"pokemon_forms.csv",
75-
"pokemon_habitats.csv",
76-
"pokemon_move_methods.csv",
77-
"pokemon_shapes.csv",
78-
"pokemon_species.csv",
79-
"regions.csv",
80-
"stats.csv",
81-
"types.csv",
82-
"versions.csv",
83-
"version_groups.csv",
84-
]
85-
8631
def test_all_csv_identifiers_are_ascii_slugs(self):
8732
"""
8833
Validate that all resource identifiers in CSV files follow the ASCII slug format.
@@ -99,30 +44,19 @@ def test_all_csv_identifiers_are_ascii_slugs(self):
9944
- Special characters (&, (), ', etc.)
10045
"""
10146
violations = []
102-
missing_files = []
103-
104-
for filename in self.CSV_FILES_TO_VALIDATE:
105-
csv_path = os.path.join(settings.BASE_DIR, "data", "v2", "csv", filename)
47+
csv_dir = os.path.join(settings.BASE_DIR, "data", "v2", "csv")
10648

107-
# Track missing files to report at the end
108-
if not os.path.exists(csv_path):
109-
missing_files.append(filename)
49+
for filename in sorted(os.listdir(csv_dir)):
50+
if not filename.endswith(".csv"):
11051
continue
11152

53+
csv_path = os.path.join(csv_dir, filename)
54+
11255
try:
11356
with open(csv_path, "r", encoding="utf-8") as csvfile:
11457
reader = csv.DictReader(csvfile)
11558

116-
# Check if the identifier column exists
11759
if "identifier" not in reader.fieldnames:
118-
violations.append(
119-
{
120-
"file": filename,
121-
"row": "N/A",
122-
"id": "N/A",
123-
"identifier": "Column 'identifier' not found",
124-
}
125-
)
12660
continue
12761

12862
for row_num, row in enumerate(reader, start=2):
@@ -153,49 +87,36 @@ def test_all_csv_identifiers_are_ascii_slugs(self):
15387
}
15488
)
15589

156-
# If there are violations or missing files, create a detailed error message
157-
if violations or missing_files:
158-
error_lines = []
90+
error_lines = []
15991

160-
# Report missing files first
161-
if missing_files:
162-
error_lines.append("\n\nMissing CSV files:")
163-
for filename in missing_files:
164-
error_lines.append(f" - {filename}")
165-
error_lines.append(
166-
"\nAll CSV files listed in CSV_FILES_TO_VALIDATE must exist."
92+
# Report violations
93+
if violations:
94+
error_lines.append(
95+
"\n\nFound {} resource(s) with invalid identifiers (not ASCII slugs):".format(
96+
len(violations)
16797
)
98+
)
99+
error_lines.append("\nIdentifiers must match pattern: ^[a-z0-9-]+$")
100+
error_lines.append("\nInvalid identifiers found in CSV files:")
168101

169-
# Report violations
170-
if violations:
102+
for v in violations:
171103
error_lines.append(
172-
"\n\nFound {} resource(s) with invalid identifiers (not ASCII slugs):".format(
173-
len(violations)
174-
)
104+
" - {file} (row {row}, id={id}): {identifier}".format(**v)
175105
)
176-
error_lines.append("\nIdentifiers must match pattern: ^[a-z0-9-]+$")
177-
error_lines.append("\nInvalid identifiers found in CSV files:")
178106

179-
for v in violations:
180-
error_lines.append(
181-
" - {file} (row {row}, id={id}): {identifier}".format(**v)
182-
)
183-
184-
error_lines.append(
185-
"\nThese identifiers contain invalid characters and must be normalized."
186-
)
187-
error_lines.append(
188-
"Update the CSV files in data/v2/csv/ to fix these identifiers."
189-
)
190-
error_lines.append("\nSuggested fixes:")
191-
error_lines.append(
192-
" - Remove Unicode apostrophes (') and replace with regular hyphens or remove"
193-
)
194-
error_lines.append(" - Remove Unicode letters (ñ → n)")
195-
error_lines.append(
196-
" - Remove parentheses and other special characters"
197-
)
198-
error_lines.append(" - Convert to lowercase")
107+
error_lines.append(
108+
"\nThese identifiers contain invalid characters and must be normalized."
109+
)
110+
error_lines.append(
111+
"Update the CSV files in data/v2/csv/ to fix these identifiers."
112+
)
113+
error_lines.append("\nSuggested fixes:")
114+
error_lines.append(
115+
" - Remove Unicode apostrophes (') and replace with regular hyphens or remove"
116+
)
117+
error_lines.append(" - Remove Unicode letters (ñ → n)")
118+
error_lines.append(" - Remove parentheses and other special characters")
119+
error_lines.append(" - Convert to lowercase")
199120

200121
self.fail("\n".join(error_lines))
201122

0 commit comments

Comments
 (0)