Skip to content

Commit

Permalink
Test all table lock modes
Browse files Browse the repository at this point in the history
  • Loading branch information
Photonios committed Apr 6, 2023
1 parent 0bb392d commit c79a8ca
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
7 changes: 7 additions & 0 deletions psqlextra/locking.py
Expand Up @@ -19,6 +19,13 @@ class PostgresTableLockMode(Enum):
EXCLUSIVE = "EXCLUSIVE"
ACCESS_EXCLUSIVE = "ACCESS EXCLUSIVE"

@property
def alias(self) -> str:
return (
"".join([word.title() for word in self.name.lower().split("_")])
+ "Lock"
)


def postgres_lock_table(
table_name: str,
Expand Down
9 changes: 4 additions & 5 deletions tests/test_locking.py
Expand Up @@ -66,18 +66,17 @@ def test_postgres_lock_table_in_schema():
assert lock_signature not in get_table_locks()


@pytest.mark.parametrize("lock_mode", list(PostgresTableLockMode))
@pytest.mark.django_db(transaction=True)
def test_postgres_lock_mode(mocked_model):
def test_postgres_lock_model(mocked_model, lock_mode):
lock_signature = (
"public",
mocked_model._meta.db_table,
"AccessExclusiveLock",
lock_mode.alias,
)

with transaction.atomic():
postgres_lock_model(
mocked_model, PostgresTableLockMode.ACCESS_EXCLUSIVE
)
postgres_lock_model(mocked_model, lock_mode)
assert lock_signature in get_table_locks()

assert lock_signature not in get_table_locks()
Expand Down

0 comments on commit c79a8ca

Please sign in to comment.