Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bart mosley committed Jan 16, 2011
1 parent f024c1e commit 522461f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 24 deletions.
1 change: 1 addition & 0 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

else:
from model import Model
from db.connection import autumn_db
from db.dbconnection import DBConnection
from db.query import Query
from db.filter import Filter
Expand Down
26 changes: 18 additions & 8 deletions db/dbconnection.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
'''
Connection class
Connection class
To be invoked from a dbconnection class that contains db=DBConn()
This class keeps track of what kind of connection you have (sqlite,mysql)
This class keeps track of what kind of connection you have (sqlite,mysql, pyodbc)
and has a 'close()' method that is easily accessible.
class marketsdb(object):
db.conn = DBConnection('mysql', db='mydatabase', **mycfg)
close = db.conn.close
*dbtype*
Values 'sqlite3', 'mysql', 'pyodbc'. 'mysql' uses the MySQLdb package to connect.
'pyodbc' supports the use of connection strings and
provides more cross platform interoperability.
Example
mycfg = {'DSN': myODBC_DSN, 'UID': myUID, 'PWD': myPWD}
class marketsdb(object):
db = DBConnection('pyodbc', db='mydatabase', **mycfg)
class Table(marketsdb, Model):
"This is now an autumnORM object representing mydatabase.Table"
pass
N.B.: If you use this to create a connection, any Query.raw_sql call MUST specify
the db connection. Using the above example:
Query.raw_sql("select * from mytable", db=marketsdb.db)
'''

Expand All @@ -39,7 +49,7 @@ def getconnection(self, dbtype=None, db=None, dbfile=None, **kwcfg):
self.user = None

if db:
kwcfg.update({'db': db})
kwcfg.update({'DATABASE': db})

self.connect(dbtype, dbfile, **kwcfg)

Expand Down
18 changes: 11 additions & 7 deletions db/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ class Query(object):

def __init__(self, query_type='SELECT *', conditions=None, model=None, db=None):
from .. import Model

if not issubclass(model, Model):
raise Exception('Query objects must be created with a model class.')

self.model = model
if db:
self.db = db
elif model:
self.db = model.db

self.type = query_type
# using conditions = {} in argument line causes each
# instance of Query to share 'conditions'.
Expand All @@ -83,16 +93,10 @@ def __init__(self, query_type='SELECT *', conditions=None, model=None, db=None):
self.conditions = {}
else:
self.conditions = conditions

self.order = ''
self.limit = ()
self.cache = None
if not issubclass(model, Model):
raise Exception('Query objects must be created with a model class.')
self.model = model
if db:
self.db = db
elif model:
self.db = model.db

def __getitem__(self, k):
if self.cache != None:
Expand Down
3 changes: 2 additions & 1 deletion model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .db.query import Query
from .db import escape
from .db.connection import autumn_db, Database
from .db.connection import autumn_db
from .validators import ValidatorChain

class ModelCache(object):
Expand Down Expand Up @@ -52,6 +52,7 @@ def __new__(cls, name, bases, attrs):
# http://www.python.org/dev/peps/pep-0249/
if not hasattr(new_class, "db"):
new_class.db = autumn_db

db = new_class.db
q = Query.raw_sql('SELECT * FROM %s LIMIT 1' % new_class.Meta.table_safe, db=new_class.db)
new_class._fields = [f[0] for f in q.description]
Expand Down
11 changes: 5 additions & 6 deletions tests/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .. import DBConnection, Model, ForeignKey, OneToMany
from .. import DBConnection, Model, ForeignKey, OneToMany, autumn_db
from .. import validators

import datetime
Expand All @@ -7,22 +7,21 @@
#autumn_db.conn.connect('mysql', user='root', db='autumn_test')
#autumn_db.conn.connect('pyodbc', driver='{MySQL ODBC 5.1 Driver}', server='localhost', database='autumn_test', user='root')

class TESTDB(object):
db = DBConnection('pyodbc', DSN='AutumnTest', UID='bondgeek')
autumn_db.conn.connect('pyodbc', DSN='AutumnTest', UID='bondgeek')

class Author(TESTDB, Model):
class Author(Model):
books = OneToMany('Book')

class Meta:
defaults = {'bio': 'No bio available'}
validations = {'first_name': validators.Length(),
'last_name': (validators.Length(), lambda x: x != 'BadGuy!')}

class Book(TESTDB, Model):
class Book(Model):
author = ForeignKey(Author)

class Meta:
table = 'books'

class Transaction(TESTDB, Model):
class Transaction(Model):
pass
4 changes: 2 additions & 2 deletions tests/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .. import Model, Query, escape
from .. import validators

from .models import Book, Author, TESTDB
from .models import Book, Author

class TestModels(unittest.TestCase):

Expand Down Expand Up @@ -56,7 +56,7 @@ def test_model(self):
# );

for table in ('author', 'books'):
Query.raw_sql('DELETE FROM %s' % escape(table), db=TESTDB.db)
Query.raw_sql('DELETE FROM %s' % escape(table))

# Test Creation
james = Author(first_name='James', last_name='Joyce')
Expand Down

0 comments on commit 522461f

Please sign in to comment.