Anda di halaman 1dari 34

Agile Technologies

and SQLAlchemy

Christopher Perkins
PyWorks 2008
What is Agile?
Adaptive

http://www.flickr.com/photos/roblee/442358096/
Collaborative

http://www.flickr.com/photos/gaetanlee/159591865/
Testing

http://www.flickr.com/photos/alisdair/135306281/
What does Python
offer?
Virtualenv

http://www.flickr.com/photos/trommetter/128400664/
Paster

http://www.flickr.com/photos/eastlothian/419836784/
Portability

http://www.flickr.com/photos/23072179@N00/2271722618/
And Now For Something
Completely Different

Courtesy xkcd.com
Thank God for
Python…
Testing
Nose

http://www.flickr.com/photos/cobalt/2373422066/
Test Discovery
Coverage

http://www.flickr.com/photos/stian_olsen/2836208345/
Why Test a Database
Schema?
Because it’s there

Base for majority of your application


Where do we start?

Myth: Databases cannot be tested.

Myth: Testing a database is hard.

Myth: Database testing is SLOW.


SQLAlchemy

+
Declarative
Example Model
from sqlalchemy import *
from sqlalchemy.orm *
from sqlalchemy.ext.declarative import declarative_base

metadata = MetaData()
Base = declarative_base(metadata=metadata)

class DBObject(object):
def __init__(self, **kw):
for item, value in kw.iteritems():
setattr(self, item, value)

barber_style_table = Table('barber_style', metadata,


Column('style_id', Integer, ForeignKey('styles.id')),
Column('barber_id', Integer, ForeignKey('barbers.id')),
)

class Barber(Base, DBObject):


__tablename__ = 'barbers'
id = Column('id', Integer, primary_key=True)
name = Column('name', String(50))
styles = relation('Style', secondary=barber_style_table)

class Style(Base, DBObject):


__tablename__ = 'styles'
id = Column('id', Integer, primary_key=True)
name = Column('name', String(50))

class Client(Base, DBObject):


__tablename__ = 'clients'
id = Column('id', Integer, primary_key=True)
name = Column('name', String(50))
style_id = Column('style_id', Integer, ForeignKey('styles.id'))
style = relation('Style', backref='clients')
Test Environment
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from barbershop.model import *

database_created = False
def setup_database():
global database_created
engine = create_engine(os.environ.get('DBURL', 'sqlite://'))
metadata.bind = engine

if not database_created:
metadata.drop_all()
metadata.create_all()
database_created = True

Session = sessionmaker(bind=engine, autoflush=True, autocommit=True)


session = Session()
return engine, session
Test Class
  
   

  
  
    

  
   
  

     
    
  

   
  

   

   
     
     
  
      ’  

Your Database will
Change
SQLAlchemy Migrate

http://farm1.static.flickr.com/73/170412399_a6c89b8dff.jpg?v=0
Version Control
Upgrade
Downgrade
Documentation
Robustness

http://farm3.static.flickr.com/2029/2229405391_a07aa5b5eb.jpg?v=0
Thanks!
Links
 Python Tutorials
 http://code.google.com/p/pythontutorials/

 This Presentation
 http://pythontutorials.googlecode.com/svn/presentations/AgileSA.ppt

 Barbershop Tutorial
 svn checkout
http://pythontutorials.googlecode.com/svn/tutorials/barbershop

 Me
 www.percious.com