|
| 1 | +import datetime as dt |
| 2 | +from contextlib import closing |
| 3 | + |
| 4 | +from django.core.management import call_command |
| 5 | +from django.db import DEFAULT_DB_ALIAS, connections |
| 6 | +from django.dispatch import receiver |
| 7 | +from django.test import TestCase |
| 8 | + |
| 9 | +from django_pgviews.signals import view_synced |
| 10 | + |
| 11 | +from ..viewtest.models import RelatedView |
| 12 | +from ..viewtest.tests import get_list_of_indexes |
| 13 | +from .models import SchemaMonthlyObservationMaterializedView, SchemaMonthlyObservationView, SchemaObservation |
| 14 | + |
| 15 | + |
| 16 | +class WeatherPinnedViewConnectionTest(TestCase): |
| 17 | + """Weather views should only return schema_db when pinned.""" |
| 18 | + |
| 19 | + def test_schema_view_using_schema_db(self): |
| 20 | + self.assertEqual(SchemaMonthlyObservationView.get_view_connection(using="schema_db"), connections["schema_db"]) |
| 21 | + |
| 22 | + def test_schema_view_using_default_db(self): |
| 23 | + self.assertIsNone(SchemaMonthlyObservationView.get_view_connection(using=DEFAULT_DB_ALIAS)) |
| 24 | + |
| 25 | + def test_schema_materialized_view_using_schema_db(self): |
| 26 | + self.assertEqual( |
| 27 | + SchemaMonthlyObservationMaterializedView.get_view_connection(using="schema_db"), connections["schema_db"] |
| 28 | + ) |
| 29 | + |
| 30 | + def test_schema_materialized_view_using_default_db(self): |
| 31 | + self.assertIsNone(SchemaMonthlyObservationMaterializedView.get_view_connection(using=DEFAULT_DB_ALIAS)) |
| 32 | + |
| 33 | + def test_other_app_view_using_schema_db(self): |
| 34 | + self.assertIsNone(RelatedView.get_view_connection(using="schema_db")) |
| 35 | + |
| 36 | + def test_other_app_view_using_default_db(self): |
| 37 | + self.assertEqual(RelatedView.get_view_connection(using=DEFAULT_DB_ALIAS), connections["default"]) |
| 38 | + |
| 39 | + |
| 40 | +class SchemaTest(TestCase): |
| 41 | + """View.refresh() should automatically select the appropriate schema.""" |
| 42 | + |
| 43 | + databases = {DEFAULT_DB_ALIAS, "schema_db"} |
| 44 | + |
| 45 | + def test_schemas(self): |
| 46 | + with closing(connections["schema_db"].cursor()) as cur: |
| 47 | + cur.execute("""SELECT schemaname FROM pg_tables WHERE tablename LIKE 'schemadbtest_schemaobservation';""") |
| 48 | + |
| 49 | + res = cur.fetchone() |
| 50 | + self.assertIsNotNone(res, "Can't find table schemadbtest_schemaobservation;") |
| 51 | + |
| 52 | + (schemaname,) = res |
| 53 | + self.assertEqual(schemaname, "other") |
| 54 | + |
| 55 | + cur.execute( |
| 56 | + """SELECT schemaname FROM pg_views WHERE viewname LIKE 'schemadbtest_schemamonthlyobservationview';""" |
| 57 | + ) |
| 58 | + |
| 59 | + res = cur.fetchone() |
| 60 | + self.assertIsNotNone(res, "Can't find schemadbtest_schemamonthlyobservationview;") |
| 61 | + |
| 62 | + (schemaname,) = res |
| 63 | + self.assertEqual(schemaname, "other") |
| 64 | + |
| 65 | + cur.execute( |
| 66 | + """SELECT schemaname FROM pg_matviews WHERE matviewname LIKE 'schemadbtest_schemamonthlyobservationmaterializedview';""" |
| 67 | + ) |
| 68 | + |
| 69 | + res = cur.fetchone() |
| 70 | + self.assertIsNotNone(res, "Can't find schemadbtest_schemamonthlyobservationmaterializedview.") |
| 71 | + |
| 72 | + (schemaname,) = res |
| 73 | + self.assertEqual(schemaname, "other") |
| 74 | + |
| 75 | + indexes = get_list_of_indexes(cur, SchemaMonthlyObservationMaterializedView) |
| 76 | + self.assertEqual(indexes, {"schemadbtes_date_9985f7_idx"}) |
| 77 | + |
| 78 | + def test_view(self): |
| 79 | + SchemaObservation.objects.create(date=dt.date(2022, 1, 1), temperature=10) |
| 80 | + SchemaObservation.objects.create(date=dt.date(2022, 1, 3), temperature=20) |
| 81 | + self.assertEqual(SchemaMonthlyObservationView.objects.count(), 1) |
| 82 | + |
| 83 | + def test_mat_view_pre_refresh(self): |
| 84 | + SchemaObservation.objects.create(date=dt.date(2022, 1, 1), temperature=10) |
| 85 | + SchemaObservation.objects.create(date=dt.date(2022, 1, 3), temperature=20) |
| 86 | + self.assertEqual(SchemaMonthlyObservationMaterializedView.objects.count(), 0) |
| 87 | + |
| 88 | + def test_mat_view_refresh(self): |
| 89 | + SchemaObservation.objects.create(date=dt.date(2022, 1, 1), temperature=10) |
| 90 | + SchemaObservation.objects.create(date=dt.date(2022, 1, 3), temperature=20) |
| 91 | + SchemaMonthlyObservationMaterializedView.refresh() |
| 92 | + self.assertEqual(SchemaMonthlyObservationMaterializedView.objects.count(), 1) |
| 93 | + |
| 94 | + def test_view_exists_on_sync(self): |
| 95 | + synced = [] |
| 96 | + |
| 97 | + @receiver(view_synced) |
| 98 | + def on_view_synced(sender, **kwargs): |
| 99 | + synced.append(sender) |
| 100 | + if sender == SchemaMonthlyObservationView: |
| 101 | + self.assertEqual( |
| 102 | + dict( |
| 103 | + {"status": "EXISTS", "has_changed": False}, |
| 104 | + update=False, |
| 105 | + force=False, |
| 106 | + signal=view_synced, |
| 107 | + using="schema_db", |
| 108 | + ), |
| 109 | + kwargs, |
| 110 | + ) |
| 111 | + if sender == SchemaMonthlyObservationMaterializedView: |
| 112 | + self.assertEqual( |
| 113 | + dict( |
| 114 | + {"status": "UPDATED", "has_changed": True}, |
| 115 | + update=False, |
| 116 | + force=False, |
| 117 | + signal=view_synced, |
| 118 | + using="schema_db", |
| 119 | + ), |
| 120 | + kwargs, |
| 121 | + ) |
| 122 | + |
| 123 | + call_command("sync_pgviews", database="schema_db", update=False) |
| 124 | + |
| 125 | + self.assertIn(SchemaMonthlyObservationView, synced) |
| 126 | + self.assertIn(SchemaMonthlyObservationMaterializedView, synced) |
| 127 | + |
| 128 | + def test_sync_pgviews_materialized_views_check_sql_changed(self): |
| 129 | + self.assertEqual(SchemaObservation.objects.count(), 0, "Test started with non-empty SchemaObservation") |
| 130 | + self.assertEqual( |
| 131 | + SchemaMonthlyObservationMaterializedView.objects.count(), 0, "Test started with non-empty mat view" |
| 132 | + ) |
| 133 | + |
| 134 | + SchemaObservation.objects.create(date=dt.date(2022, 1, 1), temperature=10) |
| 135 | + |
| 136 | + # test regular behaviour, the mat view got recreated |
| 137 | + call_command("sync_pgviews", database="schema_db", update=False) # uses default django setting, False |
| 138 | + self.assertEqual(SchemaMonthlyObservationMaterializedView.objects.count(), 1) |
| 139 | + |
| 140 | + # the mat view did not get recreated because the model hasn't changed |
| 141 | + SchemaObservation.objects.create(date=dt.date(2022, 2, 3), temperature=20) |
| 142 | + call_command("sync_pgviews", database="schema_db", update=False, materialized_views_check_sql_changed=True) |
| 143 | + self.assertEqual(SchemaMonthlyObservationMaterializedView.objects.count(), 1) |
| 144 | + |
| 145 | + # the mat view got recreated because the mat view SQL has changed |
| 146 | + |
| 147 | + # let's pretend the mat view in the DB is ordered by name, while the defined on models isn't |
| 148 | + with connections["schema_db"].cursor() as cursor: |
| 149 | + cursor.execute("DROP MATERIALIZED VIEW schemadbtest_schemamonthlyobservationmaterializedview CASCADE;") |
| 150 | + cursor.execute( |
| 151 | + """ |
| 152 | + CREATE MATERIALIZED VIEW schemadbtest_schemamonthlyobservationmaterializedview as |
| 153 | + WITH summary AS ( |
| 154 | + SELECT |
| 155 | + date_trunc('day', date) AS date, |
| 156 | + count(*) |
| 157 | + FROM schemadbtest_schemaobservation |
| 158 | + GROUP BY 1 |
| 159 | + ORDER BY date |
| 160 | + ) SELECT |
| 161 | + ROW_NUMBER() OVER () AS id, |
| 162 | + date, |
| 163 | + count |
| 164 | + FROM summary; |
| 165 | + """ |
| 166 | + ) |
| 167 | + |
| 168 | + call_command("sync_pgviews", update=False, materialized_views_check_sql_changed=True) |
| 169 | + self.assertEqual(SchemaMonthlyObservationMaterializedView.objects.count(), 2) |
| 170 | + |
| 171 | + def test_migrate_materialized_views_check_sql_changed_default(self): |
| 172 | + self.assertEqual(SchemaObservation.objects.count(), 0, "Test started with non-empty SchemaObservation") |
| 173 | + self.assertEqual( |
| 174 | + SchemaMonthlyObservationMaterializedView.objects.count(), 0, "Test started with non-empty mat view" |
| 175 | + ) |
| 176 | + |
| 177 | + SchemaObservation.objects.create(date=dt.date(2022, 1, 1), temperature=10) |
| 178 | + |
| 179 | + call_command("migrate", database="schema_db") |
| 180 | + |
| 181 | + self.assertEqual(SchemaMonthlyObservationMaterializedView.objects.count(), 1) |
0 commit comments