From 464e2f1fa69a86c92793fdf156c0034fa1c363c1 Mon Sep 17 00:00:00 2001 From: Martin Carlsson Date: Mon, 25 Jan 2021 09:24:26 +0100 Subject: [PATCH] P5-6491 use market instead of territory for price calculations (#37) --- api/lib/prices.py | 6 +- api/models/__init__.py | 2 +- api/models/inquiry.py | 1 - api/models/market.py | 118 ++------------------------------- api/resources/prices.py | 68 +++++++++++-------- api/validators.py | 25 ------- docs/prices.md | 24 +++---- tests/factories.py | 6 ++ tests/lib/test_prices.py | 67 ++++++++----------- tests/resources/test_prices.py | 51 +++++++++----- tests/test_web.py | 15 ----- 11 files changed, 127 insertions(+), 256 deletions(-) diff --git a/api/lib/prices.py b/api/lib/prices.py index 5f5a644..8558165 100644 --- a/api/lib/prices.py +++ b/api/lib/prices.py @@ -56,7 +56,7 @@ def wallpaper_price( sqm = (width / 100) * (height / 100) - if market.territory == "US": + if market.name == "US": min_sqm = 2 else: min_sqm = 1 @@ -101,7 +101,7 @@ def canvas_cm2_price(cm2): def package_price(product_group, width, height, market): - if market.territory == "US": + if market.name == "US": if product_group in ["canvas", "poster"]: if width > 110: return 150 @@ -411,7 +411,7 @@ def framed_print_price( }, } - price += extra_shipping_price[sku].get(market.territory, 0) + price += extra_shipping_price[sku].get(market.name, 0) price *= market.price_adjustments diff --git a/api/models/__init__.py b/api/models/__init__.py index b1f1111..85a5b6c 100644 --- a/api/models/__init__.py +++ b/api/models/__init__.py @@ -4,7 +4,7 @@ from .designer import Designer from .category import Category from .contract_customer import ContractCustomer from .inquiry import Inquiry -from .product import Material, Product, Currencies +from .product import Material, Product, Currencies, PrintProduct from .order import ( Order, OrderField, diff --git a/api/models/inquiry.py b/api/models/inquiry.py index 345b329..c4c4c34 100644 --- a/api/models/inquiry.py +++ b/api/models/inquiry.py @@ -1,7 +1,6 @@ # coding=UTF-8 from api.extensions import db -from api.models import market as market_model from api.lib.utils import round_half_up diff --git a/api/models/market.py b/api/models/market.py index 0871676..e63366c 100644 --- a/api/models/market.py +++ b/api/models/market.py @@ -14,8 +14,11 @@ class Market(db.Model): vat = db.Column(db.Float, nullable=False) price_adjustments = db.Column("price_adjustment", db.Float, nullable=False) currency = db.Column(db.String(3), db.ForeignKey("product-currencies.iso3char")) + exchange_rate = db.Column(db.Float) _pc = db.relationship(Currencies, uselist=False) - exchange_rate = association_proxy("_pc", "exchange_rate") + exchange_rate = association_proxy( + "_pc", "exchange_rate", creator=lambda kw: Currencies(exchange_rate=kw) + ) def to_json(self): return { @@ -26,116 +29,3 @@ class Market(db.Model): "currency": self.currency, "exchange_rate": str(self.exchange_rate), } - - # hack: market need a territory to make it compatible with current price calculations code - # this will be removed in P5-6491 - @property - def territory(self): - if self.name == 'GLOBAL': - return 'EU' - return self.name - - -class PriceAdjustments(db.Model): - - __tablename__ = "price_adjustments" - - territory = db.Column(db.String(2), primary_key=True) - adjustment = db.Column(db.Numeric) - - -# P5-6490 deprecated market model. Will be replaced with Market -class LegacyMarket(object): - def __init__(self, name, locale, currency, vat, iso3): - self.name = name - self.locale = locale - self.currency = currency - self.vat = vat - self.iso3 = iso3 - self.language = locale.split("_")[0] - self.territory = locale.split("_")[1] - self._price_adjustments = None - self._exchange_rate = None - self._price_adjustments = None - - @property - def price_adjustments(self): - if self._price_adjustments is None: - row = PriceAdjustments.query.filter_by(territory=self.territory).first() - if row: - self._price_adjustments = float(row.adjustment) - else: - self._price_adjustments = 1 - - return self._price_adjustments - - @price_adjustments.setter - def price_adjustments(self, value): - self._price_adjustments = value - - @property - def exchange_rate(self): - if self._exchange_rate is None: - row = Currencies.query.filter_by(iso3char=self.currency).first() - if row: - self._exchange_rate = row.exchange_rate - else: - self._exchange_rate = 1 - return self._exchange_rate - - @exchange_rate.setter - def exchange_rate(self, value): - self._exchange_rate = value - - def to_json(self): - return { - "name": self.name, - "locale": self.locale, - "currency": self.currency, - "vat": self.vat, - "iso3": self.iso3, - "language": self.language, - "territory": self.territory, - "price_adjustments": self.price_adjustments, - "exchange_rate": self.exchange_rate, - } - - def __repr__(self): - return self.locale - - -_markets = [ - ("Sweden", "sv_SE", "SEK", 1.25, "SWE"), - ("Norway", "nn_NO", "NOK", 1.25, "NOR"), - ("United kingdom", "en_GB", "GBP", 1.20, "GBR"), - ("Denmark", "da_DK", "DKK", 1.25, "DNK"), - ("Finland", "fi_FI", "EUR", 1.24, "FIN"), - ("Germany", "de_DE", "EUR", 1.19, "DEU"), - ("Netherlands", "nl_NL", "EUR", 1.21, "NLD"), - ("Austria", "de_AT", "EUR", 1.20, "AUT"), - ("USA", "en_US", "USD", 1, "USA"), - ("Spain", "es_ES", "EUR", 1.21, "ESP"), - ("France", "fr_FR", "EUR", 1.20, "FRA"), - ("Poland", "pl_PL", "EUR", 1.23, "POL"), - ("International", "en_EU", "EUR", 1.25, None), - ("Italy", "it_IT", "EUR", 1.22, "ITA"), -] - - -def iter_markets(): - for market in _markets: - yield LegacyMarket(*market) - - -def from_locale(locale): - for market in iter_markets(): - if market.locale == locale: - return market - return None - - -def from_territory(territory): - for market in iter_markets(): - if market.territory == territory.upper(): - return market - return None diff --git a/api/resources/prices.py b/api/resources/prices.py index 2e2323c..444e6b5 100644 --- a/api/resources/prices.py +++ b/api/resources/prices.py @@ -1,10 +1,14 @@ from flask import Blueprint, request, jsonify, abort from api.helpers import check_api_key -from api.validators import validate_territory, validate_number, validate_any_of -from api.models.inquiry import Inquiry -from api.models.product import Material, Product, PrintProduct -from api.models.contract_customer import ContractCustomer -from api.models import market as market_model +from api.validators import validate_number, validate_any_of, validate_exists +from api.models import ( + Inquiry, + Market, + Material, + Product, + PrintProduct, + ContractCustomer, +) from api.lib import pwinty from api.lib.prices import ( wallpaper_price, @@ -41,11 +45,12 @@ def get_designer(): @mod.route("/wallpaper", methods=["GET"]) @validate_number("width", "height") -@validate_territory("territory") +@validate_exists("market") def wallpaper(): width = request.args.get("width", type=int) height = request.args.get("height", type=int) - market = market_model.from_territory(request.args.get("territory")) + market_id = request.args.get("market", type=int) + market = Market.query.get_or_404(market_id) designer = get_designer() reseller = get_reseller() @@ -100,9 +105,10 @@ def wallpaper(): @mod.route("/canvas", methods=["GET"]) @validate_number("width", "height") -@validate_territory("territory") +@validate_exists("market") def canvas(): - market = market_model.from_territory(request.args.get("territory")) + market_id = request.args.get("market", type=int) + market = Market.query.get_or_404(market_id) reseller = get_reseller() designer = get_designer() @@ -148,11 +154,12 @@ def canvas(): @mod.route("/poster", methods=["GET"]) @validate_number("width", "height") -@validate_territory("territory") +@validate_exists("market") def poster(): + market_id = request.args.get("market", type=int) width = request.args.get("width", type=int) height = request.args.get("height", type=int) - market = market_model.from_territory(request.args.get("territory")) + market = Market.query.get_or_404(market_id) reseller = get_reseller() designer = get_designer() @@ -192,9 +199,10 @@ def poster(): @mod.route("/diy-frame", methods=["GET"]) @validate_number("width", "height") -@validate_territory("territory") +@validate_exists("market") def diy_frame(): - market = market_model.from_territory(request.args.get("territory")) + market_id = request.args.get("market", type=int) + market = Market.query.get_or_404(market_id) reseller = get_reseller() width, height = calculate_canvas_limits( request.args.get("width", type=int), @@ -220,10 +228,11 @@ def diy_frame(): @mod.route("/poster-hanger", methods=["GET"]) @validate_number("width") -@validate_territory("territory") +@validate_exists("market") def poster_hanger(): + market_id = request.args.get("market", type=int) width = request.args.get("width", type=int) - market = market_model.from_territory(request.args.get("territory")) + market = Market.query.get_or_404(market_id) reseller = get_reseller() data = [ @@ -242,11 +251,12 @@ def poster_hanger(): @mod.route("/framed-print", methods=["GET"]) -@validate_territory("territory") @validate_any_of("sku", pwinty.SKU_CODES) +@validate_exists("market") def framed_print(): + market_id = request.args.get("market", type=int) sku = request.args.get("sku", type=str) - market = market_model.from_territory(request.args.get("territory")) + market = Market.query.get_or_404(market_id) reseller = get_reseller() designer = get_designer() @@ -547,12 +557,12 @@ def inquiry(inquiry_id): return jsonify(data) -@mod.route("/list-prices//wallpaper") -def list_prices_wallpaper(territory): +@mod.route("/list-prices//wallpaper") +def list_prices_wallpaper(market_id): wallpapers = PrintProduct.query.wallpapers().all() result = {} material = Material.query.filter(Material.name == "standard-wallpaper").one() - market = market_model.from_territory(territory) + market = Market.query.get_or_404(market_id) for wallpaper in wallpapers: result[wallpaper.id] = wallpaper_m2_price( @@ -561,10 +571,10 @@ def list_prices_wallpaper(territory): return jsonify(result) -@mod.route("/list-prices//canvas") -def list_prices_canvas(territory): +@mod.route("/list-prices//canvas") +def list_prices_canvas(market_id): canvases = PrintProduct.query.canvases().all() - market = market_model.from_territory(territory) + market = Market.query.get_or_404(market_id) result = {} for canvas in canvases: @@ -578,10 +588,10 @@ def list_prices_canvas(territory): return jsonify(result) -@mod.route("/list-prices//poster") -def list_prices_posters(territory): +@mod.route("/list-prices//poster") +def list_prices_posters(market_id): posters = PrintProduct.query.posters().all() - market = market_model.from_territory(territory) + market = Market.query.get_or_404(market_id) result = {} for poster in posters: @@ -595,8 +605,8 @@ def list_prices_posters(territory): return jsonify(result) -@mod.route("/list-prices//framed-print") -def list_prices_framed_prints(territory): +@mod.route("/list-prices//framed-print") +def list_prices_framed_prints(market_id): from api.extensions import db from types import SimpleNamespace @@ -611,7 +621,7 @@ def list_prices_framed_prints(territory): ) rows = db.session.execute(query, {"group": 8}) - market = market_model.from_territory(territory) + market = Market.query.get_or_404(market_id) result = {} for row in rows: diff --git a/api/validators.py b/api/validators.py index 1fc3967..e508fce 100644 --- a/api/validators.py +++ b/api/validators.py @@ -1,7 +1,6 @@ from functools import wraps from flask import request import jsonschema -from api.models.market import iter_markets class ValidationError(Exception): @@ -72,30 +71,6 @@ def validate_number(*keys): return decorator -def validate_territory(*keys): - def decorator(f): - @wraps(f) - def wrapper(*args, **kwargs): - for key in keys: - if not key in request.args: - raise ValidationError("No {} is specified".format(key)) - value = request.args.get(key) - valid = False - for market in iter_markets(): - if market.territory == value: - valid = True - break - - if not valid: - raise ValidationError("{} is not a valid territory".format(value)) - - return f(*args, **kwargs) - - return wrapper - - return decorator - - def validate_any_of(key, values): def decorator(f): @wraps(f) diff --git a/docs/prices.md b/docs/prices.md index 804cfca..18b833c 100644 --- a/docs/prices.md +++ b/docs/prices.md @@ -10,14 +10,14 @@ Calculate price inc VAT of a wallpaper product. The response will contain prices | --------- | ------------| | `width` | Wallpaper width in cm e.g `100` | | `height` | Wallpaper height in cm e.g `50` | -| `territory` | Two letter iso code e.g `SE`. Territory affect the price in different ways like VAT, currency exchanges etc. | +| `market` | Market id e.g `1` for sweden. Markets affect the price in different ways like VAT, currency exchanges etc. | | `dealerurl` | Optional parameter e.g `ackes`. Should always be used when calling the api from a dealerstore. If a dealerstore exists with this url additional fees will be applied to the price. | | `product` | Optional parameter e.g `43894`. If a product with this id exists the api will calculate the price for the given product. | Sample request -`curl -X GET 'https://api.photowall.com/prices/wallpaper?width=200&height=200&territory=SE&dealerurl=ackes&product=43894'` +`curl -X GET 'https://api.photowall.com/prices/wallpaper?width=200&height=200&market=1&dealerurl=ackes&product=43894'` Sample response ``` @@ -51,14 +51,14 @@ Calculate price inc VAT of a canvas product. The response will contain informati | --------- | ------------| | `width` | Canvas width in cm e.g `100` | | `height` | Canvas height in cm e.g `50` | -| `territory` | Two letter iso code e.g `SE`. Territory affect the price in different ways like VAT, currency exchanges etc. | +| `market` | Market id e.g `1` for sweden. Markets affect the price in different ways like VAT, currency exchanges etc. | | `dealerurl` | Optional parameter e.g `ackes`. Should always be used when calling the api from a dealerstore. If a dealerstore exists with this url additional fees will be applied to the price. | | `product` | Optional parameter e.g `43894`. If a product with this id exists the api will calculate the price for the given product. | Sample request -`curl -X GET 'https://api.photowall.com/prices/canvas?width=100&height=50&territory=PL` +`curl -X GET 'https://api.photowall.com/prices/canvas?width=100&height=50&market=14` Sample response ``` @@ -94,13 +94,13 @@ Calculate price inc VAT of a poster product. The response will contain prices fo | --------- | ------------| | `width` | Poster width in cm e.g `100` | | `height` | Poster height in cm e.g `50` | -| `territory` | Two letter iso code e.g `SE`. Territory affect the price in different ways like VAT, currency exchanges etc. | +| `market` | Market id e.g `1` for sweden. Markets affect the price in different ways like VAT, currency exchanges etc. | | `dealerurl` | Optional parameter e.g `ackes`. Should always be used when calling the api from a dealerstore. If a dealerstore exists with this url additional fees will be applied to the price. | | `product` | Optional parameter e.g `43894`. If a product with this id exists the api will calculate the price for the given product. | Sample request -`curl -X GET 'https://api.photowall.com/prices/poster?width=50&height=30&territory=SE&product=46654` +`curl -X GET 'https://api.photowall.com/prices/poster?width=50&height=30&market=1&product=46654` Sample response ``` @@ -131,13 +131,13 @@ Calculate price inc VAT of a framed print product. | Parameter | Description | | --------- | ------------| | `sku` | Pwinty SKU number | -| `territory` | Two letter iso code e.g `SE`. Territory affect the price in different ways like VAT, currency exchanges etc. | +| `market` | Market id e.g `1` for sweden. Markets affect the price in different ways like VAT, currency exchanges etc. | | `dealerurl` | Optional parameter e.g `ackes`. Should always be used when calling the api from a dealerstore. If a dealerstore exists with this url additional fees will be applied to the price. | | `product` | Optional parameter e.g `43894`. If a product with this id exists the api will calculate the price for the given product. | Sample request -`curl -X GET 'https://api.photowall.com/prices/framed-print?sku=GLOBAL-CFP-12x12&territory=SE` +`curl -X GET 'https://api.photowall.com/prices/framed-print?sku=GLOBAL-CFP-12x12&market=1` Sample response ``` @@ -160,13 +160,13 @@ Calculate the price for the "do it yourself frame" (DIY). Note that width and he | --------- | ------------| | `width` | Canvas width in cm e.g `100` | | `height` | Canvas height in cm e.g `50` | -| `territory` | Two letter iso code e.g `SE`. Territory affect the price in different ways like VAT, currency exchanges etc. | +| `market` | Market id e.g `1` for sweden. Markets affect the price in different ways like VAT, currency exchanges etc. | | `dealerurl` | Optional parameter e.g `ackes`. Should always be used when calling the api from a dealerstore. If a dealerstore exists with this url additional fees will be applied to the price. | Sample request -`curl -X GET 'https://api.photowall.com/prices/diy-frame?width=150&height=150&territory=SE` +`curl -X GET 'https://api.photowall.com/prices/diy-frame?width=150&height=150&market=1` Sample response ``` @@ -188,13 +188,13 @@ Calculate the price for a poster hanger | Parameter | Description | | --------- | ------------| | `width` | Poster hanger width in cm e.g `100` | -| `territory` | Two letter iso code e.g `SE`. Territory affect the price in different ways like VAT, currency exchanges etc. | +| `market` | Market id e.g `1` for sweden. Markets affect the price in different ways like VAT, currency exchanges etc. | | `dealerurl` | Optional parameter e.g `ackes`. Should always be used when calling the api from a dealerstore. If a dealerstore exists with this url additional fees will be applied to the price. | Sample request -`curl -X GET 'https://api.photowall.com/prices/poster-hanger?width=50&territory=SE` +`curl -X GET 'https://api.photowall.com/prices/poster-hanger?width=50&market=1` Sample response ``` diff --git a/tests/factories.py b/tests/factories.py index 9215bd9..c246106 100644 --- a/tests/factories.py +++ b/tests/factories.py @@ -1,5 +1,6 @@ import factory from api import models +from api import db class DesignerFactory(factory.Factory): @@ -25,3 +26,8 @@ class InquiryFactory(factory.Factory): price_retouch = None pricepremium = 0 + + +class MarketFactory(factory.Factory): + class Meta: + model = models.Market diff --git a/tests/lib/test_prices.py b/tests/lib/test_prices.py index 80b6ed2..c65eb76 100644 --- a/tests/lib/test_prices.py +++ b/tests/lib/test_prices.py @@ -3,47 +3,38 @@ import unittest2 import mock from api.lib.prices import * -from tests.factories import DesignerFactory, ContractCustomerFactory, InquiryFactory -from api.models import market -from api.models import Inquiry -from api.models import Product -from api.models.product import Material +from tests.factories import ( + DesignerFactory, + ContractCustomerFactory, + InquiryFactory, + MarketFactory, +) +from api.models import Inquiry, Product, Material + +sweden = MarketFactory.build(name="SE", exchange_rate=1, price_adjustments=1, vat=1.25) +denmark = MarketFactory.build( + name="DK", price_adjustments=1.185, exchange_rate=0.799000, vat=1.25 +) +us = MarketFactory.build(name="US", price_adjustments=1.06, exchange_rate=0.1198, vat=1) +nl = MarketFactory.build(name="NL", price_adjustments=1.15, exchange_rate=0.1, vat=1.21) +eu = MarketFactory.build(name="EU", price_adjustments=1.13, exchange_rate=0.1, vat=1.25) + +designer = DesignerFactory.build( + pricepremium_wallpaper=17.647, + pricepremium_canvas=8, + pricepremium_poster=15.50, + pricepremium_framed_print=13, +) -sweden = market.from_territory("SE") -sweden.price_adjustments = 1 -sweden.exchange_rate = 1 +reseller = ContractCustomerFactory.build(pricepremium=15) -denmark = market.from_territory("DK") -denmark.price_adjustments = 1.185 -denmark.exchange_rate = 0.799000 - -us = market.from_territory("US") -us.price_adjustments = 1.06 -us.exchange_rate = 0.119800 - -nl = market.from_territory("NL") -nl.price_adjustments = 1.15 -nl.exchange_rate = 0.100000 - -eu = market.from_territory("EU") -eu.price_adjustments = 1.13 -eu.exchange_rate = 0.100000 - -designer = DesignerFactory() -designer.pricepremium_wallpaper = 17.647 -designer.pricepremium_canvas = 8 -designer.pricepremium_poster = 15.50 -designer.pricepremium_framed_print = 13 - - -reseller = ContractCustomerFactory(pricepremium=15) - -designer_moomin = DesignerFactory() -designer_moomin.pricepremium_wallpaper = 2 -designer_moomin.pricepremium_canvas = 3 -designer_moomin.pricepremium_poster = 4 -designer_moomin.pricepremium_framed_print = 7 +designer_moomin = DesignerFactory.build( + pricepremium_wallpaper=2, + pricepremium_canvas=3, + pricepremium_poster=4, + pricepremium_framed_print=7, +) standard_wallpaper = Material(name="standard-wallpaper", price=23600) premium_wallpaper = Material(name="premium-wallpaper", price=26000) diff --git a/tests/resources/test_prices.py b/tests/resources/test_prices.py index c5ff15f..5b29534 100644 --- a/tests/resources/test_prices.py +++ b/tests/resources/test_prices.py @@ -17,16 +17,21 @@ class TestEndpoints(flask_testing.TestCase): db.session.remove() db.drop_all() + def _add_market(self): + currency = Currencies(iso3char="SEK", exchange_rate=1) + db.session.add(currency) + market = Market(name="SE", vat=1.25, currency="SEK", price_adjustments=1) + db.session.add(market) + db.session.commit() + return market + def _add_material(self, name, groupid, price): material = Material(name=name, price=price) db.session.add(material) db.session.commit() return material - def _add_inquiry(self, product_group, price_retouch=100): - currency = Currencies(iso3char="SEK", exchange_rate=1) - db.session.add(currency) - market = Market(name="SE", vat=1.25, currency='SEK', price_adjustments=1) + def _add_inquiry(self, market, product_group, price_retouch=100): inquiry = Inquiry( market=market, product_group=product_group, @@ -56,9 +61,10 @@ class TestEndpoints(flask_testing.TestCase): return product def test_wallpaper(self): + self._add_market() self._add_material("standard-wallpaper", 1, 23600) self._add_material("premium-wallpaper", 1, 26000) - response = self.client.get("/prices/wallpaper?width=200&height=10&territory=SE") + response = self.client.get("/prices/wallpaper?width=200&height=10&market=1") self.assert200(response) expected = [ { @@ -81,12 +87,13 @@ class TestEndpoints(flask_testing.TestCase): self.assertEqual(expected, response.json["data"]) def test_wallpaper_with_product(self): + self._add_market() self._add_material("standard-wallpaper", 1, 23600) self._add_material("premium-wallpaper", 1, 26000) designer = self._add_designer(pricepremium_wallpaper=4) product = self._add_product(designer) response = self.client.get( - "/prices/wallpaper?width=200&height=200&territory=SE&product=1" + "/prices/wallpaper?width=200&height=200&market=1&product=1" ) expected = [ { @@ -109,7 +116,8 @@ class TestEndpoints(flask_testing.TestCase): self.assertEqual(expected, response.json["data"]) def test_canvas(self): - response = self.client.get("/prices/canvas?width=200&height=10&territory=SE") + self._add_market() + response = self.client.get("/prices/canvas?width=200&height=10&market=1") self.assert200(response) expected = [ { @@ -130,10 +138,11 @@ class TestEndpoints(flask_testing.TestCase): self.assertEqual(expected, response.json["data"]) def test_canvas_with_product(self): + self._add_market() designer = self._add_designer(pricepremium_canvas=2) product = self._add_product(designer) response = self.client.get( - "/prices/canvas?width=200&height=10&territory=SE&product=1" + "/prices/canvas?width=200&height=10&market=1&product=1" ) self.assert200(response) expected = [ @@ -155,7 +164,8 @@ class TestEndpoints(flask_testing.TestCase): self.assertEqual(expected, response.json["data"]) def test_diy_frame(self): - response = self.client.get("/prices/diy-frame?width=200&height=50&territory=SE") + self._add_market() + response = self.client.get("/prices/diy-frame?width=200&height=50&market=1") self.assert200(response) product = response.json["data"][0] self.assertEqual(150, product["width"], "it adjusts measurements") @@ -163,7 +173,8 @@ class TestEndpoints(flask_testing.TestCase): self.assertAlmostEqual(731.25, product["price"], places=2) def test_poster(self): - response = self.client.get("/prices/poster?width=50&height=30&territory=SE") + self._add_market() + response = self.client.get("/prices/poster?width=50&height=30&market=1") self.assert200(response) expected = [ { @@ -184,7 +195,8 @@ class TestEndpoints(flask_testing.TestCase): self.assertEqual(expected, response.json["data"]) def test_poster_hanger(self): - response = self.client.get("/prices/poster-hanger?width=50&territory=SE") + self._add_market() + response = self.client.get("/prices/poster-hanger?width=50&market=1") self.assert200(response) product = response.json["data"][0] self.assertEqual(210.0, product["price"]) @@ -192,36 +204,39 @@ class TestEndpoints(flask_testing.TestCase): self.assertEqual(50, product["width"]) def test_inquiry_wallpaper(self): + market = self._add_market() self._add_material("standard-wallpaper", 1, 236) self._add_material("premium-wallpaper", 1, 260) - self._add_inquiry(product_group="photo-wallpaper") + self._add_inquiry(market=market, product_group="photo-wallpaper") response = self.client.get("/prices/inquiry/1?width=100&height=100") self.assert200(response) self.assertEqual("wallpaper", response.json["group"]) def test_inquiry_canvas(self): + market = self._add_market() self._add_material("standard-canvas", 2, 799.2) - self._add_inquiry(product_group="canvas") + self._add_inquiry(market=market, product_group="canvas") response = self.client.get("/prices/inquiry/1?width=100&height=100") self.assert200(response) self.assertEqual("canvas", response.json["group"]) def test_inquiry_poster(self): - self._add_inquiry(product_group="poster") + market = self._add_market() + self._add_inquiry(market=market, product_group="poster") response = self.client.get("/prices/inquiry/1?width=100&height=100") self.assert200(response) self.assertEqual("poster", response.json["group"]) def test_framed_print(self): - response = self.client.get( - "/prices/framed-print?sku=GLOBAL-CFP-12x12&territory=SE" - ) + self._add_market() + response = self.client.get("/prices/framed-print?sku=GLOBAL-CFP-12x12&market=1") self.assert200(response) expected = [{"price": 597.5, "price_excl_vat": 478, "sku": "GLOBAL-CFP-12x12"}] self.assertEqual(expected, response.json["data"]) def test_framed_print_invalid_sku(self): - response = self.client.get("/prices/framed-print?sku=invalid&territory=SE") + self._add_market() + response = self.client.get("/prices/framed-print?sku=invalid&market=1") self.assert400(response) self.assertEqual( "sku contains an invalid value, must be one of: {}".format( diff --git a/tests/test_web.py b/tests/test_web.py index 1bdd7ed..27c0446 100644 --- a/tests/test_web.py +++ b/tests/test_web.py @@ -10,7 +10,6 @@ from api.validators import ( ValidationError, validate_exists, validate_number, - validate_territory, validate_jsonschema, validate_any_of, ) @@ -89,20 +88,6 @@ class TestValidators(flask_testing.TestCase): self.assertEqual(b"age is not a number", self.client.get("/?age=aaa").data) self.assertEqual(b"ok", self.client.get("/?age=1").data) - def test_validate_territory(self): - app = self.app - - @app.route("/", methods=["GET"]) - @validate_territory("territory") - def index(): - return "ok" - - self.assertEqual(b"No territory is specified", self.client.get("/").data) - self.assertEqual( - b"abc is not a valid territory", self.client.get("/?territory=abc").data - ) - self.assertEqual(b"ok", self.client.get("/?territory=SE").data) - def test_validate_jsonschema(self): app = self.app schema = {