0% found this document useful (0 votes)
87 views

Agile Technologies and Sqlalchemy

The document discusses using Python and SQLAlchemy for agile database development. It covers SQLAlchemy concepts like declarative modeling and testing databases. It emphasizes that databases can and should be tested using tools like Nose and SQLAlchemy's testing utilities.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views

Agile Technologies and Sqlalchemy

The document discusses using Python and SQLAlchemy for agile database development. It covers SQLAlchemy concepts like declarative modeling and testing databases. It emphasizes that databases can and should be tested using tools like Nose and SQLAlchemy's testing utilities.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 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

You might also like