shalabh / quixote_extras

User contributed add-ons and for Quixote. See http://mems-exchange.org/software/quixote/. This repository used to be hosted at http://www.cafepy.com/quixote_extras/. All links from old location are now redirected here.

Clone this repository (size: 917.6 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/shalabh/quixote_extras
commit 149: 5b7911ccd017
parent 148: fbb05270db35
branch: default
tags: tip
Updated readme to reflect move to bitbucket.org
Shal...@cafepy.com
10 months ago
quixote_extras / rex / tests / test_dbutil_where.py
r149:5b7911ccd017 66 loc 2.0 KB embed / history / annotate / raw /
#!/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()