forked from OCA/mis-builder
/
test_mis_report_instance.py
594 lines (570 loc) · 21.6 KB
/
test_mis_report_instance.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# Copyright 2016 ACSONE SA/NV (<http://acsone.eu>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
import odoo.tests.common as common
from odoo.tools import test_reports
from ..models.accounting_none import AccountingNone
from ..models.mis_report import TYPE_STR, SubKPITupleLengthError, SubKPIUnknownTypeError
class TestMisReportInstance(common.HttpCase):
"""Basic integration test to exercise mis.report.instance.
We don't check the actual results here too much as computation correctness
should be covered by lower level unit tests.
"""
def setUp(self):
super().setUp()
partner_model_id = self.env.ref("base.model_res_partner").id
partner_create_date_field_id = self.env.ref(
"base.field_res_partner__create_date"
).id
partner_debit_field_id = self.env.ref("account.field_res_partner__debit").id
# create a report with 2 subkpis and one query
self.report = self.env["mis.report"].create(
dict(
name="test report",
subkpi_ids=[
(0, 0, dict(name="sk1", description="subkpi 1", sequence=1)),
(0, 0, dict(name="sk2", description="subkpi 2", sequence=2)),
],
query_ids=[
(
0,
0,
dict(
name="partner",
model_id=partner_model_id,
field_ids=[(4, partner_debit_field_id, None)],
date_field=partner_create_date_field_id,
aggregate="sum",
),
)
],
)
)
# create another report with 2 subkpis, no query
self.report_2 = self.env["mis.report"].create(
dict(
name="another test report",
subkpi_ids=[
(
0,
0,
dict(
name="subkpi1_report2",
description="subkpi 1, report 2",
sequence=1,
),
),
(
0,
0,
dict(
name="subkpi2_report2",
description="subkpi 2, report 2",
sequence=2,
),
),
],
)
)
# Third report, 2 subkpis, no query
self.report_3 = self.env["mis.report"].create(
dict(
name="test report 3",
subkpi_ids=[
(
0,
0,
dict(
name="subkpi1_report3",
description="subkpi 1, report 3",
sequence=1,
),
),
(
0,
0,
dict(
name="subkpi2_report3",
description="subkpi 2, report 3",
sequence=2,
),
),
],
)
)
# kpi with accounting formulas
self.kpi1 = self.env["mis.report.kpi"].create(
dict(
report_id=self.report.id,
description="kpi 1",
name="k1",
multi=True,
expression_ids=[
(
0,
0,
dict(name="bale[200%]", subkpi_id=self.report.subkpi_ids[0].id),
),
(
0,
0,
dict(name="balp[200%]", subkpi_id=self.report.subkpi_ids[1].id),
),
],
)
)
# kpi with accounting formula and query
self.kpi2 = self.env["mis.report.kpi"].create(
dict(
report_id=self.report.id,
description="kpi 2",
name="k2",
multi=True,
expression_ids=[
(
0,
0,
dict(name="balp[200%]", subkpi_id=self.report.subkpi_ids[0].id),
),
(
0,
0,
dict(
name="partner.debit", subkpi_id=self.report.subkpi_ids[1].id
),
),
],
)
)
# kpi with a simple expression summing other multi-valued kpis
self.env["mis.report.kpi"].create(
dict(
report_id=self.report.id,
description="kpi 4",
name="k4",
multi=False,
expression="k1 + k2 + k3",
)
)
# kpi with 2 constants
self.env["mis.report.kpi"].create(
dict(
report_id=self.report.id,
description="kpi 3",
name="k3",
multi=True,
expression_ids=[
(
0,
0,
dict(
name="AccountingNone",
subkpi_id=self.report.subkpi_ids[0].id,
),
),
(0, 0, dict(name="1.0", subkpi_id=self.report.subkpi_ids[1].id)),
],
)
)
# kpi with a NameError (x not defined)
self.env["mis.report.kpi"].create(
dict(
report_id=self.report.id,
description="kpi 5",
name="k5",
multi=True,
expression_ids=[
(0, 0, dict(name="x", subkpi_id=self.report.subkpi_ids[0].id)),
(0, 0, dict(name="1.0", subkpi_id=self.report.subkpi_ids[1].id)),
],
)
)
# string-type kpi
self.env["mis.report.kpi"].create(
dict(
report_id=self.report.id,
description="kpi 6",
name="k6",
multi=True,
type=TYPE_STR,
expression_ids=[
(0, 0, dict(name='"bla"', subkpi_id=self.report.subkpi_ids[0].id)),
(
0,
0,
dict(name='"blabla"', subkpi_id=self.report.subkpi_ids[1].id),
),
],
)
)
# kpi that references another subkpi by name
self.env["mis.report.kpi"].create(
dict(
report_id=self.report.id,
description="kpi 7",
name="k7",
multi=True,
expression_ids=[
(0, 0, dict(name="k3.sk1", subkpi_id=self.report.subkpi_ids[0].id)),
(0, 0, dict(name="k3.sk2", subkpi_id=self.report.subkpi_ids[1].id)),
],
)
)
# Report 2 : kpi with AccountingNone value
self.env["mis.report.kpi"].create(
dict(
report_id=self.report_2.id,
description="AccountingNone kpi",
name="AccountingNoneKPI",
multi=False,
)
)
# Report 2 : 'classic' kpi with values for each sub-KPI
self.env["mis.report.kpi"].create(
dict(
report_id=self.report_2.id,
description="Classic kpi",
name="classic_kpi_r2",
multi=True,
expression_ids=[
(
0,
0,
dict(
name="bale[200%]", subkpi_id=self.report_2.subkpi_ids[0].id
),
),
(
0,
0,
dict(
name="balp[200%]", subkpi_id=self.report_2.subkpi_ids[1].id
),
),
],
)
)
# Report 3 : kpi with wrong tuple length
self.env["mis.report.kpi"].create(
dict(
report_id=self.report_3.id,
description="Wrong tuple length kpi",
name="wrongTupleLen",
multi=False,
expression="('hello', 'does', 'this', 'work')",
)
)
# Report 3 : 'classic' kpi
self.env["mis.report.kpi"].create(
dict(
report_id=self.report_3.id,
description="Classic kpi",
name="classic_kpi_r2",
multi=True,
expression_ids=[
(
0,
0,
dict(
name="bale[200%]", subkpi_id=self.report_3.subkpi_ids[0].id
),
),
(
0,
0,
dict(
name="balp[200%]", subkpi_id=self.report_3.subkpi_ids[1].id
),
),
],
)
)
# create a report instance
self.report_instance = self.env["mis.report.instance"].create(
dict(
name="test instance",
report_id=self.report.id,
company_id=self.env.ref("base.main_company").id,
period_ids=[
(
0,
0,
dict(
name="p1",
mode="relative",
type="d",
subkpi_ids=[(4, self.report.subkpi_ids[0].id, None)],
),
),
(
0,
0,
dict(
name="p2",
mode="fix",
manual_date_from="2014-01-01",
manual_date_to="2014-12-31",
),
),
],
)
)
# same for report 2
self.report_instance_2 = self.env["mis.report.instance"].create(
dict(
name="test instance 2",
report_id=self.report_2.id,
company_id=self.env.ref("base.main_company").id,
period_ids=[
(
0,
0,
dict(
name="p3",
mode="fix",
manual_date_from="2019-01-01",
manual_date_to="2019-12-31",
),
)
],
)
)
# and for report 3
self.report_instance_3 = self.env["mis.report.instance"].create(
dict(
name="test instance 3",
report_id=self.report_3.id,
company_id=self.env.ref("base.main_company").id,
period_ids=[
(
0,
0,
dict(
name="p4",
mode="fix",
manual_date_from="2019-01-01",
manual_date_to="2019-12-31",
),
)
],
)
)
def test_compute(self):
matrix = self.report_instance._compute_matrix()
for row in matrix.iter_rows():
vals = [c.val for c in row.iter_cells()]
if row.kpi.name == "k3":
# k3 is constant
self.assertEqual(vals, [AccountingNone, AccountingNone, 1.0])
elif row.kpi.name == "k6":
# k6 is a string kpi
self.assertEqual(vals, ["bla", "bla", "blabla"])
elif row.kpi.name == "k7":
# k7 references k3 via subkpi names
self.assertEqual(vals, [AccountingNone, AccountingNone, 1.0])
def test_multi_company_compute(self):
self.report_instance.write(
{
"multi_company": True,
"company_ids": [(6, 0, self.report_instance.company_id.ids)],
}
)
self.report_instance.report_id.kpi_ids.write({"auto_expand_accounts": True})
matrix = self.report_instance._compute_matrix()
for row in matrix.iter_rows():
if row.account_id:
account = self.env["account.account"].browse(row.account_id)
self.assertEqual(
row.label,
"%s %s [%s]"
% (account.code, account.name, account.company_id.name),
)
self.report_instance.write({"multi_company": False})
matrix = self.report_instance._compute_matrix()
for row in matrix.iter_rows():
if row.account_id:
account = self.env["account.account"].browse(row.account_id)
self.assertEqual(row.label, "{} {}".format(account.code, account.name))
def test_evaluate(self):
company = self.env.ref("base.main_company")
aep = self.report._prepare_aep(company)
r = self.report.evaluate(aep, date_from="2014-01-01", date_to="2014-12-31")
self.assertEqual(r["k3"], (AccountingNone, 1.0))
self.assertEqual(r["k6"], ("bla", "blabla"))
self.assertEqual(r["k7"], (AccountingNone, 1.0))
def test_json(self):
self.report_instance.compute()
def test_drilldown(self):
action = self.report_instance.drilldown(
dict(expr="balp[200%]", period_id=self.report_instance.period_ids[0].id)
)
account_ids = (
self.env["account.account"]
.search(
[
("code", "=like", "200%"),
("company_id", "=", self.env.ref("base.main_company").id),
]
)
.ids
)
self.assertTrue(("account_id", "in", tuple(account_ids)) in action["domain"])
self.assertEqual(action["res_model"], "account.move.line")
def test_drilldown_action_name_with_account(self):
period = self.report_instance.period_ids[0]
account = self.env["account.account"].search([], limit=1)
args = {
"period_id": period.id,
"kpi_id": self.kpi1.id,
"account_id": account.id,
}
action_name = self.report_instance._get_drilldown_action_name(args)
expected_name = "{kpi} - {account} - {period}".format(
kpi=self.kpi1.description,
account=account.display_name,
period=period.display_name,
)
assert action_name == expected_name
def test_drilldown_action_name_without_account(self):
period = self.report_instance.period_ids[0]
args = {
"period_id": period.id,
"kpi_id": self.kpi1.id,
}
action_name = self.report_instance._get_drilldown_action_name(args)
expected_name = "{kpi} - {period}".format(
kpi=self.kpi1.description,
period=period.display_name,
)
assert action_name == expected_name
def test_qweb(self):
self.report_instance.print_pdf() # get action
test_reports.try_report(
self.env.cr,
self.env.uid,
"mis_builder.report_mis_report_instance",
[self.report_instance.id],
report_type="qweb-pdf",
)
def test_xlsx(self):
self.report_instance.export_xls() # get action
test_reports.try_report(
self.env.cr,
self.env.uid,
"mis_builder.mis_report_instance_xlsx",
[self.report_instance.id],
report_type="xlsx",
)
def test_get_kpis_by_account_id(self):
account_ids = (
self.env["account.account"]
.search(
[
("code", "=like", "200%"),
("company_id", "=", self.env.ref("base.main_company").id),
]
)
.ids
)
kpi200 = {self.kpi1, self.kpi2}
res = self.report.get_kpis_by_account_id(self.env.ref("base.main_company"))
for account_id in account_ids:
self.assertTrue(account_id in res)
self.assertEqual(res[account_id], kpi200)
def test_kpi_name_get_name_search(self):
r = self.env["mis.report.kpi"].name_search("k1")
self.assertEqual(len(r), 1)
self.assertEqual(r[0][0], self.kpi1.id)
self.assertEqual(r[0][1], "kpi 1 (k1)")
r = self.env["mis.report.kpi"].name_search("kpi 1")
self.assertEqual(len(r), 1)
self.assertEqual(r[0][0], self.kpi1.id)
self.assertEqual(r[0][1], "kpi 1 (k1)")
def test_kpi_expr_name_get_name_search(self):
r = self.env["mis.report.kpi.expression"].name_search("k1")
self.assertEqual(
[i[1] for i in r],
["kpi 1 / subkpi 1 (k1.sk1)", "kpi 1 / subkpi 2 (k1.sk2)"],
)
r = self.env["mis.report.kpi.expression"].name_search("k1.sk1")
self.assertEqual([i[1] for i in r], ["kpi 1 / subkpi 1 (k1.sk1)"])
r = self.env["mis.report.kpi.expression"].name_search("k4")
self.assertEqual([i[1] for i in r], ["kpi 4 (k4)"])
def test_query_company_ids(self):
# sanity check single company mode
assert not self.report_instance.multi_company
assert self.report_instance.company_id
assert self.report_instance.query_company_ids == self.report_instance.company_id
# create a second company
c1 = self.report_instance.company_id
c2 = self.env["res.company"].create(
dict(
name="company 2",
)
)
self.report_instance.write(dict(multi_company=True, company_id=False))
self.report_instance.company_ids |= c1
self.report_instance.company_ids |= c2
assert len(self.report_instance.company_ids) == 2
self.assertFalse(self.report_instance.query_company_ids - self.env.companies)
# In a user context where there is only one company, ensure
# query_company_ids only has one company too.
assert (
self.report_instance.with_context(
allowed_company_ids=(c1.id,)
).query_company_ids
== c1
)
def test_multi_company_onchange(self):
# not multi company
self.assertTrue(self.report_instance.company_id)
self.assertFalse(self.report_instance.multi_company)
self.assertFalse(self.report_instance.company_ids)
self.assertEqual(
self.report_instance.query_company_ids[0], self.report_instance.company_id
)
# create a child company
self.env["res.company"].create(
dict(name="company 2", parent_id=self.report_instance.company_id.id)
)
self.report_instance.multi_company = True
# multi company, company_ids not set
self.assertEqual(self.report_instance.query_company_ids, self.env.companies)
# set company_ids
previous_company = self.report_instance.company_id
self.report_instance._onchange_company()
self.assertFalse(self.report_instance.company_id)
self.assertTrue(self.report_instance.multi_company)
self.assertEqual(self.report_instance.company_ids, previous_company)
self.assertEqual(self.report_instance.query_company_ids, previous_company)
# reset single company mode
self.report_instance.multi_company = False
self.report_instance._onchange_company()
self.assertEqual(
self.report_instance.query_company_ids[0], self.report_instance.company_id
)
self.assertFalse(self.report_instance.company_ids)
def test_mis_report_analytic_filters(self):
# Check that matrix has no values when using a filter with a non
# existing account
matrix = self.report_instance.with_context(
mis_report_filters={"analytic_account_id": {"value": 999}}
)._compute_matrix()
for row in matrix.iter_rows():
vals = [c.val for c in row.iter_cells()]
if row.kpi.name == "k1":
self.assertEqual(vals, [AccountingNone, AccountingNone, AccountingNone])
elif row.kpi.name == "k2":
self.assertEqual(vals, [AccountingNone, AccountingNone, None])
elif row.kpi.name == "k4":
self.assertEqual(vals, [AccountingNone, AccountingNone, 1.0])
def test_raise_when_unknown_kpi_value_type(self):
with self.assertRaises(SubKPIUnknownTypeError):
self.report_instance_2.compute()
def test_raise_when_wrong_tuple_length_with_subkpis(self):
with self.assertRaises(SubKPITupleLengthError):
self.report_instance_3.compute()
def test_unprivileged(self):
test_user = common.new_test_user(
self.env, "mis_you", groups="base.group_user,account.group_account_user"
)
self.report_instance.with_user(test_user).compute()