def initialize_database(app, db): """Drop and restore database in a consistent state""" with app.app_context(): db.drop_all() db.create_all() for i in range(100): discount_type = DiscountType(words.get_random(), 'price * %f' % random.random(), Influencer(names.get_full_name())) discount_name = words.get_random(random.randint(2, 4)) discount = Discount(discount_name, words.get_random().upper(), random.randrange(1, 100) * random.random(), discount_type) db.session.add(discount) first_discount_type = DiscountType('Save with Sherry', 'price * .8', Influencer('Sherry')) second_discount_type = DiscountType('Sunday Savings', 'price * .9', None) third_discount_type = DiscountType('Monday Funday', 'price * .95', None) first_discount = Discount('Black Friday', 'BFRIDAY', 5.1, first_discount_type) second_discount = Discount('SWEET SUNDAY', 'OFF', 300.1, second_discount_type) third_discount = Discount('Monday Funday', 'PARTY', 542.1, third_discount_type) db.session.add(first_discount) db.session.add(second_discount) db.session.add(third_discount) db.session.commit()
def status(): if flask_request.method == 'GET': discounts = Discount.query.all() app.logger.info(f"Discounts available: {len(discounts)}") influencer_count = 0 for discount in discounts: if discount.discount_type.influencer: influencer_count += 1 app.logger.info( f"Total of {influencer_count} influencer specific discounts as of this request" ) return jsonify([b.serialize() for b in discounts]) elif flask_request.method == 'POST': # create a new discount with random name and value discounts_count = len(Discount.query.all()) new_discount_type = DiscountType('Random Savings', 'price * .9', None) new_discount = Discount('Discount ' + str(discounts_count + 1), words.get_random(random.randomint(2, 4)), random.randint(10, 500), new_discount_type) app.logger.info(f"Adding discount {new_discount}") db.session.add(new_discount) db.session.commit() discounts = Discount.query.all() return jsonify([b.serialize() for b in discounts]) else: err = jsonify({'error': 'Invalid request method'}) err.status_code = 405 return err
def test_get_random(self): test_word = words.get_random() parts = test_word.split(" ") self.assertEqual(1, len(parts))
def test_get_random_seems_random(self): test_words = words.get_random(10) parts = test_words.split(" ") self.assertTrue(len(set(parts)) > 3)
def test_get_random_multiple(self): test_words = words.get_random(3) parts = test_words.split(" ") self.assertEqual(3, len(parts))