def test_user(client): """Verify users GraphQL query endpoint""" db.connect_db() emails = [ "*****@*****.**", "*****@*****.**", "*****@*****.**", ] for i, email in enumerate(emails): User.create( id=i, name="", email=email, usergroup_id="", valid_firstday="", valid_lastday="", lastlogin="", lastaction="", ) db.close_db(None) test_id = 0 matrix_email = '"%s"' % emails[test_id] graph_ql_query_string = ("""query User { user(email: %s) { id name } }""" % matrix_email) data = {"query": graph_ql_query_string} response_data = client.post("/graphql", json=data) assert response_data.status_code == 200 assert response_data.json["data"]["user"]["id"] == test_id
def test_all_bases(client): """Verify allBases GraphQL query endpoint""" bases = [ { "id": 1, "name": "oak-tree", "organisation_id": 1, "currency_name": "pound" }, { "id": 2, "name": "chicken", "organisation_id": 1, "currency_name": "peanuts" }, { "id": 3, "name": "sofa", "organisation_id": 1, "currency_name": "candles" }, ] db.connect_db() for base in bases: Base.create(**base) db.close_db(None) graph_ql_query_string = """query { allBases { id organisationId name currencyName } }""" data = {"query": graph_ql_query_string} response_data = client.post("/graphql", json=data) assert response_data.status_code == 200 all_bases = response_data.json["data"]["allBases"] for expected_base in bases: created_base = get_base_from_graphql(expected_base["id"], all_bases) assert created_base["id"] == expected_base["id"] assert created_base["organisationId"] == expected_base[ "organisation_id"] assert created_base["name"] == expected_base["name"] assert created_base["currencyName"] == expected_base["currency_name"]
def app(): """Fixture providing a baseline for unit tests that rely on database operations via the Flask app. Adapted from https://flask.palletsprojects.com/en/1.1.x/testing/#the-testing-skeleton.""" app = create_app() db_fd, db_filepath = tempfile.mkstemp(suffix=".sqlite3") app.config["DATABASE"] = { "name": db_filepath, "engine": "peewee.SqliteDatabase", } db.init_app(app) with db.database.bind_ctx(MODELS): db.database.create_tables(MODELS) db.close_db(None) with app.app_context(): yield app db.close_db(None) os.close(db_fd) os.remove(db_filepath)
def test_create_box(client): """Verify base GraphQL query endpoint""" mock_qr = {"id": 42, "code": "999"} db.connect_db() Qr.create(**mock_qr) db.close_db(None) box_creation_input_string = f"""{{ product_id: 1, items: 9999, location_id: 100000005, comments: "", size_id: 1, qr_barcode: "{mock_qr["code"]}", created_by: "1" }}""" gql_mutation_string = f"""mutation {{ createBox( box_creation_input : {box_creation_input_string} ) {{ id location_id product_id qr_id }} }}""" data = {"query": gql_mutation_string} response_data = client.post("/graphql", json=data) created_box = response_data.json["data"]["createBox"] assert response_data.status_code == 200 assert created_box["location_id"] == 100000005 assert created_box["product_id"] == 1 assert created_box["qr_id"] == mock_qr["id"]
def test_all_users(client): """Verify allUsers GraphQL query endpoint""" db.connect_db() emails = [ "*****@*****.**", "*****@*****.**", "*****@*****.**", ] for i, email in enumerate(emails): User.create( id=i, name="", email=email, usergroup_id="", valid_firstday="", valid_lastday="", lastlogin="", lastaction="", ) db.close_db(None) graph_ql_query_string = """query { allUsers { id name } }""" data = {"query": graph_ql_query_string} response_data = client.post("/graphql", json=data) print(response_data.json) assert response_data.status_code == 200 assert response_data.json["data"]["allUsers"][0]["id"] == 0