|
| 1 | +from collections import Counter |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +from aws_doc_sdk_examples_tools.metadata import Example |
| 5 | +from aws_doc_sdk_examples_tools.lliam.service_layer.dedupe_reservoir import ( |
| 6 | + make_abbrev, |
| 7 | + example_in_packages, |
| 8 | + reset_abbrev_count, |
| 9 | + dedupe_examples, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +def test_make_abbrev_continues_numbering(): |
| 14 | + """Test that numbering continues from existing numbered titles.""" |
| 15 | + counter = Counter({"Some abbrev": 2}) |
| 16 | + example = Example(id="test", file=Path(), languages={}, title_abbrev="Some abbrev") |
| 17 | + result = make_abbrev(example, counter) |
| 18 | + |
| 19 | + assert result == "Some abbrev (3)" |
| 20 | + |
| 21 | + |
| 22 | +def test_make_abbrev_first_occurrence(): |
| 23 | + """Test that first occurrence doesn't get numbered.""" |
| 24 | + counter = Counter() |
| 25 | + example = Example(id="test", file=Path(), languages={}, title_abbrev="New abbrev") |
| 26 | + result = make_abbrev(example, counter) |
| 27 | + |
| 28 | + assert result == "New abbrev" |
| 29 | + assert counter["New abbrev"] == 1 |
| 30 | + |
| 31 | + |
| 32 | +def test_example_in_packages_no_packages(): |
| 33 | + """Test that example is included when no packages specified.""" |
| 34 | + example = Example(id="test", file=Path("test_metadata.yaml"), languages={}) |
| 35 | + result = example_in_packages(example, []) |
| 36 | + |
| 37 | + assert result is True |
| 38 | + |
| 39 | + |
| 40 | +def test_example_in_packages_matching_package(): |
| 41 | + """Test that example is included when package matches.""" |
| 42 | + example = Example(id="test", file=Path("pkg1_metadata.yaml"), languages={}) |
| 43 | + result = example_in_packages(example, ["pkg1", "pkg2"]) |
| 44 | + |
| 45 | + assert result is True |
| 46 | + |
| 47 | + |
| 48 | +def test_example_in_packages_non_matching_package(): |
| 49 | + """Test that example is excluded when package doesn't match.""" |
| 50 | + example = Example(id="test", file=Path("pkg3_metadata.yaml"), languages={}) |
| 51 | + result = example_in_packages(example, ["pkg1", "pkg2"]) |
| 52 | + |
| 53 | + assert result is False |
| 54 | + |
| 55 | + |
| 56 | +def test_build_abbrev_counter(): |
| 57 | + """Test building counter from examples with existing numbered titles.""" |
| 58 | + examples = { |
| 59 | + "1": Example(id="1", file=Path(), languages={}, title_abbrev="Test (1)"), |
| 60 | + "2": Example(id="2", file=Path(), languages={}, title_abbrev="Test (2)"), |
| 61 | + "3": Example(id="3", file=Path(), languages={}, title_abbrev="Other"), |
| 62 | + "4": Example(id="4", file=Path(), languages={}, title_abbrev="Test"), |
| 63 | + } |
| 64 | + |
| 65 | + result = reset_abbrev_count(examples) |
| 66 | + |
| 67 | + assert result["1"].title_abbrev == "Test" |
| 68 | + assert result["2"].title_abbrev == "Test" |
| 69 | + assert result["3"].title_abbrev == "Other" |
| 70 | + assert result["4"].title_abbrev == "Test" |
| 71 | + |
| 72 | + |
| 73 | +def test_build_abbrev_counter_empty(): |
| 74 | + """Test building counter from empty examples list.""" |
| 75 | + result = reset_abbrev_count({}) |
| 76 | + |
| 77 | + assert len(result) == 0 |
| 78 | + |
| 79 | + |
| 80 | +def test_dedupe_examples(): |
| 81 | + """Test deduping examples with existing numbered titles.""" |
| 82 | + examples = { |
| 83 | + "ex1": Example( |
| 84 | + id="ex1", |
| 85 | + file=Path("pkg1_metadata.yaml"), |
| 86 | + languages={}, |
| 87 | + title_abbrev="Test (2) (2)", |
| 88 | + ), |
| 89 | + "ex2": Example( |
| 90 | + id="ex2", |
| 91 | + file=Path("pkg1_metadata.yaml"), |
| 92 | + languages={}, |
| 93 | + title_abbrev="Test (3) (3) (3)", |
| 94 | + ), |
| 95 | + "ex3": Example( |
| 96 | + id="ex3", file=Path("pkg1_metadata.yaml"), languages={}, title_abbrev="Test" |
| 97 | + ), |
| 98 | + "ex4": Example( |
| 99 | + id="ex4", file=Path("pkg1_metadata.yaml"), languages={}, title_abbrev="Test" |
| 100 | + ), |
| 101 | + "ex5": Example( |
| 102 | + id="ex5", file=Path("pkg1_metadata.yaml"), languages={}, title_abbrev="Test" |
| 103 | + ), |
| 104 | + "ex6": Example( |
| 105 | + id="ex6", file=Path("pkg2_metadata.yaml"), languages={}, title_abbrev="Test" |
| 106 | + ), |
| 107 | + } |
| 108 | + |
| 109 | + result = dedupe_examples(examples, []) |
| 110 | + |
| 111 | + assert len(result) == 6 |
| 112 | + title_abbrevs = sorted( |
| 113 | + [ex.title_abbrev for ex in result.values() if ex.title_abbrev] |
| 114 | + ) |
| 115 | + assert title_abbrevs == [ |
| 116 | + "Test", |
| 117 | + "Test (2)", |
| 118 | + "Test (3)", |
| 119 | + "Test (4)", |
| 120 | + "Test (5)", |
| 121 | + "Test (6)", |
| 122 | + ] |
0 commit comments