|
| 1 | +"""This module contains tests for database utility functions in core/utils/db.py""" |
| 2 | +import pytest |
| 3 | +from core.utils.db import batch_delete |
| 4 | +from django.db import transaction |
| 5 | +from users.models import User |
| 6 | +from users.tests.factories import UserFactory |
| 7 | + |
| 8 | + |
| 9 | +class TestBatchDelete: |
| 10 | + """Test suite for the batch_delete utility function""" |
| 11 | + |
| 12 | + @pytest.mark.django_db |
| 13 | + def test_batch_delete_empty_queryset(self): |
| 14 | + """Test batch deletion with an empty queryset. |
| 15 | +
|
| 16 | + This test verifies that: |
| 17 | + - Function handles empty querysets gracefully |
| 18 | + - Returns 0 for total deleted items |
| 19 | + - No errors are raised |
| 20 | + """ |
| 21 | + # Setup: Empty queryset |
| 22 | + queryset = User. objects. filter( email='[email protected]') |
| 23 | + |
| 24 | + # Action: Attempt to delete empty queryset |
| 25 | + total_deleted = batch_delete(queryset) |
| 26 | + |
| 27 | + # Assert: No items were deleted |
| 28 | + assert total_deleted == 0 |
| 29 | + |
| 30 | + @pytest.mark.django_db |
| 31 | + def test_batch_delete_single_batch(self): |
| 32 | + """Test batch deletion when all items fit in a single batch. |
| 33 | +
|
| 34 | + This test verifies that: |
| 35 | + - All items are deleted when count < batch_size |
| 36 | + - Items are actually removed from database |
| 37 | + """ |
| 38 | + # Setup: Create 5 users |
| 39 | + [UserFactory() for _ in range(5)] |
| 40 | + initial_count = User.objects.count() |
| 41 | + assert initial_count == 5 # Verify setup |
| 42 | + queryset = User.objects.all() |
| 43 | + |
| 44 | + # Action: Delete with batch size larger than number of items |
| 45 | + batch_delete(queryset, batch_size=10) |
| 46 | + |
| 47 | + # Assert: All users were deleted |
| 48 | + assert User.objects.count() == 0 |
| 49 | + |
| 50 | + @pytest.mark.django_db |
| 51 | + def test_batch_delete_multiple_batches(self): |
| 52 | + """Test batch deletion when items span multiple batches. |
| 53 | +
|
| 54 | + This test verifies that: |
| 55 | + - All items are deleted when count > batch_size |
| 56 | + - Items are deleted in correct batches |
| 57 | + - All items are removed from database |
| 58 | + """ |
| 59 | + # Setup: Create 25 users (will require 3 batches with batch_size=10) |
| 60 | + [UserFactory() for _ in range(25)] |
| 61 | + initial_count = User.objects.count() |
| 62 | + assert initial_count == 25 # Verify setup |
| 63 | + queryset = User.objects.all() |
| 64 | + |
| 65 | + # Action: Delete with batch size smaller than number of items |
| 66 | + batch_delete(queryset, batch_size=10) |
| 67 | + |
| 68 | + # Assert: All users were deleted |
| 69 | + assert User.objects.count() == 0 |
| 70 | + |
| 71 | + @pytest.mark.django_db |
| 72 | + def test_batch_delete_with_transaction(self): |
| 73 | + """Test batch deletion within a transaction. |
| 74 | +
|
| 75 | + This test verifies that: |
| 76 | + - Batch deletion works correctly inside a transaction |
| 77 | + - Changes are committed properly |
| 78 | + - No errors occur with nested transactions |
| 79 | + """ |
| 80 | + # Setup: Create 15 users |
| 81 | + [UserFactory() for _ in range(15)] |
| 82 | + initial_count = User.objects.count() |
| 83 | + assert initial_count == 15 # Verify setup |
| 84 | + queryset = User.objects.all() |
| 85 | + |
| 86 | + # Action: Delete within a transaction |
| 87 | + with transaction.atomic(): |
| 88 | + batch_delete(queryset, batch_size=10) |
| 89 | + |
| 90 | + # Assert: All users were deleted |
| 91 | + assert User.objects.count() == 0 |
| 92 | + |
| 93 | + @pytest.mark.django_db |
| 94 | + def test_batch_delete_exact_batch_size(self): |
| 95 | + """Test batch deletion when item count matches batch size exactly. |
| 96 | +
|
| 97 | + This test verifies that: |
| 98 | + - All items are deleted when count == batch_size |
| 99 | + - No extra queries are made after the last batch |
| 100 | + """ |
| 101 | + # Setup: Create exactly 10 users |
| 102 | + [UserFactory() for _ in range(10)] |
| 103 | + initial_count = User.objects.count() |
| 104 | + assert initial_count == 10 # Verify setup |
| 105 | + queryset = User.objects.all() |
| 106 | + |
| 107 | + # Action: Delete with batch size equal to number of items |
| 108 | + batch_delete(queryset, batch_size=10) |
| 109 | + |
| 110 | + # Assert: All users were deleted |
| 111 | + assert User.objects.count() == 0 |
0 commit comments