def test_build_request_body(): exchange = ( { "request": { "method": "post", "host": "api.com", "pathname": "/api", "body": json.dumps({ "foo": "hello", "bar": "bye", "zaz": "baz" }), "query": {}, "protocol": "http", "headers": {}, }, "response": { "statusCode": 200, "body": json.dumps({"message": "hello"}), "headers": {}, }, }, { "request": { "method": "post", "host": "api.com", "pathname": "/api", "body": json.dumps({ "foo": "hello", "bar": "bye", "baz": "zaz" }), "query": {}, "protocol": "http", "headers": {}, }, "response": { "statusCode": 200, "body": json.dumps({"message": "hello"}), "headers": {}, }, }, ) exchange = [HttpExchangeBuilder.from_dict(x) for x in exchange] schema = build_schema_batch(exchange, UpdateMode.GEN) request_content = schema.paths["/api"].post.requestBody.content assert_that(request_content, has_key("application/json")) schema = request_content["application/json"].schema assert "foo" in schema.properties assert "bar" in schema.properties assert "baz" in schema.properties assert "zaz" in schema.properties assert len(schema.required) == 2 assert "foo" in schema.required assert "bar" in schema.required
def test_pokeapi_schema_valid_replay(schema): pokeapi_schema = build_schema_batch(pokeapi_requests, UpdateMode.REPLAY) paths = list(pokeapi_schema.paths.keys()) assert_that(paths, has_length(14)) assert_that(paths, has_item("/v2/pokemon/")) assert_that(paths, has_item(matches_regexp(r"\/v2\/pokemon\/[\w]+\/"))) assert_that(paths, has_item(matches_regexp(r"\/v2\/type\/[\w]+"))) assert_that(paths, has_item(matches_regexp(r"\/v2\/ability\/[\w]+")))
def test_build_string_body(text_exchange): schema = build_schema_batch([text_exchange], UpdateMode.GEN) response_content = schema.paths["/v1"].get.responses["200"].content assert_that(response_content, has_key("text/plain")) media_type = response_content["text/plain"] assert media_type.schema._type == "string"
def test_schema_in_replay_mode(): reqs = [] with open("tests/build/recordings/opbank/recordings.jsonl", "r") as rr: reqs = rr.read().split("\n") reqs = [ HttpExchangeBuilder.from_dict(json.loads(r)) for r in reqs if r != "" ] r = build_schema_batch(reqs, UpdateMode.REPLAY) # this schema has four recordings, of which two correspond to /v1/payments/confirm assert 2 == len(r.paths["/v1/payments/confirm"].post.responses["201"]. content["application/json"].schema.oneOf)
def test_schema_update_with_query(exchange_wo_query, query_exchange, expected_path_name): schema = build_schema_batch([exchange_wo_query], UpdateMode.GEN) operation = schema.paths[expected_path_name].get assert operation.parameters == [] updated_schema = update_openapi(schema, query_exchange, UpdateMode.GEN) operation = updated_schema.paths[expected_path_name].get assert_that(operation.parameters, has_length(2)) first_query_param = operation.parameters[0] assert not first_query_param.required
def test_pokeapi_schema_valid(schema): # this should conflate to # /pokemon # /abilities/* # /types/* # /pokemon/* # meaning that it should recognize wildcards # from all these paths pokeapi_schema = build_schema_batch(pokeapi_requests, UpdateMode.GEN) paths = pokeapi_schema.paths.keys() assert_that(paths, has_length(4)) assert_that(paths, has_item("/v2/pokemon/")) assert_that(paths, has_item(matches_regexp(r"\/v2\/pokemon\/\{[\w]+\}"))) assert_that(paths, has_item(matches_regexp(r"\/v2\/type\/\{[\w]+\}"))) assert_that(paths, has_item(matches_regexp(r"\/v2\/ability\/\{[\w]+\}")))
def test_build_with_query(query_exchange, expected_path_name): schema = build_schema_batch([query_exchange], UpdateMode.GEN) assert_that(list(schema.paths.keys()), is_([expected_path_name])) operation = schema.paths[expected_path_name].get assert operation.parameters parameters = operation.parameters assert_that(parameters, has_length(2)) parameter_names = [param.name for param in parameters] assert_that(set(parameter_names), is_(set(["id", "car"])))
def schema(): return build_schema_batch(requests, UpdateMode.GEN)