def test_get_close_db(app): with app.app_context(): db = get_db() assert db is get_db() with pytest.raises(sqlite3.ProgrammingError) as e: db.execute("SELECT 1") assert "closed" in str(e.value)
def test_add(client, app): assert client.get("/add").status_code == 200 client.post("add", data={"items": "5 - CORNERS 8120977429 OUR"}) with app.app_context(): db = get_db() count = db.execute("select count(*) from items").fetchone()[0] assert count == 4
def test_checkin(client, app): assert client.get("/checkin").status_code == 200 client.post("checkin", data={"items": "2 - CORNERS 8120977429 OUR"}) with app.app_context(): db = get_db() item = db.execute( "select * from items where location = 'turns'").fetchone() assert item is None
def add(): if request.method == "POST": items = request.form['items'] location = "" db = get_db() error = None if not items: error = "1 or more items are required." if error is None: insert_items(items, location, db) return redirect(url_for("tracker.index")) flash(error) return render_template("tracker/add.html")
def app(): """Create and configure a new app instance for each test.""" # create a temporary file to isolate the database for each test db_fd, db_path = tempfile.mkstemp() report_path = tempfile.mkdtemp() # create the app with common test config app = create_app({ "TESTING": True, "DATABASE": db_path, "REPORT_PATH": report_path }) # create the database and load test data with app.app_context(): init_db() get_db().executescript(_data_sql) yield app # close and remove the temporary database os.close(db_fd) os.unlink(db_path) os.rmdir(report_path)
def test_checkout(client, app): assert client.get("/checkout").status_code == 200 client.post( "/checkout", data={ "items": "1 - CORNERS 8120977386 OUR\n3 - CORNERS 8120959284 OUR\n", "location": "turns" }) with app.app_context(): db = get_db() count = db.execute( "select count(*) from items where location = 'turns'").fetchone( )[0] assert count == 3
def create(): file_name = "checkedout-{}.txt".format(datetime.now().strftime("%Y%m%d")) report_file = os.path.join(current_app.config["REPORT_PATH"], file_name) error = None db = get_db() items = db.execute( "select item_id, location from items where location != ''").fetchall() if len(items) < 1: error = "Nothing checked out. No report generated." else: with open(report_file, "w") as f: for item in items: f.write("{},{}\n".format(item["item_id"], item["location"])) flash(error) return redirect(url_for("reports.view"))
def checkin(): if request.method == "POST": items = request.form['items'] location = "" db = get_db() error = None if not items: error = "1 or more items are required." elif not items_exist(items, db): error = "Item not found in database." if error is None: update_db(items, location, db) return redirect(url_for("tracker.index")) flash(error) return render_template("tracker/checkin.html")
def test_import_file(client, app): assert client.get("/import_file").status_code == 200 files = {'file': io.BytesIO(b"test-data\ntest2")} params = { name: (f, "mocked_name_{}".format(name)) for name, f in files.items() } params['bucket'] = 'test' params['keyname'] = 'mocked_name_test' params['content_type'] = 'text' response = client.post('/import_file', data=params, content_type='multipart/form-data', follow_redirects=True) assert response.status_code == 200 with app.app_context(): db = get_db() count = db.execute("select count(*) from items").fetchone()[0] assert count == 5
def import_file(): if request.method == "POST": file = request.files['file'] location = "" db = get_db() error = None bitems = file.read() items = bitems.decode("utf-8") if file.filename == "": error = "No file Name." elif items == "": error = "The file appears to be empty." elif invalid_data(items): error = "The file contains invalid data." if error is None: insert_items(items, location, db) return redirect(url_for("tracker.index")) flash(error) print(error) return render_template("tracker/import_file.html")
def index(): """Show all equipment checked out.""" db = get_db() items = db.execute("select item_id, location from items where location != ''").fetchall() return render_template("tracker/index.html", items=items)
def test_create_with_no_data(client, app): with app.app_context(): get_db().execute("update items set location = ''") get_db().commit() response = client.get("/reports/create", follow_redirects=True) assert b"Nothing checked out. No report generated." in response.data