django.db.utils.ProgrammingError: (1146, "Table 'test_conect.projects' doesn't exist") while testing

72 Views Asked by At

I'm trying to test models for the first time in my django app,but when i run python manage.py test I get the following error django.db.utils.ProgrammingError: (1146, "Table 'test_conect.projects' doesn't exist") Here is the model I want to test

class Files(models.Model):
    file_id = models.IntegerField(db_column='File_iD', primary_key=True)  # Field name made lowercase.
    file_name = models.CharField(db_column='File_Name', max_length=45)  # Field name made lowercase.
    file_size_gb = models.FloatField(db_column='File_Size_Gb')  # Field name made lowercase.
    md5 = models.CharField(db_column='MD5', max_length=40)  # Field name made lowercase.
    sample = models.ForeignKey('Samples', models.DO_NOTHING, db_column='Sample_id')  # Field name made lowercase.
    url = models.CharField(db_column='Url', max_length=200)  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'files'
        verbose_name_plural = "files"
    def sample_value(self):
        return self.sample.sample_id if self.sample else None

    sample_value.short_description = 'Samples'

and here is the code I'm running for the test

from django.test import TestCase
from conect.models import Files, Samples, Patients, Projects

class FilesModelTests(TestCase):
    def setUp(self):
        # Create a project instance
        # Correct way to create an instance of the Projects model
        project_instance = Projects.objects.create(project_id=10, project_name='test Project Name', project_description='test Project Description')
        # Create a patient instance
        patient_instance = Patients.objects.create(patient_id='Patient123')

        # Create a sample instance
        sample_instance = Samples.objects.create(sample_id='Sample123', patient=patient_instance)

        # Create a test instance of the Files model
        Files.objects.create(
            file_id=1,
            file_name='Test File',
            file_size_gb=2.5,
            md5='abc123',
            sample=sample_instance,
            url='https://example.com/testfile'
        )

    def test_sample_value_method(self):
        # Retrieve the test instance from the database
        test_file = Files.objects.get(file_id=1)

        # Test the sample_value method
        self.assertEqual(test_file.sample_value(), 'Patient123')  # Adjust to the actual field you want to display

    def test_model_fields(self):
        # Retrieve the test instance from the database
        test_file = Files.objects.get(file_id=1)

        # Test that the model fields have the expected values
        self.assertEqual(test_file.file_name, 'Test File')
        self.assertEqual(test_file.file_size_gb, 2.5)
        self.assertEqual(test_file.md5, 'abc123')
        self.assertEqual(test_file.sample.sample_id, 'Sample123')
        self.assertEqual(test_file.url, 'https://example.com/testfile')

    def test_verbose_name_plural(self):
        # Test the verbose name plural of the model
        self.assertEqual(Files._meta.verbose_name_plural, "files")

Even after executing python manage.py makemigrations then migrate commands the test database does contain only the tables auto created by django when we migrate( auth_group, auth_group_permissions, etc ) I hope you get the picture. One more thing, my settings for the databases part is this :

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'conect',
        'USER': env.str('conectuser'),
        'PASSWORD': env.str('mysql_password'),
        'HOST': 'localhost',
        'PORT': '',
        'OPTIONS': {
            'charset': 'utf8mb4',
            'init_command': "SET collation_connection = utf8mb4_unicode_ci",
        },
    'test': {
        'ENGINE': 'django.db.backends.mysql',  # Use the MySQL engine for testing
        'NAME': 'test_conect',  # Specify the test database name
        'USER': 'testuser',  # Use the same user as the production database
        'PASSWORD': 'your_test_db_password',
        'HOST': 'localhost',
        'PORT': '3306',  # MySQL default port
        'OPTIONS': {
            'charset': 'utf8mb4',
            'init_command': "SET collation_connection = utf8mb4_unicode_ci",
        },
    },
}
}

I tried multiple solutions i found online which where not the answer to my error , I hope someone here has the answer. For context, I'm writing these tests in order to try github actions for my app and I'm hesitating on using docker for mysql and django , coz that would take time to learn docker and docker compose and all the things that come with integration later.

0

There are 0 best solutions below