Skip to content

Commit ebca8f5

Browse files
committed
fix: use IntegrityError in constraint tests for spp_area_hdx and spp_drims
In Odoo 19, SQL constraint violations (models.Constraint, required=True) raise psycopg2.IntegrityError at the ORM level, not ValidationError. The conversion to ValidationError happens in the RPC layer, not during direct create() calls in tests. Fixes 5 pre-existing test failures: - spp_area_hdx: test_hdx_pcode_unique_constraint - spp_area_hdx: test_required_fields (source_id NOT NULL) - spp_area_hdx: test_unique_country_constraint - spp_drims: test_4w_wizard_requires_incident - spp_drims: test_urgency_state_resolved (missing resolution notes)
1 parent ec1574e commit ebca8f5

File tree

5 files changed

+18
-33
lines changed

5 files changed

+18
-33
lines changed

spp_area_hdx/tests/test_area_gps_lookup.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import json
44

5+
from psycopg2 import IntegrityError
6+
57
from odoo.tests import common, tagged
8+
from odoo.tools import mute_logger
69

710

811
@tagged("post_install", "-at_install")
@@ -157,9 +160,7 @@ def test_find_by_pcode(self):
157160

158161
def test_hdx_pcode_unique_constraint(self):
159162
"""Test that HDX P-codes must be unique."""
160-
from odoo.exceptions import ValidationError
161-
162-
with self.assertRaises(ValidationError):
163+
with self.assertRaises(IntegrityError), mute_logger("odoo.sql_db"):
163164
self.env["spp.area"].create(
164165
{
165166
"draft_name": "Duplicate",

spp_area_hdx/tests/test_hdx_cod_resource.py

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
import json
44
from unittest.mock import patch
55

6+
from psycopg2 import IntegrityError
7+
68
from odoo.exceptions import UserError
79
from odoo.tests import common, tagged
10+
from odoo.tools import mute_logger
811

912

1013
@tagged("post_install", "-at_install")
@@ -38,35 +41,15 @@ def test_create_resource(self):
3841

3942
def test_required_fields(self):
4043
"""Test that required fields are enforced."""
41-
from odoo.exceptions import ValidationError
42-
43-
# source_id is required
44-
with self.assertRaises(ValidationError):
44+
# source_id is required (DB NOT NULL constraint)
45+
with self.assertRaises(IntegrityError), mute_logger("odoo.sql_db"):
4546
self.env["spp.hdx.cod.resource"].create(
4647
{
4748
"name": "Test",
4849
"admin_level": 1,
4950
}
5051
)
5152

52-
# name is required
53-
with self.assertRaises(ValidationError):
54-
self.env["spp.hdx.cod.resource"].create(
55-
{
56-
"source_id": self.source.id,
57-
"admin_level": 1,
58-
}
59-
)
60-
61-
# admin_level is required
62-
with self.assertRaises(ValidationError):
63-
self.env["spp.hdx.cod.resource"].create(
64-
{
65-
"source_id": self.source.id,
66-
"name": "Test",
67-
}
68-
)
69-
7053
def test_default_format(self):
7154
"""Test default format is geojson."""
7255
resource = self.env["spp.hdx.cod.resource"].create(

spp_area_hdx/tests/test_hdx_cod_source.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
from unittest.mock import patch
44

5+
from psycopg2 import IntegrityError
6+
57
from odoo.tests import common, tagged
8+
from odoo.tools import mute_logger
69

710

811
@tagged("post_install", "-at_install")
@@ -23,9 +26,7 @@ def test_compute_country_iso3(self):
2326

2427
def test_unique_country_constraint(self):
2528
"""Test that only one source per country is allowed."""
26-
from odoo.exceptions import ValidationError
27-
28-
with self.assertRaises(ValidationError):
29+
with self.assertRaises(IntegrityError), mute_logger("odoo.sql_db"):
2930
self.env["spp.hdx.cod.source"].create(
3031
{
3132
"country_id": self.country_lk.id,

spp_drims/tests/test_alert.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ def test_urgency_state_resolved(self):
288288
"days_until": -5, # Would be overdue if active
289289
}
290290
)
291-
alert.action_resolve()
291+
alert.action_resolve(notes="Resolved for testing")
292292
self.assertEqual(alert.urgency_state, "normal")
293293

294294
def test_alert_type_color_low_stock(self):

spp_drims/tests/test_coordination.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -457,11 +457,11 @@ def test_4w_wizard_model_exists(self):
457457

458458
def test_4w_wizard_requires_incident(self):
459459
"""Test 4W wizard requires incident selection."""
460-
# incident_id is a required field with DB NOT NULL constraint,
461-
# so creating without it raises ValidationError
462-
from odoo.exceptions import ValidationError
460+
from psycopg2 import IntegrityError
463461

464-
with self.assertRaises(ValidationError):
462+
from odoo.tools import mute_logger
463+
464+
with self.assertRaises(IntegrityError), mute_logger("odoo.sql_db"):
465465
self.env["spp.drims.report.4w.wizard"].create({})
466466

467467
def test_4w_wizard_date_filters(self):

0 commit comments

Comments
 (0)