2016-03-19 11:15:09 +00:00
|
|
|
import unittest
|
2017-01-19 17:16:04 +00:00
|
|
|
from unittest import mock
|
2016-03-19 11:15:09 +00:00
|
|
|
|
2020-02-07 07:46:13 +00:00
|
|
|
from django.core.checks.database import check_database_backends
|
2020-03-03 07:30:37 +00:00
|
|
|
from django.db import connection, connections
|
2017-01-19 17:16:04 +00:00
|
|
|
from django.test import TestCase
|
2016-03-19 11:15:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DatabaseCheckTests(TestCase):
|
2018-07-12 04:12:20 +00:00
|
|
|
databases = {"default", "other"}
|
2017-02-10 13:19:32 +00:00
|
|
|
|
2020-02-07 07:46:13 +00:00
|
|
|
@mock.patch("django.db.backends.base.validation.BaseDatabaseValidation.check")
|
|
|
|
def test_database_checks_called(self, mocked_check):
|
|
|
|
check_database_backends()
|
|
|
|
self.assertFalse(mocked_check.called)
|
|
|
|
check_database_backends(databases=self.databases)
|
|
|
|
self.assertTrue(mocked_check.called)
|
2016-03-19 11:15:59 +00:00
|
|
|
|
|
|
|
@unittest.skipUnless(connection.vendor == "mysql", "Test only for MySQL")
|
|
|
|
def test_mysql_strict_mode(self):
|
2020-03-03 07:30:37 +00:00
|
|
|
def _clean_sql_mode():
|
|
|
|
for alias in self.databases:
|
|
|
|
if hasattr(connections[alias], "sql_mode"):
|
|
|
|
del connections[alias].sql_mode
|
|
|
|
|
|
|
|
_clean_sql_mode()
|
2016-03-19 11:15:59 +00:00
|
|
|
good_sql_modes = [
|
|
|
|
"STRICT_TRANS_TABLES,STRICT_ALL_TABLES",
|
|
|
|
"STRICT_TRANS_TABLES",
|
|
|
|
"STRICT_ALL_TABLES",
|
|
|
|
]
|
2020-06-20 17:17:22 +00:00
|
|
|
for sql_mode in good_sql_modes:
|
|
|
|
with mock.patch.object(
|
|
|
|
connection,
|
|
|
|
"mysql_server_data",
|
|
|
|
{"sql_mode": sql_mode},
|
2016-03-19 11:15:59 +00:00
|
|
|
):
|
2020-02-07 07:46:13 +00:00
|
|
|
self.assertEqual(check_database_backends(databases=self.databases), [])
|
2020-03-03 07:30:37 +00:00
|
|
|
_clean_sql_mode()
|
2016-03-19 11:15:59 +00:00
|
|
|
|
|
|
|
bad_sql_modes = ["", "WHATEVER"]
|
2020-06-20 17:17:22 +00:00
|
|
|
for sql_mode in bad_sql_modes:
|
|
|
|
mocker_default = mock.patch.object(
|
|
|
|
connection,
|
|
|
|
"mysql_server_data",
|
|
|
|
{"sql_mode": sql_mode},
|
|
|
|
)
|
|
|
|
mocker_other = mock.patch.object(
|
|
|
|
connections["other"],
|
|
|
|
"mysql_server_data",
|
|
|
|
{"sql_mode": sql_mode},
|
|
|
|
)
|
|
|
|
with mocker_default, mocker_other:
|
2016-03-19 11:15:59 +00:00
|
|
|
# One warning for each database alias
|
2020-02-07 07:46:13 +00:00
|
|
|
result = check_database_backends(databases=self.databases)
|
2016-03-19 11:15:59 +00:00
|
|
|
self.assertEqual(len(result), 2)
|
|
|
|
self.assertEqual([r.id for r in result], ["mysql.W002", "mysql.W002"])
|
2020-03-03 07:30:37 +00:00
|
|
|
_clean_sql_mode()
|