File size: 12,274 Bytes
1cd137c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Tests for taxonomy classification rules."""
from __future__ import annotations

from reference_db.taxonomy.reference_taxonomy import ReferenceCategory
from reference_db.taxonomy.taxonomy_rules import classify_taxonomy


class TestClearSingleCategory:
    """Products that should classify to exactly one category."""

    def test_dairy_milk(self):
        result = classify_taxonomy("Milk 2% 1L")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.DAIRY

    def test_meat_chicken(self):
        result = classify_taxonomy("Chicken Breast Boneless")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.MEAT_SEAFOOD

    def test_beverages_juice(self):
        result = classify_taxonomy("Orange Juice From Concentrate")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.BEVERAGES

    def test_bakery_bread(self):
        result = classify_taxonomy("Whole Wheat Bread")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.BAKERY

    def test_produce_apple(self):
        result = classify_taxonomy("Organic Apples")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.PRODUCE

    def test_snacks_chips(self):
        result = classify_taxonomy("Kettle Cooked Potato Chips")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.SNACKS

    def test_frozen_ice_cream(self):
        result = classify_taxonomy("Vanilla Ice Cream 1.5L")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.FROZEN


class TestPreviouslyNoEvidence:
    """Products that previously returned no_evidence and should now classify."""

    def test_sliced_almonds(self):
        result = classify_taxonomy("Sliced Almonds 250 g")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.SNACKS

    def test_sunflower_seeds(self):
        result = classify_taxonomy("Roasted Salted Sunflower Seeds 375 g")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.SNACKS

    def test_olives(self):
        result = classify_taxonomy("Kalamata Whole Olives 375 ml")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.CONDIMENTS_SAUCES

    def test_water(self):
        result = classify_taxonomy("Natural Spring Water 4 L")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.BEVERAGES

    def test_safflower_oil(self):
        result = classify_taxonomy("Safflower Oil 100% Pure 946 ml")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.OILS_VINEGARS

    def test_tacos(self):
        result = classify_taxonomy("Taco Kit Hard And Soft 393 g")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.CONDIMENTS_SAUCES

    def test_romaine(self):
        result = classify_taxonomy("Romaine Hearts 510 g")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.PRODUCE

    def test_cream_of_tartar(self):
        result = classify_taxonomy("Cream Of Tartar 100 g")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.BAKING_INGREDIENTS

    def test_eggs(self):
        result = classify_taxonomy("Brown Eggs Large Carton 12 Count")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.DAIRY

    def test_thyme(self):
        result = classify_taxonomy("Ground Thyme 80 g")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.GENERAL_GROCERY

    def test_cinnamon(self):
        result = classify_taxonomy("Cinnamon Sticks 105 g")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.GENERAL_GROCERY

    def test_guacamole(self):
        result = classify_taxonomy("Guacamole 454 g")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.CONDIMENTS_SAUCES

    def test_hummus(self):
        result = classify_taxonomy("Mini Chick Pea Hummus 228 g")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.CONDIMENTS_SAUCES

    def test_dried_mango(self):
        # Tier-0 meaning rule dried_mango_confectionery resolves dried-mango
        # slices to CONFECTIONERY before generic produce keywords fire.
        result = classify_taxonomy("Dried Mango Slices 200 g")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.CONFECTIONERY
        assert result.matched_rule == "dried_mango_confectionery"

    def test_pecan_halves(self):
        result = classify_taxonomy("Pecan Halves 155 g")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.SNACKS

    def test_walnut_pieces(self):
        result = classify_taxonomy("Walnut Pieces 250 g")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.SNACKS

    def test_cranberry(self):
        result = classify_taxonomy("Cocktail White Cranberry 1.89 L")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.PRODUCE

    def test_blueberries(self):
        result = classify_taxonomy("Fresh Blueberries 340 g")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.PRODUCE

    def test_strawberries(self):
        result = classify_taxonomy("Fresh Strawberries 454 g")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.PRODUCE


class TestGenuineAmbiguity:
    """Products that are genuinely ambiguous and should remain AMBIGUOUS."""

    def test_frozen_chicken(self):
        """Frozen + chicken resolves to FROZEN by frozen_precedence."""
        result = classify_taxonomy("Frozen Chicken Breast 4 kg")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.FROZEN
        assert result.matched_rule == "frozen_precedence"
        candidates = set(result.candidates)
        assert ReferenceCategory.FROZEN in candidates
        assert ReferenceCategory.MEAT_SEAFOOD in candidates

    def test_milk_chocolate(self):
        """Milk chocolate resolves to CONFECTIONERY by specific phrase."""
        result = classify_taxonomy("Milk Chocolate Bar 100 g")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.CONFECTIONERY
        assert result.matched_rule == "specific_milk_chocolate"
        candidates = set(result.candidates)
        assert ReferenceCategory.CONFECTIONERY in candidates

    def test_apple_juice_is_beverages(self):
        """Apple juice is handled by Tier-0 juice rule → PASS/BEVERAGES."""
        result = classify_taxonomy("Apple Juice 1 L")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.BEVERAGES
        assert result.resolution == "tier0_disambiguation"


class TestWordBoundaryBehavior:
    """Rules should not overmatch due to missing word boundaries."""

    def test_almond_not_in_compliments(self):
        """'almond' inside 'compliments' should not match almonds rule."""
        result = classify_taxonomy("Compliments Organic Peanut Butter")
        # Should not match SNACKS via almonds
        if result.category == ReferenceCategory.SNACKS:
            assert "almond" not in result.matched_rule or False

    def test_tea_not_in_team(self):
        """'tea' in 'team' should not match beverages rule."""
        result = classify_taxonomy("Team Sports Water Bottle")
        # Should not match BEVERAGES via tea
        if result.status == "PASS" and result.category == ReferenceCategory.BEVERAGES:
            assert False, "tea matched inside 'team'"

    def test_bean_not_in_beauty(self):
        """'bean' substring should not match unrelated products."""
        result = classify_taxonomy("Beauty Bar Soap")
        # Should not match food categories
        if result.status == "PASS" and result.category in (
            ReferenceCategory.PRODUCE,
            ReferenceCategory.SNACKS,
        ):
            assert False, "bean matched inside 'beauty'"

    def test_ice_cream_cups_ambiguous(self):
        """Ice cream cups should be disambiguated to ambiguous."""
        result = classify_taxonomy("Ice Cream Cups 18 Pack 75 g")
        assert result.status == "AMBIGUOUS"
        assert result.resolution == "disambiguation_rule"


class TestSpecificPhraseBehavior:
    """Specific multi-word phrases should override generic keyword matches."""

    def test_chocolate_milk_is_dairy(self):
        result = classify_taxonomy("Chocolate Milk 500 ml")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.DAIRY

    def test_orange_juice_is_beverages(self):
        result = classify_taxonomy("Orange Juice 1 L")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.BEVERAGES

    def test_peanut_butter_is_general_grocery(self):
        result = classify_taxonomy("Peanut Butter Creamy 500 g")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.GENERAL_GROCERY

    def test_ice_cream_is_frozen(self):
        result = classify_taxonomy("Chocolate Ice Cream 1.5L")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.FROZEN
class TestRegressionResolutions:
    """Previously-PASS products must remain classified after taxonomy improvements."""

    def test_canola_olive_oil(self):
        result = classify_taxonomy("Oil Canola & Extra Virgin Olive 1.89 L")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.OILS_VINEGARS

    def test_hot_dog_buns(self):
        result = classify_taxonomy("Hot Dog Top Sliced Buns 12 x 45 g")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.BAKERY

    def test_potato_deviled_egg_salad(self):
        result = classify_taxonomy("Salad Potato & Deviled Egg 908 g")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.PRODUCE

    def test_dried_fruit_mango(self):
        result = classify_taxonomy("Dried Fruit Mango 325 g")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.CONFECTIONERY

    def test_king_oyster_mushrooms(self):
        result = classify_taxonomy("Mushrooms King Oyster 170 g")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.PRODUCE

    def test_potato_egg_salad_large(self):
        result = classify_taxonomy("Salad Potato & Egg 1.81 kg")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.PRODUCE

    def test_potato_egg_salad_small(self):
        result = classify_taxonomy("Salad Potato & Egg 454 g")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.PRODUCE

    def test_chicken_stir_fry(self):
        result = classify_taxonomy("Boneless Skinless Chicken Breast Stir Fry")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.MEAT_SEAFOOD

    def test_tomato_soup(self):
        result = classify_taxonomy("Soup Tomato And Herb 540 ml")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.GENERAL_GROCERY

    def test_oyster_mushrooms(self):
        result = classify_taxonomy("Mushrooms Oyster 85 g")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.PRODUCE

    def test_sweet_potato_soup(self):
        result = classify_taxonomy(
            "Organic Soup Sweet Potato Coconut Curry 398 ml"
        )
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.GENERAL_GROCERY

    def test_potato_deviled_egg_salad_small(self):
        result = classify_taxonomy("Salad Potato & Deviled Egg 454 g")
        assert result.status == "PASS"
        assert result.category == ReferenceCategory.PRODUCE