I'm running into an issue on Django 1.2 where the second test in my test suite fails due to a duplicate key value violation. It appears that the issue is that calls to my 'default' db, test_app_django in this case, run within a transaction, but those to my application db, test_app, do not. You can see from the SQL log that only the default db has a ROLLBACK, causing later tests to fail. Does anyone know how I can ensure that interaction with my second db is also run within a transaction and appropriately rolled back?
test_member_programme.py
------------------
from datetime import date, timedelta
from django.test import TestCase
from fred_db.test.factories import StudentFactory, ProgrammeFactory
from fred_db.models import StudentProgramme
class StudentProgrammeTestCase(TestCase):
def setUp(self):
self.stud_prg = self.student_programme()
def tearDown(self):
pass
def student_programme(self):
yesterday = date.today() - timedelta(days=1)
return StudentProgramme.objects.create(
stud=StudentFactory(),
prgm=ProgrammeFactory(),
date_registered=yesterday)
def date_registered_should_be_defined_test(self):
assert hasattr(self.stud_prg, 'date_registered')
def date_registered_should_be_in_past_test(self):
assert self.stud_prg.date_registered < date.today()
---------------------
dba test_app 127.0.0.1 2012-09-04 21:51:50.806 UTC LOG: duration: 0.038 ms statement: BEGIN; SET TRANSACTION ISOLATION LEVEL READ COMMITTED dba test_app 127.0.0.1 2012-09-04 21:51:50.808 UTC LOG: duration: 0.903 ms statement: INSERT INTO "member_programme" ("mem_id", "prgm_id", "date_registered", "date_completed", "ordinality") VALUES (1, 1, E'2012-09-04', NULL, 1) dba test_app 127.0.0.1 2012-09-04 21:51:50.808 UTC LOG: duration: 0.150 ms statement: SELECT CURRVAL(pg_get_serial_sequence('"member_programme"','id')) dba test_app 127.0.0.1 2012-09-04 21:51:50.810 UTC LOG: duration: 1.796 ms statement: COMMIT dba test_app_django 127.0.0.1 2012-09-< span class="lit" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0); background-position: initial initial; background-repeat: initial initial; ">04 21:51:50.811 UTC LOG: duration: 0.056 ms statement: ROLLBACK <---- ROLLBACK ON DJANGO DB ONLY dba test_app_django 127.0.0.1 2012-09-< span class="lit" style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0); background-position: initial initial; background-repeat: initial initial; ">04 21:51:50.814 UTC LOG: disconnection: session time: 0:00:21.005 user=dba database=test_app_django host=127.0.0.1 port=60355 dba test_app 127.0.0.1 2012-09-04 21:51:50.818 UTC LOG: disconnection: session time: 0:00:04.751 user=dba database=test_app host=127.0.0.1 port=60357 dba test_app 127.0.0.1 2012-09-04 21:54:00.796 UTC LOG: connection authorized: user=dba database=test_app dba test_app 127.0.0.1 2012-09-04 21:54:00.802 UTC LOG: duration: 0.243 ms statement: SET DATESTYLE TO 'ISO' dba test_app 127.0.0.1 2012-09-04 21:54:00.802 UTC LOG: duration: 0.156 ms statement: SHOW client_encoding dba test_app 127.0.0.1 2012-09-04 21:54:00.803 UTC LOG: duration: 0.047 ms statement: SHOW default_transaction_isolation dba test_app 127.0.0.1 2012-09-04 21:54:00.803 UTC LOG: duration: 0.068 ms statement: BEGIN; SET TRANSACTION ISOLATION LEVEL READ COMMITTED dba test_app 127.0.0.1 2012-09-04 21:54:00.804 UTC LOG: duration: 0.410 ms statement: SET TIME ZONE E'Pacific/Auckland' dba test_app 127.0.0.1 2012-09-04 21:54:00.805 UTC ERROR: duplicate key value violates unique constraint "country_of_origin_code_key" Many thanks!
-- Bayard 'Kit' Randel / Software Developer Faculty of Medicine, University of Otago
No comments:
Post a Comment