Skip to content

Commit

Permalink
feat(locations): new source field to store the app name & version (#546)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn authored Nov 4, 2024
1 parent 77ed50b commit a9188c9
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 10 deletions.
3 changes: 2 additions & 1 deletion open_prices/api/locations/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
class LocationSerializer(serializers.ModelSerializer):
class Meta:
model = Location
fields = "__all__"
# fields = "__all__"
exclude = ["source"]


class LocationCreateSerializer(serializers.ModelSerializer):
Expand Down
23 changes: 23 additions & 0 deletions open_prices/api/locations/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from open_prices.locations import constants as location_constants
from open_prices.locations.factories import LocationFactory
from open_prices.locations.models import Location

LOCATION_OSM_NODE_652825274 = {
"type": location_constants.TYPE_OSM,
Expand Down Expand Up @@ -169,6 +170,28 @@ def test_location_create_online(self):
self.assertEqual(response.data["website_url"], "https://www.decathlon.fr/")
self.assertEqual(response.data["price_count"], 0) # ignored

def test_location_create_with_app_name(self):
for index, (params, result) in enumerate(
[
("?", "API"),
("?app_name=", ""),
("?app_name=test app&app_version=", "test app"),
("?app_name=mobile&app_version=1.0", "mobile (1.0)"),
]
):
with self.subTest(INPUT_OUPUT=(params, result)):
data = {
**LOCATION_OSM_NODE_652825274,
"osm_id": LOCATION_OSM_NODE_652825274["osm_id"] + index,
}
# with empty app_name
response = self.client.post(
self.url + params, data, content_type="application/json"
)
self.assertEqual(response.status_code, 201)
self.assertTrue("source" not in response.data)
self.assertEqual(Location.objects.last().source, result)


class LocationUpdateApiTest(TestCase):
@classmethod
Expand Down
11 changes: 6 additions & 5 deletions open_prices/api/locations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
LocationCreateSerializer,
LocationSerializer,
)
from open_prices.api.utils import get_object_or_drf_404
from open_prices.api.utils import get_object_or_drf_404, get_source_from_request
from open_prices.locations.models import Location


Expand All @@ -31,15 +31,16 @@ def get_serializer_class(self):
return LocationCreateSerializer
return self.serializer_class

def perform_create(self, serializer):
return serializer.save()

def create(self, request: Request, *args, **kwargs):
# validate
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
# get source
self.source = get_source_from_request(self.request)
# save
location = self.perform_create(serializer)
location = serializer.save(
source=self.source,
)
# return full location
return Response(
self.serializer_class(location).data, status=status.HTTP_201_CREATED
Expand Down
10 changes: 10 additions & 0 deletions open_prices/api/prices/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,12 @@ def setUpTestData(cls):
)

def test_price_list_without_filter(self):
self.assertEqual(Price.objects.count(), 5)
response = self.client.get(self.url)
self.assertEqual(response.data["total"], 5)

def test_price_list_filter_by_product(self):
self.assertEqual(Price.objects.count(), 5)
# product_code
url = self.url + "?product_code=8001505005707"
response = self.client.get(url)
Expand All @@ -178,6 +180,7 @@ def test_price_list_filter_by_product(self):
self.assertEqual(response.data["total"], 3)

def test_price_list_filter_by_tags(self):
self.assertEqual(Price.objects.count(), 5)
# labels_tags
url = self.url + "?labels_tags__contains=en:organic"
response = self.client.get(url)
Expand All @@ -199,6 +202,7 @@ def test_price_list_filter_by_tags(self):
self.assertEqual(response.data["total"], 1)

def test_price_list_filter_by_price(self):
self.assertEqual(Price.objects.count(), 5)
# exact price
url = self.url + "?price=15"
response = self.client.get(url)
Expand All @@ -223,11 +227,13 @@ def test_price_list_filter_by_price(self):
self.assertEqual(response.data["total"], 4)

def test_price_list_filter_by_currency(self):
self.assertEqual(Price.objects.count(), 5)
url = self.url + "?currency=EUR"
response = self.client.get(url)
self.assertEqual(response.data["total"], 4)

def test_price_list_filter_by_location(self):
self.assertEqual(Price.objects.count(), 5)
# location_osm_id
url = self.url + f"?location_osm_id={self.user_price.location_osm_id}"
response = self.client.get(url)
Expand All @@ -245,6 +251,7 @@ def test_price_list_filter_by_location(self):
self.assertEqual(response.data["total"], 4)

def test_price_list_filter_by_proof(self):
self.assertEqual(Price.objects.count(), 5)
# proof_id
url = self.url + f"?proof_id={self.user_price.proof_id}"
response = self.client.get(url)
Expand All @@ -258,6 +265,7 @@ def test_price_list_filter_by_proof(self):
self.assertEqual(response.data["total"], 1)

def test_price_list_filter_by_date(self):
self.assertEqual(Price.objects.count(), 5)
# exact date
url = self.url + "?date=2024-01-01"
response = self.client.get(url)
Expand All @@ -276,11 +284,13 @@ def test_price_list_filter_by_date(self):
self.assertEqual(response.data["total"], 2)

def test_price_list_filter_by_owner(self):
self.assertEqual(Price.objects.count(), 5)
url = self.url + f"?owner={self.user_session.user.user_id}"
response = self.client.get(url)
self.assertEqual(response.data["total"], 4)

def test_price_list_filter_by_created(self):
self.assertEqual(Price.objects.count(), 5)
url = self.url + "?created__gte=2024-01-01T00:00:00Z"
response = self.client.get(url)
self.assertEqual(response.data["total"], 5)
Expand Down
6 changes: 2 additions & 4 deletions open_prices/api/proofs/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,7 @@ def test_proof_create(self):
self.assertEqual(response.data["price_count"], 0) # ignored
self.assertEqual(response.data["owner"], self.user_session.user.user_id)
self.assertTrue("source" not in response.data)
p = Proof.objects.last()
self.assertEqual(p.source, "API") # default value
self.assertEqual(Proof.objects.last().source, "API") # default value

def test_proof_create_with_location_id(self):
location_osm = LocationFactory(**LOCATION_OSM_NODE_652825274)
Expand Down Expand Up @@ -260,8 +259,7 @@ def test_proof_create_with_app_name(self):
)
self.assertEqual(response.status_code, 201)
self.assertTrue("source" not in response.data)
p = Proof.objects.last()
self.assertEqual(p.source, result)
self.assertEqual(Proof.objects.last().source, result)


class ProofUpdateApiTest(TestCase):
Expand Down
17 changes: 17 additions & 0 deletions open_prices/locations/migrations/0006_location_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.1 on 2024-11-04 13:27

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("locations", "0005_location_type_website_url_constraints"),
]

operations = [
migrations.AddField(
model_name="location",
name="source",
field=models.CharField(blank=True, null=True),
),
]
2 changes: 2 additions & 0 deletions open_prices/locations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ class Location(models.Model):
product_count = models.PositiveIntegerField(default=0, blank=True, null=True)
proof_count = models.PositiveIntegerField(default=0, blank=True, null=True)

source = models.CharField(blank=True, null=True)

created = models.DateTimeField(default=timezone.now)
updated = models.DateTimeField(auto_now=True)

Expand Down

0 comments on commit a9188c9

Please sign in to comment.