Make models defined in unittests migrable
- Dominant language
- Python
- Stars
- 6
- Forks
- 4
- PR merge metrics
- No merged PRs in 30d
Description
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:
```python
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)
Contributor guide
Research direction
Start in the test conftest at the point where the migrations command runs, and trace how test_app models are currently discovered and registered. Implement discovery and registration for models defined in tests with the test_app label, then verify that those models are included in the migrated test database and that existing test behavior remains intact.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- django, python
- Domain
- databases, testing
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100