Skip to content

Commit

Permalink
feat(API): Allow updating Proof location fields (#539)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn authored Oct 25, 2024
1 parent d5481f2 commit 8554eaf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
42 changes: 31 additions & 11 deletions open_prices/api/proofs/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@
from open_prices.proofs.models import Proof
from open_prices.users.factories import SessionFactory

PROOF = {
"type": proof_constants.TYPE_RECEIPT,
"currency": "EUR",
"date": "2024-01-01",
"location_osm_id": 652825274,
"location_osm_type": location_constants.OSM_TYPE_NODE,
"receipt_price_count": 5,
"receipt_price_total": Decimal("45.10"),
}

LOCATION_OSM_NODE_652825274 = {
"type": location_constants.TYPE_OSM,
"osm_id": 652825274,
Expand All @@ -32,6 +22,21 @@
"osm_lat": "45.1805534",
"osm_lon": "5.7153387",
}
LOCATION_OSM_NODE_6509705997 = {
"type": location_constants.TYPE_OSM,
"osm_id": 6509705997,
"osm_type": location_constants.OSM_TYPE_NODE,
"osm_name": "Carrefour",
}
PROOF = {
"type": proof_constants.TYPE_RECEIPT,
"currency": "EUR",
"date": "2024-01-01",
"location_osm_id": LOCATION_OSM_NODE_652825274["osm_id"],
"location_osm_type": LOCATION_OSM_NODE_652825274["osm_type"],
"receipt_price_count": 5,
"receipt_price_total": Decimal("45.10"),
}


def create_fake_image() -> bytes:
Expand Down Expand Up @@ -268,7 +273,13 @@ def setUpTestData(cls):
**PROOF, price_count=15, owner=cls.user_session_1.user.user_id
)
cls.url = reverse("api:proofs-detail", args=[cls.proof.id])
cls.data = {"currency": "USD", "price_count": 20}
cls.data = {
"location_osm_id": LOCATION_OSM_NODE_6509705997["osm_id"],
"location_osm_type": LOCATION_OSM_NODE_6509705997["osm_type"],
"currency": "USD",
"receipt_price_count": 4,
"price_count": 20,
}

def test_proof_update(self):
# anonymous
Expand All @@ -293,14 +304,23 @@ def test_proof_update(self):
)
self.assertEqual(response.status_code, 404) # 403 ?
# authenticated
self.assertEqual(
self.proof.location_osm_id, LOCATION_OSM_NODE_652825274["osm_id"]
)
self.assertEqual(self.proof.currency, "EUR")
self.assertEqual(self.proof.receipt_price_count, 5)
response = self.client.patch(
self.url,
self.data,
headers={"Authorization": f"Bearer {self.user_session_1.token}"},
content_type="application/json",
)
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.data["location_osm_id"], LOCATION_OSM_NODE_6509705997["osm_id"]
)
self.assertEqual(response.data["currency"], "USD")
self.assertEqual(response.data["receipt_price_count"], 4)
self.assertEqual(Proof.objects.get(id=self.proof.id).price_count, 15) # ignored


Expand Down
6 changes: 2 additions & 4 deletions open_prices/proofs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,15 @@ def with_stats(self):
class Proof(models.Model):
FILE_FIELDS = ["file_path", "mimetype", "image_thumb_path"]
UPDATE_FIELDS = [
# "location_osm_id",
# "location_osm_type",
"location_osm_id",
"location_osm_type",
"type",
"currency",
"date",
"receipt_price_count",
"receipt_price_total",
]
CREATE_FIELDS = UPDATE_FIELDS + [
"location_osm_id",
"location_osm_type",
"location_id", # extra field (optional)
]
FIX_PRICE_FIELDS = ["location", "date", "currency"]
Expand Down
1 change: 1 addition & 0 deletions open_prices/proofs/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"osm_name": "Monoprix",
}
LOCATION_OSM_NODE_6509705997 = {
"type": location_constants.TYPE_OSM,
"osm_id": 6509705997,
"osm_type": location_constants.OSM_TYPE_NODE,
"osm_name": "Carrefour",
Expand Down

0 comments on commit 8554eaf

Please sign in to comment.