Permalink
Browse files

some tests improvements

  • Loading branch information...
1 parent 2cd5666 commit d0d7d15971f6db01df48be0fcfb8e9547650f84b @wandenberg wandenberg committed Sep 7, 2012
Showing with 121 additions and 96 deletions.
  1. +9 −2 tests/__init__.py
  2. +6 −3 tests/cli_test.py
  3. +13 −14 tests/config_test.py
  4. +15 −7 tests/core_test.py
  5. +3 −2 tests/helpers_test.py
  6. +3 −3 tests/log_test.py
  7. +5 −5 tests/main_test.py
  8. +12 −13 tests/mssql_test.py
  9. +12 −13 tests/mysql_test.py
  10. +17 −18 tests/oracle_test.py
  11. +26 −16 tests/run_test.py
View
@@ -3,6 +3,8 @@
import unittest
import codecs
from simple_db_migrate.config import *
+from StringIO import StringIO
+from mock import patch
def create_file(file_name, content=None, encoding='utf-8'):
f = codecs.open(file_name, 'w', encoding)
@@ -17,8 +19,8 @@ def create_migration_file(file_name, sql_up='', sql_down=''):
def delete_files(pattern):
filelist=glob.glob(pattern)
- for file in filelist:
- os.remove(file)
+ for _file in filelist:
+ os.remove(_file)
def create_config(host='localhost', username='root', password='', database='migration_example', migrations_dir='.', utc_timestamp=False, script_encoding='utf-8'):
config_file = '''
@@ -34,7 +36,12 @@ def create_config(host='localhost', username='root', password='', database='migr
return FileConfig('test_config_file.conf')
class BaseTest(unittest.TestCase):
+ def setUp(self):
+ self.stdout_mock = patch('sys.stdout', new_callable=StringIO)
+ self.stdout_mock.start()
+
def tearDown(self):
+ self.stdout_mock.stop()
delete_files('*.log')
delete_files('*test_migration.migration')
delete_files('migrations/*test_migration.migration')
View
@@ -1,7 +1,7 @@
import unittest
from mock import patch
from StringIO import StringIO
-from simple_db_migrate.cli import *
+from simple_db_migrate.cli import CLI
class CLITest(unittest.TestCase):
@@ -30,12 +30,15 @@ def test_it_should_define_colors_values_when_asked_to_show_collors(self):
self.assertEqual("\033[31m", CLI.color["RED"])
self.assertEqual("\033[0m", CLI.color["END"])
- def test_it_should_exit_with_help_options(self):
+ @patch('sys.stdout', new_callable=StringIO)
+ def test_it_should_exit_with_help_options(self, stdout_mock):
try:
CLI.parse(["-h"])
except SystemExit, e:
self.assertEqual(0, e.code)
+ self.assertTrue(stdout_mock.getvalue().find("Displays simple-db-migrate's version and exit") > 0)
+ stdout_mock.buf = ''
try:
CLI.parse(["--help"])
except SystemExit, e:
@@ -80,7 +83,7 @@ def test_it_should_not_has_a_default_value_for_schema_version(self):
def test_it_should_accept_schema_version_options(self):
self.assertEqual("schema_version_value", CLI.parse(["-m", "schema_version_value"])[0].schema_version)
- self.assertEqual("schema_version_value", CLI.parse(["--config", "schema_version_value"])[0].config_file)
+ self.assertEqual("schema_version_value", CLI.parse(["--migration", "schema_version_value"])[0].schema_version)
def test_it_should_not_has_a_default_value_for_new_migration(self):
self.assertEqual(None, CLI.parse([])[0].new_migration)
View
@@ -1,7 +1,6 @@
-import codecs
+import os
import unittest
-from mock import patch, MagicMock, NonCallableMagicMock
-from simple_db_migrate.config import *
+from simple_db_migrate.config import Config, FileConfig
class ConfigTest(unittest.TestCase):
@@ -55,25 +54,25 @@ def test_it_should_parse_migrations_dir_with_relative_dirs_using_config_dir_para
def test_it_should_return_value_from_a_dict(self):
- dict = {"some_key": "some_value"}
- self.assertEqual("some_value", Config._get(dict, "some_key"))
+ _dict = {"some_key": "some_value"}
+ self.assertEqual("some_value", Config._get(_dict, "some_key"))
def test_it_should_return_default_value_for_an_inexistent_dict_value(self):
- dict = {"some_key": "some_value"}
- self.assertEqual("default_value", Config._get(dict, "ANOTHER_KEY", "default_value"))
+ _dict = {"some_key": "some_value"}
+ self.assertEqual("default_value", Config._get(_dict, "ANOTHER_KEY", "default_value"))
- def test_it_should_raise_exception_for_an_inexistent_config_value_without_specify_a_default_value(self):
- dict = {"some_key": "some_value"}
+ def test_it_should_raise_exception_for_an_inexistent_dict_value_without_specify_a_default_value(self):
+ _dict = {"some_key": "some_value"}
try:
- Config._get(dict, "ANOTHER_KEY")
+ Config._get(_dict, "ANOTHER_KEY")
except Exception, e:
self.assertEqual("invalid key ('ANOTHER_KEY')", str(e))
def test_it_should_accept_non_empty_stringand_false_as_default_value(self):
- dict = {"some_key": "some_value"}
- self.assertEqual(None, Config._get(dict,"ANOTHER_KEY", None))
- self.assertEqual("", Config._get(dict,"ANOTHER_KEY", ""))
- self.assertEqual(False, Config._get(dict,"ANOTHER_KEY", False))
+ _dict = {"some_key": "some_value"}
+ self.assertEqual(None, Config._get(_dict,"ANOTHER_KEY", None))
+ self.assertEqual("", Config._get(_dict,"ANOTHER_KEY", ""))
+ self.assertEqual(False, Config._get(_dict,"ANOTHER_KEY", False))
def test_it_should_save_config_values(self):
config = Config()
View
@@ -1,15 +1,15 @@
# coding: utf-8
import os
-import time
import unittest
from mock import patch, Mock
-from simple_db_migrate.config import *
-from simple_db_migrate.core import *
+from simple_db_migrate.core import Migration
+from simple_db_migrate.core import SimpleDBMigrate
from tests import BaseTest, create_file, create_migration_file, delete_files, create_config
class SimpleDBMigrateTest(BaseTest):
def setUp(self):
+ super(SimpleDBMigrateTest, self).setUp()
if not os.path.exists(os.path.abspath('migrations')):
os.mkdir(os.path.abspath('migrations'))
self.config = create_config(migrations_dir='.:migrations')
@@ -115,7 +115,7 @@ def test_it_should_check_if_migration_version_exists(self):
self.assertFalse(db_migrate.check_if_version_exists('19000101000000'))
@patch('simple_db_migrate.core.SimpleDBMigrate.get_all_migration_versions', return_value=[])
- def test_it_should_use_get_all_migrations_versions_method_to_get_all_migration_versions_up_to_a_version(self, get_all_migration_versions_mock):
+ def test_it_should_use_get_all_migrations_versions_method_to_check_if_migration_version_exists(self, get_all_migration_versions_mock):
db_migrate = SimpleDBMigrate(self.config)
db_migrate.check_if_version_exists('20090214115100')
self.assertEqual(1, get_all_migration_versions_mock.call_count)
@@ -157,6 +157,8 @@ def setUp(self):
create_migration_file('20090727104700_test_migration.migration', sql_up='xxx', sql_down='yyy')
create_migration_file('20090727141400_test_migration.migration', sql_up='xxx', sql_down='yyy')
create_migration_file('20090727141503_test_migration.migration', sql_up='xxx', sql_down='yyy')
+ create_migration_file('20090727141505_01_test_migration.migration', sql_up='xxx', sql_down='yyy')
+ create_migration_file('20090727141505_02_test_migration.migration', sql_up='xxx', sql_down='yyy')
create_migration_file('20090727113900_empty_sql_up_test_migration.migration', sql_up='', sql_down='zzz')
create_migration_file('20090727113900_empty_sql_down_test_migration.migration', sql_up='zzz', sql_down='')
create_file('20090727114700_empty_file_test_migration.migration')
@@ -273,25 +275,31 @@ def test_it_should_raise_exception_when_migration_file_is_empty(self):
def test_it_should_raise_exception_when_migration_file_do_not_have_sql_up_constant(self):
self.assertRaisesWithMessage(Exception, "migration file is incorrect; it does not define 'SQL_UP' or 'SQL_DOWN' (%s)" % os.path.abspath('20090727114700_without_sql_up_test_migration.migration'), Migration, '20090727114700_without_sql_up_test_migration.migration')
- def test_it_should_raise_exception_when_migration_file_do_not_have_sql_up_constant(self):
+ def test_it_should_raise_exception_when_migration_file_do_not_have_sql_down_constant(self):
self.assertRaisesWithMessage(Exception, "migration file is incorrect; it does not define 'SQL_UP' or 'SQL_DOWN' (%s)" % os.path.abspath('20090727114700_without_sql_down_test_migration.migration'), Migration, '20090727114700_without_sql_down_test_migration.migration')
def test_it_should_compare_to_migration_versions_and_tell_which_is_newer(self):
m1 = Migration('20090727104700_test_migration.migration')
m2 = Migration('20090727141400_test_migration.migration')
m3 = Migration('20090727141503_test_migration.migration')
+ m4 = Migration('20090727141505_01_test_migration.migration')
+ m5 = Migration('20090727141505_02_test_migration.migration')
self.assertEqual(-1, m1.compare_to(m2))
self.assertEqual(-1, m2.compare_to(m3))
self.assertEqual(-1, m1.compare_to(m3))
+ self.assertEqual(-1, m4.compare_to(m5))
self.assertEqual(1, m2.compare_to(m1))
self.assertEqual(1, m3.compare_to(m2))
self.assertEqual(1, m3.compare_to(m1))
+ self.assertEqual(1, m5.compare_to(m4))
self.assertEqual(0, m1.compare_to(m1))
self.assertEqual(0, m2.compare_to(m2))
self.assertEqual(0, m3.compare_to(m3))
+ self.assertEqual(0, m4.compare_to(m4))
+ self.assertEqual(0, m5.compare_to(m5))
def test_it_should_raise_exception_when_file_does_not_exist(self):
try:
@@ -301,8 +309,8 @@ def test_it_should_raise_exception_when_file_does_not_exist(self):
@patch('simple_db_migrate.core.Migration.is_file_name_valid', return_value=False)
def test_it_should_raise_exception_when_file_name_is_invalid(self, is_file_name_valid_mock):
- self.assertRaisesWithMessage(Exception, 'invalid migration file name (simple-db-migrate.conf)', Migration, 'simple-db-migrate.conf')
- is_file_name_valid_mock.assert_called_with('simple-db-migrate.conf')
+ self.assertRaisesWithMessage(Exception, 'invalid migration file name (simple-db-migrate.conf)', Migration, 'simple-db-migrate.conf')
+ is_file_name_valid_mock.assert_called_with('simple-db-migrate.conf')
def test_it_should_validate_if_filename_has_only_alphanumeric_chars_and_migration_extension(self):
self.assertTrue(Migration.is_file_name_valid('20090214120600_valid_migration_file_name.migration'))
View
@@ -1,6 +1,7 @@
import unittest
-
-from simple_db_migrate.helpers import *
+import os
+import sys
+from simple_db_migrate.helpers import Lists, Utils
class ListsTest(unittest.TestCase):
View
@@ -4,11 +4,11 @@
from datetime import datetime
from mock import patch, call, Mock
from simple_db_migrate.log import LOG
-from tests import BaseTest, create_file, create_migration_file, delete_files, create_config
+from tests import BaseTest, delete_files
class LogTest(BaseTest):
def tearDown(self):
- BaseTest.tearDown(self)
+ super(LogTest, self).tearDown()
delete_files('log_dir_test/path/subpath/*.log')
if os.path.exists('log_dir_test/path/subpath'):
os.rmdir('log_dir_test/path/subpath')
@@ -29,7 +29,7 @@ def test_it_should_not_raise_error_if_log_dir_is_not_specified(self):
@patch('os.makedirs', side_effect=os.makedirs)
def test_it_should_create_log_dir_if_does_not_exists(self, makedirs_mock):
- log = LOG('log_dir_test/path/subpath')
+ LOG('log_dir_test/path/subpath')
expected_calls = [
call('log_dir_test/path/subpath'),
call('log_dir_test/path', 511),
View
@@ -1,16 +1,16 @@
import unittest
import os
-import glob
import re
from StringIO import StringIO
from mock import patch, call, Mock
-from simple_db_migrate.main import *
-from simple_db_migrate.core import Migration, SimpleDBMigrate
-from simple_db_migrate.config import *
-from tests import BaseTest, create_file, create_migration_file, delete_files, create_config
+from simple_db_migrate.core import Migration
+from simple_db_migrate.main import Main
+from simple_db_migrate.config import Config
+from tests import BaseTest, create_migration_file
class MainTest(BaseTest):
def setUp(self):
+ super(MainTest, self).setUp()
self.initial_config = {
'database_host': 'localhost',
'database_name': 'test',
View
@@ -1,14 +1,14 @@
-import os
import unittest
-import simple_db_migrate
+import sys
+import simple_db_migrate.core
from mock import patch, Mock, MagicMock, call
-from simple_db_migrate.config import *
-from simple_db_migrate.mssql import *
-from tests import BaseTest, create_file, create_migration_file, delete_files, create_config
+from simple_db_migrate.mssql import MSSQL
+from tests import BaseTest
class MSSQLTest(BaseTest):
def setUp(self):
+ super(MSSQLTest, self).setUp()
self.execute_returns = {'select count(*) from __db_version__;': 0}
self.close_returns = {}
self.last_execute_command = '';
@@ -31,17 +31,16 @@ def setUp(self):
"__iter__":Mock(side_effect=self.iter_side_effect)})
self.db_driver_mock = Mock(**{"connect.return_value": self.db_mock})
+ @patch.dict('sys.modules', _mssql=MagicMock())
def test_it_should_use_mssql_as_driver(self):
- mssql_mock = MagicMock()
- with patch.dict('sys.modules', _mssql=mssql_mock):
- mssql = MSSQL(self.config_mock)
- self.assertNotEqual(0, mssql_mock.connect.call_count)
+ MSSQL(self.config_mock)
+ self.assertNotEqual(0, sys.modules['_mssql'].connect.call_count)
def test_it_should_stop_process_when_an_error_occur_during_connect_database(self):
self.db_driver_mock.connect.side_effect = Exception("error when connecting")
try:
- mssql = MSSQL(self.config_mock, self.db_driver_mock)
+ MSSQL(self.config_mock, self.db_driver_mock)
self.fail("it should not get here")
except Exception, e:
self.assertEqual("could not connect to database: error when connecting", str(e))
@@ -53,7 +52,7 @@ def test_it_should_stop_process_when_an_error_occur_during_connect_database(self
def test_it_should_create_database_and_version_table_on_init_if_not_exists(self):
- mssql = MSSQL(self.config_mock, self.db_driver_mock)
+ MSSQL(self.config_mock, self.db_driver_mock)
expected_query_calls = [
call("if not exists ( select 1 from sysdatabases where name = 'migration_test' ) create database migration_test;"),
@@ -72,7 +71,7 @@ def test_it_should_create_database_and_version_table_on_init_if_not_exists(self)
def test_it_should_drop_database_on_init_if_its_asked(self):
self.config_dict["drop_db_first"] = True
- mssql = MSSQL(self.config_mock, self.db_driver_mock)
+ MSSQL(self.config_mock, self.db_driver_mock)
expected_query_calls = [
call("if exists ( select 1 from sysdatabases where name = 'migration_test' ) drop database migration_test;"),
@@ -94,7 +93,7 @@ def test_it_should_stop_process_when_an_error_occur_during_drop_database(self):
self.db_mock.execute_non_query.side_effect = Exception("error when dropping")
try:
- mssql = MSSQL(self.config_mock, self.db_driver_mock)
+ MSSQL(self.config_mock, self.db_driver_mock)
self.fail("it should not get here")
except Exception, e:
self.assertEqual("can't drop database 'migration_test'; \nerror when dropping", str(e))
View
@@ -1,14 +1,14 @@
-import os
import unittest
-import simple_db_migrate
+import sys
+import simple_db_migrate.core
from mock import patch, Mock, MagicMock, call
-from simple_db_migrate.config import *
-from simple_db_migrate.mysql import *
-from tests import BaseTest, create_file, create_migration_file, delete_files, create_config
+from simple_db_migrate.mysql import MySQL
+from tests import BaseTest
class MySQLTest(BaseTest):
def setUp(self):
+ super(MySQLTest, self).setUp()
self.execute_returns = {}
self.fetchone_returns = {'select count(*) from __db_version__;': [0]}
self.close_returns = {}
@@ -30,17 +30,16 @@ def setUp(self):
self.db_mock = Mock(**{"cursor.return_value": self.cursor_mock})
self.db_driver_mock = Mock(**{"connect.return_value": self.db_mock})
+ @patch.dict('sys.modules', MySQLdb=MagicMock())
def test_it_should_use_mysqldb_as_driver(self):
- mysqldb_mock = MagicMock()
- with patch.dict('sys.modules', MySQLdb=mysqldb_mock):
- mysql = MySQL(self.config_mock)
- self.assertNotEqual(0, mysqldb_mock.connect.call_count)
+ MySQL(self.config_mock)
+ self.assertNotEqual(0, sys.modules['MySQLdb'].connect.call_count)
def test_it_should_stop_process_when_an_error_occur_during_connect_database(self):
self.db_driver_mock.connect.side_effect = Exception("error when connecting")
try:
- mysql = MySQL(self.config_mock, self.db_driver_mock)
+ MySQL(self.config_mock, self.db_driver_mock)
self.fail("it should not get here")
except Exception, e:
self.assertEqual("could not connect to database: error when connecting", str(e))
@@ -54,7 +53,7 @@ def test_it_should_stop_process_when_an_error_occur_during_connect_database(self
def test_it_should_create_database_and_version_table_on_init_if_not_exists(self):
- mysql = MySQL(self.config_mock, self.db_driver_mock)
+ MySQL(self.config_mock, self.db_driver_mock)
expected_query_calls = [
call('create database if not exists `migration_test`;')
@@ -75,7 +74,7 @@ def test_it_should_create_database_and_version_table_on_init_if_not_exists(self)
def test_it_should_drop_database_on_init_if_its_asked(self):
self.config_dict["drop_db_first"] = True
- mysql = MySQL(self.config_mock, self.db_driver_mock)
+ MySQL(self.config_mock, self.db_driver_mock)
expected_query_calls = [
call('set foreign_key_checks=0; drop database if exists `migration_test`;'),
@@ -99,7 +98,7 @@ def test_it_should_stop_process_when_an_error_occur_during_drop_database(self):
self.db_mock.query.side_effect = Exception("error when dropping")
try:
- mysql = MySQL(self.config_mock, self.db_driver_mock)
+ MySQL(self.config_mock, self.db_driver_mock)
self.fail("it should not get here")
except Exception, e:
self.assertEqual("can't drop database 'migration_test'; \nerror when dropping", str(e))
Oops, something went wrong.

0 comments on commit d0d7d15

Please sign in to comment.