@@ -8,7 +8,8 @@ def test_categoriser_applies_highest_priority_match():
88 ]
99 categoriser = Categoriser (rules )
1010 txn = {"merchant_normalised" : "Countdown Ponsonby" }
11- assert categoriser .categorise (txn ) == "Groceries"
11+ category , category_type = categoriser .categorise (txn )
12+ assert category == "Groceries"
1213
1314
1415def test_categoriser_skips_empty_patterns ():
@@ -17,7 +18,8 @@ def test_categoriser_skips_empty_patterns():
1718 {"pattern" : "ferry" , "category" : "Transport" },
1819 ]
1920 categoriser = Categoriser (rules )
20- assert categoriser .categorise ({"merchant_normalised" : "Ferry ride" }) == "Transport"
21+ category , category_type = categoriser .categorise ({"merchant_normalised" : "Ferry ride" })
22+ assert category == "Transport"
2123
2224
2325def test_categoriser_honours_amount_conditions ():
@@ -36,14 +38,10 @@ def test_categoriser_honours_amount_conditions():
3638 },
3739 ]
3840 categoriser = Categoriser (rules )
39- assert (
40- categoriser .categorise ({"merchant_normalised" : "New World" , "amount" : "12.00" })
41- == "Groceries"
42- )
43- assert (
44- categoriser .categorise ({"merchant_normalised" : "New World" , "amount" : "5.00" })
45- == "Snacks"
46- )
41+ category1 , _ = categoriser .categorise ({"merchant_normalised" : "New World" , "amount" : "12.00" })
42+ assert category1 == "Groceries"
43+ category2 , _ = categoriser .categorise ({"merchant_normalised" : "New World" , "amount" : "5.00" })
44+ assert category2 == "Snacks"
4745
4846
4947def test_categoriser_supports_exact_amounts_and_or_conditions ():
@@ -68,18 +66,12 @@ def test_categoriser_supports_exact_amounts_and_or_conditions():
6866 },
6967 ]
7068 categoriser = Categoriser (rules )
71- assert (
72- categoriser .categorise ({"merchant_normalised" : "Coffee" , "amount" : "4.50" })
73- == "Work Coffee"
74- )
75- assert (
76- categoriser .categorise ({"merchant_normalised" : "Coffee" , "amount" : "0" })
77- == "Free Coffee"
78- )
79- assert (
80- categoriser .categorise ({"merchant_normalised" : "Coffee" , "amount" : "-4" })
81- == "Discount Coffee"
82- )
69+ category1 , _ = categoriser .categorise ({"merchant_normalised" : "Coffee" , "amount" : "4.50" })
70+ assert category1 == "Work Coffee"
71+ category2 , _ = categoriser .categorise ({"merchant_normalised" : "Coffee" , "amount" : "0" })
72+ assert category2 == "Free Coffee"
73+ category3 , _ = categoriser .categorise ({"merchant_normalised" : "Coffee" , "amount" : "-4" })
74+ assert category3 == "Discount Coffee"
8375
8476
8577def test_categoriser_ignores_amount_condition_when_unparseable ():
@@ -91,7 +83,8 @@ def test_categoriser_ignores_amount_condition_when_unparseable():
9183 }
9284 ]
9385 categoriser = Categoriser (rules )
94- assert categoriser .categorise ({"merchant_normalised" : "Countdown" , "amount" : "1" }) == "Groceries"
86+ category , _ = categoriser .categorise ({"merchant_normalised" : "Countdown" , "amount" : "1" })
87+ assert category == "Groceries"
9588
9689
9790def test_categoriser_falls_back_when_amount_missing ():
@@ -104,10 +97,42 @@ def test_categoriser_falls_back_when_amount_missing():
10497 {"pattern" : "fuel" , "category" : "Misc" },
10598 ]
10699 categoriser = Categoriser (rules )
107- assert categoriser .categorise ({"merchant_normalised" : "Fuel stop" }) == "Misc"
100+ category , _ = categoriser .categorise ({"merchant_normalised" : "Fuel stop" })
101+ assert category == "Misc"
108102
109103
110104def test_detect_transfer_checks_multiple_fields ():
111105 txn = {"description_raw" : "Internal Transfer" , "merchant_normalised" : "BNZ" }
112106 assert Categoriser .detect_transfer (txn ) is True
113107 assert Categoriser .detect_transfer ({"description_raw" : "Cafe" }) is False
108+
109+
110+ def test_categoriser_returns_category_type ():
111+ rules = [
112+ {
113+ "pattern" : "countdown" ,
114+ "category" : "Groceries" ,
115+ "category_type" : "E" ,
116+ "priority" : 10 ,
117+ },
118+ {
119+ "pattern" : "sharesies" ,
120+ "category" : "Investments" ,
121+ "category_type" : "Iv" ,
122+ "priority" : 5 ,
123+ },
124+ ]
125+ categoriser = Categoriser (rules )
126+
127+ category , category_type = categoriser .categorise ({"merchant_normalised" : "Countdown" })
128+ assert category == "Groceries"
129+ assert category_type == "E"
130+
131+ category , category_type = categoriser .categorise ({"merchant_normalised" : "Sharesies" })
132+ assert category == "Investments"
133+ assert category_type == "Iv"
134+
135+ # Test uncategorised transaction
136+ category , category_type = categoriser .categorise ({"merchant_normalised" : "Unknown Store" })
137+ assert category == "Uncategorised"
138+ assert category_type == ""
0 commit comments