Files
api2/tests/resources/test_prices.py
T

83 lines
3.1 KiB
Python

import json
import flask_testing
from api import create_app, db
from api.models.product import Material, MaterialPrice
from api.models.inquiry import Inquiry
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)
material.material_price = MaterialPrice(price=price, groupid=groupid)
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 test_wallpaper(self):
self._add_material('standard-wallpaper', 1, 236)
self._add_material('premium-wallpaper', 1, 260)
response = self.client.get(
'/prices/wallpaper?width=200&height=10&territory=SE')
self.assert200(response)
products = response.json['data']
self.assertEqual(2, len(products))
def test_canvas(self):
self._add_material('standard-canvas', 2, 799.2)
response = self.client.get(
'/prices/canvas?width=200&height=10&territory=SE')
self.assert200(response)
products = response.json['data']
self.assertEqual(2, len(products))
for product in products:
if product['framed']:
self.assertEqual(150, product['width'])
self.assertEqual(40, product['height'])
self.assertEqual(1213, product['price'])
else:
self.assertEqual(200, product['width'])
self.assertEqual(20, product['height'])
self.assertEqual(400, product['price'])
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(923.857, product['price'], places=2)
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'])