# Path: tests/testapp/models.py
# class SizeFieldModel(Model):
#     binary1 = SizedBinaryField(size_class=1)
#     binary2 = SizedBinaryField(size_class=2)
#     binary3 = SizedBinaryField(size_class=3)
#     binary4 = SizedBinaryField(size_class=4)
#     text1 = SizedTextField(size_class=1)
#     text2 = SizedTextField(size_class=2)
#     text3 = SizedTextField(size_class=3)
#     text4 = SizedTextField(size_class=4)
# 
# Path: tests/testapp/utils.py
# def column_type(table_name, column_name):
#     with connection.cursor() as cursor:
#         cursor.execute(
#             """SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS
#                WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = %s AND
#                      COLUMN_NAME = %s""",
#             (table_name, column_name),
#         )
#         return cursor.fetchone()[0]


import pytest
from django.core.management import call_command
from django.db import connection, models
from django.db.migrations.writer import MigrationWriter
from django.db.transaction import atomic
from django.db.utils import DataError
from django.test import TestCase, TransactionTestCase
from django.test.utils import isolate_apps, override_settings
from django_mysql.models import SizedBinaryField, SizedTextField
from django_mysql.test.utils import override_mysql_variables
from tests.testapp.models import SizeFieldModel
from tests.testapp.utils import column_type
    """
    Used below, has a different path for deconstruct()
    """


@isolate_apps("tests.testapp")
@forceDataError
class SizedBinaryFieldTests(TestCase):
    def test_binaryfield_checks(self):
        class Invalid(models.Model):
            field = SizedBinaryField(size_class=5)

        errors = Invalid.check()
        assert len(errors) == 1
        assert errors[0].id == "django_mysql.E007"
        assert errors[0].msg == "size_class must be 1, 2, 3, or 4"

    def test_binaryfield_default_length(self):
        # By default, SizedBinaryField should act like BinaryField
        field = SizedBinaryField()
        assert field.size_class == 4
        assert field.db_type(None) == "longblob"

    @atomic
    def test_binary_1_max_length(self):
        # Okay
        m = SizeFieldModel(binary1=bytes(1) * (2**8 - 1))
        m.save()

        # Bad - Data too long

# Path: tests/testapp/models.py
# class SizeFieldModel(Model):
#     binary1 = SizedBinaryField(size_class=1)
#     binary2 = SizedBinaryField(size_class=2)
#     binary3 = SizedBinaryField(size_class=3)
#     binary4 = SizedBinaryField(size_class=4)
#     text1 = SizedTextField(size_class=1)
#     text2 = SizedTextField(size_class=2)
#     text3 = SizedTextField(size_class=3)
#     text4 = SizedTextField(size_class=4)
# 
# Path: tests/testapp/utils.py
# def column_type(table_name, column_name):
#     with connection.cursor() as cursor:
#         cursor.execute(
#             """SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS
#                WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = %s AND
#                      COLUMN_NAME = %s""",
#             (table_name, column_name),
#         )
#         return cursor.fetchone()[0]


# Path: tests/testapp/test_size_fields.py
import pytest
from django.core.management import call_command
from django.db import connection, models
from django.db.migrations.writer import MigrationWriter
from django.db.transaction import atomic
from django.db.utils import DataError
from django.test import TestCase, TransactionTestCase
from django.test.utils import isolate_apps, override_settings
from django_mysql.models import SizedBinaryField, SizedTextField
from django_mysql.test.utils import override_mysql_variables
from tests.testapp.models import SizeFieldModel
from tests.testapp.utils import column_type

    """
    Used below, has a different path for deconstruct()
    """


@isolate_apps("tests.testapp")
@forceDataError
class SizedBinaryFieldTests(TestCase):
    def test_binaryfield_checks(self):
        class Invalid(models.Model):
            field = SizedBinaryField(size_class=5)

        errors = Invalid.check()
        assert len(errors) == 1
        assert errors[0].id == "django_mysql.E007"
        assert errors[0].msg == "size_class must be 1, 2, 3, or 4"

    def test_binaryfield_default_length(self):
        # By default, SizedBinaryField should act like BinaryField
        field = SizedBinaryField()
        assert field.size_class == 4
        assert field.db_type(None) == "longblob"

    @atomic
    def test_binary_1_max_length(self):
        # Okay
        m = SizeFieldModel(binary1=bytes(1) * (2**8 - 1))
        m.save()

        # Bad - Data too long

        m = SizeFieldModel(binary1=bytes(1) * (2**8))