Skip to content

Make models defined in unittests migrable #37

Description

@surister

Right now when testing we create models. In testing those models sometimes we test the generated sql of the model or fields and run operations like Model.objects.create or Models.objects.get to tests. This tests the entire django life-cycle of inserts/create/delete/updates and is specially useful when doing bulk-testing on many different types.

Right now for a model to be picked up by migrations and to be persisted in the test database it needs to be defined in tests.test_app.models, it would be amazing if we could define a model in a unit test like:

def test_generated_field():
    """
    Verify that a generated field works in CrateDB.
    """

    class SomeModel(CrateModel):
        f1 = fields.IntegerField()
        f2 = fields.IntegerField()
        f = fields.GeneratedField(
            expression=F("f1") / F("f2"), output_field=models.IntegerField()
        )
        ff = fields.GeneratedField(
            expression=F("f1") + 1, output_field=models.IntegerField(), db_persist=False
        )
        f_func = fields.GeneratedField(
            expression=UUID(), output_field=models.CharField(max_length=120)
        )

        class Meta:
            app_label = "test_app"

And it to be picked up by the test machinery and migrated.

We could accomplish this in the conftest, where we run the migrations command. With apps.register_model('test_app', SomeModel) we can register any model we want.

So the feature would be:

  1. Discover any Model defined in any tests that has the test_app label, we'd be normalizing two apps for our tests, _CRATE_TEST and test_app one to be completed ignored and the other ones for migrated models.
  2. Register said models.
  3. Think if we'd need to remove models after every test run, most likely not. (We don't even do that right now)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions