diff --git a/pricing/VERSION b/pricing/VERSION index 5625e59..7e32cd5 100644 --- a/pricing/VERSION +++ b/pricing/VERSION @@ -1 +1 @@ -1.2 +1.3 diff --git a/pricing/app/app.py b/pricing/app/app.py index 3266829..41592a1 100644 --- a/pricing/app/app.py +++ b/pricing/app/app.py @@ -2,8 +2,10 @@ import os from flask import Flask, jsonify, request app = Flask(__name__) -VERSION = "v2" +VERSION = "v3" UNIT_PRICE = 12 +BULK_MIN_QUANTITY = 10 +BULK_DISCOUNT = 0.10 COMMIT = os.environ.get("APP_COMMIT", "development") BUILD = os.environ.get("APP_SEMVER", "development") @@ -11,7 +13,10 @@ BUILD = os.environ.get("APP_SEMVER", "development") def total_price(quantity): if quantity < 1 or quantity > 1000: raise ValueError("quantity must be between 1 and 1000") - return UNIT_PRICE * quantity + total = UNIT_PRICE * quantity + if quantity >= BULK_MIN_QUANTITY: + total = round(total * (1 - BULK_DISCOUNT), 2) + return total @app.get("/") diff --git a/pricing/app/test_app.py b/pricing/app/test_app.py index 669294b..c55c386 100644 --- a/pricing/app/test_app.py +++ b/pricing/app/test_app.py @@ -12,12 +12,18 @@ class PricingTests(unittest.TestCase): self.assertEqual(response.status_code, 200) self.assertEqual(response.json["total"], 36) + def test_bulk_discount(self): + self.assertEqual(total_price(9), 108) + self.assertEqual(total_price(10), 108.0) + r = self.client.get("/price?quantity=12") + self.assertEqual(r.json["total"], 129.6) + def test_invalid_quantity(self): for value in ["0", "-1", "1001", "abc", "1.5"]: self.assertEqual(self.client.get("/price?quantity=" + value).status_code, 400) def test_release_identity(self): - self.assertEqual(self.client.get("/").json["version"], "v2") + self.assertEqual(self.client.get("/").json["version"], "v3") self.assertEqual(self.client.get("/health").json["status"], "ok")