def create_categories(session, user1): """ Creates multiple Category objects and adds them to the session :return: categories """ drama = Category(name="Drama", user=user1) scifi = Category(name="Science Fiction", user=user1) action = Category(name="Action", user=user1) kids = Category(name="Kids", user=user1) horror = Category(name="Horror", user=user1) fantasy = Category(name="Fantasy", user=user1) comedy = Category(name="Comedy", user=user1) categories = dict() categories["drama"] = drama categories["scifi"] = scifi categories["action"] = action categories["kids"] = kids categories["horror"] = horror categories["fantasy"] = fantasy categories["comedy"] = comedy for cat in categories.values(): session.add(cat) return categories
def setUpClass(self): # configurations app.config["TESTING"] = True app.config["DEBUG"] = False app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + TEST_DB self.app = app.test_client() db.drop_all() db.create_all() self.FOOD_ITEM = "soy_milk" self.CATEGORY = "beverages" self.SINGLE_FOOD_URL = "/api/foods/" + self.FOOD_ITEM self.FOOD_BY_CATEGORY_URL = "/api/food_category/" + self.CATEGORY self.ALL_FOODS_URL = "/api/foods" category = Category(name=self.CATEGORY) category.save() food = Food(name=self.FOOD_ITEM, url="url", category_id=1, calories="123", carbs="678", protein="78", sugar="987", fat="678", sodium="12") food.save()
def test_category_object_is_deleted(self): bean_based = Category(name="bean_based") bean_based.save() bean_based = Category.query.filter_by(name="bean_based").one() bean_based.remove() self.assertIsNone(Category.query.filter_by(name="bean_based").first()) self.assertEqual(0, len(Category.query.all()))
def test_category_is_unique(self): with self.assertRaises(sqlalchemy.exc.IntegrityError): drinks = Category(name="drinks") drinks.save() water = Category(name="drinks") water.save()
def test_category_object_not_constructed_well(self): with self.assertRaises(sqlalchemy.exc.IntegrityError): rice_based = Category() rice_based.save()
def test_category_object_exists(self): rice_based = Category(name="rice_based") rice_based.save() rice_based = Category.query.filter_by(name="rice_based").one() self.assertIsInstance(rice_based, Category) self.assertEqual("rice_based", rice_based.name)
from application import db, Category db.create_all() db.session.add_all([Category(name="Soccer"), Category(name="Basketball"), Category(name="Baseball"), Category(name="Frisbee"), Category(name="Snowboarding"), Category(name="Rock Climbing"), Category(name="Foosball"), Category(name="Skating"), Category(name="Hockey")]) db.session.commit()
session = db.session #Dumb user user1 = User(name="ilaria", email="*****@*****.**", picture="") session.add(user1) session.commit() user2 = User(name="kevin", email="*****@*****.**", picture="") session.add(user2) session.commit() #Python section category1 = Category(name="Python", user=user2) session.add(category1) session.commit() item1 = Item( title="Learning Python", author="Mark Lutz", description= "Get a comprehensive, in-depth introduction to the core Python language with this hands-on book. Based on author Mark Lutz’s popular training course, this updated fifth edition will help you quickly write efficient, high-quality code with Python. It’s an ideal way to begin, whether you’re new to programming or a professional developer versed in other languages.", category=category1) session.add(item1) session.commit() item2 = Item(