Files
api2/tests/resources/test_prices.py
T

223 lines
7.6 KiB
Python

import json
import flask_testing
from api import create_app, db
from api.models.product import Material, Product
from api.models.inquiry import Inquiry
from api.models.designer import Designer
from api.lib import pwinty
class TestEndpoints(flask_testing.TestCase):
def create_app(self):
return create_app("tests.config")
def setUp(self):
db.create_all()
def tearDown(self):
db.session.remove()
db.drop_all()
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, territory, product_group, price_retouch=100):
inquiry = Inquiry(
territory=territory,
product_group=product_group,
price_retouch=price_retouch,
)
db.session.add(inquiry)
db.session.commit()
return inquiry
def _add_designer(self, pricepremium):
designer = Designer("Disney", "disney", "US", "USD", pricepremium=pricepremium)
db.session.add(designer)
db.session.commit()
return designer
def _add_product(self, designer=None):
product = Product(designer=designer)
db.session.add(product)
db.session.commit()
return product
def test_wallpaper(self):
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")
self.assert200(response)
expected = [
{
"m2_price": 295.0,
"m2_price_excl_vat": 236.0,
"price_excl_vat": 236.0,
"price": 295.0,
"material": "standard-wallpaper",
"material_id": 1,
},
{
"m2_price": 325.0,
"m2_price_excl_vat": 260.0,
"price_excl_vat": 260.0,
"price": 325.0,
"material": "premium-wallpaper",
"material_id": 2,
},
]
self.assertEqual(expected, response.json["data"])
def test_wallpaper_with_product(self):
self._add_material("standard-wallpaper", 1, 23600)
self._add_material("premium-wallpaper", 1, 26000)
designer = self._add_designer(4)
product = self._add_product(designer)
response = self.client.get(
"/prices/wallpaper?width=200&height=200&territory=SE&product=1"
)
expected = [
{
"price": 1227.2,
"price_excl_vat": 981.76,
"material_id": 1,
"m2_price": 306.8,
"m2_price_excl_vat": 245.44,
"material": "standard-wallpaper",
},
{
"price": 1352.0000000000002,
"price_excl_vat": 1081.6000000000001,
"material_id": 2,
"m2_price": 338.00000000000006,
"m2_price_excl_vat": 270.40000000000003,
"material": "premium-wallpaper",
},
]
self.assertEqual(expected, response.json["data"])
def test_canvas(self):
response = self.client.get("/prices/canvas?width=200&height=10&territory=SE")
self.assert200(response)
expected = [
{
"framed": True,
"height": 40,
"price": 1008.0,
"price_excl_vat": 806.4,
"width": 150,
},
{
"framed": False,
"height": 20,
"price": 575.47,
"price_excl_vat": 460.37600000000003,
"width": 200,
},
]
self.assertEqual(expected, response.json["data"])
def test_canvas_with_product(self):
designer = self._add_designer(4)
product = self._add_product(designer)
response = self.client.get(
"/prices/canvas?width=200&height=10&territory=SE&product=1"
)
self.assert200(response)
expected = [
{
"framed": True,
"height": 40,
"price": 1048.32,
"price_excl_vat": 838.656,
"width": 150,
},
{
"framed": False,
"height": 20,
"price": 598.4888000000001,
"price_excl_vat": 478.79104000000007,
"width": 200,
},
]
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.assert200(response)
product = response.json["data"][0]
self.assertEqual(150, product["width"], "it adjusts measurements")
self.assertEqual(50, product["height"])
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.assert200(response)
expected = [
{
"hanger": True,
"height": 30,
"price": 397.5,
"price_excl_vat": 318,
"width": 50,
},
{
"hanger": False,
"height": 30,
"price": 250.0,
"price_excl_vat": 200,
"width": 50,
},
]
self.assertEqual(expected, response.json["data"])
def test_poster_hanger(self):
response = self.client.get("/prices/poster-hanger?width=50&territory=SE")
self.assert200(response)
product = response.json["data"][0]
self.assertEqual(210.0, product["price"])
self.assertEqual(168, product["price_excl_vat"])
self.assertEqual(50, product["width"])
def test_inquiry_wallpaper(self):
self._add_material("standard-wallpaper", 1, 236)
self._add_material("premium-wallpaper", 1, 260)
self._add_inquiry(territory="SE", 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):
self._add_material("standard-canvas", 2, 799.2)
self._add_inquiry(territory="SE", 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(territory="SE", 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.assert200(response)
expected = [{"price": 557.5, "price_excl_vat": 446, "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.assert400(response)
self.assertEqual(
"sku contains an invalid value, must be one of: {}".format(
pwinty.SKU_CODES
),
response.json["message"],
)