File: test_database.py

package info (click to toggle)
python-sqlalchemy-utils 0.30.12-2~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 1,056 kB
  • sloc: python: 10,350; makefile: 160
file content (81 lines) | stat: -rw-r--r-- 2,222 bytes parent folder | download | duplicates (2)
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
import os

import sqlalchemy as sa
from flexmock import flexmock
from pytest import mark

from sqlalchemy_utils import create_database, database_exists, drop_database
from tests import TestCase

pymysql = None
try:
    import pymysql  # noqa
except ImportError:
    pass


class DatabaseTest(TestCase):
    def test_create_and_drop(self):
        assert not database_exists(self.url)
        create_database(self.url)
        assert database_exists(self.url)
        drop_database(self.url)
        assert not database_exists(self.url)


class TestDatabaseSQLite(DatabaseTest):
    url = 'sqlite:///sqlalchemy_utils.db'

    def setup(self):
        if os.path.exists('sqlalchemy_utils.db'):
            os.remove('sqlalchemy_utils.db')

    def test_exists_memory(self):
        assert database_exists('sqlite:///:memory:')


@mark.skipif('pymysql is None')
class TestDatabaseMySQL(DatabaseTest):
    url = 'mysql+pymysql://travis@localhost/db_test_sqlalchemy_util'


@mark.skipif('pymysql is None')
class TestDatabaseMySQLWithQuotedName(DatabaseTest):
    url = 'mysql+pymysql://travis@localhost/db_test_sqlalchemy-util'


class TestDatabasePostgresWithQuotedName(DatabaseTest):
    url = 'postgres://postgres@localhost/db_test_sqlalchemy-util'

    def test_template(self):
        (
            flexmock(sa.engine.Engine)
            .should_receive('execute')
            .with_args(
                '''CREATE DATABASE "db_test_sqlalchemy-util"'''
                " ENCODING 'utf8' "
                'TEMPLATE "my-template"'
            )
        )
        create_database(
            'postgres://postgres@localhost/db_test_sqlalchemy-util',
            template='my-template'
        )


class TestDatabasePostgres(DatabaseTest):
    url = 'postgres://postgres@localhost/db_test_sqlalchemy_util'

    def test_template(self):
        (
            flexmock(sa.engine.Engine)
            .should_receive('execute')
            .with_args(
                "CREATE DATABASE db_test_sqlalchemy_util ENCODING 'utf8' "
                "TEMPLATE my_template"
            )
        )
        create_database(
            'postgres://postgres@localhost/db_test_sqlalchemy_util',
            template='my_template'
        )