Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Jan 12, 2025
1 parent 2a966e1 commit d05065d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
21 changes: 19 additions & 2 deletions open_prices/api/prices/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,31 @@ class PriceListApiTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.url = reverse("api:prices-list")
PriceFactory(price=15)
location_osm = LocationFactory(**LOCATION_OSM_NODE_652825274)
proof = ProofFactory(type=proof_constants.TYPE_RECEIPT)
PriceFactory(
price=15,
proof_id=proof.id,
location_id=location_osm.id,
location_osm_id=location_osm.osm_id,
location_osm_type=location_osm.osm_type,
)
PriceFactory(price=0)
PriceFactory(price=50)

def test_price_list(self):
# anonymous
with self.assertNumQueries(1 + 1): # thanks to select_related
# thanks to select_related, we only have 2 queries:
# - 1 to count the number of prices
# - 1 to get the prices and their associated proof/location/product
with self.assertNumQueries(1 + 1):
response = self.client.get(self.url)
self.assertEqual(response.data["total"], 3)
self.assertEqual(len(response.data["items"]), 3)
self.assertTrue("id" in response.data["items"][0])
self.assertEqual(response.data["items"][0]["price"], 15.00) # default order
self.assertTrue("proof" in response.data["items"][0])
self.assertTrue("location" in response.data["items"][0])


class PriceListPaginationApiTest(TestCase):
Expand Down Expand Up @@ -312,9 +325,13 @@ def test_price_list_filter_by_created(self):
def test_price_list_filter_by_product_categories_tags(self):
self.assertEqual(Price.objects.count(), 5)
url = self.url + "?product__categories_tags__contains=en:breakfasts"
# thanks to select_related, we only have 2 queries:
# - 1 to count the number of prices
# - 1 to get the prices (even when filtering on product fields)
with self.assertNumQueries(1 + 1):
response = self.client.get(url)
self.assertEqual(response.data["total"], 1)
self.assertTrue("product" in response.data["items"][0])


class PriceDetailApiTest(TestCase):
Expand Down
4 changes: 2 additions & 2 deletions open_prices/api/proofs/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_proof_list(self):
# anonymous
# thanks to select_related, we only have 2 queries:
# - 1 to count the number of proofs of the user
# - 1 to get the proofs and their associated locations (select_related)
# - 1 to get the proofs and their associated location
with self.assertNumQueries(2):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
Expand Down Expand Up @@ -418,7 +418,7 @@ def setUpTestData(cls):

def test_price_tag_list(self):
# Check that we can access price tags anonymously
# We only have 2 queries:
# We only have 3 queries:
# - 1 to count the number of price tags
# - 1 to get the price tags and their associated proof
# - 1 to get the price tag predictions (prefetch related)
Expand Down

0 comments on commit d05065d

Please sign in to comment.