def test_delete_promotion(self): """ Test Delete Promotion """ test_promotion = self._create_promotion() test_promotion.create() self.assertEqual(len(Promotions.all()), 1) # delete the pet and make sure it isn't in the database test_promotion.delete() self.assertEqual(len(Promotions.all()), 0)
def setUpClass(cls): """ This runs once before the entire test suite """ global DATABASE_URI app.config['TESTING'] = True app.config['DEBUG'] = False if 'VCAP_SERVICES' in os.environ: vcap = json.loads(os.environ['VCAP_SERVICES']) DATABASE_URI = vcap['user-provided'][0]['credentials']['url'] app.config["SQLALCHEMY_DATABASE_URI"] = DATABASE_URI app.logger.setLevel(logging.CRITICAL) Promotions.init_db(app)
def test_add_promotion(self): """ Test Add Promotion to database""" test_promotion = Promotions.all() self.assertEqual(test_promotion, []) test_promotion = self._create_promotion() self.assertTrue(test_promotion != None) self.assertEqual(test_promotion.id, None) test_promotion.create() # Asert that it was assigned an id and shows up in the database self.assertEqual(test_promotion.id, 1) test_promotion = Promotions.all() self.assertEqual(len(test_promotion), 1)
def delete(self, promotion_id): app.logger.info("Request to delete promotion with id: %s", promotion_id) promotion = Promotions.find(promotion_id) if promotion: promotion.delete() app.logger.info("Promotion with ID [%s] delete complete.", promotion_id) return '', status.HTTP_204_NO_CONTENT
def _create_promotion(self): return Promotions(name="Test", description="Testing Promotion", promo_code="ABC123", start_date=datetime.strptime('2021-01-01 00:00:00', DATETIME), end_date=datetime.strptime('2022-01-01 00:00:00', DATETIME), is_active=True)
def test_find_by_name(self): """ Find a Promotion by Name """ self._create_promotion().create() Promotions(name="Second", description="Testing Second Promotion", promo_code="EFG456", start_date=datetime.strptime('2021-01-01 00:00:00', DATETIME), end_date=datetime.strptime('2022-01-01 00:00:00', DATETIME), is_active=False).create() promotions = Promotions.find_by_name("Second") self.assertEqual(promotions[0].name, "Second") self.assertEqual(promotions[0].description, "Testing Second Promotion") self.assertEqual(promotions[0].promo_code, "EFG456") self.assertEqual(promotions[0].start_date, datetime.strptime('2021-01-01 00:00:00', DATETIME)) self.assertEqual(promotions[0].end_date, datetime.strptime('2022-01-01 00:00:00', DATETIME)) self.assertEqual(promotions[0].is_active, False)
def get(self, promotion_id): """Get promotion""" app.logger.info("Request for Promotion with id: %s", promotion_id) promotion = Promotions.find(promotion_id) if not promotion: api.abort( status.HTTP_404_NOT_FOUND, "Promotion with id '{}' was not found.".format(promotion_id)) app.logger.info("Returning Promotion: %s", promotion.name) return promotion.serialize(), status.HTTP_200_OK
def get(self): """ Get all Promotions This endpoint will list all promotions based on the data that is stored """ app.logger.info("Request for promotions list") promotion = [] name = request.args.get("name") description = request.args.get("description") promo_code = request.args.get("promo_code") if name: promotion = Promotions.find_by_name(name) elif description: promotion = Promotions.find_by_description(description) elif promo_code: promotion = Promotions.find_by_promo(promo_code) else: promotion = Promotions.all() results = [promotion.serialize() for promotion in promotion] app.logger.info("Returning %d promotions", len(results)) return results, status.HTTP_200_OK
def test_find_promotion(self): """ Test Find Promotion """ test_promotion = self._create_promotion() test_promotion.create() promotion = Promotions.find(test_promotion.id) self.assertEqual(promotion.id, test_promotion.id) self.assertEqual(promotion.name, "Test") self.assertEqual(promotion.description, "Testing Promotion") self.assertEqual(promotion.promo_code, "ABC123") self.assertEqual(promotion.start_date, datetime.strptime('2021-01-01 00:00:00', DATETIME)) self.assertEqual(promotion.end_date, datetime.strptime('2022-01-01 00:00:00', DATETIME)) self.assertEqual(promotion.is_active, True)
def test_update_promotion(self): """ Test Update Promotion """ test_promotion = self._create_promotion() test_promotion.create() self.assertEqual(test_promotion.id, 1) # Change it an update it test_promotion.description = "Updated Description" test_promotion.update() self.assertEqual(test_promotion.id, 1) # Fetch it back and make sure the id hasn't changed # but the data did change test_promotion = Promotions.all() self.assertEqual(len(test_promotion), 1) self.assertEqual(test_promotion[0].description, "Updated Description")
def put(self, promotion_id): """Cancel a promotion""" app.logger.info("Request to cancel promotion with id: %s", promotion_id) promotion = Promotions.find(promotion_id) if not promotion: api.abort( status.HTTP_404_NOT_FOUND, 'Promotion with id [{}] was not found.'.format(promotion_id)) promotion.end_date = datetime.datetime.now() promotion.is_active = False promotion.id = promotion_id promotion.update() app.logger.info("Promotion with ID [%s] canceled.", promotion.id) return promotion.serialize(), status.HTTP_200_OK
def post(self): """Creates a Promotion""" app.logger.info("Request to create a promotion") check_content_type("application/json") promotion = Promotions() app.logger.debug('Payload = %s', api.payload) data = api.payload promotion.deserialize(data) 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 put(self, promotion_id): """Update a promotion""" app.logger.info("Request to update promotion with id: %s", promotion_id) check_content_type("application/json") promotion = Promotions.find(promotion_id) if not promotion: api.abort( status.HTTP_404_NOT_FOUND, "Promotion with id '{}' was not found.".format(promotion_id)) app.logger.debug('Payload = %s', api.payload) data = api.payload promotion.deserialize(data) promotion.id = promotion_id promotion.update() app.logger.info("Promotion with ID [%s] updated.", promotion.id) return promotion.serialize(), status.HTTP_200_OK
def init_db(): """ Initialies the SQLAlchemy app """ global app Promotions.init_db(app)
def test_deserialize_bad_promotion_data(self): """ Test deserialization of bad promotion data """ data = "this is not a dictionary" promotion = Promotions() self.assertRaises(DataValidationError, promotion.deserialize, data)