1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
import os
import shutil
import unittest
import subprocess
import shlex
def run_cmd(app, cmd):
"""Run a command and return a tuple with (stdout, stderr, exit_code)"""
os.environ['FLASK_APP'] = app
process = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(stdout, stderr) = process.communicate()
print('\n$ ' + cmd)
print(stdout.decode('utf-8'))
print(stderr.decode('utf-8'))
return stdout, stderr, process.wait()
class TestMigrate(unittest.TestCase):
def setUp(self):
os.chdir(os.path.split(os.path.abspath(__file__))[0])
try:
os.remove('app.db')
except OSError:
pass
try:
shutil.rmtree('migrations')
except OSError:
pass
try:
shutil.rmtree('temp_folder')
except OSError:
pass
def tearDown(self):
try:
os.remove('app.db')
except OSError:
pass
try:
shutil.rmtree('migrations')
except OSError:
pass
try:
shutil.rmtree('temp_folder')
except OSError:
pass
def atest_alembic_version(self):
from flask_migrate import alembic_version
self.assertEqual(len(alembic_version), 3)
for v in alembic_version:
self.assertTrue(isinstance(v, int))
def test_migrate_upgrade(self):
(o, e, s) = run_cmd('app.py', 'flask db init')
self.assertTrue(s == 0)
(o, e, s) = run_cmd('app.py', 'flask db check')
self.assertTrue(s != 0)
(o, e, s) = run_cmd('app.py', 'flask db migrate')
self.assertTrue(s == 0)
(o, e, s) = run_cmd('app.py', 'flask db check')
self.assertTrue(s != 0)
(o, e, s) = run_cmd('app.py', 'flask db upgrade')
self.assertTrue(s == 0)
(o, e, s) = run_cmd('app.py', 'flask db check')
self.assertTrue(s == 0)
from .app import app, db, User
with app.app_context():
db.engine.dispose()
db.session.add(User(name='test'))
db.session.commit()
def test_custom_directory(self):
(o, e, s) = run_cmd('app_custom_directory.py', 'flask db init')
self.assertTrue(s == 0)
(o, e, s) = run_cmd('app_custom_directory.py', 'flask db migrate')
self.assertTrue(s == 0)
(o, e, s) = run_cmd('app_custom_directory.py', 'flask db upgrade')
self.assertTrue(s == 0)
from .app_custom_directory import app, db, User
with app.app_context():
db.engine.dispose()
db.session.add(User(name='test'))
db.session.commit()
def test_custom_directory_path(self):
(o, e, s) = run_cmd('app_custom_directory_path.py', 'flask db init')
self.assertTrue(s == 0)
(o, e, s) = run_cmd('app_custom_directory_path.py', 'flask db migrate')
self.assertTrue(s == 0)
(o, e, s) = run_cmd('app_custom_directory_path.py', 'flask db upgrade')
self.assertTrue(s == 0)
from .app_custom_directory_path import app, db, User
with app.app_context():
db.engine.dispose()
db.session.add(User(name='test'))
db.session.commit()
def test_compare_type(self):
(o, e, s) = run_cmd('app_compare_type1.py', 'flask database init')
self.assertTrue(s == 0)
(o, e, s) = run_cmd('app_compare_type1.py', 'flask database migrate')
self.assertTrue(s == 0)
(o, e, s) = run_cmd('app_compare_type1.py', 'flask database upgrade')
self.assertTrue(s == 0)
(o, e, s) = run_cmd('app_compare_type2.py', 'flask database migrate')
self.assertTrue(s == 0)
self.assertTrue(b'Detected type change from VARCHAR(length=128) '
b'to String(length=10)' in e)
|