Пример #1
0
 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()
Пример #2
0
 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)
Пример #3
0
 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")
Пример #4
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")
Пример #5
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 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}
Пример #7
0
    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}