File: test_materialized_view_model.py

package info (click to toggle)
python-django-postgres-extra 2.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,096 kB
  • sloc: python: 9,057; makefile: 17; sh: 7; sql: 1
file content (34 lines) | stat: -rw-r--r-- 1,074 bytes parent folder | download | duplicates (3)
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
from django.db import connection, models

from psqlextra.backend.schema import PostgresSchemaEditor

from .fake_model import define_fake_materialized_view_model, get_fake_model


def test_materialized_view_model_refresh():
    """Tests whether a materialized view can be refreshed."""

    underlying_model = get_fake_model({"name": models.TextField()})

    model = define_fake_materialized_view_model(
        {"name": models.TextField()},
        {"query": underlying_model.objects.filter(name="test1")},
    )

    underlying_model.objects.create(name="test1")
    underlying_model.objects.create(name="test2")

    schema_editor = PostgresSchemaEditor(connection)
    schema_editor.create_materialized_view_model(model)

    # materialized view should only show records name="test"1
    objs = list(model.objects.all())
    assert len(objs) == 1
    assert objs[0].name == "test1"

    # create another record with "test1" and refresh
    underlying_model.objects.create(name="test1")
    model.refresh()

    objs = list(model.objects.all())
    assert len(objs) == 2