Exemple #1
0
    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)
Exemple #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)
 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)
Exemple #4
0
 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)
Exemple #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 test_add_a_promotion(self):
     """ Create a promotion """
     promotion = PromotionFactory(code="SAVE50")
     promotion.save()
     promotions = Promotion.all()
     self.assertEqual(len(promotions), 1)
     self.assertEqual(promotions[0].code, "SAVE50")
Exemple #7
0
    def get(self):
        """ Returns all of the Promotions """
        app.logger.info('Request to list Promotions...')
        promotions = []
        category = request.args.get('category')
        productid = request.args.get('productid')
        available = request.args.get('available')
        discount = request.args.get('discount')
        if category:
            app.logger.info('Filtering by category: %s', category)
            promotions = Promotion.find_by_category(category)
        elif productid:
            app.logger.info('Filtering by productid:%s', productid)
            promotions = Promotion.find_by_productid(productid)
        elif available:
            app.logger.info('Filtering by available: %s', available)
            is_available = available.lower() in ['yes', 'y', 'true', 't', '1']
            promotions = Promotion.find_by_availability(is_available)
        elif discount:
            app.logger.info('Filtering by discount:%s', discount)
            promotions = Promotion.find_by_discount(discount)
        else:
            promotions = Promotion.all()

        app.logger.info('[%s] Promotions returned', len(promotions))
        results = [promotion.serialize() for promotion in promotions]
        return results, status.HTTP_200_OK
Exemple #8
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)
Exemple #10
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=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)
Exemple #11
0
def list_promotions():
    """ Returns all of the Promotions """
    app.logger.info("Request for promotions list")
    promotions = []
    name = request.args.get("name")
    if name:
        promotions = Promotion.find_by_name(name)
    else:
        promotions = Promotion.all()

    results = [promotion.serialize() for promotion in promotions]
    return make_response(jsonify(results), status.HTTP_200_OK)
    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)
Exemple #13
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")
Exemple #14
0
 def test_find_promotion(self):
     """ Find a Promotion by ID """
     promotions = PromotionFactory.create_batch(3)
     for promotion in promotions:
         promotion.create()
     logging.debug(promotions)
     # make sure they got saved
     self.assertEqual(len(Promotion.all()), 3)
     # find the 2nd promotion in the list
     promotion = Promotion.find(promotions[1].id)
     self.assertIsNot(promotion, None)
     self.assertEqual(promotion.id, promotions[1].id)
     self.assertEqual(promotion.name, promotions[1].name)
     self.assertEqual(promotion.description, promotions[1].description)
     self.assertEqual(promotion.end_date, promotions[1].end_date)
     self.assertEqual(promotion.start_date, promotions[1].start_date)
Exemple #15
0
 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 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