Skip to content

Commit

Permalink
add a test to lock in index change order
Browse files Browse the repository at this point in the history
  • Loading branch information
mikealfare committed Apr 25, 2024
1 parent 3618093 commit f6cfed6
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions tests/unit/test_materialized_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from copy import deepcopy

from dbt.adapters.contracts.relation import RelationType
from dbt.adapters.relation_configs.config_change import RelationConfigChangeAction

from dbt.adapters.postgres.relation import PostgresRelation
from dbt.adapters.postgres.relation_configs import PostgresIndexConfig


def test_index_config_changes():
index_0_old = {
"column_names": {"column_0"},
"unique": True,
"method": "btree",
}
index_1_old = {
"column_names": {"column_1"},
"unique": True,
"method": "btree",
}
index_2_old = {
"column_names": {"column_2"},
"unique": True,
"method": "btree",
}
existing_indexes = frozenset(
PostgresIndexConfig.from_dict(index) for index in [index_0_old, index_1_old, index_2_old]
)

index_0_new = deepcopy(index_0_old)
index_2_new = deepcopy(index_2_old)
index_2_new.update(method="hash")
index_3_new = {
"column_names": {"column_3"},
"unique": True,
"method": "hash",
}
new_indexes = frozenset(
PostgresIndexConfig.from_dict(index) for index in [index_0_new, index_2_new, index_3_new]
)

relation = PostgresRelation.create(
database="my_database",
schema="my_schema",
identifier="my_materialized_view",
type=RelationType.MaterializedView,
)

index_changes = relation._get_index_config_changes(existing_indexes, new_indexes)

assert isinstance(index_changes, list)
assert len(index_changes) == len(["drop 1", "drop 2", "create 2", "create 3"])
assert index_changes[0].action == RelationConfigChangeAction.drop
assert index_changes[1].action == RelationConfigChangeAction.drop
assert index_changes[2].action == RelationConfigChangeAction.create
assert index_changes[3].action == RelationConfigChangeAction.create

0 comments on commit f6cfed6

Please sign in to comment.