Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added unit test for pmt #53

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions g2p_social_registry_proxy_means_test/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_proxy_means_test_line, test_proxy_means_test_params, test_res_partner
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from odoo.exceptions import ValidationError
from odoo.tests import TransactionCase


class TestSRProxyMeanTestLine(TransactionCase):
@classmethod
def setUpClass(self):
super().setUpClass()
self.group_kind = self.env["g2p.group.kind"].create({"name": "Test Kind"})

def setUp(self):
super().setUp()
self.env["sr.proxy.means.test.params"].search([]).unlink()

self.pmt_params = self.env["sr.proxy.means.test.params"].create(
{"pmt_name": "Test PMT", "target": "individual", "kind": self.group_kind.id, "target_name": True}
)

self.pmt_line = self.env["sr.proxy.means.test.line"].create(
{"pmt_id": self.pmt_params.id, "pmt_field": "income", "pmt_weightage": 1.0}
)

def tearDown(self):
self.pmt_line.unlink()
self.pmt_params.unlink()
super().tearDown()

def test_check_unique_field_weightage(self):
with self.assertRaises(ValidationError):
self.env["sr.proxy.means.test.line"].create(
{"pmt_id": self.pmt_params.id, "pmt_field": "income", "pmt_weightage": 2.0}
)

def test_get_fields_label(self):
fields = self.pmt_line.get_fields_label()

self.assertTrue(isinstance(fields, list))
self.assertTrue(len(fields) > 0, "Should return at least one field")

if fields:
self.assertTrue(all(isinstance(f, tuple) and len(f) == 2 for f in fields))

excluded_fields = {"pmt_score", "message_needaction_counter"}
field_names = [f[0] for f in fields]
for excluded in excluded_fields:
self.assertNotIn(excluded, field_names)

self.assertIn("income", field_names)

def test_field_type_filtering(self):
fields = self.pmt_line.get_fields_label()

ir_fields = self.env["ir.model.fields"].search(
[("model", "=", "res.partner"), ("name", "in", [f[0] for f in fields])]
)

for field in ir_fields:
self.assertIn(field.ttype, ["integer", "float"])

field_names = [f.name for f in ir_fields]
self.assertIn("income", field_names)
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from odoo.exceptions import ValidationError
from odoo.tests import TransactionCase


class TestSRProxyMeanTestParams(TransactionCase):
def setUp(self):
super().setUp()
self.group_kind = self.env["g2p.group.kind"].create({"name": "Test Kind"})

self.pmt_params = self.env["sr.proxy.means.test.params"].create(
{"pmt_name": "Test PMT", "target": "individual", "kind": self.group_kind.id}
)

def test_create_duplicate_pmt(self):
with self.assertRaises(ValidationError):
self.env["sr.proxy.means.test.params"].create(
{"pmt_name": "Duplicate PMT", "target": "individual", "kind": self.group_kind.id}
)

def test_write_duplicate_pmt(self):
other_pmt = self.env["sr.proxy.means.test.params"].create(
{"pmt_name": "Other PMT", "target": "group", "kind": self.group_kind.id}
)

with self.assertRaises(ValidationError):
other_pmt.write({"target": "individual"})

def test_onchange_target(self):
self.pmt_params.target = "group"
self.pmt_params._onchange_target()
self.assertFalse(self.pmt_params.target_name)

self.pmt_params.target = "individual"
self.pmt_params._onchange_target()
self.assertTrue(self.pmt_params.target_name)

def test_unlink(self):
partner = self.env["res.partner"].create({"name": "Test Partner", "kind": self.group_kind.id})

self.env["sr.proxy.means.test.line"].create(
{"pmt_id": self.pmt_params.id, "pmt_field": "income", "pmt_weightage": 1.0}
)

self.pmt_params.unlink()

self.assertEqual(partner.pmt_score, 0)

def test_compute_related_partners_pmt_score(self):
partner = self.env["res.partner"].create(
{"name": "Test Partner", "kind": self.group_kind.id, "income": 1000}
)

self.env["sr.proxy.means.test.line"].create(
{"pmt_id": self.pmt_params.id, "pmt_field": "income", "pmt_weightage": 0.5}
)

self.pmt_params.compute_related_partners_pmt_score()
self.assertEqual(partner.pmt_score, 2001)

def test_invalid_computation_conditions(self):
invalid_pmt = self.env["sr.proxy.means.test.params"].create({"pmt_name": False, "target": False})

invalid_pmt.compute_related_partners_pmt_score()
68 changes: 68 additions & 0 deletions g2p_social_registry_proxy_means_test/tests/test_res_partner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from unittest.mock import patch

from odoo.tests import TransactionCase


class TestResPartner(TransactionCase):
def setUp(self):
super().setUp()
self.group_kind = self.env["g2p.group.kind"].create({"name": "Test Kind"})

self.pmt_params = self.env["sr.proxy.means.test.params"].create(
{"pmt_name": "Test PMT", "target": "individual", "kind": self.group_kind.id}
)

self.pmt_line = self.env["sr.proxy.means.test.line"].create(
{"pmt_id": self.pmt_params.id, "pmt_field": "income", "pmt_weightage": 0.5}
)

self.partner = self.env["res.partner"].create(
{"name": "Test Partner", "kind": self.group_kind.id, "income": 1000, "is_group": False}
)

def test_compute_pmt_score(self):
self.partner._compute_pmt_score()
self.assertEqual(self.partner.pmt_score, 2001)

def test_compute_pmt_score_group(self):
group_pmt = self.env["sr.proxy.means.test.params"].create(
{"pmt_name": "Group PMT", "target": "group", "kind": self.group_kind.id}
)

self.env["sr.proxy.means.test.line"].create(
{"pmt_id": group_pmt.id, "pmt_field": "income", "pmt_weightage": 0.75}
)

group_partner = self.env["res.partner"].create(
{"name": "Test Group", "kind": self.group_kind.id, "income": 1000, "is_group": True}
)

group_partner._compute_pmt_score()
self.assertEqual(group_partner.pmt_score, 750)

def test_compute_pmt_score_no_params(self):
self.env["sr.proxy.means.test.params"].search([]).unlink()

self.partner._compute_pmt_score()
self.assertEqual(self.partner.pmt_score, 0)

def test_compute_existing_pmt_scores(self):
partner2 = self.env["res.partner"].create(
{"name": "Test Partner 2", "kind": self.group_kind.id, "income": 2000}
)

self.env["res.partner"].compute_existing_pmt_scores()

self.assertEqual(self.partner.pmt_score, 2001)
self.assertEqual(partner2.pmt_score, 4001)

def test_write_triggers_pmt_computation(self):
with patch.object(type(self.partner), "_compute_pmt_score") as mock_compute:
self.partner.write({"income": 2000})
mock_compute.assert_called()

def test_custom_fields_trigger_computation(self):
with patch.object(type(self.partner), "_get_fields_with_x_prefix", return_value=["gender"]):
with patch.object(type(self.partner), "_compute_pmt_score") as mock_compute:
self.partner.write({"x_gender": 2})
mock_compute.assert_called()
Loading