P5-6491 use market instead of territory for price calculations (#37)

This commit is contained in:
Martin Carlsson
2021-01-25 09:24:26 +01:00
committed by GitHub
parent dee7344ba5
commit 464e2f1fa6
11 changed files with 127 additions and 256 deletions
+3 -3
View File
@@ -56,7 +56,7 @@ def wallpaper_price(
sqm = (width / 100) * (height / 100) sqm = (width / 100) * (height / 100)
if market.territory == "US": if market.name == "US":
min_sqm = 2 min_sqm = 2
else: else:
min_sqm = 1 min_sqm = 1
@@ -101,7 +101,7 @@ def canvas_cm2_price(cm2):
def package_price(product_group, width, height, market): def package_price(product_group, width, height, market):
if market.territory == "US": if market.name == "US":
if product_group in ["canvas", "poster"]: if product_group in ["canvas", "poster"]:
if width > 110: if width > 110:
return 150 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 price *= market.price_adjustments
+1 -1
View File
@@ -4,7 +4,7 @@ from .designer import Designer
from .category import Category from .category import Category
from .contract_customer import ContractCustomer from .contract_customer import ContractCustomer
from .inquiry import Inquiry from .inquiry import Inquiry
from .product import Material, Product, Currencies from .product import Material, Product, Currencies, PrintProduct
from .order import ( from .order import (
Order, Order,
OrderField, OrderField,
-1
View File
@@ -1,7 +1,6 @@
# coding=UTF-8 # coding=UTF-8
from api.extensions import db from api.extensions import db
from api.models import market as market_model
from api.lib.utils import round_half_up from api.lib.utils import round_half_up
+4 -114
View File
@@ -14,8 +14,11 @@ class Market(db.Model):
vat = db.Column(db.Float, nullable=False) vat = db.Column(db.Float, nullable=False)
price_adjustments = db.Column("price_adjustment", 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")) currency = db.Column(db.String(3), db.ForeignKey("product-currencies.iso3char"))
exchange_rate = db.Column(db.Float)
_pc = db.relationship(Currencies, uselist=False) _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): def to_json(self):
return { return {
@@ -26,116 +29,3 @@ class Market(db.Model):
"currency": self.currency, "currency": self.currency,
"exchange_rate": str(self.exchange_rate), "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
+39 -29
View File
@@ -1,10 +1,14 @@
from flask import Blueprint, request, jsonify, abort from flask import Blueprint, request, jsonify, abort
from api.helpers import check_api_key from api.helpers import check_api_key
from api.validators import validate_territory, validate_number, validate_any_of from api.validators import validate_number, validate_any_of, validate_exists
from api.models.inquiry import Inquiry from api.models import (
from api.models.product import Material, Product, PrintProduct Inquiry,
from api.models.contract_customer import ContractCustomer Market,
from api.models import market as market_model Material,
Product,
PrintProduct,
ContractCustomer,
)
from api.lib import pwinty from api.lib import pwinty
from api.lib.prices import ( from api.lib.prices import (
wallpaper_price, wallpaper_price,
@@ -41,11 +45,12 @@ def get_designer():
@mod.route("/wallpaper", methods=["GET"]) @mod.route("/wallpaper", methods=["GET"])
@validate_number("width", "height") @validate_number("width", "height")
@validate_territory("territory") @validate_exists("market")
def wallpaper(): def wallpaper():
width = request.args.get("width", type=int) width = request.args.get("width", type=int)
height = request.args.get("height", 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() designer = get_designer()
reseller = get_reseller() reseller = get_reseller()
@@ -100,9 +105,10 @@ def wallpaper():
@mod.route("/canvas", methods=["GET"]) @mod.route("/canvas", methods=["GET"])
@validate_number("width", "height") @validate_number("width", "height")
@validate_territory("territory") @validate_exists("market")
def canvas(): 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() reseller = get_reseller()
designer = get_designer() designer = get_designer()
@@ -148,11 +154,12 @@ def canvas():
@mod.route("/poster", methods=["GET"]) @mod.route("/poster", methods=["GET"])
@validate_number("width", "height") @validate_number("width", "height")
@validate_territory("territory") @validate_exists("market")
def poster(): def poster():
market_id = request.args.get("market", type=int)
width = request.args.get("width", type=int) width = request.args.get("width", type=int)
height = request.args.get("height", 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() reseller = get_reseller()
designer = get_designer() designer = get_designer()
@@ -192,9 +199,10 @@ def poster():
@mod.route("/diy-frame", methods=["GET"]) @mod.route("/diy-frame", methods=["GET"])
@validate_number("width", "height") @validate_number("width", "height")
@validate_territory("territory") @validate_exists("market")
def diy_frame(): 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() reseller = get_reseller()
width, height = calculate_canvas_limits( width, height = calculate_canvas_limits(
request.args.get("width", type=int), request.args.get("width", type=int),
@@ -220,10 +228,11 @@ def diy_frame():
@mod.route("/poster-hanger", methods=["GET"]) @mod.route("/poster-hanger", methods=["GET"])
@validate_number("width") @validate_number("width")
@validate_territory("territory") @validate_exists("market")
def poster_hanger(): def poster_hanger():
market_id = request.args.get("market", type=int)
width = request.args.get("width", 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() reseller = get_reseller()
data = [ data = [
@@ -242,11 +251,12 @@ def poster_hanger():
@mod.route("/framed-print", methods=["GET"]) @mod.route("/framed-print", methods=["GET"])
@validate_territory("territory")
@validate_any_of("sku", pwinty.SKU_CODES) @validate_any_of("sku", pwinty.SKU_CODES)
@validate_exists("market")
def framed_print(): def framed_print():
market_id = request.args.get("market", type=int)
sku = request.args.get("sku", type=str) 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() reseller = get_reseller()
designer = get_designer() designer = get_designer()
@@ -547,12 +557,12 @@ def inquiry(inquiry_id):
return jsonify(data) return jsonify(data)
@mod.route("/list-prices/<string:territory>/wallpaper") @mod.route("/list-prices/<int:market_id>/wallpaper")
def list_prices_wallpaper(territory): def list_prices_wallpaper(market_id):
wallpapers = PrintProduct.query.wallpapers().all() wallpapers = PrintProduct.query.wallpapers().all()
result = {} result = {}
material = Material.query.filter(Material.name == "standard-wallpaper").one() 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: for wallpaper in wallpapers:
result[wallpaper.id] = wallpaper_m2_price( result[wallpaper.id] = wallpaper_m2_price(
@@ -561,10 +571,10 @@ def list_prices_wallpaper(territory):
return jsonify(result) return jsonify(result)
@mod.route("/list-prices/<string:territory>/canvas") @mod.route("/list-prices/<int:market_id>/canvas")
def list_prices_canvas(territory): def list_prices_canvas(market_id):
canvases = PrintProduct.query.canvases().all() canvases = PrintProduct.query.canvases().all()
market = market_model.from_territory(territory) market = Market.query.get_or_404(market_id)
result = {} result = {}
for canvas in canvases: for canvas in canvases:
@@ -578,10 +588,10 @@ def list_prices_canvas(territory):
return jsonify(result) return jsonify(result)
@mod.route("/list-prices/<string:territory>/poster") @mod.route("/list-prices/<int:market_id>/poster")
def list_prices_posters(territory): def list_prices_posters(market_id):
posters = PrintProduct.query.posters().all() posters = PrintProduct.query.posters().all()
market = market_model.from_territory(territory) market = Market.query.get_or_404(market_id)
result = {} result = {}
for poster in posters: for poster in posters:
@@ -595,8 +605,8 @@ def list_prices_posters(territory):
return jsonify(result) return jsonify(result)
@mod.route("/list-prices/<string:territory>/framed-print") @mod.route("/list-prices/<int:market_id>/framed-print")
def list_prices_framed_prints(territory): def list_prices_framed_prints(market_id):
from api.extensions import db from api.extensions import db
from types import SimpleNamespace from types import SimpleNamespace
@@ -611,7 +621,7 @@ def list_prices_framed_prints(territory):
) )
rows = db.session.execute(query, {"group": 8}) rows = db.session.execute(query, {"group": 8})
market = market_model.from_territory(territory) market = Market.query.get_or_404(market_id)
result = {} result = {}
for row in rows: for row in rows:
-25
View File
@@ -1,7 +1,6 @@
from functools import wraps from functools import wraps
from flask import request from flask import request
import jsonschema import jsonschema
from api.models.market import iter_markets
class ValidationError(Exception): class ValidationError(Exception):
@@ -72,30 +71,6 @@ def validate_number(*keys):
return decorator 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 validate_any_of(key, values):
def decorator(f): def decorator(f):
@wraps(f) @wraps(f)
+12 -12
View File
@@ -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` | | `width` | Wallpaper width in cm e.g `100` |
| `height` | Wallpaper height in cm e.g `50` | | `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. | | `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. | | `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 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 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` | | `width` | Canvas width in cm e.g `100` |
| `height` | Canvas height in cm e.g `50` | | `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. | | `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. | | `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 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 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` | | `width` | Poster width in cm e.g `100` |
| `height` | Poster height in cm e.g `50` | | `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. | | `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. | | `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 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 Sample response
``` ```
@@ -131,13 +131,13 @@ Calculate price inc VAT of a framed print product.
| Parameter | Description | | Parameter | Description |
| --------- | ------------| | --------- | ------------|
| `sku` | Pwinty SKU number | | `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. | | `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. | | `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 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 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` | | `width` | Canvas width in cm e.g `100` |
| `height` | Canvas height in cm e.g `50` | | `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. | | `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 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 Sample response
``` ```
@@ -188,13 +188,13 @@ Calculate the price for a poster hanger
| Parameter | Description | | Parameter | Description |
| --------- | ------------| | --------- | ------------|
| `width` | Poster hanger width in cm e.g `100` | | `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. | | `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 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 Sample response
``` ```
+6
View File
@@ -1,5 +1,6 @@
import factory import factory
from api import models from api import models
from api import db
class DesignerFactory(factory.Factory): class DesignerFactory(factory.Factory):
@@ -25,3 +26,8 @@ class InquiryFactory(factory.Factory):
price_retouch = None price_retouch = None
pricepremium = 0 pricepremium = 0
class MarketFactory(factory.Factory):
class Meta:
model = models.Market
+29 -38
View File
@@ -3,47 +3,38 @@
import unittest2 import unittest2
import mock import mock
from api.lib.prices import * from api.lib.prices import *
from tests.factories import DesignerFactory, ContractCustomerFactory, InquiryFactory from tests.factories import (
from api.models import market DesignerFactory,
from api.models import Inquiry ContractCustomerFactory,
from api.models import Product InquiryFactory,
from api.models.product import Material 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") reseller = ContractCustomerFactory.build(pricepremium=15)
sweden.price_adjustments = 1
sweden.exchange_rate = 1
denmark = market.from_territory("DK") designer_moomin = DesignerFactory.build(
denmark.price_adjustments = 1.185 pricepremium_wallpaper=2,
denmark.exchange_rate = 0.799000 pricepremium_canvas=3,
pricepremium_poster=4,
us = market.from_territory("US") pricepremium_framed_print=7,
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
standard_wallpaper = Material(name="standard-wallpaper", price=23600) standard_wallpaper = Material(name="standard-wallpaper", price=23600)
premium_wallpaper = Material(name="premium-wallpaper", price=26000) premium_wallpaper = Material(name="premium-wallpaper", price=26000)
+33 -18
View File
@@ -17,16 +17,21 @@ class TestEndpoints(flask_testing.TestCase):
db.session.remove() db.session.remove()
db.drop_all() 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): def _add_material(self, name, groupid, price):
material = Material(name=name, price=price) material = Material(name=name, price=price)
db.session.add(material) db.session.add(material)
db.session.commit() db.session.commit()
return material return material
def _add_inquiry(self, product_group, price_retouch=100): def _add_inquiry(self, market, 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)
inquiry = Inquiry( inquiry = Inquiry(
market=market, market=market,
product_group=product_group, product_group=product_group,
@@ -56,9 +61,10 @@ class TestEndpoints(flask_testing.TestCase):
return product return product
def test_wallpaper(self): def test_wallpaper(self):
self._add_market()
self._add_material("standard-wallpaper", 1, 23600) self._add_material("standard-wallpaper", 1, 23600)
self._add_material("premium-wallpaper", 1, 26000) 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) self.assert200(response)
expected = [ expected = [
{ {
@@ -81,12 +87,13 @@ class TestEndpoints(flask_testing.TestCase):
self.assertEqual(expected, response.json["data"]) self.assertEqual(expected, response.json["data"])
def test_wallpaper_with_product(self): def test_wallpaper_with_product(self):
self._add_market()
self._add_material("standard-wallpaper", 1, 23600) self._add_material("standard-wallpaper", 1, 23600)
self._add_material("premium-wallpaper", 1, 26000) self._add_material("premium-wallpaper", 1, 26000)
designer = self._add_designer(pricepremium_wallpaper=4) designer = self._add_designer(pricepremium_wallpaper=4)
product = self._add_product(designer) product = self._add_product(designer)
response = self.client.get( 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 = [ expected = [
{ {
@@ -109,7 +116,8 @@ class TestEndpoints(flask_testing.TestCase):
self.assertEqual(expected, response.json["data"]) self.assertEqual(expected, response.json["data"])
def test_canvas(self): 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) self.assert200(response)
expected = [ expected = [
{ {
@@ -130,10 +138,11 @@ class TestEndpoints(flask_testing.TestCase):
self.assertEqual(expected, response.json["data"]) self.assertEqual(expected, response.json["data"])
def test_canvas_with_product(self): def test_canvas_with_product(self):
self._add_market()
designer = self._add_designer(pricepremium_canvas=2) designer = self._add_designer(pricepremium_canvas=2)
product = self._add_product(designer) product = self._add_product(designer)
response = self.client.get( 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) self.assert200(response)
expected = [ expected = [
@@ -155,7 +164,8 @@ class TestEndpoints(flask_testing.TestCase):
self.assertEqual(expected, response.json["data"]) self.assertEqual(expected, response.json["data"])
def test_diy_frame(self): 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) self.assert200(response)
product = response.json["data"][0] product = response.json["data"][0]
self.assertEqual(150, product["width"], "it adjusts measurements") 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) self.assertAlmostEqual(731.25, product["price"], places=2)
def test_poster(self): 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) self.assert200(response)
expected = [ expected = [
{ {
@@ -184,7 +195,8 @@ class TestEndpoints(flask_testing.TestCase):
self.assertEqual(expected, response.json["data"]) self.assertEqual(expected, response.json["data"])
def test_poster_hanger(self): 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) self.assert200(response)
product = response.json["data"][0] product = response.json["data"][0]
self.assertEqual(210.0, product["price"]) self.assertEqual(210.0, product["price"])
@@ -192,36 +204,39 @@ class TestEndpoints(flask_testing.TestCase):
self.assertEqual(50, product["width"]) self.assertEqual(50, product["width"])
def test_inquiry_wallpaper(self): def test_inquiry_wallpaper(self):
market = self._add_market()
self._add_material("standard-wallpaper", 1, 236) self._add_material("standard-wallpaper", 1, 236)
self._add_material("premium-wallpaper", 1, 260) 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") response = self.client.get("/prices/inquiry/1?width=100&height=100")
self.assert200(response) self.assert200(response)
self.assertEqual("wallpaper", response.json["group"]) self.assertEqual("wallpaper", response.json["group"])
def test_inquiry_canvas(self): def test_inquiry_canvas(self):
market = self._add_market()
self._add_material("standard-canvas", 2, 799.2) 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") response = self.client.get("/prices/inquiry/1?width=100&height=100")
self.assert200(response) self.assert200(response)
self.assertEqual("canvas", response.json["group"]) self.assertEqual("canvas", response.json["group"])
def test_inquiry_poster(self): 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") response = self.client.get("/prices/inquiry/1?width=100&height=100")
self.assert200(response) self.assert200(response)
self.assertEqual("poster", response.json["group"]) self.assertEqual("poster", response.json["group"])
def test_framed_print(self): def test_framed_print(self):
response = self.client.get( self._add_market()
"/prices/framed-print?sku=GLOBAL-CFP-12x12&territory=SE" response = self.client.get("/prices/framed-print?sku=GLOBAL-CFP-12x12&market=1")
)
self.assert200(response) self.assert200(response)
expected = [{"price": 597.5, "price_excl_vat": 478, "sku": "GLOBAL-CFP-12x12"}] expected = [{"price": 597.5, "price_excl_vat": 478, "sku": "GLOBAL-CFP-12x12"}]
self.assertEqual(expected, response.json["data"]) self.assertEqual(expected, response.json["data"])
def test_framed_print_invalid_sku(self): 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.assert400(response)
self.assertEqual( self.assertEqual(
"sku contains an invalid value, must be one of: {}".format( "sku contains an invalid value, must be one of: {}".format(
-15
View File
@@ -10,7 +10,6 @@ from api.validators import (
ValidationError, ValidationError,
validate_exists, validate_exists,
validate_number, validate_number,
validate_territory,
validate_jsonschema, validate_jsonschema,
validate_any_of, 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"age is not a number", self.client.get("/?age=aaa").data)
self.assertEqual(b"ok", self.client.get("/?age=1").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): def test_validate_jsonschema(self):
app = self.app app = self.app
schema = { schema = {