From 2c4e7deb9124a6ae21cba79dbc91662f898a175e Mon Sep 17 00:00:00 2001 From: Jonas Carson Date: Mon, 29 Jul 2024 14:16:07 -0700 Subject: [PATCH] Add test to verify charset, collation and engines match --- primed/tests/__init__.py | 0 primed/tests/test_db_config.py | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 primed/tests/__init__.py create mode 100644 primed/tests/test_db_config.py diff --git a/primed/tests/__init__.py b/primed/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/primed/tests/test_db_config.py b/primed/tests/test_db_config.py new file mode 100644 index 00000000..d547c587 --- /dev/null +++ b/primed/tests/test_db_config.py @@ -0,0 +1,40 @@ +# test_db_settings.py +import pytest +from django.db import connection +from django.conf import settings + +@pytest.mark.django_db +def test_character_set_and_storage_engine(): + with connection.cursor() as cursor: + if settings.DATABASES['default']['ENGINE'] == 'django.db.backends.mysql': + # For MariaDB/MySQL + cursor.execute("SHOW VARIABLES LIKE 'character_set_server';") + charset = cursor.fetchone() + print(f"Character set: {charset[1]}") + + cursor.execute("SHOW VARIABLES LIKE 'collation_server';") + collation = cursor.fetchone() + print(f"Collation: {collation[1]}") + + cursor.execute("SHOW TABLE STATUS;") + tables = cursor.fetchall() + for table in tables: + print(f"Table: {table[0]}, Engine: {table[1]}") + assert table[1] == 'InnoDB' + + assert charset[1] == 'utf8mb4' + assert collation[1] == 'utf8mb4_general_ci' + + elif settings.DATABASES['default']['ENGINE'] == 'django.db.backends.sqlite3': + # For SQLite + cursor.execute("PRAGMA encoding;") + charset = cursor.fetchone() + print(f"Character set: {charset[0]}") + + cursor.execute("SELECT name FROM sqlite_master WHERE type='table';") + tables = cursor.fetchall() + for table in tables: + print(f"Table: {table[0]}, Engine: SQLite") + + # SQLite always uses UTF-8 encoding + assert charset[0].lower() == 'utf-8'