Skip to content

Commit

Permalink
API: return DecimalFields as decimal instead of string. ref #368
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Aug 9, 2024
1 parent ecd3c5d commit 006d187
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
2 changes: 2 additions & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
"ORDERING_PARAM": "order_by",
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination",
"PAGE_SIZE": 100,
"COERCE_DECIMAL_TO_STRING": False,
}

SPECTACULAR_SETTINGS = {
Expand Down Expand Up @@ -188,6 +189,7 @@
"queue_limit": 50,
"bulk": 10,
"orm": "default",
"sync": True if DEBUG else False,
}


Expand Down
8 changes: 7 additions & 1 deletion open_prices/api/locations/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ class LocationDetailApiTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.location = LocationFactory(
osm_id=652825274, osm_type="NODE", osm_name="Monoprix", price_count=15
osm_id=652825274,
osm_type="NODE",
osm_name="Monoprix",
osm_lat="45.1805534",
osm_lon="5.7153387",
price_count=15,
)
cls.url = reverse("api:locations-detail", args=[cls.location.id])

Expand All @@ -83,6 +88,7 @@ def test_location_detail(self):
# existing location
response = self.client.get(self.url)
self.assertEqual(response.data["id"], self.location.id)
# self.assertEqual(response.data["osm_lat"], 45.1805534)

def test_location_detail_by_osm(self):
# 404
Expand Down
10 changes: 5 additions & 5 deletions open_prices/api/prices/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_price_list_order_by(self):
url = reverse("api:prices-list") + "?order_by=-price"
response = self.client.get(url)
self.assertEqual(response.data["count"], 3)
self.assertEqual(response.data["results"][0]["price"], "50.00")
self.assertEqual(response.data["results"][0]["price"], 50.00)

def test_price_list_filter_by_product_code(self):
url = reverse("api:prices-list") + "?product_code=8001505005707"
Expand All @@ -61,16 +61,16 @@ def test_price_list_filter_by_price(self):
url = reverse("api:prices-list") + "?price=15"
response = self.client.get(url)
self.assertEqual(response.data["count"], 1)
self.assertEqual(response.data["results"][0]["price"], "15.00")
self.assertEqual(response.data["results"][0]["price"], 15.00)
# lte / gte
url = reverse("api:prices-list") + "?price__gte=20"
response = self.client.get(url)
self.assertEqual(response.data["count"], 1)
self.assertEqual(response.data["results"][0]["price"], "50.00")
self.assertEqual(response.data["results"][0]["price"], 50.00)
url = reverse("api:prices-list") + "?price__lte=20"
response = self.client.get(url)
self.assertEqual(response.data["count"], 2)
self.assertEqual(response.data["results"][0]["price"], "15.00")
self.assertEqual(response.data["results"][0]["price"], 15.00)

def test_price_list_filter_by_currency(self):
url = reverse("api:prices-list") + "?currency=EUR"
Expand Down Expand Up @@ -201,7 +201,7 @@ def test_price_create(self):
)
self.assertEqual(response.status_code, 201)
self.assertEqual(response.data["product_code"], "8001505005707")
self.assertEqual(response.data["price"], "15.00")
self.assertEqual(response.data["price"], 15.00)
self.assertEqual(response.data["currency"], "EUR")
self.assertEqual(response.data["date"], "2024-01-01")
self.assertTrue("source" not in response.data)
Expand Down
15 changes: 15 additions & 0 deletions open_prices/common/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.test import TestCase

from open_prices.common.utils import is_float


class UtilsTest(TestCase):
@classmethod
def setUpTestData(cls):
pass

def test_is_float(self):
for PRICE_OK in [0, 1.5, 10, "0", "1.5", "10.00"]:
self.assertTrue(is_float(PRICE_OK))
for PRICE_NOT_OK in ["", " ", "a", "1,5", "1.5.0"]:
self.assertFalse(is_float(PRICE_NOT_OK))

0 comments on commit 006d187

Please sign in to comment.