def test_delete_a_promotion(self): """ Delete a Promotion """ promotion = Promotion("A1234", "BOGO", False, "20") promotion.save() self.assertEqual(len(Promotion.all()), 1) # delete the promotion and make sure it isn't in the database promotion.delete() self.assertEqual(len(Promotion.all()), 0)
def test_save(self): promo = Promotion() self.assertEqual(len(Promotion.all()), 0) promo.save() promos = Promotion.all() self.assertEqual(len(promos), 1) data = {'id': promo.id, 'name': 'default', 'promo_type': '$', 'value': 0.0, 'start_date': '9999-12-31 23:59:59', 'end_date': '9999-12-31 23:59:59', 'detail': 'n/a'} self.assertEqual(promos[0].serialize(), data)
def test_delete_a_promotion(self): """ Delete a Promotion """ promotion = Promotion(name="20%OFF", product_id=9527, discount_ratio=80) promotion.save() self.assertEqual(len(Promotion.all()), 1) # delete the promotion and make sure it isn't in the database promotion.delete() self.assertEqual(len(Promotion.all()), 0)
def test_delete_a_promotion(self): """ Delete a promotion in the database """ promotion = Promotion(promo_name="random", goods_name="random_good", category="random_category", price=20, discount=20, available=True) promotion.save() self.assertEqual(len(Promotion.all()), 1) #delete the promotion promotion.delete() self.assertEqual(len(Promotion.all()), 0)
def test_add_a_promotion(self): """ Create a promotion and add it to the database """ promotions = Promotion.all() self.assertEqual(promotions, []) promotion = Promotion(name="20%OFF", product_id=9527, discount_ratio=80) self.assertTrue(promotion is not None) self.assertEqual(promotion.promotion_id, None) promotion.save() # Asert that it was assigned an id and shows up in the database self.assertEqual(promotion.promotion_id, 1) self.assertEqual(promotion.counter, 0) promotions = Promotion.all() self.assertEqual(len(promotions), 1)
def list_promotions(): r""" Returns all of the Promotions Search the promotions in the database. <ul> <li>Only the first non-empty field will be considered.</li> <li>An empty query will result in a list of all promotion entries in the database</li> </ul> --- tags: - promotions produces: - application/json parameters: - name: name in: query description: name of the promotion required: false type: string - name: product_id in: query description: product_id for the promotion required: false type: integer minimum: 0 format: int32 - name: discount_ratio in: query description: discount ratio of the promotion required: false type: integer maximum: 100.0 minimum: 0 format: int32 responses: 200: description: search results matching criteria schema: type: array items: $ref: '#/definitions/ResponsePromotionObject' 400: description: bad input parameter """ promotions = [] promotion_id = request.args.get('promotion_id') name = request.args.get('name') product_id = request.args.get('product_id') discount_ratio = request.args.get('discount_ratio') if name: promotions = Promotion.find_by_name(name) elif product_id: promotions = Promotion.find_by_product_id(product_id) elif discount_ratio: promotions = Promotion.find_by_discount_ratio(discount_ratio) else: promotions = Promotion.all() results = [promotion.serialize() for promotion in promotions] return make_response(jsonify(results), status.HTTP_200_OK)
def test_remove_all(self): """ Remove all entries """ Promotion(name="20%OFF", product_id=9527, discount_ratio=80).save() Promotion(name="50%OFF", product_id=26668).save() Promotion.remove_all() promotions = Promotion.all() self.assertEqual(len(promotions), 0)
def test_add_a_promotion(self): """ Create a promotion and add it to the database """ promotions = Promotion.all() self.assertEqual(promotions, []) promotion = Promotion("A1234", "BOGO", True, "20") self.assertNotEqual(promotion, None) self.assertEqual(promotion.id, None) promotion.save() # Asert that it was assigned an id and shows up in the database self.assertNotEqual(promotion.id, None) promotions = Promotion.all() self.assertEqual(len(promotions), 1) self.assertEqual(promotions[0].productid, "A1234") self.assertEqual(promotions[0].category, "BOGO") self.assertEqual(promotions[0].available, True) self.assertEqual(promotions[0].discount, "20")
def test_add_a_promotion(self): """ Create a promotion and add it to the database """ promotions = Promotion.all() self.assertEqual(promotions, []) promotion = Promotion(promo_name="random", goods_name="random_good", category="random_category", price=20, discount=20, available=True) self.assertTrue(promotion != None) self.assertEqual(promotion.id, None) promotion.save() # Asert that it was assigned an id and shows up in the database self.assertEqual(promotion.id, 1) promotions = Promotion.all() self.assertEqual(len(promotions), 1)
def test_delete_all_promotions(self): """ Delete all promotions in the database """ promotion1 = Promotion(promo_name="random1", goods_name="random_good1", category="random_category1", price=20, discount=20, available=True) promotion1.save() promotion2 = Promotion(promo_name="random2", goods_name="random_good2", category="random_category2", price=20, discount=20, available=True) promotion2.save() self.assertEqual(len(Promotion.all()), 2) Promotion.remove_all() self.assertEqual(len(Promotion.all()), 0)
def get(self): """ Returns all of the Promotions """ conditions = request.values.to_dict() payload = Promotion.all() if conditions: payload = Promotion.find_by_conditions(conditions) payload = [promo.serialize() for promo in payload] flask_app.logger.info("GET promotions success") return payload, 200
def test_update_a_promotion(self): """ Update a Promotion """ promotion = Promotion("A1234", "BOGO", True, "20") promotion.save() self.assertNotEqual(promotion.id, None) # Change it an save it promotion.category = "Percentage" promotion.save() # Fetch it back and make sure the id hasn't changed # but the data did change promotions = Promotion.all() self.assertEqual(len(promotions), 1) self.assertEqual(promotions[0].category, "Percentage") self.assertEqual(promotions[0].productid, "A1234")
def list_promotions(): """ Returns all of the Promotions """ promotions = [] promotion_id = request.args.get('promotion_id') name = request.args.get('name') product_id = request.args.get('product_id') discount_ratio = request.args.get('discount_ratio') if name: promotions = Promotion.find_by_name(name) elif product_id: promotions = Promotion.find_by_product_id(product_id) elif discount_ratio: promotions = Promotion.find_by_discount_ratio(discount_ratio) else: promotions = Promotion.all() results = [promotion.serialize() for promotion in promotions] return make_response(jsonify(results), status.HTTP_200_OK)
def test_update_a_promotion(self): """ Update a promotion in the database """ promotion = Promotion(promo_name="random", goods_name="random_good", category="random_category", price=20, discount=20, available=True) promotion.save() self.assertEqual(promotion.id, 1) #change it and save it promotion.category = "random_afterchange" promotion.save() self.assertEqual(promotion.id, 1) #Fetch it back to make sure the id not changed, but only the data promotions = Promotion.all() self.assertEqual(len(promotions), 1) self.assertEqual(promotions[0].category, "random_afterchange")
def test_partial_update_an_promotion(self): """ Partial update a Promotion """ promotion = Promotion(name="20%OFF", product_id=9527, discount_ratio=80) promotion.save() self.assertEqual(promotion.promotion_id, 1) # Change it an save it promotion.name = "BUY1GET1FREE" promotion.discount_ratio = 50 promotion.save() self.assertEqual(promotion.promotion_id, 1) # Fetch it back and make sure the id hasn't changed # but the data did change promotions = Promotion.all() self.assertEqual(len(promotions), 1) self.assertEqual(promotions[0].name, "BUY1GET1FREE") self.assertEqual(promotions[0].product_id, 9527) self.assertEqual(promotions[0].discount_ratio, 50)
def list_promotions(): """ Returns all of the Promotions """ promotions = [] category = request.args.get('category') productid = request.args.get('productid') discount = request.args.get('discount') if category: app.logger.info('Find by category') promotions = Promotion.find_by_category(category) elif productid: app.logger.info('Find by productid') promotions = Promotion.find_by_productid(productid) elif discount: app.logger.info('Find by discount') promotions = Promotion.find_by_discount(discount) else: app.logger.info('Find all') promotions = Promotion.all() app.logger.info('[%s] Promotions returned', len(promotions)) results = [promotion.serialize() for promotion in promotions] return make_response(jsonify(results), status.HTTP_200_OK)
def test_delete(self): promo = Promotion() promo.save() self.assertEqual(len(Promotion.all()), 1) promo.delete() self.assertEqual(len(Promotion.all()), 0)