#!/usr/bin/env python
"""dbutil_test.py -- Test suite for dbutil.py
This program is designed to run under py.test
(http://codespeak.net/py/current/doc/test.html), but it can also be run
standalone. The advantage of py.test is that if an assertion fails, it will
show the values of all variables in the test expression, and also show a more
detailed traceback. py.test is available only as a Subversion checkout.
If the environment variable 'DEBUG' is non-empty, it will log interesting
information to standard error.
"""
import os, logging
import dbutil
if os.environ.get('DEBUG'):
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
def test_and():
where = dbutil.Where({})
where.parts = ['a', 'b', 'c']
assert str(where) == "((a) AND (b) AND (c))"
def test_or():
where = dbutil.Where({})
where.parts = ['a', 'b', 'c']
where.and_ = False
assert str(where) == "((a) OR (b) OR (c))"
def test_relation():
relations = [('and', True), ('or', False)]
for (r, truth) in relations:
for relation in [r, r.upper(), r.capitalize()]:
where = dbutil.Where({'relation': relation})
where.relation('relation')
msg = "expected %s for relation %r" % (truth, relation)
assert where.and_ == truth, msg
def test_compare():
where = dbutil.Where({'foo': 'bar'})
where.compare('foo', '<', 'food')
assert where.parts[0] == "food < 'bar'"
def test_substring():
where = dbutil.Where({'foo': 'ba'})
where.substring('foo')
assert where.parts[0] == "foo LIKE '%ba%'"
def test_in_list():
valuesMap = {'flavors':
['chocolate', 'vanilla', 'strawberry'] }
where = dbutil.Where(valuesMap)
where.in_list('flavors')
result = "flavors IN ('chocolate', 'vanilla', 'strawberry')"
assert where.parts[0] == result
def run_all_tests():
test_and()
test_or()
test_relation()
test_compare()
test_substring()
test_in_list()
if __name__ == "__main__": run_all_tests()