File: test_parse_mysql_cnf.py

package info (click to toggle)
python-django-extensions 4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,812 kB
  • sloc: python: 18,601; javascript: 7,354; makefile: 108; xml: 17
file content (76 lines) | stat: -rw-r--r-- 1,911 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
# -*- coding: utf-8 -*-
import os
import shutil
from tempfile import mkdtemp

from django.test import TestCase

from django_extensions.management.mysql import parse_mysql_cnf


class ParseMysqlCnfTests(TestCase):
    """Tests for parse_mysql_cnf."""

    @classmethod
    def setUpClass(cls):
        cls.tmpdir = mkdtemp()

    @classmethod
    def tearDownClass(cls):
        shutil.rmtree(cls.tmpdir)

    def test_should_return_empty_strings_if_read_default_file_option_is_missing(self):
        dbinfo = {}

        result = parse_mysql_cnf(dbinfo)

        self.assertEqual(result, ("", "", "", "", ""))

    def test_should_parse_my_cnf_and_retun_connection_settings(self):
        my_cnf_path = os.path.join(self.tmpdir, "my.cnf")
        with open(my_cnf_path, "w") as f:
            f.write(
                """[client]
database = test_name
user = test_user
password = test_password
host = localhost
port = 3306
socket = /var/lib/mysqld/mysql.sock
"""
            )

        dbinfo = {
            "ENGINE": "django.db.backends.mysql",
            "OPTIONS": {
                "read_default_file": my_cnf_path,
            },
        }

        result = parse_mysql_cnf(dbinfo)

        self.assertEqual(
            result,
            (
                "test_user",
                "test_password",
                "test_name",
                "/var/lib/mysqld/mysql.sock",
                "3306",
            ),
        )

    def test_should_return_empty_strings_if_NoSectionError_exception_occured(self):
        my_cnf_path = os.path.join(self.tmpdir, "my.cnf")
        with open(my_cnf_path, "w") as f:
            f.write("")

        dbinfo = {
            "ENGINE": "django.db.backends.mysql",
            "OPTIONS": {
                "read_default_file": my_cnf_path,
            },
        }
        result = parse_mysql_cnf(dbinfo)

        self.assertEqual(result, ("", "", "", "", ""))