-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
test_calculator.py
324 lines (282 loc) · 9.43 KB
/
test_calculator.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
import pytest
import pandas as pd
import numpy as np
from ccc.parameters import Specification, DepreciationParams
from ccc.data import Assets
from ccc.calculator import Calculator
import os
CURRENT_PATH = os.path.abspath(os.path.dirname(__file__))
setattr(DepreciationParams, "defaults", os.path.join(
CURRENT_PATH, "..", "tax_depreciation_rules.json"))
def test_Calculator_exception1():
'''
Raise exception for not passing parameters object
'''
assets = Assets()
dp = DepreciationParams()
with pytest.raises(Exception):
assert Calculator(dp=dp, assets=assets)
def test_Calculator_exception2():
'''
Raise exception for not passing depreciation parameters object
'''
p = Specification()
assets = Assets()
with pytest.raises(Exception):
assert Calculator(p=p, assets=assets)
def test_Calculator_exception3():
'''
Raise exception for not passing assets object
'''
p = Specification()
dp = DepreciationParams()
with pytest.raises(Exception):
assert Calculator(p=p, dp=dp)
def test_calc_other():
'''
Test calc_other method
'''
assets = Assets()
p = Specification()
dp = DepreciationParams()
calc = Calculator(p, dp, assets)
df = calc.calc_by_asset()
calc_other_df = calc.calc_other(df)
assert ('ucc_mix' in calc_other_df.keys())
assert ('metr_mix' in calc_other_df.keys())
assert ('mettr_mix' in calc_other_df.keys())
assert ('tax_wedge_mix' in calc_other_df.keys())
assert ('eatr_mix' in calc_other_df.keys())
def test_calc_base():
'''
Test calc_base method
'''
assets = Assets()
p = Specification()
dp = DepreciationParams()
calc = Calculator(p, dp, assets)
calc.calc_base()
calc_base_df = calc._Calculator__assets.df
assert ('z_mix' in calc_base_df.keys())
assert ('rho_mix' in calc_base_df.keys())
def test_calc_all():
'''
Test calc_all method
'''
assets = Assets()
p = Specification()
dp = DepreciationParams()
calc = Calculator(p, dp, assets)
calc.calc_all()
calc_all_df = calc._Calculator__assets.df
assert ('z_mix' in calc_all_df.keys())
assert ('rho_mix' in calc_all_df.keys())
assert ('ucc_mix' in calc_all_df.keys())
assert ('metr_mix' in calc_all_df.keys())
assert ('mettr_mix' in calc_all_df.keys())
assert ('tax_wedge_mix' in calc_all_df.keys())
assert ('eatr_mix' in calc_all_df.keys())
def test_calc_by_asset():
'''
Test calc_by_asset method
'''
assets = Assets()
p = Specification()
dp = DepreciationParams()
calc = Calculator(p, dp, assets)
asset_df = calc.calc_by_asset()
assert ('major_asset_group' in asset_df.keys())
@pytest.mark.parametrize('include_land,include_inventories',
[(False, False), (True, True)],
ids=['No land or inv', 'Both land and inv'])
def test_calc_by_industry(include_land, include_inventories):
'''
Test calc_by_industry method
'''
assets = Assets()
p = Specification()
dp = DepreciationParams()
calc = Calculator(p, dp, assets)
ind_df = calc.calc_by_industry(
include_land=include_land,
include_inventories=include_inventories)
assert ('major_industry' in ind_df.keys())
@pytest.mark.parametrize('include_land,include_inventories',
[(False, False), (True, True)],
ids=['No land or inv', 'Both land and inv'])
def test_summary_table(include_land, include_inventories):
'''
Test summary_table method.
'''
cyr = 2018
assets = Assets()
p = Specification(year=cyr)
dp = DepreciationParams()
calc1 = Calculator(p, dp, assets)
assert calc1.current_year == cyr
p.update_specification({'CIT_rate': 0.38})
calc2 = Calculator(p, dp, assets)
assert calc2.current_year == cyr
summary_df = calc1.summary_table(
calc2, include_land=include_land,
include_inventories=include_inventories)
assert isinstance(summary_df, pd.DataFrame)
@pytest.mark.parametrize('include_land,include_inventories',
[(False, False), (True, True)],
ids=['No land or inv', 'Both land and inv'])
def test_asset_share_table(include_land, include_inventories):
'''
Test asset_share_table method.
'''
cyr = 2018
assets = Assets()
p = Specification(year=cyr)
dp = DepreciationParams()
calc1 = Calculator(p, dp, assets)
assert calc1.current_year == cyr
asset_df = calc1.asset_share_table(
include_land=include_land,
include_inventories=include_inventories)
assert isinstance(asset_df, pd.DataFrame)
@pytest.mark.parametrize('include_land,include_inventories',
[(False, False), (True, True)],
ids=['No land or inv', 'Both land and inv'])
def test_asset_summary_table(include_land, include_inventories):
'''
Test asset_summary_table method.
'''
cyr = 2018
assets = Assets()
p = Specification(year=cyr)
dp = DepreciationParams()
calc1 = Calculator(p, dp, assets)
assert calc1.current_year == cyr
p.update_specification({'CIT_rate': 0.38})
calc2 = Calculator(p, dp, assets)
assert calc2.current_year == cyr
asset_df = calc1.asset_summary_table(
calc2, include_land=include_land,
include_inventories=include_inventories)
assert isinstance(asset_df, pd.DataFrame)
@pytest.mark.parametrize('include_land,include_inventories',
[(False, False), (True, True)],
ids=['No land or inv', 'Both land and inv'])
def test_industry_summary_table(include_land, include_inventories):
'''
Test industry_summary_table method.
'''
cyr = 2018
assets = Assets()
p = Specification(year=cyr)
dp = DepreciationParams()
calc1 = Calculator(p, dp, assets)
assert calc1.current_year == cyr
p.update_specification({'CIT_rate': 0.38})
calc2 = Calculator(p, dp, assets)
assert calc2.current_year == cyr
ind_df = calc1.industry_summary_table(
calc2, include_land=include_land,
include_inventories=include_inventories)
assert isinstance(ind_df, pd.DataFrame)
@pytest.mark.parametrize('corporate', [True, False],
ids=['Corporate', 'Non-Corporate'])
def test_range_plot(corporate):
'''
Test range_plot method.
'''
assets = Assets()
p = Specification()
dp = DepreciationParams()
calc = Calculator(p, dp, assets)
p2 = Specification(year=2026)
p2.update_specification({'CIT_rate': 0.25})
calc2 = Calculator(p2, dp, assets)
fig = calc.range_plot(calc2, corporate=corporate,
include_title=True)
assert fig
fig = calc.range_plot(calc2, output_variable='rho',
corporate=corporate, include_title=True)
assert fig
@pytest.mark.parametrize('corporate', [True, False],
ids=['Corporate', 'Non-Corporate'])
def test_grouped_bar(corporate):
'''
Test grouped_bar method.
'''
assets = Assets()
p = Specification()
dp = DepreciationParams()
calc = Calculator(p, dp, assets)
p2 = Specification(year=2026)
p2.update_specification({'CIT_rate': 0.25})
calc2 = Calculator(p2, dp, assets)
fig = calc.grouped_bar(calc2, corporate=corporate)
assert fig
fig = calc.grouped_bar(calc2, output_variable='rho',
corporate=corporate, group_by_asset=False)
assert fig
def test_asset_bubble():
'''
Test asset bubble plot method.
'''
assets = Assets()
p = Specification()
dp = DepreciationParams()
calc = Calculator(p, dp, assets)
p2 = Specification(year=2026)
p2.update_specification({'CIT_rate': 0.25})
calc2 = Calculator(p2, dp, assets)
fig = calc.asset_bubble(calc2, include_title=True)
assert fig
fig = calc.asset_bubble(calc2, output_variable='rho_mix',
include_title=True)
assert fig
def test_bubble_widget():
'''
Test asset bubble plot method.
'''
assets = Assets()
p = Specification()
dp = DepreciationParams()
calc = Calculator(p, dp, assets)
p2 = Specification(year=2026)
p2.update_specification({'CIT_rate': 0.25})
calc2 = Calculator(p2, dp, assets)
fig = calc.bubble_widget(calc2)
assert fig
def test_store_assets():
assets = Assets()
p = Specification()
dp = DepreciationParams()
calc1 = Calculator(p, dp, assets)
calc1.store_assets()
assert isinstance(calc1, Calculator)
def test_restore_assets():
assets = Assets()
p = Specification()
dp = DepreciationParams()
calc1 = Calculator(p, dp, assets)
calc1.store_assets()
calc1.restore_assets()
assert isinstance(calc1, Calculator)
def test_p_param_return_value():
assets = Assets()
p = Specification()
dp = DepreciationParams()
calc1 = Calculator(p, dp, assets)
obj = calc1.p_param('tau_int')
assert np.allclose(obj, np.array([0.31774924]))
def test_p_param_set_value():
assets = Assets()
p = Specification()
dp = DepreciationParams()
calc1 = Calculator(p, dp, assets)
new_tau_int = np.array([0.396])
calc1.p_param('tau_int', new_tau_int)
assert np.allclose(calc1._Calculator__p.tau_int, new_tau_int)
def test_data_year():
assets = Assets()
p = Specification()
dp = DepreciationParams()
calc1 = Calculator(p, dp, assets)
assert calc1.data_year == 2013