[9390] | 1 | # Tests for fee management. |
---|
| 2 | import unittest |
---|
| 3 | from zope.interface.verify import verifyClass, verifyObject |
---|
[11949] | 4 | from waeup.ikoba.fees import IFeeTable |
---|
| 5 | from waeup.ikoba.fees import FeeTable, nested_list, nested_tuple |
---|
[9390] | 6 | |
---|
| 7 | SAMPLE_PARAMS = (('locals', 'non-locals'), ('art', 'science'), |
---|
| 8 | ('fac1', 'fac2')) |
---|
| 9 | |
---|
| 10 | SAMPLE_VALUES = (((10, 20), # locals, art, fac1/fac2 |
---|
| 11 | (11, 21)), # science, fac1/fac2 |
---|
| 12 | ((12, 22), # non-locals, art, fac1/fac2 |
---|
| 13 | (13, 23))) # science, fac1/fac2 |
---|
| 14 | |
---|
| 15 | class HelperTests(unittest.TestCase): |
---|
| 16 | |
---|
| 17 | def test_nested_list(self): |
---|
| 18 | # we can turn nested lists into nested tuples |
---|
| 19 | self.assertEqual( |
---|
| 20 | nested_list(()), []) |
---|
| 21 | self.assertEqual( |
---|
| 22 | nested_list((1,)), [1]) |
---|
| 23 | self.assertEqual( |
---|
| 24 | nested_list(((1,2), (3,))), [[1,2],[3]]) |
---|
| 25 | self.assertEqual( |
---|
| 26 | nested_list((((1,2),(3,4)),(5,6))), [[[1,2],[3,4]],[5,6]]) |
---|
| 27 | return |
---|
| 28 | |
---|
| 29 | def test_nested_tuples(self): |
---|
| 30 | # we can turn nested tuples into nested lists |
---|
| 31 | self.assertEqual( |
---|
| 32 | nested_tuple([]), ()) |
---|
| 33 | self.assertEqual( |
---|
| 34 | nested_tuple([1,]), (1,)) |
---|
| 35 | self.assertEqual( |
---|
| 36 | nested_tuple([[1,2], [3,]]), ((1,2),(3,))) |
---|
| 37 | self.assertEqual( |
---|
| 38 | nested_tuple([[[1,2],[3,4]],[5,6]]), (((1,2),(3,4)),(5,6))) |
---|
| 39 | return |
---|
| 40 | |
---|
| 41 | class FeeTests(unittest.TestCase): |
---|
| 42 | |
---|
| 43 | def test_iface(self): |
---|
| 44 | # make sure, we comply with interfaces |
---|
| 45 | obj = FeeTable() |
---|
| 46 | verifyClass(IFeeTable, FeeTable) |
---|
| 47 | verifyObject(IFeeTable, obj) |
---|
| 48 | return |
---|
| 49 | |
---|
| 50 | def test_constructor(self): |
---|
| 51 | # we can set values in constructor |
---|
| 52 | fees = FeeTable(params=SAMPLE_PARAMS, values=SAMPLE_VALUES) |
---|
| 53 | self.assertEqual(fees.values, SAMPLE_VALUES) |
---|
| 54 | self.assertEqual(fees.params, SAMPLE_PARAMS) |
---|
| 55 | return |
---|
| 56 | |
---|
| 57 | def test_import_values(self): |
---|
| 58 | # we can import a set of values (clearing old stuff) |
---|
| 59 | fees = FeeTable() |
---|
| 60 | fees.import_values(SAMPLE_PARAMS, SAMPLE_VALUES) |
---|
| 61 | self.assertEqual(fees.params, SAMPLE_PARAMS) |
---|
| 62 | self.assertEqual(fees.values, SAMPLE_VALUES) |
---|
| 63 | return |
---|
| 64 | |
---|
| 65 | def test_import_values_no_values(self): |
---|
| 66 | # w/o values the table is emptied |
---|
| 67 | fees = FeeTable() |
---|
| 68 | fees.import_values() |
---|
| 69 | self.assertEqual(fees.params, ()) |
---|
| 70 | self.assertEqual(fees.values, ()) |
---|
| 71 | return |
---|
| 72 | |
---|
| 73 | def test_as_dict(self): |
---|
| 74 | # we can get a fee table as dict |
---|
| 75 | fees = FeeTable(SAMPLE_PARAMS, SAMPLE_VALUES) |
---|
| 76 | result = fees.as_dict() |
---|
| 77 | self.assertEqual( |
---|
| 78 | result, |
---|
| 79 | { |
---|
| 80 | ('locals', 'art', 'fac1'): 10, |
---|
| 81 | ('locals', 'science', 'fac1'): 11, |
---|
| 82 | ('non-locals', 'art', 'fac1'): 12, |
---|
| 83 | ('non-locals', 'science', 'fac1'): 13, |
---|
| 84 | ('locals', 'art', 'fac2'): 20, |
---|
| 85 | ('locals', 'science', 'fac2'): 21, |
---|
| 86 | ('non-locals', 'art', 'fac2'): 22, |
---|
| 87 | ('non-locals', 'science', 'fac2'): 23, |
---|
| 88 | } |
---|
| 89 | ) |
---|
| 90 | |
---|
| 91 | def test_get_fee(self): |
---|
| 92 | # we can get values |
---|
| 93 | fees = FeeTable(SAMPLE_PARAMS, SAMPLE_VALUES) |
---|
| 94 | result1 = fees.get_fee(('locals', 'science', 'fac2'),) |
---|
| 95 | result2 = fees.get_fee(('non-locals', 'art', 'fac1'),) |
---|
| 96 | self.assertEqual(result1, 21) |
---|
| 97 | self.assertEqual(result2, 12) |
---|
| 98 | return |
---|
| 99 | |
---|
| 100 | def test_get_fee_invalid_key(self): |
---|
| 101 | # invalid param values will raise a key error |
---|
| 102 | fees = FeeTable(SAMPLE_PARAMS, SAMPLE_VALUES) |
---|
| 103 | self.assertRaises( |
---|
| 104 | KeyError, fees.get_fee, ('NONSENSE', 'science', 'fac2')) |
---|
| 105 | self.assertRaises( |
---|
| 106 | KeyError, fees.get_fee, ('locals', 'NONSENSE', 'fac2')) |
---|
| 107 | self.assertRaises( |
---|
| 108 | KeyError, fees.get_fee, ('locals', 'science', 'NONSENSE')) |
---|
| 109 | # also the order of params matters |
---|
| 110 | self.assertRaises( # 'science' and 'locals' is wrong order... |
---|
| 111 | KeyError, fees.get_fee, ('science', 'locals', 'fac2')) |
---|
| 112 | return |
---|
| 113 | |
---|
| 114 | def test_set_fee(self): |
---|
| 115 | # we can set values |
---|
| 116 | fees = FeeTable(SAMPLE_PARAMS, SAMPLE_VALUES) |
---|
| 117 | result1 = fees.get_fee(('locals', 'science', 'fac2'),) |
---|
| 118 | fees.set_fee(('locals', 'science', 'fac2'), 42) |
---|
| 119 | result2 = fees.get_fee(('locals', 'science', 'fac2'),) |
---|
| 120 | self.assertEqual(result1, 21) |
---|
| 121 | self.assertEqual(result2, 42) |
---|
| 122 | return |
---|
| 123 | |
---|
| 124 | def test_set_fee_invalid(self): |
---|
| 125 | # invalid param values will raise a key error |
---|
| 126 | fees = FeeTable(SAMPLE_PARAMS, SAMPLE_VALUES) |
---|
| 127 | self.assertRaises( |
---|
| 128 | KeyError, fees.set_fee, ('NONSENSE', 'science', 'fac2'), 12) |
---|
| 129 | self.assertRaises( |
---|
| 130 | KeyError, fees.set_fee, ('locals', 'NONSENSE', 'fac2'), 12) |
---|
| 131 | self.assertRaises( |
---|
| 132 | KeyError, fees.set_fee, ('locals', 'science', 'NONSENSE'), 12) |
---|
| 133 | # also the order of params matters |
---|
| 134 | self.assertRaises( # 'science' and 'locals' is wrong order... |
---|
| 135 | KeyError, fees.set_fee, ('science', 'locals', 'fac2'), 12) |
---|
| 136 | return |
---|