python - Odoo: test unique constraint? -
it looks odoo fails @ testing unique constraint.
i have added constraint:
_sql_constraints = [( 'uniq_line', 'unique(routing_phase_id, project_id)', 'phase have unique per routing!') ]
constraint works, running unittest, can't test it.
i tried this:
from psycopg2 import integrityerror self.assertraises(integrityerror): self.env['project.routing.line'].create( self.project_routing_line_1.copy_data()[0])
i error, when running test:
2016-05-28 13:59:16,575 19786 error pas_test openerp.sql_db: bad query: insert "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() @ time zone 'utc'), (now() @ 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) exists
so don't it, test catches integrityeror (or not?), odoo still tries create record of course fails. doing wrong here?
update
i started thinking, maybe because sql constraint, python not catch such exceptions @ right time (like using assertraises
) because happens after python validations? explain why test fails.
encountered issue myself , found existing test used odoo themselves.
they use openerp.tools.mute_logger('openerp.sql_db')
decorator on methods testing integrityerror raised.
i added decorator test method , worked. guess integrityerror being logged squelches exception being raised.
Comments
Post a Comment