Skip to content

Commit

Permalink
Drop and Create Tables done
Browse files Browse the repository at this point in the history
  • Loading branch information
heynemann committed Aug 30, 2010
1 parent 4501cc7 commit 4267beb
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 9 deletions.
20 changes: 19 additions & 1 deletion db_migrate/domain/db.py
Expand Up @@ -6,9 +6,11 @@
Must be db-agnostic as far as the supported Dbs go.
'''

from db_migrate import lib #sets the lib folder to be the first in PYTHONPATH
from sqlalchemy.schema import MetaData
from sqlalchemy.engine import create_engine

from db_migrate import lib #sets the lib folder to be the first in PYTHONPATH

class Db(object):
'''Class responsible for managing the communication with the database.'''

Expand All @@ -18,6 +20,7 @@ def __init__(self, config):
self.config = config
self.connection = None
self.engine = None
self.meta = MetaData()

self.connection_strings = {
'postgre' : 'postgresql://%(user)s:%(pass)s@%(host)s/%(db)s',
Expand Down Expand Up @@ -55,6 +58,7 @@ def connect(self, to_main_database=False):

self.engine = create_engine(conn_str)
self.connection = self.engine.connect()
self.meta.bind = self.engine

def close(self):
'''
Expand Down Expand Up @@ -88,6 +92,20 @@ def drop_database(self):

self.execute(sql, to_main_database=True)

def create_table(self, table):
'''Creates a table with the given fields.'''
if not self.connection:
self.connect()

table.create(checkfirst=True)

def drop_table(self, table):
'''Creates a table with the given fields.'''
if not self.connection:
self.connect()

table.drop(checkfirst=True)

@property
def connection_string(self):
'''Returns the proper connection string according to config.'''
Expand Down
4 changes: 3 additions & 1 deletion db_migrate/ui/console.py
Expand Up @@ -30,12 +30,14 @@ def run(self, arguments=None):
self.parse_arguments(arguments)

if not self.arguments:
action_key = "AutoMigrate"
action_key = "migrate"
else:
action_key = self.arguments[0]

action_to_execute = self.assert_and_get_action(action_key)

action_to_execute(arguments=self.arguments, options=self.options)

def assert_and_get_action(self, action_key):
if not action_key in console_actions.ACTIONS:
msg = 'The specified action of %s was not found. Available actions are: %s' % \
Expand Down
3 changes: 3 additions & 0 deletions db_migrate/ui/console_actions.py
Expand Up @@ -12,3 +12,6 @@ def function(fn):
return fn
return function

@action('migrate')
def auto_migrate(arguments, options):
pass
Empty file added tests/__init__.py
Empty file.
33 changes: 33 additions & 0 deletions tests/functional/domain/test_db.py
@@ -1,6 +1,8 @@
#!/usr/bin/env python
# encoding: utf-8

from sqlalchemy import *

from db_migrate.config import Config
from db_migrate.domain.db import Db

Expand Down Expand Up @@ -91,3 +93,34 @@ def test_can_create_and_drop_database():
results = new_db.execute('show databases', to_main_database=True)
dbs = [result[0] for result in results.fetchall()]
assert 'db_migrate_test_database_2' not in dbs

def test_create_and_drop_table():
db = Db(config=TEST_DB_CONFIG)

tbl = Table('test_table', db.meta,
Column('user_id', Integer, primary_key = True),
Column('user_name', String(16), nullable = False),
Column('email_address', String(60), key='email'),
Column('password', String(20), nullable = False)
)

db.create_table(tbl)

new_db = Db(config=TEST_DB_CONFIG)

results = new_db.execute('show tables')
tables = [result[0] for result in results.fetchall()]

assert 'test_table' in tables

#shouldn't do anything
db.create_table(tbl)

db.drop_table(tbl)

results = new_db.execute('show tables')
tables = [result[0] for result in results.fetchall()]

assert 'test_table' not in tables

db.drop_table(tbl)
28 changes: 28 additions & 0 deletions tests/unit/domain/test_db.py
Expand Up @@ -204,3 +204,31 @@ def test_query_scalar():
result = db.query_scalar('select 1 from dual')

assert result == 1

@with_fakes
@with_patched_object(Db, 'connect', Fake(callable=True))
def test_create_table():
clear_expectations()

config = fake_config()

db = Db(config)

tbl = Fake('table')
tbl.expects('create').with_args(checkfirst=True)

db.create_table(tbl)

@with_fakes
@with_patched_object(Db, 'connect', Fake(callable=True))
def test_drop_table():
clear_expectations()

config = fake_config()

db = Db(config)

tbl = Fake('table')
tbl.expects('drop').with_args(checkfirst=True)

db.drop_table(tbl)
24 changes: 17 additions & 7 deletions tests/unit/ui/test_console.py
Expand Up @@ -21,17 +21,27 @@ def test_console_run_calls_parse_arguments():
Console.parse_arguments.with_args(args)
Console.parse_arguments.next_call().with_args(sys.argv[1:])

Console.assert_and_get_action.with_args('test').returns_fake()
Console.assert_and_get_action.next_call().with_args('AutoMigrate').returns_fake()
called = Fake('called')
called.called = False
def do_something(arguments, options):
called.called = True

Console.assert_and_get_action.with_args('test').returns(do_something)
Console.assert_and_get_action.next_call().with_args('migrate').returns(do_something)

console = Console()

console.arguments = ['test']
console.run(arguments=args)

assert called.called
called.called = False

console.arguments = []
console.run()

assert called.called

@with_fakes
@with_patched_object(cons, 'OptionParser', Fake(callable=True))
def test_console_parse_arguments_creates_proper_optparse():
Expand All @@ -55,14 +65,14 @@ def test_console_parse_arguments_creates_proper_optparse():
def test_assert_and_get_action_exits_with_error_if_action_not_found():
clear_expectations()

msg = 'The specified action of AutoMigrate was not found. Available actions are: '
msg = 'The specified action of migrate was not found. Available actions are: '
cons.console_actions.ACTIONS = {}
cons.Actions.expects('error_and_exit').with_args(msg).raises(ValueError('Exit'))

console = Console()

try:
console.assert_and_get_action('AutoMigrate')
console.assert_and_get_action('migrate')
except ValueError, err:
assert str(err) == "Exit"
return
Expand All @@ -73,9 +83,9 @@ def test_assert_and_get_action_exits_with_error_if_action_not_found():
def test_assert_and_get_action_returns_action_callable():
clear_expectations()

msg = 'The specified action of AutoMigrate was not found. Available actions are: '
cons.console_actions.ACTIONS = {'AutoMigrate':'whatever'}
msg = 'The specified action of migrate was not found. Available actions are: '
cons.console_actions.ACTIONS = {'migrate':'whatever'}

console = Console()

assert console.assert_and_get_action('AutoMigrate') == "whatever"
assert console.assert_and_get_action('migrate') == "whatever"

0 comments on commit 4267beb

Please sign in to comment.