-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from iNishant/feature/get-or-none-wrapper
QuerysetGetOrNoneWrapper, type checking
- Loading branch information
Showing
8 changed files
with
200 additions
and
82 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
ruff==0.3.7 | ||
model-bakery==1.17.0 | ||
build==1.2.1 | ||
twine==5.0.0 | ||
twine==5.0.0 | ||
mypy==1.10.0 | ||
mypy-extensions==1.0.0 |
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,70 @@ | ||
from datetime import datetime, timezone | ||
|
||
from django.test import TransactionTestCase | ||
from model_bakery import baker | ||
|
||
from django_querysets_single_query_fetch.service import ( | ||
QuerysetsSingleQueryFetch, | ||
QuerysetCountWrapper, | ||
) | ||
from testapp.models import OnlineStore, StoreProduct, StoreProductCategory | ||
|
||
|
||
class QuerysetCountWrapperPostgresTestCase(TransactionTestCase): | ||
def setUp(self) -> None: | ||
self.today = datetime.now(tz=timezone.utc) | ||
self.store = baker.make(OnlineStore, expired_on=self.today) | ||
self.store = OnlineStore.objects.get( | ||
id=self.store.id | ||
) # force refresh from db so that types are the default | ||
# types | ||
self.category = baker.make(StoreProductCategory, store=self.store) | ||
self.product_1 = baker.make(StoreProduct, store=self.store, selling_price=50.22) | ||
self.product_2 = baker.make( | ||
StoreProduct, store=self.store, category=self.category, selling_price=100.33 | ||
) | ||
|
||
def test_fetch_count(self): | ||
""" | ||
- test fetch count works in single query | ||
_ test fetch count works with filter querysets | ||
_ test fetch count works with other querysets | ||
""" | ||
# test fetch count works in single query | ||
count_queryset = StoreProduct.objects.filter() | ||
with self.assertNumQueries(1): | ||
results = QuerysetsSingleQueryFetch( | ||
querysets=[ | ||
QuerysetCountWrapper(queryset=count_queryset), | ||
] | ||
).execute() | ||
self.assertEqual(len(results), 1) | ||
self.assertEqual(results[0], count_queryset.count()) | ||
|
||
# test fetch count works with filter querysets | ||
count_filter_queryset = StoreProduct.objects.filter(id=self.product_1.id) | ||
with self.assertNumQueries(1): | ||
results = QuerysetsSingleQueryFetch( | ||
querysets=[ | ||
QuerysetCountWrapper(queryset=count_filter_queryset), | ||
] | ||
).execute() | ||
self.assertEqual(len(results), 1) | ||
self.assertEqual(results[0], count_filter_queryset.count()) | ||
|
||
# test fetch count works with other querysets | ||
count_queryset = StoreProduct.objects.filter() | ||
count_filter_queryset = StoreProduct.objects.filter(id=self.product_1.id) | ||
queryset = StoreProduct.objects.filter() | ||
with self.assertNumQueries(1): | ||
results = QuerysetsSingleQueryFetch( | ||
querysets=[ | ||
QuerysetCountWrapper(queryset=count_queryset), | ||
QuerysetCountWrapper(queryset=count_filter_queryset), | ||
queryset, | ||
] | ||
).execute() | ||
self.assertEqual(len(results), 3) | ||
self.assertEqual(results[0], count_queryset.count()) | ||
self.assertEqual(results[1], count_filter_queryset.count()) | ||
self.assertEqual(results[2], list(queryset)) |
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,62 @@ | ||
from datetime import datetime, timezone | ||
from django_querysets_single_query_fetch.service import ( | ||
QuerysetsSingleQueryFetch, | ||
QuerysetGetOrNoneWrapper, | ||
) | ||
from django.test import TransactionTestCase | ||
from model_bakery import baker | ||
|
||
from testapp.models import OnlineStore, StoreProduct, StoreProductCategory | ||
|
||
|
||
class QuerysetGetOrNoneWrapperPostgresTestCase(TransactionTestCase): | ||
def setUp(self) -> None: | ||
self.today = datetime.now(tz=timezone.utc) | ||
self.store = baker.make(OnlineStore, expired_on=self.today) | ||
self.store = OnlineStore.objects.get( | ||
id=self.store.id | ||
) # force refresh from db so that types are the default | ||
# types | ||
self.category = baker.make(StoreProductCategory, store=self.store) | ||
self.product_1 = baker.make(StoreProduct, store=self.store, selling_price=50.22) | ||
self.product_2 = baker.make( | ||
StoreProduct, store=self.store, category=self.category, selling_price=100.33 | ||
) | ||
|
||
def test_get_or_none_wrapper_with_single_row_matching(self): | ||
with self.assertNumQueries(1): | ||
results = QuerysetsSingleQueryFetch( | ||
querysets=[ | ||
QuerysetGetOrNoneWrapper( | ||
StoreProduct.objects.filter(id=self.product_1.id) | ||
), | ||
] | ||
).execute() | ||
self.assertEqual(len(results), 1) | ||
product = results[0] | ||
self.assertEqual(product.id, self.product_1.id) | ||
|
||
def test_get_or_none_wrapper_with_no_row_matching(self): | ||
with self.assertNumQueries(1): | ||
results = QuerysetsSingleQueryFetch( | ||
querysets=[ | ||
QuerysetGetOrNoneWrapper(StoreProduct.objects.filter(id=-1)), | ||
] | ||
).execute() | ||
self.assertEqual(len(results), 1) | ||
product = results[0] | ||
self.assertIsNone(product) | ||
|
||
def test_get_or_none_wrapper_with_multiple_rows_matching(self): | ||
with self.assertNumQueries(1): | ||
# get in this case can return either product 1 or product 2 | ||
results = QuerysetsSingleQueryFetch( | ||
querysets=[ | ||
QuerysetGetOrNoneWrapper(StoreProduct.objects.all()), | ||
] | ||
).execute() | ||
self.assertEqual(len(results), 1) | ||
product = results[0] | ||
self.assertTrue( | ||
(product.id == self.product_1.id) or (product.id == self.product_2.id) | ||
) |