def test_serialize(self): """ Test serialization of a Promotion """ promotion = Promotion( id=1, title="Halloween Special", description="Some items off in honor of the spookiest month.", promo_code="hween", promo_type=PromoType.DISCOUNT, amount=25, start_date=datetime(2020, 10, 20), end_date=datetime(2020, 11, 1), is_site_wide=True, ) product_1 = Product() product_1.id = 123 promotion.products.append(product_1) product_2 = Product() product_2.id = 456 promotion.products.append(product_2) self.assertEqual( promotion.serialize(), { "id": 1, "title": "Halloween Special", "description": "Some items off in honor of the spookiest month.", "promo_code": "hween", "promo_type": "DISCOUNT", "amount": 25, "start_date": "2020-10-20T00:00:00", "end_date": "2020-11-01T00:00:00", "is_site_wide": True, "products": [123, 456], }, )
def test_serialize_a_promotion(self): """ Test serialization of a Promotion""" promotion = Promotion( name="New_Sale", description="Amazing", start_date=datetime.strptime('2001-01-01 00:00:00', '%Y-%m-%d %H:%M:%S'), end_date=datetime.strptime('2001-01-01 00:00:00', '%Y-%m-%d %H:%M:%S')) data = promotion.serialize() self.assertNotEqual(data, None) self.assertIn("id", data) self.assertEqual(data["id"], None) self.assertIn("name", data) self.assertEqual(data["name"], "New_Sale") self.assertIn("description", data) self.assertEqual(data["description"], "Amazing") self.assertIn("start_date", data) self.assertEqual( datetime.strptime(data["start_date"], DATETIME_FORMAT), datetime.strptime('2001-01-01 00:00:00', DATETIME_FORMAT)) self.assertIn("end_date", data) self.assertEqual( datetime.strptime(data["end_date"], DATETIME_FORMAT), datetime.strptime('2001-01-01 00:00:00', DATETIME_FORMAT))
def test_serialize_a_promotion(self): """ Serialize a Promotion """ promotion = Promotion("A002", "dog", False) data = promotion.serialize() self.assertNotEqual(data, None) self.assertNotIn('_id', data) self.assertIn('productid', data) self.assertEqual(data['productid'], "A002") self.assertIn('category', data) self.assertEqual(data['category'], "dog") self.assertIn('available', data) self.assertEqual(data['available'], False)
def create_promotions(): """ Creates a Promotion This endpoint will create a Promotion based the data in the body that is posted """ app.logger.info("Request to create a promotion") check_content_type("application/json") promotion = Promotion() promotion.deserialize(request.get_json()) promotion.create() message = promotion.serialize() #location_url = url_for("get_promotions", promotion_id=promotion.id, _external=True) location_url = "not implemented" return make_response(jsonify(message), status.HTTP_201_CREATED, {"Location": location_url})
def post(self): """ Add a promotion This endpoint will return a Promotion based on it's id and the URL to that promotion """ app.logger.info('Request to create a Promotion') check_content_type('application/json') promotion = Promotion() app.logger.debug('Payload = %s', api.payload) promotion.deserialize(api.payload) promotion.save() message = promotion.serialize() location_url = api.url_for(PromotionResource, promotion_id=promotion.id, _external=True) return message, status.HTTP_201_CREATED, {'Location': location_url}
def post(self): """ Creates a Promotion This endpoint will create a Promotion based the data in the body that is posted or data that is sent via an html form post. """ app.logger.info('Request to Create a Promotion') content_type = request.headers.get('Content-Type') if not content_type: abort(status.HTTP_400_BAD_REQUEST, "No Content-Type set") data = {} # Check for form submission data if content_type == 'application/x-www-form-urlencoded': app.logger.info('Processing FORM data') app.logger.info(type(request.form)) app.logger.info(request.form) data = { 'productid': request.form['productid'], 'category': request.form['category'], 'available': request.form['available'].lower() in ['yes', 'y', 'true', 't', '1'], 'discount': request.form['discount'] } elif content_type == 'application/json': app.logger.info('Processing JSON data') data = request.get_json() else: message = 'Unsupported Content-Type: {}'.format(content_type) app.logger.info(message) abort(status.HTTP_400_BAD_REQUEST, message) promotion = Promotion() try: promotion.deserialize(data) except DataValidationError as error: raise BadRequest(str(error)) promotion.save() app.logger.info('Promotion with new id [%s] saved!', promotion.id) location_url = api.url_for(PromotionResource, promotion_id=promotion.id, _external=True) return promotion.serialize(), status.HTTP_201_CREATED, {'Location': location_url}
def test_deserialize_a_promotion(self): """ Test deserialization of a promotion """ promotion = Promotion( name="New_Sale", description="Amazing", start_date=datetime.strptime('2001-01-01 00:00:00', '%Y-%m-%d %H:%M:%S'), end_date=datetime.strptime('2001-01-01 00:00:00', '%Y-%m-%d %H:%M:%S')) data = promotion.serialize() promotion.deserialize(data) self.assertNotEqual(promotion, None) self.assertEqual(promotion.id, None) self.assertEqual(promotion.name, "New_Sale") self.assertEqual(promotion.description, "Amazing") self.assertEqual( promotion.start_date, datetime.strptime('2001-01-01 00:00:00', DATETIME_FORMAT)) self.assertEqual( promotion.end_date, datetime.strptime('2001-01-01 00:00:00', DATETIME_FORMAT))
def test_deserialize(self): """ Test deserialization of a promotion """ promotion = Promotion( id=2, title="Thanksgiving Special", description="Some items off in honor of the most grateful month.", promo_code="tgiving", promo_type=PromoType.DISCOUNT, amount=50, start_date=datetime(2020, 11, 1), end_date=datetime(2020, 11, 30), is_site_wide=False, ) product_1 = Product() product_1.id = 123 promotion.products.append(product_1) product_2 = Product() product_2.id = 456 promotion.products.append(product_2) db.session.add(product_1) db.session.add(product_2) data = promotion.serialize() promotion.deserialize(data) self.assertNotEqual(promotion, None) self.assertEqual(promotion.id, 2) self.assertEqual(promotion.title, "Thanksgiving Special") self.assertEqual( promotion.description, "Some items off in honor of the most grateful month.") self.assertEqual(promotion.promo_code, "tgiving") self.assertEqual(promotion.amount, 50) self.assertEqual(promotion.start_date, "2020-11-01T00:00:00") self.assertEqual(promotion.end_date, "2020-11-30T00:00:00") self.assertEqual(promotion.is_site_wide, False) self.assertEqual( [product.id for product in promotion.products], [123, 456], )
def post(self): """ Creates a Promotion This endpoint will create a Promotion based the data in the body that is posted """ app.logger.info("Request to create a promotion") check_content_type("application/json") json = request.get_json() if "products" in json: for product_id in json["products"]: if product_id != "" and Product.query.get(product_id) is None: Product(id=product_id).create() promotion = Promotion() promotion.deserialize(json) promotion.create() location_url = api.url_for( PromotionResource, promotion_id=promotion.id, _external=True ) app.logger.info("Promotion with ID [%s] created.", promotion.id) return ( promotion.serialize(), status.HTTP_201_CREATED, {"Location": location_url}, )