def test_add_mock_no_data(): schema = {"$ref": "#/components/schemas/item"} components = { "schemas": { "item": { "type": "object", "required": ["foo", "baz"], "x-hmt-id-path": "itemId", "properties": { "foo": {"type": "number"}, "bar": {"type": "string"}, "itemId": {"type": "string"}, }, } } } spec = spec_dict( path="/items/{id}", response_schema=schema, components=components, method="get" ) spec["paths"]["/items/{id}"]["x-hmt-entity"] = "item" spec["paths"]["/items/{id}"]["get"]["x-hmt-operation"] = "read" spec = convert_to_OpenAPIObject(spec) store = MockDataStore() store.add_mock(OpenAPISpecification(spec, "items")) assert 0 == len(store["items"].item) store["items"].item.insert({"foo": 10, "bar": "val", "itemId": "id123"}) assert 1 == len(store["items"].item) assert "val" == store["items"].item["id123"]["bar"]
def test_fake_array(mock_data_store): faker = StatefulFaker(mock_data_store) request = RequestBuilder.from_dict( dict(method="get", protocol="http", path="/items", host="api.com") ) schema = {"type": "array", "items": {"$ref": "#/components/schemas/item"}} components = { "schemas": { "item": { "type": "object", "required": ["foo", "bar"], "x-hmt-id-path": "itemId", "properties": { "foo": {"type": "number"}, "bar": {"type": "string"}, "itemId": {"type": "string"}, }, } } } spec = spec_dict( path="/items", response_schema=schema, components=components, method="get" ) spec["paths"]["/items"]["x-hmt-entity"] = "item" spec["paths"]["/items"]["get"]["x-hmt-operation"] = "read" spec = convert_to_OpenAPIObject(spec) mock_data_store.add_mock(OpenAPISpecification(spec, "default")) schema["components"] = components spec = OpenAPISpecification(source="default", api=spec) res = faker.process(spec, request) assert valid_schema(res.bodyAsJson, schema) assert 0 == len(res.bodyAsJson) mock_data_store["default"].item.insert({"foo": 10, "bar": "val", "itemId": "id123"}) res = faker.process(spec, request) assert valid_schema(res.bodyAsJson, schema) assert 1 == len(res.bodyAsJson) mock_data_store["default"].item.insert( {"foo": 10, "bar": "val", "itemId": "id1234"} ) res = faker.process(spec, request) assert valid_schema(res.bodyAsJson, schema) assert 2 == len(res.bodyAsJson)
def test_clear(): schema = {"$ref": "#/components/schemas/item"} components = { "schemas": { "item": { "type": "object", "required": ["foo", "baz"], "x-hmt-id-path": "itemId", "properties": { "foo": { "type": "number" }, "bar": { "type": "string" }, "itemId": { "type": "string" }, }, } } } spec = spec_dict(path="/items/{id}", response_schema=schema, components=components, method="get") spec["paths"]["/items/{id}"]["x-hmt-entity"] = "item" spec["paths"]["/items/{id}"]["get"]["x-hmt-operation"] = "read" spec = convert_to_OpenAPIObject(spec) entity = Entity("item", spec) mock_data = MockData() mock_data.add_entity(entity) entity.insert({"foo": 10, "bar": "val", "itemId": "id123"}) mock_data["global_data"] = "global" assert 1 == len(mock_data.item) assert "global_data" in mock_data mock_data.clear() assert 0 == len(mock_data.item) assert "global_data" not in mock_data
def test_storage(mock_data_store, http_client, base_url): schema = {"$ref": "#/components/schemas/item"} components = { "schemas": { "item": { "type": "object", "required": ["foo", "baz"], "x-hmt-id-path": "itemId", "properties": { "foo": {"type": "number"}, "bar": {"type": "string"}, "itemId": {"type": "string"}, }, } } } spec = spec_dict( path="/items/{id}", response_schema=schema, components=components, method="get" ) spec["paths"]["/items/{id}"]["x-hmt-entity"] = "item" spec["paths"]["/items/{id}"]["get"]["x-hmt-operation"] = "read" spec["x-hmt-data"] = {"item": [{"foo": 10, "bar": "val", "itemId": "id123"}]} spec = convert_to_OpenAPIObject(spec) mock_data_store.add_mock(OpenAPISpecification(spec, "items")) req = HTTPRequest(base_url + "/admin/storage", method="DELETE") response = yield http_client.fetch(req) assert response.code == 200 assert 0 == len(mock_data_store["items"].item) req = HTTPRequest(base_url + "/admin/storage/reset", method="POST", body="") response = yield http_client.fetch(req) assert response.code == 200 assert 1 == len(mock_data_store["items"].item) with pytest.raises(HTTPClientError): req = HTTPRequest(base_url + "/admin/storage/reset", method="DELETE") response = yield http_client.fetch(req) assert 1 == len(mock_data_store["items"].item)
def test_insert(): schema = {"$ref": "#/components/schemas/item"} components = { "schemas": { "item": { "type": "object", "required": ["foo", "baz"], "x-hmt-id-path": "itemId", "properties": { "foo": { "type": "number" }, "bar": { "type": "string" }, "itemId": { "type": "string" }, }, } } } spec = spec_dict(path="/items/{id}", response_schema=schema, components=components, method="get") spec["paths"]["/items/{id}"]["x-hmt-entity"] = "item" spec["paths"]["/items/{id}"]["get"]["x-hmt-operation"] = "read" spec = convert_to_OpenAPIObject(spec) entity = Entity("item", spec) entity.insert({"foo": 10, "bar": "val", "itemId": "id123"}) res = entity["id123"] assert 10 == res["foo"]
def test_insert(mock_data_store): faker = StatefulFaker(mock_data_store) request_schema = { "type": "object", "properties": {"item": {"$ref": "#/components/schemas/item"}}, } response_schema = {"$ref": "#/components/schemas/item"} components = { "schemas": { "item": { "type": "object", "required": ["foo"], "x-hmt-id-path": "itemId", "properties": { "foo": {"type": "number"}, "bar": {"type": "string"}, "baz": {"type": "string"}, "itemId": {"type": "string"}, }, } } } spec = spec_dict( path="/items", request_schema=request_schema, response_schema=response_schema, components=components, method="post", ) spec["paths"]["/items"]["x-hmt-entity"] = "item" spec["paths"]["/items"]["post"]["x-hmt-operation"] = "insert" spec = convert_to_OpenAPIObject(spec) mock_data_store.add_mock(OpenAPISpecification(spec, "default")) schema = response_schema schema["components"] = components spec = OpenAPISpecification(source="default", api=spec) request = RequestBuilder.from_dict( dict( method="post", protocol="http", path="/items", host="api.com", bodyAsJson={"item": {"foo": 10, "bar": "val"}}, ) ) res = faker.process(spec, request) assert valid_schema(res.bodyAsJson, schema) assert res.bodyAsJson["itemId"] is not None assert 10 == res.bodyAsJson["foo"] assert 1 == len(mock_data_store["default"].item) assert "val" == mock_data_store["default"].item[res.bodyAsJson["itemId"]]["bar"] request = RequestBuilder.from_dict( dict( method="post", protocol="http", path="/items", host="api.com", bodyAsJson={"item": {"foo": 20, "bar": "val1", "itemId": "id123"}}, ) ) res = faker.process(spec, request) assert valid_schema(res.bodyAsJson, schema) assert "id123" == res.bodyAsJson["itemId"] assert 20 == res.bodyAsJson["foo"] assert 2 == len(mock_data_store["default"].item) assert "val1" == mock_data_store["default"].item[res.bodyAsJson["itemId"]]["bar"] request = RequestBuilder.from_dict( dict( method="post", protocol="http", path="/items", host="api.com", bodyAsJson={"item": {"foo": 30, "itemId": "id123"}}, ) ) res = faker.process(spec, request) assert 2 == len(mock_data_store["default"].item) assert "bar" not in mock_data_store["default"].item[res.bodyAsJson["itemId"]] assert 30 == mock_data_store["default"].item[res.bodyAsJson["itemId"]]["foo"]
def test_insert_from_request(): schema = {"$ref": "#/components/schemas/item"} components = { "schemas": { "item": { "type": "object", "required": ["foo", "baz"], "x-hmt-id-path": "itemId", "properties": { "foo": { "type": "number" }, "bar": { "type": "string" }, "itemId": { "type": "string" }, }, } } } spec = spec_dict( path="/items/create", response_schema=schema, request_schema=schema, components=components, method="post", ) spec["paths"]["/items/create"]["x-hmt-entity"] = "item" spec["paths"]["/items/create"]["post"]["x-hmt-operation"] = "insert" spec = convert_to_OpenAPIObject(spec) entity = Entity("item", spec) request = RequestBuilder.from_dict( dict( method="post", protocol="http", path="/items", host="api.com", bodyAsJson={ "foo": 15, "bar": "val2" }, )) entity.insert_from_request("/items/create", request) res = next(iter(entity.values())) assert 15 == res["foo"] assert res["itemId"] is not None request = RequestBuilder.from_dict( dict( method="post", protocol="http", path="/items", host="api.com", bodyAsJson={ "foo": 10, "bar": "val", "itemId": "id123" }, )) entity.insert_from_request("/items/create", request) assert len(entity) == 2 res = entity["id123"] assert 10 == res["foo"]
def test_query(): schema_single = {"$ref": "#/components/schemas/item"} schema_array = { "accounts": { "items": { "$ref": "#/components/schemas/items" }, "type": "array" } } components = { "schemas": { "item": { "type": "object", "required": ["foo", "baz"], "x-hmt-id-path": "itemId", "properties": { "foo": { "type": "number" }, "bar": { "type": "string" }, "itemId": { "type": "string" }, }, } } } spec = spec_dict( path="/items/{id}", response_schema=schema_single, components=components, method="get", ) spec["paths"]["/items/{id}"]["x-hmt-entity"] = "item" spec["paths"]["/items/{id}"]["get"]["x-hmt-operation"] = "read" add_item( spec, path="/items", response_schema=schema_array, components=components, method="get", ) spec["paths"]["/items"]["x-hmt-entity"] = "item" spec["paths"]["/items"]["get"]["x-hmt-operation"] = "read" spec = convert_to_OpenAPIObject(spec) entity = Entity("item", spec) entity.insert({"foo": 10, "bar": "val", "itemId": "id123"}) entity.insert({"foo": 20, "bar": "val1", "itemId": "id1234"}) entity.insert({"foo": 30, "bar": "val2", "itemId": "id12345"}) res = entity.query_one( "/items/{id}", RequestBuilder.from_dict( dict(method="get", protocol="http", path="/items/id1234", host="api.com")), ) assert 20 == res["foo"] res = entity.query( "/items", RequestBuilder.from_dict( dict(method="get", protocol="http", path="/items", host="api.com")), ) assert 3 == len(res)