From 3a0cee2a24dfdbabc2ab4e1423e744374d57f80d Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 26 Nov 2005 19:26:30 +0000 Subject: [PATCH] refactoring test to be more consistent, added multipk test to engine --- test/attributes.py | 1 + test/engines.py | 40 ++++++++++++++++++++++++++++++--------- test/objectstore.py | 46 +++++++++++++++++++++++++++++++++++++++++++-- test/tables.py | 14 +------------- test/testbase.py | 16 ++++++++++++++++ 5 files changed, 93 insertions(+), 24 deletions(-) diff --git a/test/attributes.py b/test/attributes.py index 1a59c8af2f..a7dd3bf4e4 100644 --- a/test/attributes.py +++ b/test/attributes.py @@ -6,6 +6,7 @@ import unittest, sys, os class AttributesTest(PersistTest): + """tests for the attributes.py module, which deals with tracking attribute changes on an object.""" def testbasic(self): class User(object):pass manager = attributes.AttributeManager() diff --git a/test/engines.py b/test/engines.py index 5f2ae8d646..5a1dfcd0cc 100644 --- a/test/engines.py +++ b/test/engines.py @@ -13,17 +13,10 @@ from testbase import PersistTest import testbase import unittest, re +db = testbase.get_db() class EngineTest(PersistTest): - def testsqlite(self): - db = sqllite.engine({'filename':':memory'}, echo = testbase.echo) - self.do_tableops(db) - - def testpostgres(self): - db = postgres.engine({'database':'test', 'host':'127.0.0.1', 'user':'scott', 'password':'tiger'}, echo = testbase.echo) - self.do_tableops(db) - - def do_tableops(self, db): + def testbasic(self): # really trip it up with a circular reference users = Table('users', db, Column('user_id', INT, primary_key = True), @@ -66,6 +59,35 @@ class EngineTest(PersistTest): addresses.drop() users.drop() + + def testmultipk(self): + table = Table( + 'multi', db, + Column('multi_id', Integer, primary_key=True), + Column('multi_rev', Integer, primary_key=True), + Column('name', String(50), nullable=False), + Column('value', String(100)) + ) + table.create() + # clear out table registry + db.tables.clear() + + try: + table = Table('multi', db, autoload=True) + finally: + table.drop() + + print repr( + [table.c['multi_id'].primary_key, + table.c['multi_rev'].primary_key + ] + ) + table.create() + table.insert().execute({'multi_rev':1,'name':'row1', 'value':'value1'}) + table.insert().execute({'multi_rev':18,'name':'row2', 'value':'value2'}) + table.insert().execute({'multi_rev':3,'name':'row3', 'value':'value3'}) + table.select().execute().fetchall() + table.drop() if __name__ == "__main__": diff --git a/test/objectstore.py b/test/objectstore.py index 362651b237..e82ce59d60 100644 --- a/test/objectstore.py +++ b/test/objectstore.py @@ -12,14 +12,20 @@ import tables class HistoryTest(AssertMixin): def setUpAll(self): db.echo = False + users.create() addresses.create() db.echo = testbase.echo def tearDownAll(self): db.echo = False addresses.drop() + users.drop() db.echo = testbase.echo def testattr(self): + """tests the rolling back of scalar and list attributes. this kind of thing + should be tested mostly in attributes.py which tests independently of the ORM + objects, but I think here we are going for + the Mapper not interfering with it.""" m = mapper(User, users, properties = dict(addresses = relation(Address, addresses))) u = User() u.user_id = 7 @@ -28,10 +34,46 @@ class HistoryTest(AssertMixin): u.addresses[0].email_address = 'hi' u.addresses.append(Address()) u.addresses[1].email_address = 'there' - self.echo(repr(u.__dict__)) + data = [User, + {'user_name' : 'afdas', + 'addresses' : (Address, [{'email_address':'hi'}, {'email_address':'there'}]) + }, + ] + self.assert_result([u], data[0], *data[1:]) + self.echo(repr(u.addresses)) objectstore.uow().rollback_object(u) - self.echo(repr(u.__dict__)) + data = [User, + {'user_name' : None, + 'addresses' : (Address, []) + }, + ] + self.assert_result([u], data[0], *data[1:]) + +class PKTest(AssertMixin): + def setUpAll(self): + db.echo = False + self.table = Table( + 'multi', db, + Column('multi_id', Integer, primary_key=True), + Column('multi_rev', Integer, primary_key=True), + Column('name', String(50), nullable=False), + Column('value', String(100)) + ) + self.table.create() + db.echo = testbase.echo + def tearDownAll(self): + db.echo = False + self.table.drop() + db.echo = testbase.echo + def testprimarykey(self): + class Entry(object): + pass + Entry.mapper = mapper(Entry, self.table) + e = Entry() + e.name = 'entry1' + e.value = 'this is entry 1' + objectstore.commit() class SaveTest(AssertMixin): diff --git a/test/tables.py b/test/tables.py index b1a241fa4c..a2ee265e40 100644 --- a/test/tables.py +++ b/test/tables.py @@ -9,20 +9,8 @@ import testbase __ALL__ = ['db', 'users', 'addresses', 'orders', 'orderitems', 'keywords', 'itemkeywords'] ECHO = testbase.echo +db = testbase.get_db() -DBTYPE = 'sqlite_memory' -#DBTYPE = 'postgres' -#DBTYPE = 'sqlite_file' - -if DBTYPE == 'sqlite_memory': - db = sqlalchemy.engine.create_engine('sqlite', {'filename':':memory:'}, echo = testbase.echo) -elif DBTYPE == 'sqlite_file': - import sqlalchemy.databases.sqlite as sqllite - db = sqlalchemy.engine.create_engine('sqlite', {'filename':'querytest.db'}, echo = testbase.echo) -elif DBTYPE == 'postgres': - db = sqlalchemy.engine.create_engine('postgres', {'database':'test', 'host':'127.0.0.1', 'user':'scott', 'password':'tiger'}, echo=testbase.echo) - -db = testbase.EngineAssert(db) users = Table('users', db, Column('user_id', Integer, Sequence('user_id_seq', optional=True), primary_key = True), diff --git a/test/testbase.py b/test/testbase.py index 3b16d3105c..1f7187bf21 100644 --- a/test/testbase.py +++ b/test/testbase.py @@ -6,6 +6,21 @@ import sqlalchemy.databases.postgres as postgres echo = True +def get_db(): + DBTYPE = 'sqlite_memory' + #DBTYPE = 'postgres' + #DBTYPE = 'sqlite_file' + + if DBTYPE == 'sqlite_memory': + db = engine.create_engine('sqlite', {'filename':':memory:'}, echo = echo) + elif DBTYPE == 'sqlite_file': + db = engine.create_engine('sqlite', {'filename':'querytest.db'}, echo = echo) + elif DBTYPE == 'postgres': + db = engine.create_engine('postgres', {'database':'test', 'host':'127.0.0.1', 'user':'scott', 'password':'tiger'}, echo=echo) + + db = EngineAssert(db) + return db + class PersistTest(unittest.TestCase): """persist base class, provides default setUpAll, tearDownAll and echo functionality""" def __init__(self, *args, **params): @@ -18,6 +33,7 @@ class PersistTest(unittest.TestCase): def tearDownAll(self): pass + class AssertMixin(PersistTest): """given a list-based structure of keys/properties which represent information within an object structure, and a list of actual objects, asserts that the list of objects corresponds to the structure.""" -- 2.47.2