1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
from django.db import migrations
from django.db.migrations.autodetector import MigrationAutodetector
from django.db.migrations.state import ProjectState
from psqlextra.fields import HStoreField
def _make_project_state(model_states):
"""Shortcut to make :see:ProjectState from a list of predefined models."""
project_state = ProjectState()
for model_state in model_states:
project_state.add_model(model_state.clone())
return project_state
def _detect_changes(before_states, after_states):
"""Uses the migration autodetector to detect changes in the specified
project states."""
return MigrationAutodetector(
_make_project_state(before_states), _make_project_state(after_states)
)._detect_changes()
def _assert_autodetector(changes, expected):
"""Asserts whether the results of the auto detector are as expected."""
assert "tests" in changes
assert len("tests") > 0
operations = changes["tests"][0].operations
for i, expected_operation in enumerate(expected):
real_operation = operations[i]
_, _, real_args, real_kwargs = real_operation.field.deconstruct()
(
_,
_,
expected_args,
expected_kwargs,
) = expected_operation.field.deconstruct()
assert real_args == expected_args
assert real_kwargs == expected_kwargs
def test_hstore_autodetect_uniqueness():
"""Tests whether changes in the `uniqueness` option are properly detected
by the auto detector."""
before = [
migrations.state.ModelState(
"tests", "Model1", [("title", HStoreField())]
)
]
after = [
migrations.state.ModelState(
"tests", "Model1", [("title", HStoreField(uniqueness=["en"]))]
)
]
changes = _detect_changes(before, after)
_assert_autodetector(
changes,
[
migrations.AlterField(
"Model1", "title", HStoreField(uniqueness=["en"])
)
],
)
def test_hstore_autodetect_required():
"""Tests whether changes in the `required` option are properly detected by
the auto detector."""
before = [
migrations.state.ModelState(
"tests", "Model1", [("title", HStoreField())]
)
]
after = [
migrations.state.ModelState(
"tests", "Model1", [("title", HStoreField(required=["en"]))]
)
]
changes = _detect_changes(before, after)
_assert_autodetector(
changes,
[
migrations.AlterField(
"Model1", "title", HStoreField(required=["en"])
)
],
)
|