def test_update_promotion(self):
     """ Update a promotion """
     promotion = PromotionFactory()
     promotion.save()
     self.assertEqual(len(Promotion.find_by_code(promotion.code)), 1)
     promotion.percentage = 80
     promotion.start_date = promotion.start_date+1000
     promotion.expiry_date = promotion.expiry_date+1000
     promotion.save()
     self.assertEqual(len(Promotion.find_by_code(promotion.code)), 1)
    def test_find_by_code(self):
        """ Find Promotions by code """
        self.assertEqual(len(Promotion.all()), 0)
        codes = ['SAVE15', 'SAVE20', 'SAVE30']
        counts = [10, 15, 2]
        for count, code in zip(counts, codes):
            PromotionFactory.batch_create(count, code=code)

        for count, code in zip(counts, codes):
            promotions = Promotion.find_by_code(code)
            self.assertEqual(len(promotions), count)
            for promotion in promotions:
                self.assertEqual(promotion.code, code)
    def get(self):
        """
        List promotions.

        This endpoint will return all promotions if no promotion code is provided.
        If a promotion code is provided, it returns a list of promotions having
        the that promotion code.
        While no promotion is found, no matter a code is provided or not, rather
        than raising a NotFound, we return an empty list to indicate that nothing
        is found.
        """
        app.logger.info('Request to list Promotions...')
        args = promotion_args.parse_args()
        code = args['promotion-code']
        promotions = []
        if code:
            app.logger.info('Request for promotion list with code %s', code)
            promotions = Promotion.find_by_code(code)
        else:
            app.logger.info('Request for promotion list')
            promotions = Promotion.all()
        results = [p.serialize() for p in promotions]
        return results, status.HTTP_200_OK