Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: raw delete expired instead of
Queryset.delete
(#235)
* feat: raw delete expired instead of `Queryset.delete` This is avoid unncessary loading of objects in-memory in certain conditions * minor: verbose fix * minor: verbose fix * minor: verbose fix * minor: verbose simplified * test: unit test for `utils.raw_delete` added * minor: fix some lint issues * minor: fix some lint issues * minor: fix method names * [test] `MigrationTests`: calling `makemigrations --check` instead of calling internal logic of `makemigrations` to check if no pending migrations * [test] import optimized * [test] fix - wrong function name
- Loading branch information
Showing
4 changed files
with
62 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from celery import uuid | ||
from django.test import TransactionTestCase | ||
|
||
from django_celery_results.models import TaskResult | ||
from django_celery_results.utils import raw_delete | ||
|
||
_TEST_RECORDS_COUNT = 100 | ||
|
||
|
||
class RawDeleteTest(TransactionTestCase): | ||
"""Unit test for `utils.raw_delete` func.""" | ||
|
||
def setUp(self): | ||
# setup test results to be used against raw_delete | ||
TaskResult.objects.bulk_create( | ||
[TaskResult(task_id=uuid()) for _ in range(_TEST_RECORDS_COUNT)] | ||
) | ||
assert TaskResult.objects.count() == _TEST_RECORDS_COUNT | ||
|
||
def _get_sample_result_ids(self, count): | ||
return TaskResult.objects.values_list("pk").order_by("?")[:count] | ||
|
||
def test_all_rows_delete(self): | ||
raw_delete( | ||
queryset=TaskResult.objects.all() | ||
) | ||
assert TaskResult.objects.count() == 0 | ||
|
||
def test_correct_rows_deleted(self): | ||
# sample random 10 rows | ||
sample_size = 10 | ||
sample_ids = self._get_sample_result_ids(count=sample_size) | ||
# update task_name to "test" | ||
TaskResult.objects.filter(pk__in=sample_ids).update(task_name="test") | ||
# "raw delete" results with name = "test" | ||
raw_delete( | ||
queryset=TaskResult.objects.filter(task_name="test") | ||
) | ||
assert TaskResult.objects.filter(task_name="test").count() == 0 | ||
results_remaining = _TEST_RECORDS_COUNT - sample_size | ||
assert TaskResult.objects.count() == results_remaining |