def test_create_a_promotion_associated_with_new_product(self): """ Create a promotion which is associated with a product thats not in our db yet""" promotions = Promotion.all() products = Product.all() self.assertEqual(promotions, []) self.assertEqual(products, []) promotion = Promotion( title="test_create", promo_type=PromoType.DISCOUNT, amount=10, start_date=datetime(2020, 10, 17), end_date=datetime(2020, 10, 18), is_site_wide=True, ) product = Product(id=123) promotion.products.append(product) self.assertTrue(product is not None) promotion.create() # Assert that it was assigned an id and shows up in the database self.assertEqual(promotion.id, 1) self.assertEqual(product.id, 123) products = Product.all() promotions = Promotion.all() self.assertEqual(len(products), 1) self.assertEqual(len(promotions), 1)
def test_delete_a_promotion(self): """ Delete a Promotion """ promotion = Promotion( name="Default", description="default description", start_date=datetime.strptime('2001-01-01 00:00:00', '%Y-%d-%m %H:%M:%S'), end_date=datetime.strptime('2001-01-01 00:00:00', '%Y-%d-%m %H:%M:%S')) promotion.create() 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 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 test_delete_a_promotion(self): """ Delete a Promotion """ promotion = Promotion( title="test_create", promo_type=PromoType.DISCOUNT, amount=10, start_date=datetime(2020, 10, 17), end_date=datetime(2020, 10, 18), is_site_wide=True, ) promotion.create() 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_add_a_promotion(self): """ Create a promotion and add it to the database """ promotions = Promotion.all() self.assertEqual(promotions, []) promotion = Promotion(title="test_create", promo_type=PromoType.DISCOUNT, amount=10, start_date="Sat, 17 Oct 2020 00:00:00 GMT", end_date="Sun, 18 Oct 2020 00:00:00 GMT", is_site_wide=True) self.assertTrue(promotion != None) self.assertEqual(promotion.id, None) promotion.create() # 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_add_a_promotion(self): """ Create a promotion and add it to the database """ promotions = Promotion.all() self.assertEqual(promotions, []) promotion = Promotion( name="Default", description="default description", start_date=datetime.strptime('2001-01-01 00:00:00', '%Y-%d-%m %H:%M:%S'), end_date=datetime.strptime('2001-01-01 00:00:00', '%Y-%d-%m %H:%M:%S')) self.assertTrue(promotion != None) self.assertEqual(promotion.id, None) promotion.create() # 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_add_a_promotion(self): """ Create a promotion and add it to the database """ promotions = Promotion.all() self.assertEqual(promotions, []) promotion = Promotion( title="test_create", promo_type=PromoType.DISCOUNT, amount=10, start_date=datetime(2020, 10, 17), end_date=datetime(2020, 10, 18), is_site_wide=True, ) self.assertTrue(promotion is not None) self.assertEqual(promotion.id, None) promotion.create() # Assert 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_update_a_promotion(self): """ Update a Promotion """ promotion = Promotion( title="test_create", promo_type=PromoType.DISCOUNT, amount=10, start_date=datetime(2020, 10, 17), end_date=datetime(2020, 10, 18), is_site_wide=True, ) promotion.create() self.assertEqual(promotion.id, 1) # Change it and update it promotion.title = "test_update" promotion.update() self.assertEqual(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].title, "test_update")
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}, )
def test_find_or_404_found(self): """ Find or return 404 found """ p1 = Promotion(name="discount", description="discount description", start_date=datetime.strptime('2001-01-01 00:00:00', '%Y-%d-%m %H:%M:%S'), end_date=datetime.strptime('2001-01-01 00:00:00', '%Y-%d-%m %H:%M:%S')) p2 = Promotion(name="buy one get one", description="buy one get one description", start_date=datetime.strptime('2001-01-01 00:00:00', '%Y-%d-%m %H:%M:%S'), end_date=datetime.strptime('2001-01-01 00:00:00', '%Y-%d-%m %H:%M:%S')) p3 = Promotion(name="promo code", description="promo code description", start_date=datetime.strptime('2001-01-01 00:00:00', '%Y-%d-%m %H:%M:%S'), end_date=datetime.strptime('2001-01-01 00:00:00', '%Y-%d-%m %H:%M:%S')) promotions = [p1, p2, p3] p1.create() p2.create() p3.create()
def test_key_error_on_delete(self, bad_mock): """ Test KeyError on delete """ bad_mock.side_effect = KeyError() promotion = Promotion("A002", "dog", False) promotion.create() promotion.delete()
def test_document_not_exist(self, bad_mock): """ Test a Bad Document Exists """ bad_mock.return_value = False promotion = Promotion("A002", "dog", False) promotion.create() self.assertIsNone(promotion.id)
def test_http_error(self, bad_mock): """ Test a Bad Create with HTTP error """ bad_mock.side_effect = HTTPError() promotion = Promotion("A002", "dog", False) promotion.create() self.assertIsNone(promotion.id)