[sqlalchemy] Re: Unit testing with SA?

2007-02-02 Thread Christopher Arndt

Allen schrieb:
 How do people writing db applications with SA do unit testing on their
 code?  Are there any good db unit testing frameworks that work well
 with SA?  If I can't find anything I will probably just roll my own
 but I would like to keep from reinventing the wheel if I can help it.

Basically you have to initialize your database for each test and destroy it
afterwards again (otherwise it wouldn't be a 'unit' test). In the 'unittest'
module, you can use the 'setUp' and tearDown' methods of the TestCase class for
this purpose.

For example:

# model contains the SA table and mapper classes
import unittest
import sqlalchemy
import model

class SATestCase(unittest.TestCase):

def setUp(self):
# do what you have to do to bind the metadata/engine to the model here

# create all model tables
for item in dir(self.model):
item = getattr(self.model, item)
if isinstance(item, sqlalchemy.Table):
item.create(checkfirst=True)

def tearDown(self):
# drop all model tables
for item in dir(self.model):
item = getattr(self.model, item)
if isinstance(item, sqlalchemy.Table):
item.drop(checkfirst=True)
# close database

def testFoo(self):
# here comes your unit test


Chris

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Tagging example

2007-01-26 Thread Christopher Arndt

Damjan schrieb:
 Now, creating pages and then adding tags to them like so:
 p1.tags.append(Tag('test'))
 p2.tags.append(Tag('test'))
 
 will actually create two 'test' tags in the database...

Have a look at my tagging tutorial at

http://paddyland.serveblog.net/article/16#updating-tags

Although it uses SQLObject, the pattern used in the 'update_tags' method
(http://paddyland.serveblog.net/article/16#updating-tags) can be applied to
SQLAlchemy as well.

 also does p1.tags be a list of Tag instances? Can it be a list of
 normal strings?

taglist = [str(tag.name) for tag in p1.tags]


HTH, Chris

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] class-object-like attribute lookup

2007-01-25 Thread Christopher Arndt

Hi all,

I'm still learning SA, so please forgive me, if I'm asking something obvious or
have overlooked something in the docs...

The subject onyl gives a vague idea of what I'm trying to accomplish, but since
I couldn't find better term for it, I had trouble searching the lists, docs, 
etc.

Suppose I have an object 'Bookmark' mapped to a table 'bookmarks':

bookmarks_tbl = Table('bookmarks', metadata,
Column('id', Integer, primary_key=True),
Column('owner_id', Integer, ForeignKey('users.user_id')),
Column('parent_id', Integer, ForeignKey('bookmarks.id')),
Column('title', String(100)),
Column('description', String(1000)),
Column('url', String(250))
)

As you can see 'parent_id' is a self-referencing FK to the bookmarks table. The
idea now is to allow users to have their own copies of mapped 'Bookmark'
objects, that are a sort of child of an existing 'Bookmark' object and allow
them to overwrite certain columns like e.g. 'title' and 'description'. But if
the column is NULL, it should be looked up in the parent object/row. Sort-of
like in class/object attributes in Python classes.

I hope, I made it clear what I mean. Is this feasible? Is there support in SA
for this or has anybody done anything like this? Any pointers or ideas are very
much appreciated!

Cheers, Chris


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: class-object-like attribute lookup

2007-01-25 Thread Christopher Arndt

Karl Guertin schrieb:
 Looks like you want recursive single table inheritance [1].

It doesn't need to be recursive, just a two-level parent-child relation. And I
don't want to add any columns, so I was thinking that table inheritance is not
the right approach, but I'm not sure about that.

Chris

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---