|
| 1 | +import pytest |
| 2 | + |
| 3 | +from django.core.exceptions import SuspiciousOperation |
| 4 | +from django.db import connection, models |
| 5 | +from django.test.utils import CaptureQueriesContext |
| 6 | + |
| 7 | +from psqlextra.backend.schema import PostgresSchemaEditor |
| 8 | + |
| 9 | +from .fake_model import delete_fake_model, get_fake_model |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def fake_model(): |
| 14 | + model = get_fake_model( |
| 15 | + { |
| 16 | + "name": models.TextField(), |
| 17 | + } |
| 18 | + ) |
| 19 | + |
| 20 | + yield model |
| 21 | + |
| 22 | + delete_fake_model(model) |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def fake_model_non_concrete_field(fake_model): |
| 27 | + model = get_fake_model( |
| 28 | + { |
| 29 | + "fk": models.ForeignKey( |
| 30 | + fake_model, on_delete=models.CASCADE, related_name="fakes" |
| 31 | + ), |
| 32 | + } |
| 33 | + ) |
| 34 | + |
| 35 | + yield model |
| 36 | + |
| 37 | + delete_fake_model(model) |
| 38 | + |
| 39 | + |
| 40 | +def test_schema_editor_vacuum_not_in_transaction(fake_model): |
| 41 | + schema_editor = PostgresSchemaEditor(connection) |
| 42 | + |
| 43 | + with pytest.raises(SuspiciousOperation): |
| 44 | + schema_editor.vacuum_table(fake_model._meta.db_table) |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.parametrize( |
| 48 | + "kwargs,query", |
| 49 | + [ |
| 50 | + (dict(), "VACUUM %s"), |
| 51 | + (dict(full=True), "VACUUM (FULL) %s"), |
| 52 | + (dict(analyze=True), "VACUUM (ANALYZE) %s"), |
| 53 | + (dict(parallel=8), "VACUUM (PARALLEL 8) %s"), |
| 54 | + (dict(analyze=True, verbose=True), "VACUUM (VERBOSE, ANALYZE) %s"), |
| 55 | + ( |
| 56 | + dict(analyze=True, parallel=8, verbose=True), |
| 57 | + "VACUUM (VERBOSE, ANALYZE, PARALLEL 8) %s", |
| 58 | + ), |
| 59 | + (dict(freeze=True), "VACUUM (FREEZE) %s"), |
| 60 | + (dict(verbose=True), "VACUUM (VERBOSE) %s"), |
| 61 | + (dict(disable_page_skipping=True), "VACUUM (DISABLE_PAGE_SKIPPING) %s"), |
| 62 | + (dict(skip_locked=True), "VACUUM (SKIP_LOCKED) %s"), |
| 63 | + (dict(index_cleanup=True), "VACUUM (INDEX_CLEANUP) %s"), |
| 64 | + (dict(truncate=True), "VACUUM (TRUNCATE) %s"), |
| 65 | + ], |
| 66 | +) |
| 67 | +@pytest.mark.django_db(transaction=True) |
| 68 | +def test_schema_editor_vacuum_table(fake_model, kwargs, query): |
| 69 | + schema_editor = PostgresSchemaEditor(connection) |
| 70 | + |
| 71 | + with CaptureQueriesContext(connection) as ctx: |
| 72 | + schema_editor.vacuum_table(fake_model._meta.db_table, **kwargs) |
| 73 | + |
| 74 | + queries = [query["sql"] for query in ctx.captured_queries] |
| 75 | + assert queries == [ |
| 76 | + query % connection.ops.quote_name(fake_model._meta.db_table) |
| 77 | + ] |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.django_db(transaction=True) |
| 81 | +def test_schema_editor_vacuum_table_columns(fake_model): |
| 82 | + schema_editor = PostgresSchemaEditor(connection) |
| 83 | + |
| 84 | + with CaptureQueriesContext(connection) as ctx: |
| 85 | + schema_editor.vacuum_table( |
| 86 | + fake_model._meta.db_table, ["id", "name"], analyze=True |
| 87 | + ) |
| 88 | + |
| 89 | + queries = [query["sql"] for query in ctx.captured_queries] |
| 90 | + assert queries == [ |
| 91 | + 'VACUUM (ANALYZE) %s ("id", "name")' |
| 92 | + % connection.ops.quote_name(fake_model._meta.db_table) |
| 93 | + ] |
| 94 | + |
| 95 | + |
| 96 | +@pytest.mark.django_db(transaction=True) |
| 97 | +def test_schema_editor_vacuum_model(fake_model): |
| 98 | + schema_editor = PostgresSchemaEditor(connection) |
| 99 | + |
| 100 | + with CaptureQueriesContext(connection) as ctx: |
| 101 | + schema_editor.vacuum_model(fake_model, analyze=True, parallel=8) |
| 102 | + |
| 103 | + queries = [query["sql"] for query in ctx.captured_queries] |
| 104 | + assert queries == [ |
| 105 | + "VACUUM (ANALYZE, PARALLEL 8) %s" |
| 106 | + % connection.ops.quote_name(fake_model._meta.db_table) |
| 107 | + ] |
| 108 | + |
| 109 | + |
| 110 | +@pytest.mark.django_db(transaction=True) |
| 111 | +def test_schema_editor_vacuum_model_fields(fake_model): |
| 112 | + schema_editor = PostgresSchemaEditor(connection) |
| 113 | + |
| 114 | + with CaptureQueriesContext(connection) as ctx: |
| 115 | + schema_editor.vacuum_model( |
| 116 | + fake_model, |
| 117 | + [fake_model._meta.get_field("name")], |
| 118 | + analyze=True, |
| 119 | + parallel=8, |
| 120 | + ) |
| 121 | + |
| 122 | + queries = [query["sql"] for query in ctx.captured_queries] |
| 123 | + assert queries == [ |
| 124 | + 'VACUUM (ANALYZE, PARALLEL 8) %s ("name")' |
| 125 | + % connection.ops.quote_name(fake_model._meta.db_table) |
| 126 | + ] |
| 127 | + |
| 128 | + |
| 129 | +@pytest.mark.django_db(transaction=True) |
| 130 | +def test_schema_editor_vacuum_model_non_concrete_fields( |
| 131 | + fake_model, fake_model_non_concrete_field |
| 132 | +): |
| 133 | + schema_editor = PostgresSchemaEditor(connection) |
| 134 | + |
| 135 | + with CaptureQueriesContext(connection) as ctx: |
| 136 | + schema_editor.vacuum_model( |
| 137 | + fake_model, |
| 138 | + [fake_model._meta.get_field("fakes")], |
| 139 | + analyze=True, |
| 140 | + parallel=8, |
| 141 | + ) |
| 142 | + |
| 143 | + queries = [query["sql"] for query in ctx.captured_queries] |
| 144 | + assert queries == [ |
| 145 | + "VACUUM (ANALYZE, PARALLEL 8) %s" |
| 146 | + % connection.ops.quote_name(fake_model._meta.db_table) |
| 147 | + ] |
0 commit comments