def test_find_by_availability(self): """ Find a Promotion by Availability """ Promotion("A002", "dog", False).save() Promotion("kitty", "cat", True).save() promotions = Promotion.find_by_availability(True) self.assertEqual(len(promotions), 1) self.assertEqual(promotions[0].productid, "kitty")
def test_promotion_deserialize_exceptions(self): """ Test Promotion deserialization exceptions""" promotion = PromotionFactory() json_data = json.dumps(dict( percentage=promotion.percentage, start_date=promotion.start_date, )) promotion_deserialized = Promotion() try: promotion_deserialized.deserialize(json.loads(json_data)) except DataValidationError: self.assertRaises(DataValidationError) json_data = json.dumps(dict( code=promotion.code, percentage=promotion.percentage, expiry_date="shouldn't like this", start_date=promotion.start_date, products=promotion.products )) promotion_deserialized = Promotion() try: promotion_deserialized.deserialize(json.loads(json_data)) except DataValidationError: self.assertRaises(DataValidationError)
def test_find_by_category(self): """ Find a Promotion by Category """ Promotion("A002", "dog").save() Promotion("kitty", "cat").save() promotions = Promotion.find_by_category("cat") self.assertNotEqual(len(promotions), 0) self.assertEqual(promotions[0].category, "cat") self.assertEqual(promotions[0].productid, "kitty")
def setUp(self): """ Initialize the Cloudant database """ self.app = app.test_client() Promotion.init_db("tests") Promotion.remove_all() Promotion("A001", "BOGO", True, 10).save() Promotion("A002", "B2GO", True, 10).save() Promotion("A003", "B3GO", False, 10).save()
def test_find_promotion(self): """ Find a Promotion by id """ Promotion("A002", "dog").save() # saved_promotion = Promotion("kitty", "cat").save() saved_promotion = Promotion("kitty", "cat") saved_promotion.save() promotion = Promotion.find(saved_promotion.id) self.assertIsNot(promotion, None) self.assertEqual(promotion.id, saved_promotion.id) self.assertEqual(promotion.productid, "kitty")
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_key_error_on_update(self, bad_mock): """ Test KeyError on update """ bad_mock.side_effect = KeyError() promotion = Promotion("A002", "dog", False) promotion.save() promotion.productid = 'Fifi' promotion.update()
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_delete_a_promotion(self): """ Delete a Promotion """ promotion = Promotion("A002", "dog") 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_create_a_promotion(self): """ Create a promotion and assert that it exists """ promotion = Promotion("A002", "BOGO", True, 10) self.assertNotEqual(promotion, None) #self.assertEqual(promotion.id, None) self.assertEqual(promotion.productid, "A002") self.assertEqual(promotion.category, "BOGO") self.assertEqual(promotion.available, True) self.assertEqual(promotion.discount, 10)
def test_deserialize_a_promotion(self): """ Deserialize a Promotion """ data = {"productid": "A002", "category": "BOGO", "available": True, "discount": 10 } promotion = Promotion() promotion.deserialize(data) self.assertNotEqual(promotion, None) #self.assertEqual(promotion.id, None) self.assertEqual(promotion.productid, "A002") self.assertEqual(promotion.category, "BOGO") self.assertEqual(promotion.available, True) self.assertEqual(promotion.discount, 10)
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 test_update_a_promotion_fail(self): 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, ) try: promotion.update() except: print("Update called with empty ID field")
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 test_update_a_promotion(self): """ Update a Promotion """ promotion = Promotion("A002", "dog", True) promotion.save() self.assertNotEqual(promotion.id, None) # Change it an save it promotion.category = "k9" 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, "k9") self.assertEqual(promotions[0].productid, "A002")
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 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("A002", "BOGO", True, 10) 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, "A002") self.assertEqual(promotions[0].category, "BOGO") self.assertEqual(promotions[0].available, True) self.assertEqual(promotions[0].discount, 10)
def test_create_a_promotion(self): """ Create a promotion and assert that it exists """ 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 is not None) self.assertEqual(promotion.id, None) self.assertEqual(promotion.title, "test_create") self.assertEqual(promotion.promo_type, PromoType.DISCOUNT) self.assertEqual(promotion.amount, 10) self.assertEqual(promotion.start_date, "Sat, 17 Oct 2020 00:00:00 GMT") self.assertEqual(promotion.end_date, "Sun, 18 Oct 2020 00:00:00 GMT") self.assertEqual(promotion.is_site_wide, True)
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_create_a_promotion(self): """ Create a promotion and assert that it exists """ 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) self.assertEqual(promotion.title, "test_create") self.assertEqual(promotion.promo_type, PromoType.DISCOUNT) self.assertEqual(promotion.amount, 10) self.assertEqual(promotion.start_date, datetime(2020, 10, 17)) self.assertEqual(promotion.end_date, datetime(2020, 10, 18)) self.assertEqual(promotion.is_site_wide, True)
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 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_promotion_deserialize(self): """ Test Promotion deserialization""" promotion = PromotionFactory() json_data = json.dumps(dict( code=promotion.code, percentage=promotion.percentage, expiry_date=promotion.expiry_date, start_date=promotion.start_date, products=promotion.products )) promotion_deserialized = Promotion() promotion_deserialized.deserialize(json.loads(json_data)) self.assertEqual(promotion.code, promotion_deserialized.code) self.assertEqual(promotion.percentage, promotion_deserialized.percentage) self.assertEqual(promotion.expiry_date, promotion_deserialized.expiry_date) self.assertEqual(promotion.start_date, promotion_deserialized.start_date)
def test_create_a_promotion(self): """ Create a promotion and assert that it exists """ 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) self.assertEqual(promotion.name, "Default") self.assertEqual(promotion.description, "default description") self.assertEqual( promotion.start_date, datetime.strptime('2001-01-01 00:00:00', '%Y-%d-%m %H:%M:%S')) self.assertEqual( promotion.end_date, datetime.strptime('2001-01-01 00:00:00', '%Y-%d-%m %H:%M:%S'))
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))