-
Notifications
You must be signed in to change notification settings - Fork 1
/
incomes_test.py
384 lines (343 loc) · 14.7 KB
/
incomes_test.py
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
import unittest
import unittest.mock
import incomes
import funds
import utils
import world
class IncomeTest(unittest.TestCase):
def testGiveMeMoney(self):
income = incomes.Income()
amount, taxable, year_rec = income.GiveMeMoney(utils.YearRecord())
self.assertEqual(amount, 0)
self.assertEqual(taxable, True)
self.assertIn(incomes.IncomeReceipt(0, incomes.INCOME_TYPE_NONE),
year_rec.incomes)
def testEarningsEmployed(self):
income = incomes.Earnings()
year_rec = utils.YearRecord()
year_rec.is_employed = True
with unittest.mock.patch('random.normalvariate') as my_random:
my_random.return_value = 10000
amount, taxable, year_rec = income.GiveMeMoney(year_rec)
self.assertEqual(amount, 10000)
self.assertEqual(taxable, True)
self.assertIn(incomes.IncomeReceipt(10000, incomes.INCOME_TYPE_EARNINGS),
year_rec.incomes)
def testEarningsEmployedNegativeRandom(self):
income = incomes.Earnings()
year_rec = utils.YearRecord()
year_rec.is_employed = True
with unittest.mock.patch('random.normalvariate') as my_random:
my_random.return_value = -10000
amount, taxable, year_rec = income.GiveMeMoney(year_rec)
self.assertEqual(amount, 0)
self.assertEqual(taxable, True)
self.assertIn(incomes.IncomeReceipt(0, incomes.INCOME_TYPE_EARNINGS),
year_rec.incomes)
def testEarningsUnemployed(self):
income = incomes.Earnings()
amount, taxable, year_rec = income.GiveMeMoney(utils.YearRecord())
self.assertEqual(amount, 0)
self.assertEqual(taxable, True)
self.assertIn(incomes.IncomeReceipt(0, incomes.INCOME_TYPE_EARNINGS),
year_rec.incomes)
def testEIBenefitsEmployedInsuredWorking(self):
income = incomes.EI()
last_year_rec = utils.YearRecord()
last_year_rec.is_employed = True
last_year_rec.insurable_earnings = 100
this_year_rec = utils.YearRecord()
this_year_rec.is_employed = True
this_year_rec.is_retired = False
income.AnnualUpdate(last_year_rec)
amount, taxable, year_rec = income.GiveMeMoney(this_year_rec)
self.assertEqual(amount, 0)
self.assertEqual(taxable, True)
self.assertIn(incomes.IncomeReceipt(0, incomes.INCOME_TYPE_EI),
year_rec.incomes)
def testEIBenefitsUnemployedInsuredWorking(self):
income = incomes.EI()
last_year_rec = utils.YearRecord()
last_year_rec.is_employed = True
last_year_rec.insurable_earnings = 100
this_year_rec = utils.YearRecord()
this_year_rec.is_employed = False
this_year_rec.is_retired = False
income.AnnualUpdate(last_year_rec)
amount, taxable, year_rec = income.GiveMeMoney(this_year_rec)
self.assertAlmostEqual(amount, 55)
self.assertEqual(taxable, True)
# done differently due to floating point equality checks
self.assertEqual(year_rec.incomes[0].income_type, incomes.INCOME_TYPE_EI)
self.assertAlmostEqual(year_rec.incomes[0].amount, 55)
self.assertEqual(len(year_rec.incomes), 1)
def testEIBenefitsUnemployedUninsuredWorking(self):
income = incomes.EI()
last_year_rec = utils.YearRecord()
last_year_rec.is_employed = False
last_year_rec.insurable_earnings = 0
this_year_rec = utils.YearRecord()
this_year_rec.is_employed = False
this_year_rec.is_retired = False
income.AnnualUpdate(last_year_rec)
amount, taxable, year_rec = income.GiveMeMoney(this_year_rec)
self.assertEqual(amount, 0)
self.assertEqual(taxable, True)
self.assertIn(incomes.IncomeReceipt(0, incomes.INCOME_TYPE_EI),
year_rec.incomes)
def testEIBenefitsUnemployedInsuredRetired(self):
income = incomes.EI()
last_year_rec = utils.YearRecord()
last_year_rec.is_employed = True
last_year_rec.insurable_earnings = 100
this_year_rec = utils.YearRecord()
this_year_rec.is_employed = False
this_year_rec.is_retired = True
income.AnnualUpdate(last_year_rec)
amount, taxable, year_rec = income.GiveMeMoney(this_year_rec)
self.assertEqual(amount, 0)
self.assertEqual(taxable, True)
self.assertIn(incomes.IncomeReceipt(0, incomes.INCOME_TYPE_EI),
year_rec.incomes)
def testEIBenefitsUnemployedUninsuredRetired(self):
income = incomes.EI()
last_year_rec = utils.YearRecord()
last_year_rec.is_employed = False
last_year_rec.insurable_earnings = 0
this_year_rec = utils.YearRecord()
this_year_rec.is_employed = False
this_year_rec.is_retired = True
income.AnnualUpdate(last_year_rec)
amount, taxable, year_rec = income.GiveMeMoney(this_year_rec)
self.assertEqual(amount, 0)
self.assertEqual(taxable, True)
self.assertIn(incomes.IncomeReceipt(0, incomes.INCOME_TYPE_EI),
year_rec.incomes)
def testCPPDuringWorkingPeriod(self):
income = incomes.CPP()
amount, taxable, year_rec = income.GiveMeMoney(utils.YearRecord())
self.assertEqual(amount, 0)
self.assertEqual(taxable, True)
self.assertIn(incomes.IncomeReceipt(0, incomes.INCOME_TYPE_CPP),
year_rec.incomes)
def testCPPAnnualUpdatePositiveEarnings(self):
income = incomes.CPP()
income.ympe_fractions = []
year_rec = utils.YearRecord()
year_rec.is_retired = False
year_rec.pensionable_earnings = 100
income.AnnualUpdate(year_rec)
self.assertEqual(income.ympe_fractions, [100/world.YMPE])
def testCPPAnnualUpdateZeroEarnings(self):
income = incomes.CPP()
income.ympe_fractions = []
year_rec = utils.YearRecord()
year_rec.is_retired = False
year_rec.pensionable_earnings = 0
income.AnnualUpdate(year_rec)
self.assertEqual(income.ympe_fractions, [0])
def testCPPRetired65IncludesZeroEarnings(self):
income = incomes.CPP()
income.ympe_fractions = [0.8]*37 + [0]*10
fake_person = unittest.mock.MagicMock()
fake_person.age = 65
fake_person.year = 2049
fake_person.cpi_history = [1, 1, 1, 1, 1, 1]
fake_person.cpi = 1
income.OnRetirement(fake_person)
self.assertAlmostEqual(income.benefit_amount, 13694.3691932)
def testCPPRetired65AllPositiveEarnings(self):
income = incomes.CPP()
income.ympe_fractions = [0.8]*42 + [0]*5
fake_person = unittest.mock.MagicMock()
fake_person.age = 65
fake_person.year = 2049
fake_person.cpi_history = [1, 1, 1, 1, 1, 1]
fake_person.cpi = 1
income.OnRetirement(fake_person)
self.assertAlmostEqual(income.benefit_amount, 14438.3065467)
def testCPPRetired68AllPositiveEarnings(self):
income = incomes.CPP()
income.ympe_fractions = [0.8]*45 + [0]*5
fake_person = unittest.mock.MagicMock()
fake_person.age = 68
fake_person.year = 2052
fake_person.cpi_history = [1, 1, 1, 1, 1, 1]
fake_person.cpi = 1
income.OnRetirement(fake_person)
self.assertAlmostEqual(income.benefit_amount, 18624.5036950)
def testCPPRetired72AllPositiveEarnings(self):
income = incomes.CPP()
income.ympe_fractions = [0.8]*49 + [0]*5
fake_person = unittest.mock.MagicMock()
fake_person.age = 72
fake_person.year = 2056
fake_person.cpi_history = [1, 1, 1, 1, 1, 1]
fake_person.cpi = 1
income.OnRetirement(fake_person)
self.assertAlmostEqual(income.benefit_amount, 21981.3428000)
def testCPPRetired63AllPositiveEarnings(self):
income = incomes.CPP()
income.ympe_fractions = [0.8]*40 + [0]*5
fake_person = unittest.mock.MagicMock()
fake_person.age = 63
fake_person.year = 2047
fake_person.cpi_history = [1, 1, 1, 1, 1, 1]
fake_person.cpi = 1
income.OnRetirement(fake_person)
self.assertAlmostEqual(income.benefit_amount, 12115.6655268)
def testCPPRetired65AllPositiveEarningsWithInflation(self):
income = incomes.CPP()
income.ympe_fractions = [0.8]*42 + [0]*5
fake_person = unittest.mock.MagicMock()
fake_person.age = 65
fake_person.year = 2049
# We use a silly value for the current year's CPI to check we aren't using it.
fake_person.cpi_history = [1, 1.02, 1.0404, 1.061208, 1.08243216, 100]
fake_person.cpi = 100
income.OnRetirement(fake_person)
self.assertAlmostEqual(income.benefit_amount, 150.334266907) # benefit_amount is stored as real $!
def testOASBeforeRetirement(self):
income = incomes.OAS()
year_rec = utils.YearRecord()
year_rec.age = 60
amount, taxable, year_rec = income.GiveMeMoney(year_rec)
self.assertEqual(amount, 0)
self.assertEqual(taxable, True)
self.assertIn(incomes.IncomeReceipt(0, incomes.INCOME_TYPE_OAS),
year_rec.incomes)
def testOASAtRetirement(self):
income = incomes.OAS()
year_rec = utils.YearRecord()
year_rec.age = 65
amount, taxable, year_rec = income.GiveMeMoney(year_rec)
self.assertEqual(amount, world.OAS_BENEFIT)
self.assertEqual(taxable, True)
self.assertIn(incomes.IncomeReceipt(world.OAS_BENEFIT, incomes.INCOME_TYPE_OAS),
year_rec.incomes)
def testOASAfterRetirement(self):
income = incomes.OAS()
year_rec = utils.YearRecord()
year_rec.age = 70
amount, taxable, year_rec = income.GiveMeMoney(year_rec)
self.assertEqual(amount, world.OAS_BENEFIT)
self.assertEqual(taxable, True)
self.assertIn(incomes.IncomeReceipt(world.OAS_BENEFIT, incomes.INCOME_TYPE_OAS),
year_rec.incomes)
def testOASUsesCPI(self):
income = incomes.OAS()
year_rec = utils.YearRecord()
year_rec.age = 66
year_rec.cpi = 1.02
amount, _, _ = income.GiveMeMoney(year_rec)
self.assertEqual(amount, world.OAS_BENEFIT * 1.02)
def testGISCalcIncomeBaseUsesLesserIncome(self):
income = incomes.GIS()
income.last_year_income_base = 5000
year_rec = utils.YearRecord()
year_rec.incomes = [incomes.IncomeReceipt(8000, incomes.INCOME_TYPE_EARNINGS)]
year_rec.ei_premium = 0
year_rec.cpp_contribution = 0
self.assertEqual(income._CalcIncomeBase(year_rec), 5000)
self.assertEqual(income.last_year_income_base, 8000)
def testGISCalcIncomeBaseIncomes(self):
income = incomes.GIS()
income.last_year_income_base = 10000
year_rec = utils.YearRecord()
year_rec.incomes = [incomes.IncomeReceipt(1000, incomes.INCOME_TYPE_EARNINGS),
incomes.IncomeReceipt(2000, incomes.INCOME_TYPE_CPP),
incomes.IncomeReceipt(3000, incomes.INCOME_TYPE_EI),
incomes.IncomeReceipt(4000, incomes.INCOME_TYPE_OAS)]
year_rec.ei_premium = 0
year_rec.cpp_contribution = 0
self.assertEqual(income._CalcIncomeBase(year_rec), 6000)
def testGISCalcIncomeBaseRRSPWithdrawals(self):
income = incomes.GIS()
income.last_year_income_base = 10000
year_rec = utils.YearRecord()
year_rec.withdrawals = [funds.WithdrawReceipt(2000, 0, funds.FUND_TYPE_RRSP),
funds.WithdrawReceipt(3000, 0, funds.FUND_TYPE_BRIDGING)]
year_rec.ei_premium = 0
year_rec.cpp_contribution = 0
self.assertEqual(income._CalcIncomeBase(year_rec), 5000)
def testGISCalcIncomeBaseCapitalGains(self):
income = incomes.GIS()
income.last_year_income_base = 10000
year_rec = utils.YearRecord()
year_rec.withdrawals = [funds.WithdrawReceipt(2000, 1000, funds.FUND_TYPE_NONREG)]
year_rec.tax_receipts = [funds.TaxReceipt(500, funds.FUND_TYPE_NONREG)]
year_rec.ei_premium = 0
year_rec.cpp_contribution = 0
self.assertEqual(income._CalcIncomeBase(year_rec), 750)
def testGISCalcIncomeBaseIncomesAndPayrollDeductions(self):
income = incomes.GIS()
income.last_year_income_base = 10000
year_rec = utils.YearRecord()
year_rec.incomes = [incomes.IncomeReceipt(5000, incomes.INCOME_TYPE_EARNINGS)]
year_rec.ei_premium = 2000
year_rec.cpp_contribution = 2000
self.assertEqual(income._CalcIncomeBase(year_rec), 1000)
def setUpYearRecForGIS(self, income_base=0, cpi=1, has_oas=True):
year_rec = utils.YearRecord()
year_rec.cpi = cpi
year_rec.incomes = []
if has_oas:
year_rec.incomes.append(incomes.IncomeReceipt(world.OAS_BENEFIT, incomes.INCOME_TYPE_OAS))
# We want to force some values for the current year's income base
year_rec.ei_premium = 0
year_rec.cpp_contribution = 0
year_rec.incomes.append(incomes.IncomeReceipt(income_base, incomes.INCOME_TYPE_EARNINGS))
return year_rec
def testGISBenefitPositiveIncomeNoOAS(self):
income = incomes.GIS()
income.last_year_income_base = 1000
year_rec = self.setUpYearRecForGIS(has_oas=False, income_base=1000)
amount, taxable, year_rec = income.GiveMeMoney(year_rec)
self.assertEqual(amount, 0)
self.assertFalse(taxable)
self.assertIn(incomes.IncomeReceipt(0, incomes.INCOME_TYPE_GIS),
year_rec.incomes)
def testGISBenefitIncomeBelowClawbackExemption(self):
"""Income base is below clawback exemption and supplement exemption"""
income = incomes.GIS()
income.last_year_income_base = 10
year_rec = self.setUpYearRecForGIS(has_oas=True, income_base=10)
amount, _, _ = income.GiveMeMoney(year_rec)
self.assertAlmostEqual(amount, 9925.64)
def testGISBenefitIncomeBelowSupplementExemption(self):
"""Income base is below supplement exemption but above clawback exemption"""
income = incomes.GIS()
income.last_year_income_base = 1000
year_rec = self.setUpYearRecForGIS(has_oas=True, income_base=1000)
amount, _, _ = income.GiveMeMoney(year_rec)
self.assertAlmostEqual(amount, 9431.64)
def testGISBenefitPositiveIncome(self):
"""Income base is above both clawback and supplement exemption"""
income = incomes.GIS()
income.last_year_income_base = 5000
year_rec = self.setUpYearRecForGIS(has_oas=True, income_base=5000)
amount, _, _ = income.GiveMeMoney(year_rec)
self.assertAlmostEqual(amount, 7289.46)
def testGISBenefitPositiveIncomeWithInflation(self):
"""Income base is above both clawback and supplement exemption (with inflation)"""
income = incomes.GIS()
income.last_year_income_base = 10000
year_rec = self.setUpYearRecForGIS(has_oas=True, income_base=10000, cpi=2)
amount, _, _ = income.GiveMeMoney(year_rec)
self.assertAlmostEqual(amount, 14572.92)
def testGISBenefitPositiveIncomeNoSupplement(self):
"""Getting regular GIS benefit but supplemental GIS benefit should be 0"""
income = incomes.GIS()
income.last_year_income_base = 10000
year_rec = self.setUpYearRecForGIS(has_oas=True, income_base=10000)
amount, _, _ = income.GiveMeMoney(year_rec)
self.assertAlmostEqual(amount, 4021.37)
def testGISBenefitReallyPositiveIncome(self):
"""Regular and supplemental GIS benefit should both be 0"""
income = incomes.GIS()
income.last_year_income_base = 20000
year_rec = self.setUpYearRecForGIS(has_oas=True, income_base=20000)
amount, _, _ = income.GiveMeMoney(year_rec)
self.assertEqual(amount, 0)
if __name__ == '__main__':
unittest.main()