| 1 |
from unittest import mock, skipUnless |
| 2 |
|
| 3 |
from django.db import connection |
| 4 |
from django.db.backends.mysql.features import DatabaseFeatures |
| 5 |
from django.test import TestCase |
| 6 |
|
| 7 |
|
| 8 |
@skipUnless(connection.vendor == "mysql", "MySQL tests") |
| 9 |
class TestFeatures(TestCase): |
| 10 |
def test_supports_transactions(self): |
| 11 |
""" |
| 12 |
All storage engines except MyISAM support transactions. |
| 13 |
""" |
| 14 |
del connection.features.supports_transactions |
| 15 |
with mock.patch( |
| 16 |
"django.db.connection.features._mysql_storage_engine", "InnoDB" |
| 17 |
): |
| 18 |
self.assertTrue(connection.features.supports_transactions) |
| 19 |
del connection.features.supports_transactions |
| 20 |
with mock.patch( |
| 21 |
"django.db.connection.features._mysql_storage_engine", "MyISAM" |
| 22 |
): |
| 23 |
self.assertFalse(connection.features.supports_transactions) |
| 24 |
del connection.features.supports_transactions |
| 25 |
|
| 26 |
def test_allows_auto_pk_0(self): |
| 27 |
with mock.MagicMock() as _connection: |
| 28 |
_connection.sql_mode = {"NO_AUTO_VALUE_ON_ZERO"} |
| 29 |
database_features = DatabaseFeatures(_connection) |
| 30 |
self.assertIs(database_features.allows_auto_pk_0, True) |
| 31 |
|
| 32 |
def test_allows_group_by_selected_pks(self): |
| 33 |
with mock.MagicMock() as _connection: |
| 34 |
_connection.mysql_is_mariadb = False |
| 35 |
database_features = DatabaseFeatures(_connection) |
| 36 |
self.assertIs(database_features.allows_group_by_selected_pks, True) |
| 37 |
|
| 38 |
with mock.MagicMock() as _connection: |
| 39 |
_connection.mysql_is_mariadb = False |
| 40 |
_connection.sql_mode = {} |
| 41 |
database_features = DatabaseFeatures(_connection) |
| 42 |
self.assertIs(database_features.allows_group_by_selected_pks, True) |
| 43 |
|
| 44 |
with mock.MagicMock() as _connection: |
| 45 |
_connection.mysql_is_mariadb = True |
| 46 |
_connection.sql_mode = {"ONLY_FULL_GROUP_BY"} |
| 47 |
database_features = DatabaseFeatures(_connection) |
| 48 |
self.assertIs(database_features.allows_group_by_selected_pks, False) |