forked from JesperDramsch/python-deadlines
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_fuzzy_match.py
More file actions
648 lines (538 loc) · 24 KB
/
Copy pathtest_fuzzy_match.py
File metadata and controls
648 lines (538 loc) · 24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
"""Tests for fuzzy matching logic in conference synchronization.
This module tests the fuzzy_match function that compares conference names
between YAML and CSV sources to find matches. Tests use real DataFrames
and only mock external I/O (file system, user input).
Key behaviors tested:
- Exact name matching (100% score)
- Similar name matching (90%+ score with user confirmation)
- Dissimilar names not matching
- Title match structure in returned DataFrame
- CFP filling with TBA when missing
"""
import sys
from pathlib import Path
from unittest.mock import patch
import pandas as pd
import pytest
sys.path.insert(0, str(Path(__file__).parent))
sys.path.append(str(Path(__file__).parent.parent / "utils"))
from hypothesis_strategies import HYPOTHESIS_AVAILABLE
from tidy_conf.interactive_merge import fuzzy_match
class TestExactMatching:
"""Test fuzzy matching behavior when names are identical."""
def test_exact_match_scores_100(self, mock_title_mappings):
"""Identical conference names should match with 100% confidence.
Contract: When names are exactly equal, fuzzy_match should:
- Find the match automatically (no user prompt)
- Combine the data from both sources
"""
df_yml = pd.DataFrame(
{
"conference": ["PyCon Germany & PyData Conference"],
"year": [2026],
"cfp": ["2025-12-21 23:59:59"],
"link": ["https://2026.pycon.de/"],
"place": ["Darmstadt, Germany"],
"start": ["2026-04-14"],
"end": ["2026-04-17"],
},
)
df_remote = pd.DataFrame(
{
"conference": ["PyCon Germany & PyData Conference"],
"year": [2026],
"cfp": ["2025-12-21 23:59:59"],
"link": ["https://pycon.de/"],
"place": ["Darmstadt, Germany"],
"start": ["2026-04-14"],
"end": ["2026-04-17"],
},
)
result, _remote, _report = fuzzy_match(df_yml, df_remote)
# Should find the match
assert not result.empty, "Result should not be empty for exact match"
assert len(result) == 1, f"Expected 1 merged conference, got {len(result)}"
# Conference name should be preserved
assert "PyCon Germany" in str(result["conference"].iloc[0]) or "PyData" in str(
result["conference"].iloc[0],
), f"Conference name corrupted: {result['conference'].iloc[0]}"
def test_exact_match_no_user_prompt(self, mock_title_mappings):
"""Exact matches should not prompt the user for confirmation.
We verify this by NOT mocking input and expecting no interaction.
"""
df_yml = pd.DataFrame(
{
"conference": ["DjangoCon US"],
"year": [2026],
"cfp": ["2026-03-16 11:00:00"],
"link": ["https://djangocon.us/"],
"place": ["Chicago, USA"],
"start": ["2026-09-14"],
"end": ["2026-09-18"],
},
)
df_remote = pd.DataFrame(
{
"conference": ["DjangoCon US"],
"year": [2026],
"cfp": ["2026-03-16 11:00:00"],
"link": ["https://2026.djangocon.us/"],
"place": ["Chicago, USA"],
"start": ["2026-09-14"],
"end": ["2026-09-18"],
},
)
# This should not prompt - if it does, test will hang or fail
with patch("builtins.input", side_effect=AssertionError("Should not prompt for exact match")):
result, _, _report = fuzzy_match(df_yml, df_remote)
assert len(result) == 1
class TestSimilarNameMatching:
"""Test fuzzy matching when names are similar but not identical."""
def test_similar_names_prompt_user(self, mock_title_mappings):
"""Similar names (90%+ match) should prompt user for confirmation.
Contract: When similarity is 90-99%, fuzzy_match should:
- Ask the user if the conferences match
- If accepted, treat as match
- If rejected, keep separate
"""
df_yml = pd.DataFrame(
{
"conference": ["PyCon US"],
"year": [2026],
"cfp": ["2025-12-18 23:59:59"],
"link": ["https://us.pycon.org/2026/"],
"place": ["Pittsburgh, USA"],
"start": ["2026-05-06"],
"end": ["2026-05-11"],
},
)
df_remote = pd.DataFrame(
{
"conference": ["PyCon United States"],
"year": [2026],
"cfp": ["2025-12-18 23:59:59"],
"link": ["https://pycon.us/"],
"place": ["Pittsburgh, PA, USA"],
"start": ["2026-05-06"],
"end": ["2026-05-11"],
},
)
# User accepts the match
with patch("builtins.input", return_value="y"):
result, _, _report = fuzzy_match(df_yml, df_remote)
# Match should be accepted
assert not result.empty
# Original YAML name should be preserved
assert "PyCon" in str(result["conference"].iloc[0])
def test_user_rejects_similar_match(self, mock_title_mappings):
"""When user rejects a fuzzy match, conferences stay separate.
Contract: Rejecting a fuzzy match should:
- Keep YAML conference in result with original name
- Keep CSV conference in remote for later processing
"""
df_yml = pd.DataFrame(
{
"conference": ["PyCon US"],
"year": [2026],
"cfp": ["2025-12-18 23:59:59"],
"link": ["https://us.pycon.org/2026/"],
"place": ["Pittsburgh, USA"],
"start": ["2026-05-06"],
"end": ["2026-05-11"],
},
)
df_remote = pd.DataFrame(
{
"conference": ["PyCon United States"],
"year": [2026],
"cfp": ["2025-12-18 23:59:59"],
"link": ["https://pycon.us/"],
"place": ["Pittsburgh, PA, USA"],
"start": ["2026-05-06"],
"end": ["2026-05-11"],
},
)
# User rejects the match
with patch("builtins.input", return_value="n"):
result, remote, _report = fuzzy_match(df_yml, df_remote)
# YAML conference should still be in result (may be normalized to "PyCon USA")
conf_list = result["conference"].tolist()
assert any("PyCon" in c for c in conf_list), f"YAML conference should be preserved, got: {conf_list}"
# Remote conference should still be available
assert len(remote) >= 1, "Remote conference should be preserved after rejection"
class TestDissimilarNames:
"""Test that dissimilar conference names are not matched."""
def test_dissimilar_names_no_match(self, mock_title_mappings):
"""Conferences with very different names should not match.
Contract: When similarity is below 90%, fuzzy_match should:
- NOT prompt user
- Keep conferences separate
"""
df_yml = pd.DataFrame(
{
"conference": ["PyCon US"],
"year": [2026],
"cfp": ["2025-12-18 23:59:59"],
"link": ["https://us.pycon.org/2026/"],
"place": ["Pittsburgh, USA"],
"start": ["2026-05-06"],
"end": ["2026-05-11"],
},
)
df_remote = pd.DataFrame(
{
"conference": ["DjangoCon Europe"],
"year": [2026],
"cfp": ["2026-03-01 23:59:00"],
"link": ["https://djangocon.eu/"],
"place": ["Amsterdam, Netherlands"],
"start": ["2026-06-01"],
"end": ["2026-06-05"],
},
)
# Should not prompt for dissimilar names
with patch("builtins.input", side_effect=AssertionError("Should not prompt for dissimilar names")):
result, remote, _report = fuzzy_match(df_yml, df_remote)
# Both conferences should exist separately (PyCon US may be normalized to PyCon USA)
conf_list = result["conference"].tolist()
assert any("PyCon" in c for c in conf_list), f"PyCon conference should be in result: {conf_list}"
assert "DjangoCon Europe" in remote["conference"].tolist()
def test_different_conference_types_not_matched(self, mock_title_mappings):
"""PyCon vs DjangoCon should never be incorrectly matched."""
df_yml = pd.DataFrame(
{
"conference": ["PyCon Germany"],
"year": [2026],
"cfp": ["2025-12-21 23:59:59"],
"link": ["https://pycon.de/"],
"place": ["Darmstadt, Germany"],
"start": ["2026-04-14"],
"end": ["2026-04-17"],
},
)
df_remote = pd.DataFrame(
{
"conference": ["DjangoCon Germany"], # Similar location, different type
"year": [2026],
"cfp": ["2026-01-15 23:59:00"],
"link": ["https://djangocon.de/"],
"place": ["Berlin, Germany"],
"start": ["2026-06-01"],
"end": ["2026-06-03"],
},
)
# User should be prompted (names are somewhat similar)
# We reject to verify they stay separate
with patch("builtins.input", return_value="n"):
result, remote, _report = fuzzy_match(df_yml, df_remote)
# Both should exist separately
result["conference"].tolist()
remote["conference"].tolist()
# Verify no incorrect merging happened
assert len(result) >= 1 and len(remote) >= 1, "Both conferences should be preserved when rejected"
class TestTitleMatchStructure:
"""Test that the title_match column/index is correctly structured."""
def test_result_has_title_match_index(self, mock_title_mappings):
"""Result DataFrame should have title_match as index name."""
df_yml = pd.DataFrame(
{
"conference": ["Test Conference"],
"year": [2026],
"cfp": ["2026-01-15 23:59:00"],
"link": ["https://test.conf/"],
"place": ["Test City"],
"start": ["2026-06-01"],
"end": ["2026-06-03"],
},
)
df_remote = pd.DataFrame(
{
"conference": ["Other Conference"],
"year": [2026],
"cfp": ["2026-02-15 23:59:00"],
"link": ["https://other.conf/"],
"place": ["Other City"],
"start": ["2026-07-01"],
"end": ["2026-07-03"],
},
)
_result, remote, _report = fuzzy_match(df_yml, df_remote)
# Remote should have title_match as index name
assert (
remote.index.name == "title_match"
), f"Remote index name should be 'title_match', got '{remote.index.name}'"
def test_title_match_values_are_strings(self, mock_title_mappings):
"""Title match values should be strings, not integers or tuples."""
df_yml = pd.DataFrame(
{
"conference": ["Test Conference"],
"year": [2026],
"cfp": ["2026-01-15 23:59:00"],
"link": ["https://test.conf/"],
"place": ["Test City"],
"start": ["2026-06-01"],
"end": ["2026-06-03"],
},
)
df_remote = pd.DataFrame(
{
"conference": ["Test Conference"],
"year": [2026],
"cfp": ["2026-01-15 23:59:00"],
"link": ["https://test.conf/"],
"place": ["Test City"],
"start": ["2026-06-01"],
"end": ["2026-06-03"],
},
)
result, _, _report = fuzzy_match(df_yml, df_remote)
# Check index values are strings
for idx in result.index:
assert isinstance(idx, str), f"Index value should be string, got {type(idx)}: {idx}"
class TestCFPHandling:
"""Test CFP field handling in fuzzy match results."""
def test_missing_cfp_filled_with_tba(self, mock_title_mappings):
"""Missing CFP values should be filled with 'TBA'.
Contract: fuzzy_match should fill NaN CFP values with 'TBA'
to indicate "To Be Announced".
"""
df_yml = pd.DataFrame(
{
"conference": ["Test Conference"],
"year": [2026],
"cfp": [None], # Missing CFP
"link": ["https://test.conf/"],
"place": ["Test City"],
"start": ["2026-06-01"],
"end": ["2026-06-03"],
},
)
df_remote = pd.DataFrame(
{
"conference": ["Other Conference"],
"year": [2026],
"cfp": ["2026-02-15 23:59:00"],
"link": ["https://other.conf/"],
"place": ["Other City"],
"start": ["2026-07-01"],
"end": ["2026-07-03"],
},
)
result, _, _report = fuzzy_match(df_yml, df_remote)
# Check that CFP is filled with TBA for the conference that had None
test_conf_rows = result[result["conference"].str.contains("Test", na=False)]
if len(test_conf_rows) > 0:
cfp_value = test_conf_rows["cfp"].iloc[0]
assert cfp_value == "TBA" or pd.notna(
cfp_value,
), f"Missing CFP should be filled with 'TBA', got: {cfp_value}"
class TestEmptyDataFrames:
"""Test fuzzy matching behavior with empty DataFrames."""
def test_empty_remote_handled_gracefully(self, mock_title_mappings):
"""Fuzzy match should handle empty remote DataFrame without crashing."""
df_yml = pd.DataFrame(
{
"conference": ["Test Conference"],
"year": [2026],
"cfp": ["2026-01-15 23:59:00"],
"link": ["https://test.conf/"],
"place": ["Test City"],
"start": ["2026-06-01"],
"end": ["2026-06-03"],
},
)
df_remote = pd.DataFrame(columns=["conference", "year", "cfp", "link", "place", "start", "end"])
result, _remote, _report = fuzzy_match(df_yml, df_remote)
# Should not crash, result should contain YAML data
assert not result.empty, "Result should not be empty when YAML has data"
assert "Test Conference" in result["conference"].tolist() or "Test Conference" in result.index.tolist()
class TestRealDataMatching:
"""Test fuzzy matching with realistic test fixtures."""
def test_matches_pycon_de_variants(self, mock_title_mappings_with_data, minimal_yaml_df, minimal_csv_df):
"""REGRESSION: PyCon DE variants should match PyCon Germany.
This was a bug where 'PyCon DE & PyData' in CSV didn't match
'PyCon Germany & PyData Conference' in YAML, causing data loss.
"""
# Filter to just PyCon Germany from YAML
pycon_yml = minimal_yaml_df[minimal_yaml_df["conference"].str.contains("Germany", na=False)].copy()
# Filter to just PyCon DE from CSV
pycon_csv = minimal_csv_df[minimal_csv_df["conference"].str.contains("PyCon DE", na=False)].copy()
if len(pycon_yml) > 0 and len(pycon_csv) > 0:
# With proper mappings, these should match without user prompt
with patch("builtins.input", return_value="y"):
result, _, _report = fuzzy_match(pycon_yml, pycon_csv)
# Should have merged the data
assert len(result) >= 1, "PyCon DE should match PyCon Germany"
def test_europython_variants_match(self, mock_title_mappings, minimal_yaml_df, minimal_csv_df):
"""EuroPython Conference (CSV) should match EuroPython (YAML)."""
# Filter to EuroPython entries
euro_yml = minimal_yaml_df[minimal_yaml_df["conference"].str.contains("EuroPython", na=False)].copy()
euro_csv = minimal_csv_df[minimal_csv_df["conference"].str.contains("EuroPython", na=False)].copy()
if len(euro_yml) > 0 and len(euro_csv) > 0:
# User accepts the match
with patch("builtins.input", return_value="y"):
result, _, _report = fuzzy_match(euro_yml, euro_csv)
# Should match
assert len(result) >= 1
class TestFuzzyMatchThreshold:
"""Test the fuzzy match confidence threshold behavior."""
def test_below_90_percent_no_prompt(self, mock_title_mappings):
"""Matches below 90% confidence should not prompt user.
Contract: Below 90% similarity, conferences are considered
different and should not be merged.
"""
df_yml = pd.DataFrame(
{
"conference": ["ABC Conference"],
"year": [2026],
"cfp": ["2026-01-15 23:59:00"],
"link": ["https://abc.conf/"],
"place": ["ABC City"],
"start": ["2026-06-01"],
"end": ["2026-06-03"],
},
)
df_remote = pd.DataFrame(
{
"conference": ["XYZ Symposium"], # Very different name
"year": [2026],
"cfp": ["2026-02-15 23:59:00"],
"link": ["https://xyz.conf/"],
"place": ["XYZ City"],
"start": ["2026-07-01"],
"end": ["2026-07-03"],
},
)
# Should not prompt
with patch("builtins.input", side_effect=AssertionError("Should not prompt below threshold")):
_result, remote, _report = fuzzy_match(df_yml, df_remote)
# Both should be preserved separately
assert len(remote) >= 1
class TestDataPreservation:
"""Test that original data is preserved through fuzzy matching."""
def test_yaml_data_not_lost(self, mock_title_mappings):
"""YAML conference data should not be silently dropped.
Contract: All YAML conferences should appear in the result,
even if they don't match anything in remote.
"""
df_yml = pd.DataFrame(
{
"conference": ["Unique YAML Conference"],
"year": [2026],
"cfp": ["2026-01-15 23:59:00"],
"link": ["https://unique-yaml.conf/"],
"place": ["YAML City"],
"start": ["2026-06-01"],
"end": ["2026-06-03"],
"mastodon": ["https://fosstodon.org/@unique"], # Extra field
},
)
df_remote = pd.DataFrame(
{
"conference": ["Unique CSV Conference"],
"year": [2026],
"cfp": ["2026-02-15 23:59:00"],
"link": ["https://unique-csv.conf/"],
"place": ["CSV City"],
"start": ["2026-07-01"],
"end": ["2026-07-03"],
},
)
result, _, _report = fuzzy_match(df_yml, df_remote)
# YAML conference should be in result
yaml_conf_found = any("Unique YAML Conference" in str(name) for name in result["conference"].tolist())
assert yaml_conf_found, f"YAML conference should be preserved, got: {result['conference'].tolist()}"
# Extra field (mastodon) should also be preserved if it exists in result columns
if "mastodon" in result.columns:
yaml_rows = result[result["conference"].str.contains("YAML", na=False)]
if len(yaml_rows) > 0:
assert pd.notna(yaml_rows["mastodon"].iloc[0]), "Extra YAML field (mastodon) should be preserved"
# ---------------------------------------------------------------------------
# Property-based tests using Hypothesis
# ---------------------------------------------------------------------------
if HYPOTHESIS_AVAILABLE:
from hypothesis import HealthCheck
from hypothesis import assume
from hypothesis import given
from hypothesis import settings
from hypothesis import strategies as st
@pytest.mark.skipif(not HYPOTHESIS_AVAILABLE, reason="hypothesis not installed")
class TestFuzzyMatchProperties:
"""Property-based tests for fuzzy matching."""
@given(st.lists(st.text(min_size=5, max_size=30), min_size=1, max_size=5, unique=True))
@settings(max_examples=50, suppress_health_check=[HealthCheck.filter_too_much])
def test_fuzzy_match_preserves_all_yaml_entries(self, names):
"""All YAML entries should appear in result (no silent data loss)."""
# Filter out empty or whitespace-only names
names = [n for n in names if len(n.strip()) > 3]
assume(len(names) > 0)
with patch("tidy_conf.interactive_merge.load_title_mappings") as mock1, patch(
"tidy_conf.titles.load_title_mappings",
) as mock2, patch("tidy_conf.interactive_merge.update_title_mappings"):
mock1.return_value = ([], {})
mock2.return_value = ([], {})
df_yml = pd.DataFrame(
{
"conference": names,
"year": [2026] * len(names),
"cfp": ["2026-01-15 23:59:00"] * len(names),
"link": [f"https://conf{i}.org/" for i in range(len(names))],
"place": ["Test City"] * len(names),
"start": ["2026-06-01"] * len(names),
"end": ["2026-06-03"] * len(names),
},
)
df_remote = pd.DataFrame(
columns=["conference", "year", "cfp", "link", "place", "start", "end"],
)
result, _, _report = fuzzy_match(df_yml, df_remote)
# All input conferences should be in result
assert len(result) >= len(names), f"Expected at least {len(names)} results, got {len(result)}"
@given(
st.text(
alphabet=st.characters(
whitelist_categories=("L", "N", "Zs"), # Letters, Numbers, Spaces
whitelist_characters="-&:", # Common punctuation in conference names
),
min_size=10,
max_size=50,
),
)
@settings(max_examples=30)
def test_exact_match_always_scores_100(self, name):
"""Identical names should always match perfectly."""
# Filter to realistic conference names (no control chars, has letters)
assume(len(name.strip()) > 5)
assume(any(c.isalpha() for c in name)) # Must have at least one letter
with patch("tidy_conf.interactive_merge.load_title_mappings") as mock1, patch(
"tidy_conf.titles.load_title_mappings",
) as mock2, patch("tidy_conf.interactive_merge.update_title_mappings"):
mock1.return_value = ([], {})
mock2.return_value = ([], {})
df_yml = pd.DataFrame(
{
"conference": [name],
"year": [2026],
"cfp": ["2026-01-15 23:59:00"],
"link": ["https://test.org/"],
"place": ["Test City"],
"start": ["2026-06-01"],
"end": ["2026-06-03"],
},
)
df_remote = pd.DataFrame(
{
"conference": [name], # Same name
"year": [2026],
"cfp": ["2026-01-15 23:59:00"],
"link": ["https://other.org/"],
"place": ["Test City"],
"start": ["2026-06-01"],
"end": ["2026-06-03"],
},
)
# No user prompts should be needed for exact match
with patch("builtins.input", side_effect=AssertionError("Should not prompt")):
result, _, _report = fuzzy_match(df_yml, df_remote)
# Should be merged (1 result, not 2)
assert len(result) == 1, f"Exact match should merge, got {len(result)} results"