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, ("", "", "", "", ""))
|