samedi 28 mai 2016

Odoo: test unique constraint?

It looks like Odoo fails at testing unique constraint.

I have added this constraint:

_sql_constraints = [(
    'uniq_line',
    'unique(routing_phase_id, project_id)',
    'Phase have to be unique per Routing!')
]

Constraint itself works, but running unittest, I can't successfully test it.

I tried this:

from psycopg2 import IntegrityError

with self.assertRaises(IntegrityError):
     self.env['project.routing.line'].create(
        self.project_routing_line_1.copy_data()[0])

I get this error, when running test:

2016-05-28 13:59:16,575 19786 ERROR pas_test openerp.sql_db: bad query: INSERT INTO "project_routing_line" ("id", "routing_phase_id", "sequence", "next_routing_phase_id", "duration", "project_id", "return_routing_phase_id", "need_approve", "create_uid", "write_uid", "create_date", "write_date") VALUES(nextval('project_routing_line_id_seq'), 1, 5, NULL, 10.0, 5, 3, false, 1, 1, (now() at time zone 'UTC'), (now() at time zone 'UTC')) RETURNING id
Traceback (most recent call last):
  File "/home/oerp/openerp80/odoo/openerp/sql_db.py", line 234, in execute
    res = self._obj.execute(query, params)
IntegrityError: duplicate key value violates unique constraint "project_routing_line_uniq_line"
DETAIL:  Key (routing_phase_id, project_id)=(1, 5) already exists

So I don't get it, test catches IntegrityEror (or it does not?), but Odoo still tries to create record which of course fails. Am I doing something wrong here?

1 commentaire:

  1. It works with me, thanks to this:
    https://stackoverflow.com/questions/37499977/odoo-test-unique-constraint

    RépondreSupprimer