|
| 1 | +import uuid |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from django.db import connection, models, transaction |
| 6 | + |
| 7 | +from psqlextra.locking import ( |
| 8 | + PostgresTableLockMode, |
| 9 | + postgres_lock_model, |
| 10 | + postgres_lock_table, |
| 11 | +) |
| 12 | + |
| 13 | +from .fake_model import get_fake_model |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def mocked_model(): |
| 18 | + return get_fake_model( |
| 19 | + { |
| 20 | + "name": models.TextField(), |
| 21 | + } |
| 22 | + ) |
| 23 | + |
| 24 | + |
| 25 | +def get_table_locks(): |
| 26 | + with connection.cursor() as cursor: |
| 27 | + return connection.introspection.get_table_locks(cursor) |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.django_db(transaction=True) |
| 31 | +def test_postgres_lock_table(mocked_model): |
| 32 | + lock_signature = ( |
| 33 | + "public", |
| 34 | + mocked_model._meta.db_table, |
| 35 | + "AccessExclusiveLock", |
| 36 | + ) |
| 37 | + with transaction.atomic(): |
| 38 | + postgres_lock_table( |
| 39 | + mocked_model._meta.db_table, PostgresTableLockMode.ACCESS_EXCLUSIVE |
| 40 | + ) |
| 41 | + assert lock_signature in get_table_locks() |
| 42 | + |
| 43 | + assert lock_signature not in get_table_locks() |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.django_db(transaction=True) |
| 47 | +def test_postgres_lock_table_in_schema(): |
| 48 | + schema_name = str(uuid.uuid4())[:8] |
| 49 | + table_name = str(uuid.uuid4())[:8] |
| 50 | + quoted_schema_name = connection.ops.quote_name(schema_name) |
| 51 | + quoted_table_name = connection.ops.quote_name(table_name) |
| 52 | + |
| 53 | + with connection.cursor() as cursor: |
| 54 | + cursor.execute(f"CREATE SCHEMA {quoted_schema_name}") |
| 55 | + cursor.execute( |
| 56 | + f"CREATE TABLE {quoted_schema_name}.{quoted_table_name} AS SELECT 'hello world'" |
| 57 | + ) |
| 58 | + |
| 59 | + lock_signature = (schema_name, table_name, "ExclusiveLock") |
| 60 | + with transaction.atomic(): |
| 61 | + postgres_lock_table( |
| 62 | + table_name, PostgresTableLockMode.EXCLUSIVE, schema_name=schema_name |
| 63 | + ) |
| 64 | + assert lock_signature in get_table_locks() |
| 65 | + |
| 66 | + assert lock_signature not in get_table_locks() |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.django_db(transaction=True) |
| 70 | +def test_postgres_lock_mode(mocked_model): |
| 71 | + lock_signature = ( |
| 72 | + "public", |
| 73 | + mocked_model._meta.db_table, |
| 74 | + "AccessExclusiveLock", |
| 75 | + ) |
| 76 | + |
| 77 | + with transaction.atomic(): |
| 78 | + postgres_lock_model( |
| 79 | + mocked_model, PostgresTableLockMode.ACCESS_EXCLUSIVE |
| 80 | + ) |
| 81 | + assert lock_signature in get_table_locks() |
| 82 | + |
| 83 | + assert lock_signature not in get_table_locks() |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.django_db(transaction=True) |
| 87 | +def test_postgres_lock_model_in_schema(mocked_model): |
| 88 | + schema_name = str(uuid.uuid4())[:8] |
| 89 | + quoted_schema_name = connection.ops.quote_name(schema_name) |
| 90 | + quoted_table_name = connection.ops.quote_name(mocked_model._meta.db_table) |
| 91 | + |
| 92 | + with connection.cursor() as cursor: |
| 93 | + cursor.execute(f"CREATE SCHEMA {quoted_schema_name}") |
| 94 | + cursor.execute( |
| 95 | + f"CREATE TABLE {quoted_schema_name}.{quoted_table_name} (LIKE public.{quoted_table_name} INCLUDING ALL)" |
| 96 | + ) |
| 97 | + |
| 98 | + lock_signature = (schema_name, mocked_model._meta.db_table, "ExclusiveLock") |
| 99 | + with transaction.atomic(): |
| 100 | + postgres_lock_model( |
| 101 | + mocked_model, |
| 102 | + PostgresTableLockMode.EXCLUSIVE, |
| 103 | + schema_name=schema_name, |
| 104 | + ) |
| 105 | + assert lock_signature in get_table_locks() |
| 106 | + |
| 107 | + assert lock_signature not in get_table_locks() |
0 commit comments