def test_delete_item_db(session): """Test if through session we can delete an item Args: session (SQLAlchemy Object Session): It's the session object from SQLALchemy Instance """ # Añadimos el item add_item = Items("Conjured Mana Cake", 5, 8) session.add(add_item) session.commit() # Ahora lo eliminamos session.query(Items).filter(Items.name == "Conjured Mana Cake", Items.sell_in == 5, Items.quality == 8).delete() session.commit() # Verificamos si efectivamente lo ha eliminado si ningún row es igual al que eliminamos, CAMBIARLO y REFACTORIZARLO for item in session.query(Items).all(): assert item.name != "Conjured Mana Cake" assert item.sell_in != 5 assert item.quality != 8
def test_udpate_quality_items_db(session): """Test if through session we can update the quality of all items Args: session (SQLAlchemy Object Session): It's the session object from SQLALchemy Instance """ # Añadimos el item add_item = Items("Conjured Mana Cake", 5, 8) session.add(add_item) session.commit() for item in session.query(Items).all(): # Creamos el objeto Item a partir de la info de la Lista que insertamos en los párametros del método: createObjectItem() itemObject = Factory.createObjectItem([item.name, item.sell_in, item.quality]) # Actualizamos la calidad del item itemObject.update_quality() # Actualizamos los datos de cada item item.sell_in = itemObject.get_sell_in() item.quality = itemObject.get_quality() # Guardamos los datos actualizados session.commit() # Testeamos si efectivamente se actualizo el item assert item.name == "Conjured Mana Cake" assert item.sell_in == 4 assert item.quality == 6
def db(app): """Get the Database Object of SQLAlchemy, where before get the _db it's open the app_context() and init the app in the DB and create all Models, after the Models and database is filled in with the basic items from loadInventory(), after each test the data of models and database restart by default. The scope of this fixture is for each test function Args: app (flask instance): App Flask Yields: SQLAlchemy Instance: Yields a SQLAlchemy Object with the session init """ with app.test_client() as client: with app.app_context(): _db.init_app(app) # _db.drop_all() _db.create_all() # Obtenemos la lista con los items inventario = Factory.loadInventory() # Poblamos la Base de datos introduciendo los datos for item in inventario: add_item = Items( name=item["name"], sell_in=item["sell_in"], quality=item["quality"] ) _db.session.add(add_item) _db.session.commit() yield _db _db.session.query(Items).delete() _db.session.commit()
def client(app): """Get the test_client of APP Flask, where before of Yield sentence, it adds the basic items into database and models with the main goal of each test use this data/items to test it and after each test the data of models and database restart by default. This help us to isolate each test from another test Args: app (Flask instance): APP Flask Yields: test_client: Yields a test_client from APP Flask to test our test cases """ with app.test_client() as client: with app.app_context(): _db.init_app(app) # _db.drop_all() _db.create_all() # Obtenemos la lista con los items inventario = Factory.loadInventory() # Poblamos la Base de datos introduciendo los datos for item in inventario: add_item = Items( name=item["name"], sell_in=item["sell_in"], quality=item["quality"] ) _db.session.add(add_item) _db.session.commit() yield client _db.session.query(Items).delete() _db.session.commit()
def test_add_item_db(session): """Test if through session we can add an item Args: session (SQLAlchemy Object Session): It's the session object from SQLALchemy Instance """ add_item = Items("Conjured Mana Cake", 5, 8) session.add(add_item) session.commit() assert add_item.name == "Conjured Mana Cake" assert add_item.sell_in == 5 assert add_item.quality == 8
def test_get_items_db(session): """Test if through session we can get all items Args: session (SQLAlchemy Object Session): It's the session object from SQLALchemy Instance """ add_item = Items("Conjured Mana Cake", 5, 8) session.add(add_item) session.commit() # Funciona, pero al parecer no carga los datos que ya están en la base de datos, por lo cual no tiene ninguno, lo que implica que tengo que agregar un nuevo objeto/row items_db = [item for item in session.query(Items).all()] assert len(items_db) == 1
def test_get_by_sellin_db(session): """Test if through session we can get an item by its sell_in Args: session (SQLAlchemy Object Session): It's the session object from SQLALchemy Instance """ # Añadimos el item add_item = Items("Conjured Mana Cake", 5, 8) session.add(add_item) session.commit() # Get el item por su sell_in, en este caso como solo hay un item de esa calidad, retornará el del conjured mana cake get_conjured_by_sellin = session.query(Items).filter( Items.sell_in == 5).first() assert get_conjured_by_sellin.name == "Conjured Mana Cake" assert get_conjured_by_sellin.sell_in == 5 assert get_conjured_by_sellin.quality == 8
def init_db(): # Obtenemos la DB db = get_db() # Obtenemos la lista con los items inventario = Factory.loadInventory() # Creo todos los Models db.create_all() # Poblamos la Base de datos introduciendo los datos for item in inventario: add_item = Items(name=item["name"], sell_in=item["sell_in"], quality=item["quality"]) db.session.add(add_item) db.session.commit()