Message from discussion
testing views with models sqlalchemy
Received: by 10.236.191.168 with SMTP id g28mr382007yhn.64.1311290035873;
Thu, 21 Jul 2011 16:13:55 -0700 (PDT)
X-BeenThere: pylons-discuss@googlegroups.com
Received: by 10.90.213.8 with SMTP id l8ls913848agg.6.gmail; Thu, 21 Jul 2011
16:13:50 -0700 (PDT)
Received: by 10.236.144.228 with SMTP id n64mr394770yhj.33.1311290030196;
Thu, 21 Jul 2011 16:13:50 -0700 (PDT)
Received: by 10.150.25.1 with SMTP id 1msyby;
Thu, 21 Jul 2011 15:21:30 -0700 (PDT)
MIME-Version: 1.0
Received: by 10.91.207.4 with SMTP id j4mr148167agq.21.1311286890186; Thu, 21
Jul 2011 15:21:30 -0700 (PDT)
Received: by e8g2000yqi.googlegroups.com with HTTP; Thu, 21 Jul 2011 15:21:30
-0700 (PDT)
Date: Thu, 21 Jul 2011 15:21:30 -0700 (PDT)
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.16)
Gecko/20110302 Iceweasel/3.5.16 (like Firefox/3.5.16) (.NET CLR 3.5.30729),gzip(gfe)
Message-ID: <9472e764-cf1e-4c1a-b3a4-a2a8edd141ef@e8g2000yqi.googlegroups.com>
Subject: [pyramid] testing views with models sqlalchemy
From: Whitiz <schugsc...@gmail.com>
To: pylons-discuss <pylons-discuss@googlegroups.com>
Content-Type: text/plain; charset=ISO-8859-1
I've tried several approaches to do unit testing of views that have
Model access sqlalchemy,
but nothing seems to work.
A problem always arises when running the second test.
I am using the internal memory 'sqlite://'
The first test works, because I need initialized models prior to call
calling the tests the second test fails because drop_all and/or
rollback to do not appears to work and the models can not be
recreated.
Below is example code based on the tutorial with sqlalchemy.
with this code it fails with
IntegrityError: (IntegrityError) column name is not unique u'INSERT
INTO models (name, value) VALUES (?, ?)' (u'root', 55)
while running the second test.
import unittest
from pyramid.config import Configurator
from pyramid import testing
from tutorial.models import MyModel, DBSession, Base
import transaction
def _initTestingDB():
print "initTestingDB"
from sqlalchemy import create_engine
engine = create_engine('sqlite://')
DBSession.configure(bind=engine)
Base.metadata.bind = engine
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
create_models(DBSession)
return DBSession
def create_models(dbSession):
session = dbSession()
model = MyModel(name=u'root', value=55)
session.add(model)
session.flush()
transaction.commit()
def clear_models(dbSession):
session = dbSession()
old_object = session.query(MyModel)
session.delete(old_object)
session.flush()
transaction.commit()
print "clear_models"
class TestMyView(unittest.TestCase):
def setUp(self):
self.config = testing.setUp()
self.dbSession=_initTestingDB()
def tearDown(self):
testing.tearDown()
clear_models(self.dbSession)
print "rollback"
self.dbSession.rollback()
def test_it(self):
from tutorial.views import my_view
request = testing.DummyRequest()
info = my_view(request)
self.assertEqual(info['root'].name, 'root')
self.assertEqual(info['project'], 'tutorial')
def test_it2(self):
from tutorial.views import my_view
request = testing.DummyRequest()
info = my_view(request)
self.assertEqual(info['root'].name, 'root')