Skip to content

Commit

Permalink
Test fixes for product pricing
Browse files Browse the repository at this point in the history
Note that a number of fixes were made as some tests were date-sensitive,
i.e. they broke when going from 2023 to 2024.

Refs: PV-758
  • Loading branch information
danjacob-anders authored and mhieta committed Jan 8, 2024
1 parent e39badc commit 8ade745
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 87 deletions.
2 changes: 1 addition & 1 deletion parking_permits/models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def get_talpa_pricing(self, is_low_emission, is_secondary):
}
"""
price_gross = self.get_modified_unit_price(is_low_emission, is_secondary)
price_net = price_gross / (1 + (self.vat or 0))
price_net = price_gross / Decimal(1 + (self.vat or 0))

return {
"price_gross": round_up(price_gross),
Expand Down
42 changes: 21 additions & 21 deletions parking_permits/tests/models/test_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,30 @@ def test_create_for_customer_should_create_order_with_items(self):
self.assertEqual(order_items[1].unit_price, Decimal(50))
self.assertEqual(order_items[1].quantity, 2)

@freeze_time("2023-03-15")
def test_create_for_permits_should_create_order_items_with_start_end_date_for_open_ended_permits(
self,
):
start_time = timezone.now()
permit = ParkingPermitFactory(
parking_zone=self.zone,
customer=self.customer,
contract_type=ContractType.OPEN_ENDED,
status=ParkingPermitStatus.DRAFT,
start_time=start_time,
month_count=6,
)
order = Order.objects.create_for_permits([permit])
order_items = order.order_items.all().order_by("-quantity")
self.assertEqual(order_items.count(), 1)
order_item = order_items[0]
self.assertEqual(
order_item.start_time.date(), timezone.localtime(start_time).date()
)
self.assertEqual(
order_item.end_time.date(),
get_end_time(start_time, 1).date(),
)
with freeze_time(date(CURRENT_YEAR, 3, 5)):
start_time = timezone.now()
permit = ParkingPermitFactory(
parking_zone=self.zone,
customer=self.customer,
contract_type=ContractType.OPEN_ENDED,
status=ParkingPermitStatus.DRAFT,
start_time=start_time,
month_count=6,
)
order = Order.objects.create_for_permits([permit])
order_items = order.order_items.all().order_by("-quantity")
self.assertEqual(order_items.count(), 1)
order_item = order_items[0]
self.assertEqual(
order_item.start_time.date(), timezone.localtime(start_time).date()
)
self.assertEqual(
order_item.end_time.date(),
get_end_time(start_time, 1).date(),
)

def test_create_renewable_order_should_create_renewal_order(self):
start_time = timezone.make_aware(datetime(CURRENT_YEAR, 3, 15))
Expand Down
4 changes: 2 additions & 2 deletions parking_permits/tests/models/test_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,6 @@ def test_get_talpa_pricing_with_discount(self):
pricing = product.get_talpa_pricing(True, False)
assert pricing == {
"price_gross": "10.00",
"price_net": "16.13",
"price_vat": "3.87",
"price_net": "8.06",
"price_vat": "1.94",
}
85 changes: 23 additions & 62 deletions parking_permits/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@

from ..models import Customer
from ..models.common import SourceSystem
from ..utils import round_up
from .keys import rsa_key


Expand Down Expand Up @@ -180,10 +179,16 @@ class BaseResolveEndpointTestCase(APITestCase):
permit_id = "80000001"

def prepare_test_data(
self, permit_id, unit_price, low_emission_discount, primary_permit=True
self,
permit_id,
unit_price,
low_emission_discount,
primary_permit=True,
):
start_date = datetime.date(2023, 1, 1)
end_date = datetime.date(2023, 12, 31)
now = datetime.date.today()
start_date = datetime.date(now.year, 1, 1)
end_date = datetime.date(now.year, 12, 31)

vehicle = VehicleFactory(
power_type=VehiclePowerTypeFactory(identifier="01", name="Bensin"),
emission=45,
Expand All @@ -198,10 +203,10 @@ def prepare_test_data(
euro_min_class_limit=6,
)
permit_start_time = datetime.datetime(
2023, 9, 12, 13, 46, 0, tzinfo=datetime.timezone.utc
now.year, 9, 12, 13, 46, 0, tzinfo=datetime.timezone.utc
)
permit_end_time = datetime.datetime(
2023, 10, 11, 23, 59, 0, tzinfo=datetime.timezone.utc
now.year, 10, 11, 23, 59, 0, tzinfo=datetime.timezone.utc
)
zone_a = ParkingZoneFactory(name="A")
product_detail_list = [[(start_date, end_date), unit_price]]
Expand Down Expand Up @@ -325,17 +330,13 @@ def test_resolve_price_view_for_normal_emission_vehicle(self):
)
response = self.client.post(url, data, format="json")
self.assertEqual(response.status_code, 200)
vat = product.vat
price_vat = unit_price * vat
self.assertEqual(
response.data.get("subscriptionId"), self.talpa_subscription_id
)
self.assertEqual(response.data.get("userId"), self.user_id)
self.assertEqual(
response.data.get("priceNet"), round_up(float(unit_price - price_vat))
)
self.assertEqual(response.data.get("priceVat"), round_up(float(price_vat)))
self.assertEqual(response.data.get("priceGross"), round_up(float(unit_price)))
self.assertEqual(response.data.get("priceNet"), "48.39")
self.assertEqual(response.data.get("priceVat"), "11.61")
self.assertEqual(response.data.get("priceGross"), "60.00")

def test_resolve_price_view_for_low_emission_vehicle(self):
unit_price = Decimal(60)
Expand All @@ -353,28 +354,15 @@ def test_resolve_price_view_for_low_emission_vehicle(self):
)
response = self.client.post(url, data, format="json")
self.assertEqual(response.status_code, 200)
vat = product.vat
low_emission_price = (
unit_price - unit_price * low_emission_discount
) # discount price
low_emission_price_vat = low_emission_price * vat
self.assertEqual(
response.data.get("subscriptionId"), self.talpa_subscription_id
)
self.assertEqual(response.data.get("userId"), self.user_id)
self.assertEqual(
response.data.get("priceGross"), round_up(float(low_emission_price))
)
self.assertEqual(
response.data.get("priceVat"), round_up(float(low_emission_price_vat))
)
self.assertEqual(
response.data.get("priceNet"),
round_up(float(low_emission_price - low_emission_price_vat)),
)
self.assertEqual(response.data.get("priceGross"), "45.00")
self.assertEqual(response.data.get("priceVat"), "8.71")
self.assertEqual(response.data.get("priceNet"), "36.29")

def test_resolve_price_view_for_secondary_normal_emission_vehicle(self):
secondary_vehicle_increase_rate = Decimal(0.5)
unit_price = Decimal(60)
low_emission_discount = Decimal(0)
permit, product = self.prepare_test_data(
Expand All @@ -390,28 +378,15 @@ def test_resolve_price_view_for_secondary_normal_emission_vehicle(self):
)
response = self.client.post(url, data, format="json")
self.assertEqual(response.status_code, 200)
vat = product.vat
secondary_vehicle_price = (
unit_price + unit_price * secondary_vehicle_increase_rate
) # secondary vehicle price
secondary_vehicle_price_vat = secondary_vehicle_price * vat
self.assertEqual(
response.data.get("subscriptionId"), self.talpa_subscription_id
)
self.assertEqual(response.data.get("userId"), self.user_id)
self.assertEqual(
response.data.get("priceGross"), round_up(float(secondary_vehicle_price))
)
self.assertEqual(
response.data.get("priceVat"), round_up(float(secondary_vehicle_price_vat))
)
self.assertEqual(
response.data.get("priceNet"),
round_up(float(secondary_vehicle_price - secondary_vehicle_price_vat)),
)
self.assertEqual(response.data.get("priceGross"), "90.00")
self.assertEqual(response.data.get("priceVat"), "17.42")
self.assertEqual(response.data.get("priceNet"), "72.58")

def test_resolve_price_view_for_secondary_low_emission_vehicle(self):
secondary_vehicle_increase_rate = Decimal(0.5)
unit_price = Decimal(60)
low_emission_discount = Decimal(0.25)
permit, product = self.prepare_test_data(
Expand All @@ -427,27 +402,13 @@ def test_resolve_price_view_for_secondary_low_emission_vehicle(self):
)
response = self.client.post(url, data, format="json")
self.assertEqual(response.status_code, 200)
vat = product.vat

modified_price = unit_price
modified_price -= modified_price * low_emission_discount
modified_price += modified_price * secondary_vehicle_increase_rate
modified_price_vat = modified_price * vat

self.assertEqual(
response.data.get("subscriptionId"), self.talpa_subscription_id
)
self.assertEqual(response.data.get("userId"), self.user_id)
self.assertEqual(
response.data.get("priceGross"), round_up(float(modified_price))
)
self.assertEqual(
response.data.get("priceVat"), round_up(float(modified_price_vat))
)
self.assertEqual(
response.data.get("priceNet"),
round_up(float(modified_price - modified_price_vat)),
)
self.assertEqual(response.data.get("priceGross"), "67.50")
self.assertEqual(response.data.get("priceVat"), "13.06")
self.assertEqual(response.data.get("priceNet"), "54.44")

def test_resolve_price_view_should_return_error_if_permit_products_missing(
self,
Expand Down
1 change: 0 additions & 1 deletion parking_permits/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
get_meta_item,
get_meta_value,
get_user_from_api_view_method_args,
round_up,
snake_to_camel_dict,
)

Expand Down

0 comments on commit 8ade745

Please sign in to comment.