Skip to content

Commit

Permalink
Add CLI test for generating migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
karenc committed Apr 24, 2016
1 parent 000b466 commit 6ffaee0
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions dbmigrator/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
# See LICENCE.txt for details.
# ###

import os
import shutil
import sys
import tempfile
import unittest
try:
from unittest import mock
Expand Down Expand Up @@ -246,3 +249,78 @@ def test_mark_as_false_already_false(self):
stdout = out.getvalue()
self.assertIn('20160228202637_add_table False', stdout)
self.assertIn('20160228212456_cool_stuff False', stdout)


class GenerateTestCase(BaseTestCase):
@mock.patch('dbmigrator.utils.timestamp')
def test(self, timestamp):
timestamp.return_value = '20160423231932'
filename = '{}_a_new_migration.py'.format(timestamp())
expected_path = os.path.join(
testing.test_migrations_directories[0], filename)

def cleanup():
if os.path.exists(expected_path):
os.remove(expected_path)
self.addCleanup(cleanup)

testing.install_test_packages()

with testing.captured_output() as (out, err):
self.target(['--context', 'package-a', 'generate',
'a_new_migration'])

stdout = out.getvalue()
stderr = err.getvalue()

self.assertEqual(
'Generated migration script "dbmigrator/tests/data/package-a/'
'package_a/migrations/{}"\n'.format(filename),
stdout)
self.assertEqual('', stderr)

self.assertTrue(os.path.exists(expected_path))

with open(expected_path, 'r') as f:
content = f.read()

self.assertIn('# -*- coding: utf-8 -*-', content)
self.assertIn('def up(cursor):', content)
self.assertIn('def down(cursor):', content)

def test_no_migrations_directory(self):
with self.assertRaises(Exception) as cm:
self.target(['generate', 'a_new_migration'])

self.assertEqual('migrations directory undefined',
str(cm.exception))

def test_multiple_migrations_directory(self):
tmp_dir = tempfile.gettempdir()
tmp_dir2 = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, tmp_dir2)
with self.assertRaises(Exception) as cm:
self.target(['--migrations-directory', tmp_dir,
'--migrations-directory', tmp_dir2,
'generate', 'a_new_migration'])

self.assertEqual('more than one migrations directory specified',
str(cm.exception))

@mock.patch('dbmigrator.utils.timestamp')
def test_migrations_directory_does_not_exist(self, timestamp):
timestamp.return_value = '20160423231932'
filename = '{}_a_new_migration.py'.format(timestamp())
tmp_dir = tempfile.gettempdir()
directory = '{}/dbmigrator-tests/m'.format(tmp_dir)
expected_path = os.path.join(directory, filename)

def cleanup():
if os.path.exists(expected_path):
os.remove(expected_path)
self.addCleanup(cleanup)

self.target(['--migrations-directory', directory,
'generate', 'a_new_migration'])

self.assertTrue(os.path.exists(expected_path))

0 comments on commit 6ffaee0

Please sign in to comment.