def test_app(): app = Flask(__name__, template_folder="../tyko/templates") db = SQLAlchemy(app) tyko.create_app(app, verify_db=False) tyko.database.init_database(db.engine) app.config["TESTING"] = True return app.test_client()
def test_create_and_read2(data_type, data_value): app = Flask(__name__, template_folder="../tyko/templates") db = SQLAlchemy(app) tyko.create_app(app, verify_db=False) tyko.database.init_database(db.engine) app.config["TESTING"] = True with app.test_client() as server: route = "/api/{}/".format(data_type) create_resp = server.post(route, data=json.dumps(data_value), content_type='application/json') assert create_resp.status == "200 OK", "Failed to create a new entity with {}".format( route) new_id = json.loads(create_resp.data)["id"] assert new_id is not None read_res = server.get("/api/{}/{}".format(data_type, new_id)) assert read_res.status_code == 200, "{} failed with status {}".format( route, read_res.status_code) read_resp_data = json.loads(read_res.data) data_object = read_resp_data[data_type] for k, v in data_value.items(): if k == "files" and isinstance(v, list): continue assert data_object[k] == v
def test_api_routes(route): app = Flask(__name__, template_folder="../tyko/templates") db = SQLAlchemy(app) tyko.create_app(app, verify_db=False) tyko.database.init_database(db.engine) app.config["TESTING"] = True with app.test_client() as server: resp = server.get(route) assert resp.status == "200 OK"
def test_pbcore_valid_id(tmpdir): app = flask.Flask(__name__, template_folder="../tyko/" "templates") app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:" db = SQLAlchemy(app) tyko.create_app(app, verify_db=False) tyko.database.init_database(db.engine) app.config["TESTING"] = True with app.test_client() as server: sample_collection = server.post( "/api/collection/", data=json.dumps({ "collection_name": "My dummy collection", "department": "preservation", }), content_type='application/json').get_json() sample_project = server.post( "/api/project/", data=json.dumps({ "title": "my dumb project", }), content_type='application/json').get_json() sample_object = server.post( flask.url_for("project_add_object", project_id=sample_project['id']), data=json.dumps({ "name": "My dummy object", "collectionId": sample_collection['id'] }), content_type='application/json').get_json()['object'] sample_item = server.post(flask.url_for( "object_item", project_id=sample_project['id'], object_id=sample_object['object_id']), data=json.dumps({ "name": "My dummy item", "format_id": 1 }), content_type='application/json').get_json() pbcore_xml = server.get( flask.url_for("object_pbcore", id=sample_object['object_id'])).get_data() doc = etree.fromstring(pbcore_xml) print(str(etree.tostring(doc, pretty_print=True), encoding="utf-8")) assert PBCORE_SCHEMA.validate( doc) is True, PBCORE_SCHEMA.error_log.filter_from_errors( ).last_error.message
def test_get_object_pbcore(): app = Flask(__name__, template_folder="../tyko/templates") app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:" db = SQLAlchemy(app) tyko.create_app(app, verify_db=False) tyko.database.init_database(db.engine) app.config["TESTING"] = True with app.test_client() as server: create_resp = server.post("/api/object/", data=json.dumps({"name": "my stupid object"}), content_type='application/json') assert create_resp.status == "200 OK", "Failed to create a new object" new_id = json.loads(create_resp.data)["id"] assert new_id is not None pbcore_req_res = server.get("/api/object/{}-pbcore.xml".format(new_id)) assert pbcore_req_res.status == "200 OK", "Failed create a PBCore record for id {}".format( new_id)
def test_static_pages(route): app = tyko.create_app(verify_db=False) app.config["TESTING"] = True with app.test_client() as server: resp = server.get(route) assert resp.status == "200 OK"