def categories(db: Session = Depends(get_db)): # Leo todas las categorías categories = db.query(models.Category).all() # Leo las categorías usando la sintaxis ORM de SQLAlchemy cats = [ schema.Category(id=id, name=category.name) for id, category in enumerate(categories) ] return {"categories": cats}
def test_02_add_2nd_category(self): session = Session() try: category = schema.Category(name='Gaming consoles') session.add(category) session.commit() except Exception as e: session.rollback() raise (e) self.assertEqual(category.id, 2)
def test_01_add_category(self): session = Session() try: category = schema.Category(name='Electronics') session.add(category) session.commit() except Exception as e: session.rollback() raise (e) self.assertEqual(category.id, 1)
def test_03_add_child_category(self): session = Session() try: parent = session.query( schema.Category).filter_by(name='Electronics').first() category = schema.Category(name='Televisions', parent_id=parent.id) session.add(category) session.commit() except Exception as e: session.rollback() raise (e) self.assertEqual(parent.children[0].id, 3)
r = requests.put('http://localhost:5000/item', json={'id': '2'}) self.assertEqual(r.status_code, 400) def test_14_update_item_error_2(self): r = requests.put('http://localhost:5000/category', json={'parent': 'itm.1', 'id': '2'}) self.assertEqual(r.status_code, 400) if __name__ == '__main__': dbname = "dbtest" os.remove(dbname) schema.main(dbname) engine = create_engine("sqlite:///"+dbname, echo=False) Session = sessionmaker(bind=engine) session = Session() try: category1 = schema.Category(name='Electronics') session.add(category1) session.commit() category2 = schema.Category(name='Televisions', parent_id=category1.id) session.add(category2) session.commit() category3 = schema.Category(name='Gaming consoles') session.add(category3) session.commit() item1 = schema.Item(label='49-inch LCD', category_id=category2.id) session.add(item1) session.commit() item2 = schema.Item(label='40-inch plasma', category_id=category2.id) session.add(item2) session.commit() item3 = schema.Item(label='32-inch CRT', category_id=category2.id)