How can I make citext extension work on Django tests?
Asked Answered
S

0

6

I'm running tests against a model having a ArrayField(CICharField()) field and they return a string instead of a list. I use pytest-django to run the tests.

I know that returning a string instead of a list was a known problem in the past, but it's fixed in my current Django version (2.1.13).

When I reproduce the test from the shell, everything works ok. Things seems to be related to the tests environment, specially how test suite creates the database. As far as I understand, citext extension is not installed properly during the tests.

If I run the test battery twice, keeping the database (using --reuse-db), the second (and next other) run work perfectly well. That makes me think that the extension is installed "too late".

Probably not very useful, but this is the result I'm getting:

self = <tests.test_ciarrayfield.CIArrayFieldTestCase testMethod=test_save_load>

    def test_save_load(self):
        print(settings.INSTALLED_APPS)
        instance = ProductFactory(sku="SKU1", product_types=[])
        loaded = Product.objects.get()
>       self.assertEqual(instance.product_types, loaded.product_types)
E       AssertionError: [] != '{}'

src/tests/test_ciarrayfield.py:14: AssertionError

And part of my model:

class Product(models.Model):
    product_types = ArrayField(
        CICharField(max_length=750),
        default=list, blank=True)

The obvious expected result is to get a list on that field after getting the instance from the database, and not a string. As I said, this happens to me on the shell but not on the tests (unless I keep the test database and run the tests twice).

Simasimah answered 24/10, 2019 at 16:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.