OCA reporting engine fork for dev and update.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

59 lines
1.6 KiB

# Copyright 2020 Creu Blanca
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo.tests.common import TransactionCase
from odoo.exceptions import ValidationError
class TestFormula(TransactionCase):
def setUp(self):
super().setUp()
self.kpi = self.env["kpi.kpi"].create(
{
"name": "DEMO KPI",
"widget": "number",
"computation_method": "code",
}
)
def test_forbidden_words_01(self):
self.kpi.code = """
result = {"value": 0}
self.env.cr.commit()
"""
with self.assertRaises(ValidationError):
self.kpi.compute()
def test_forbidden_words_02(self):
self.kpi.code = """
result = {"value": 0}
self.env.cr.rollback()
"""
with self.assertRaises(ValidationError):
self.kpi.compute()
def test_forbidden_words_03(self):
self.kpi.code = """
result = {"value": 0}
self.env.cr.execute("CoMMiT")
"""
with self.assertRaises(ValidationError):
self.kpi.compute()
def test_computation(self):
self.assertFalse(self.kpi.value)
self.kpi.compute()
self.assertEqual(self.kpi.value, {})
self.kpi.code = """
result = {}
result['value'] = len(model.search([('id', '=', %s)]))
result['previous'] = len(model.search([('id', '!=', %s)]))
""" % (
self.kpi.id,
self.kpi.id,
)
self.kpi.compute()
value = self.kpi.value
self.assertTrue(value.get("value"))
self.assertEqual(value.get("value"), 1)
self.assertEqual(value.get("previous"), self.kpi.search_count([]) - 1)