aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/sqlalchemy_migrate-0.7.2-py2.7.egg/migrate/tests/versioning/test_template.py
blob: 16bdc36bb0ce5097733b01d27a02e94c5eaba3ea (plain)
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
#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
import shutil

import migrate.versioning.templates
from migrate.versioning.template import *
from migrate.versioning import api

from migrate.tests import fixture


class TestTemplate(fixture.Pathed):
    def test_templates(self):
        """We can find the path to all repository templates"""
        path = str(Template())
        self.assert_(os.path.exists(path))

    def test_repository(self):
        """We can find the path to the default repository"""
        path = Template().get_repository()
        self.assert_(os.path.exists(path))

    def test_script(self):
        """We can find the path to the default migration script"""
        path = Template().get_script()
        self.assert_(os.path.exists(path))

    def test_custom_templates_and_themes(self):
        """Users can define their own templates with themes"""
        new_templates_dir = os.path.join(self.temp_usable_dir, 'templates')
        manage_tmpl_file = os.path.join(new_templates_dir, 'manage/custom.py_tmpl')
        repository_tmpl_file = os.path.join(new_templates_dir, 'repository/custom/README')
        script_tmpl_file = os.path.join(new_templates_dir, 'script/custom.py_tmpl')
        sql_script_tmpl_file = os.path.join(new_templates_dir, 'sql_script/custom.py_tmpl')

        MANAGE_CONTENTS = 'print "manage.py"'
        README_CONTENTS = 'MIGRATE README!'
        SCRIPT_FILE_CONTENTS = 'print "script.py"'
        new_repo_dest = self.tmp_repos()
        new_manage_dest = self.tmp_py()

        # make new templates dir
        shutil.copytree(migrate.versioning.templates.__path__[0], new_templates_dir)
        shutil.copytree(os.path.join(new_templates_dir, 'repository/default'),
            os.path.join(new_templates_dir, 'repository/custom'))

        # edit templates
        f = open(manage_tmpl_file, 'w').write(MANAGE_CONTENTS)
        f = open(repository_tmpl_file, 'w').write(README_CONTENTS)
        f = open(script_tmpl_file, 'w').write(SCRIPT_FILE_CONTENTS)
        f = open(sql_script_tmpl_file, 'w').write(SCRIPT_FILE_CONTENTS)

        # create repository, manage file and python script
        kw = {}
        kw['templates_path'] = new_templates_dir
        kw['templates_theme'] = 'custom'
        api.create(new_repo_dest, 'repo_name', **kw)
        api.script('test', new_repo_dest, **kw)
        api.script_sql('postgres', 'foo', new_repo_dest, **kw)
        api.manage(new_manage_dest, **kw)

        # assert changes
        self.assertEqual(open(new_manage_dest).read(), MANAGE_CONTENTS)
        self.assertEqual(open(os.path.join(new_repo_dest, 'manage.py')).read(), MANAGE_CONTENTS)
        self.assertEqual(open(os.path.join(new_repo_dest, 'README')).read(), README_CONTENTS)
        self.assertEqual(open(os.path.join(new_repo_dest, 'versions/001_test.py')).read(), SCRIPT_FILE_CONTENTS)
        self.assertEqual(open(os.path.join(new_repo_dest, 'versions/002_foo_postgres_downgrade.sql')).read(), SCRIPT_FILE_CONTENTS)
        self.assertEqual(open(os.path.join(new_repo_dest, 'versions/002_foo_postgres_upgrade.sql')).read(), SCRIPT_FILE_CONTENTS)