# HG changeset patch # User Radek Brich # Date 1348919634 -7200 # Node ID 08e4dfe1b0cb3bc6689c2cedf9885e37ec5de6fe # Parent b82c7c2fb5af65411fc6d4999d460f47de0b0f91 Add test for MyManager (enable only when MySQLdb is available). Configure tests using pgtoolkit.conf (same as used by other executables). diff -r b82c7c2fb5af -r 08e4dfe1b0cb .hgignore --- a/.hgignore Sat Sep 29 12:08:47 2012 +0200 +++ b/.hgignore Sat Sep 29 13:53:54 2012 +0200 @@ -1,7 +1,7 @@ -.*\.pyc -.project -.pydevproject -.settings/org.eclipse.core.resources.prefs -pgtoolkit\.conf -build -tests/tests\.conf +^.*\.pyc$ +^.project +^.pydevproject +^.settings/org.eclipse.core.resources.prefs +^pgtoolkit\.conf$ +^build +^tests\.conf$ diff -r b82c7c2fb5af -r 08e4dfe1b0cb TESTING --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TESTING Sat Sep 29 13:53:54 2012 +0200 @@ -0,0 +1,15 @@ +TESTING +======= + +All test should work at least with Python 2.7 and 3.2 + + +How to test +----------- + +1. copy pgtoolkit.conf.example to pgtoolkit.conf, modify it so it points to your test database + +2. create test tables using sql/tests.sql + +3. run tests.py + diff -r b82c7c2fb5af -r 08e4dfe1b0cb mytoolkit/mymanager.py --- a/mytoolkit/mymanager.py Sat Sep 29 12:08:47 2012 +0200 +++ b/mytoolkit/mymanager.py Sat Sep 29 13:53:54 2012 +0200 @@ -2,7 +2,7 @@ # # MyManager - manage database connections (MySQL version) # -# Requires: Python 2.6, MySQLdb +# Requires: Python 2.6 / 2.7, MySQLdb # # Part of pgtoolkit # http://hg.devl.cz/pgtoolkit @@ -93,7 +93,7 @@ self.keep_open = keep_open self.parameters = kw self.adjust_parameters() - + def adjust_parameters(self): '''Rename Postgres parameters to proper value for MySQL.''' m = {'dbname' : 'db', 'password' : 'passwd'} @@ -166,9 +166,9 @@ def close_conn(self, name='default'): '''Close all connections of given name. - + Connection credentials are still saved. - + ''' while len(self.conn_pool[name]): conn = self.conn_pool[name].pop() @@ -176,9 +176,9 @@ def destroy_conn(self, name='default'): '''Destroy connection. - + Counterpart of create_conn. - + ''' if not name in self.conn_known: raise MyManagerError('Connection name "%s" not registered.' % name) @@ -213,10 +213,10 @@ def put_conn(self, conn, name='default'): '''Put connection back to pool. - + Name must be same as used for get_conn, otherwise things become broken. - + ''' self.lock.acquire() try: @@ -241,10 +241,10 @@ @contextmanager def cursor(self, name='default'): '''Cursor context. - + Uses any connection of name 'name' from pool and returns cursor for that connection. - + ''' conn = self.get_conn(name) diff -r b82c7c2fb5af -r 08e4dfe1b0cb pgtoolkit.conf.example --- a/pgtoolkit.conf.example Sat Sep 29 12:08:47 2012 +0200 +++ b/pgtoolkit.conf.example Sat Sep 29 13:53:54 2012 +0200 @@ -1,6 +1,8 @@ ### named connections databases = { - 'test' : 'host=127.0.0.1 user=test password=test dbname=test' + # database for tests.py (postgres, mysql - remove one of the lines to skip particular tests) + 'test' : 'host=127.0.0.1 dbname=test user=test password=test', + 'test_mysql' : 'host=127.0.0.1 db=test user=test password=test', } ### meta database (contains connection parameters for other databases) diff -r b82c7c2fb5af -r 08e4dfe1b0cb sql/tests.sql --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sql/tests.sql Sat Sep 29 13:53:54 2012 +0200 @@ -0,0 +1,6 @@ +CREATE TABLE test +( + id serial NOT NULL, + name varchar, + PRIMARY KEY (id) +); diff -r b82c7c2fb5af -r 08e4dfe1b0cb tests.py --- a/tests.py Sat Sep 29 12:08:47 2012 +0200 +++ b/tests.py Sat Sep 29 13:53:54 2012 +0200 @@ -5,12 +5,21 @@ from tests import test_rowdict from tests import testpgmanager +enable_mysql = True +try: + from tests import test_mymanager +except ImportError: + enable_mysql = False + loader = unittest.TestLoader() suite = unittest.TestSuite() suite.addTests(loader.loadTestsFromModule(test_rowdict)) suite.addTests(loader.loadTestsFromModule(testpgmanager)) +if enable_mysql: + suite.addTests(loader.loadTestsFromModule(test_mymanager)) + runner = unittest.TextTestRunner(verbosity=2) result = runner.run(suite) diff -r b82c7c2fb5af -r 08e4dfe1b0cb tests/TESTS --- a/tests/TESTS Sat Sep 29 12:08:47 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,15 +0,0 @@ -TESTS -===== - -All test should work at least with Python 2.6, 2.7 and 3.2 - - -How to test ------------ - -1. Copy tests.conf.example to tests.conf and modify it so it points to your test database. - -2. Create test tables using tests.sql - -3. Run individual tests. - diff -r b82c7c2fb5af -r 08e4dfe1b0cb tests/config.py --- a/tests/config.py Sat Sep 29 12:08:47 2012 +0200 +++ b/tests/config.py Sat Sep 29 13:53:54 2012 +0200 @@ -1,8 +1,3 @@ -import sys - -sys.path.insert(0, '..') - - class Config(dict): def __init__(self, fname): with open(fname) as f: diff -r b82c7c2fb5af -r 08e4dfe1b0cb tests/test_mymanager.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_mymanager.py Sat Sep 29 13:53:54 2012 +0200 @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +from tests.config import Config +from mytoolkit import mymanager + +import unittest + + +class TestMyManager(unittest.TestCase): + + def setUp(self): + cfg = Config('pgtoolkit.conf') + test_db_conn_params = cfg['databases']['test_mysql'] + params = self.params_to_mapping(test_db_conn_params) + self.m = mymanager.get_instance() + self.m.create_conn(**params) + + def tearDown(self): + self.m.destroy_conn() + + def params_to_mapping(self, params): + return dict([param.split('=') for param in params.split(' ')]) + + def test_mysql_query(self): + with self.m.cursor() as curs: + ajaj = 1 + curs.execute('SELECT %(ajaj)s AS ajaj', locals()) + row = curs.fetchone_dict() + self.assertEqual(row.ajaj, ajaj) + + +if __name__ == '__main__': + unittest.main() + diff -r b82c7c2fb5af -r 08e4dfe1b0cb tests/testpgmanager.py --- a/tests/testpgmanager.py Sat Sep 29 12:08:47 2012 +0200 +++ b/tests/testpgmanager.py Sat Sep 29 13:53:54 2012 +0200 @@ -1,4 +1,4 @@ -#!/usr/bin/env python3.2 +#!/usr/bin/env python3 from tests.config import Config from pgtoolkit import pgmanager @@ -9,10 +9,11 @@ class TestSuite(unittest.TestCase): def setUp(self): - cfg = Config('tests.conf') + cfg = Config('pgtoolkit.conf') + test_db_conn_params = cfg['databases']['test'] self.m = pgmanager.get_instance() - self.m.create_conn(**cfg) - self.m.create_conn('autocommit', isolation_level='autocommit', **cfg) + self.m.create_conn(dsn=test_db_conn_params) + self.m.create_conn('autocommit', isolation_level='autocommit', dsn=test_db_conn_params) def tearDown(self): self.m.destroy_conn() @@ -48,6 +49,7 @@ self.assertTrue(curs.rowcount == 1) self.m.put_conn(conn) + if __name__ == '__main__': unittest.main() diff -r b82c7c2fb5af -r 08e4dfe1b0cb tests/tests.conf.example --- a/tests/tests.conf.example Sat Sep 29 12:08:47 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,4 +0,0 @@ -hostaddr='127.0.0.1' -dbname='test' -user='test' -password='pass' diff -r b82c7c2fb5af -r 08e4dfe1b0cb tests/tests.sql --- a/tests/tests.sql Sat Sep 29 12:08:47 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,6 +0,0 @@ -CREATE TABLE test -( - id serial NOT NULL, - name varchar, - PRIMARY KEY (id) -);