Buckets:
| """Tests for OpenAPI import functionality.""" | |
| from uuid import uuid4 | |
| import pytest | |
| from tessera.models.enums import ResourceType | |
| from tessera.services.openapi import ( | |
| _extract_tessera_guarantees, | |
| _infer_nullability_from_schema, | |
| _merge_guarantees, | |
| endpoints_to_assets, | |
| generate_fqn, | |
| parse_openapi, | |
| ) | |
| class TestOpenAPIParser: | |
| """Tests for OpenAPI spec parsing.""" | |
| def test_parse_simple_spec(self) -> None: | |
| """Test parsing a simple OpenAPI 3.x spec.""" | |
| spec = { | |
| "openapi": "3.0.0", | |
| "info": { | |
| "title": "Pet Store API", | |
| "version": "1.0.0", | |
| "description": "A sample pet store API", | |
| }, | |
| "paths": { | |
| "/pets": { | |
| "get": { | |
| "operationId": "listPets", | |
| "summary": "List all pets", | |
| "responses": { | |
| "200": { | |
| "content": { | |
| "application/json": { | |
| "schema": { | |
| "type": "array", | |
| "items": {"$ref": "#/components/schemas/Pet"}, | |
| } | |
| } | |
| } | |
| } | |
| }, | |
| }, | |
| "post": { | |
| "operationId": "createPet", | |
| "summary": "Create a pet", | |
| "requestBody": { | |
| "content": { | |
| "application/json": {"schema": {"$ref": "#/components/schemas/Pet"}} | |
| } | |
| }, | |
| "responses": { | |
| "201": { | |
| "content": { | |
| "application/json": { | |
| "schema": {"$ref": "#/components/schemas/Pet"} | |
| } | |
| } | |
| } | |
| }, | |
| }, | |
| }, | |
| "/pets/{petId}": { | |
| "get": { | |
| "operationId": "getPet", | |
| "summary": "Get a pet by ID", | |
| "responses": { | |
| "200": { | |
| "content": { | |
| "application/json": { | |
| "schema": {"$ref": "#/components/schemas/Pet"} | |
| } | |
| } | |
| } | |
| }, | |
| }, | |
| }, | |
| }, | |
| "components": { | |
| "schemas": { | |
| "Pet": { | |
| "type": "object", | |
| "properties": { | |
| "id": {"type": "integer"}, | |
| "name": {"type": "string"}, | |
| "tag": {"type": "string"}, | |
| }, | |
| "required": ["id", "name"], | |
| } | |
| } | |
| }, | |
| } | |
| result = parse_openapi(spec) | |
| assert result.title == "Pet Store API" | |
| assert result.version == "1.0.0" | |
| assert result.description == "A sample pet store API" | |
| assert len(result.endpoints) == 3 | |
| assert len(result.errors) == 0 | |
| # Check endpoints | |
| endpoints_by_path_method = {(e.path, e.method): e for e in result.endpoints} | |
| # GET /pets | |
| get_pets = endpoints_by_path_method[("/pets", "GET")] | |
| assert get_pets.operation_id == "listPets" | |
| assert get_pets.summary == "List all pets" | |
| assert get_pets.response_schema is not None | |
| assert get_pets.response_schema["type"] == "array" | |
| # POST /pets | |
| post_pets = endpoints_by_path_method[("/pets", "POST")] | |
| assert post_pets.operation_id == "createPet" | |
| assert post_pets.request_schema is not None | |
| assert post_pets.response_schema is not None | |
| def test_parse_spec_with_no_schemas(self) -> None: | |
| """Test parsing a spec with endpoints that have no schemas.""" | |
| spec = { | |
| "openapi": "3.0.0", | |
| "info": {"title": "Simple API", "version": "1.0.0"}, | |
| "paths": { | |
| "/health": { | |
| "get": { | |
| "summary": "Health check", | |
| "responses": {"200": {"description": "OK"}}, | |
| } | |
| } | |
| }, | |
| } | |
| result = parse_openapi(spec) | |
| assert result.title == "Simple API" | |
| assert len(result.endpoints) == 1 | |
| assert result.endpoints[0].request_schema is None | |
| assert result.endpoints[0].response_schema is None | |
| assert result.endpoints[0].combined_schema == {"type": "object"} | |
| def test_parse_invalid_openapi_version(self) -> None: | |
| """Test that non-3.x OpenAPI specs are rejected.""" | |
| spec = { | |
| "swagger": "2.0", | |
| "info": {"title": "Old API", "version": "1.0.0"}, | |
| "paths": {}, | |
| } | |
| result = parse_openapi(spec) | |
| assert len(result.errors) > 0 | |
| assert "3.x" in result.errors[0].lower() or "3." in result.errors[0] | |
| def test_parse_spec_with_refs(self) -> None: | |
| """Test that $ref pointers are properly resolved.""" | |
| spec = { | |
| "openapi": "3.0.0", | |
| "info": {"title": "Ref Test API", "version": "1.0.0"}, | |
| "paths": { | |
| "/users": { | |
| "post": { | |
| "requestBody": { | |
| "content": { | |
| "application/json": { | |
| "schema": {"$ref": "#/components/schemas/User"} | |
| } | |
| } | |
| }, | |
| "responses": { | |
| "201": { | |
| "content": { | |
| "application/json": { | |
| "schema": {"$ref": "#/components/schemas/User"} | |
| } | |
| } | |
| } | |
| }, | |
| } | |
| } | |
| }, | |
| "components": { | |
| "schemas": { | |
| "User": { | |
| "type": "object", | |
| "properties": { | |
| "id": {"type": "integer"}, | |
| "email": {"type": "string", "format": "email"}, | |
| }, | |
| } | |
| } | |
| }, | |
| } | |
| result = parse_openapi(spec) | |
| assert len(result.endpoints) == 1 | |
| endpoint = result.endpoints[0] | |
| # Check that $ref was resolved | |
| assert endpoint.request_schema is not None | |
| assert endpoint.request_schema.get("type") == "object" | |
| assert "properties" in endpoint.request_schema | |
| assert "id" in endpoint.request_schema["properties"] | |
| class TestGenerateFqn: | |
| """Tests for FQN generation from API endpoints.""" | |
| def test_simple_path(self) -> None: | |
| """Test FQN for a simple path.""" | |
| fqn = generate_fqn("Users API", "/users", "GET") | |
| assert fqn == "api.users_api.get_users" | |
| def test_path_with_parameters(self) -> None: | |
| """Test FQN for a path with URL parameters.""" | |
| fqn = generate_fqn("Users API", "/users/{userId}/posts/{postId}", "GET") | |
| assert fqn == "api.users_api.get_users_userid_posts_postid" | |
| def test_root_path(self) -> None: | |
| """Test FQN for root path.""" | |
| fqn = generate_fqn("Health API", "/", "GET") | |
| assert fqn == "api.health_api.get_root" | |
| def test_special_characters_in_title(self) -> None: | |
| """Test that special characters in title are handled.""" | |
| fqn = generate_fqn("My Cool API v2.0", "/data", "POST") | |
| assert fqn == "api.my_cool_api_v20.post_data" | |
| def test_empty_title(self) -> None: | |
| """Test handling of empty title.""" | |
| fqn = generate_fqn("", "/test", "PUT") | |
| assert fqn == "api.unknown.put_test" | |
| class TestEndpointsToAssets: | |
| """Tests for converting parsed endpoints to asset definitions.""" | |
| def test_converts_endpoints_to_assets(self) -> None: | |
| """Test basic conversion of endpoints to assets.""" | |
| spec = { | |
| "openapi": "3.0.0", | |
| "info": {"title": "Test API", "version": "2.0.0"}, | |
| "paths": { | |
| "/items": { | |
| "get": { | |
| "operationId": "listItems", | |
| "responses": { | |
| "200": { | |
| "content": { | |
| "application/json": { | |
| "schema": { | |
| "type": "array", | |
| "items": {"type": "string"}, | |
| } | |
| } | |
| } | |
| } | |
| }, | |
| } | |
| } | |
| }, | |
| } | |
| parse_result = parse_openapi(spec) | |
| team_id = uuid4() | |
| assets = endpoints_to_assets(parse_result, team_id, "staging") | |
| assert len(assets) == 1 | |
| asset = assets[0] | |
| assert asset.fqn == "api.test_api.get_items" | |
| assert asset.resource_type == ResourceType.API_ENDPOINT | |
| assert asset.metadata["openapi_source"]["api_title"] == "Test API" | |
| assert asset.metadata["openapi_source"]["api_version"] == "2.0.0" | |
| assert asset.metadata["openapi_source"]["path"] == "/items" | |
| assert asset.metadata["openapi_source"]["method"] == "GET" | |
| assert asset.metadata["openapi_source"]["operation_id"] == "listItems" | |
| def test_schema_in_asset(self) -> None: | |
| """Test that combined schema is included in asset.""" | |
| spec = { | |
| "openapi": "3.0.0", | |
| "info": {"title": "Test API", "version": "1.0.0"}, | |
| "paths": { | |
| "/data": { | |
| "post": { | |
| "requestBody": { | |
| "content": { | |
| "application/json": { | |
| "schema": { | |
| "type": "object", | |
| "properties": {"name": {"type": "string"}}, | |
| } | |
| } | |
| } | |
| }, | |
| "responses": { | |
| "201": { | |
| "content": { | |
| "application/json": { | |
| "schema": { | |
| "type": "object", | |
| "properties": {"id": {"type": "integer"}}, | |
| } | |
| } | |
| } | |
| } | |
| }, | |
| } | |
| } | |
| }, | |
| } | |
| parse_result = parse_openapi(spec) | |
| assets = endpoints_to_assets(parse_result, uuid4(), "production") | |
| assert len(assets) == 1 | |
| schema = assets[0].schema_def | |
| assert schema["type"] == "object" | |
| assert "request" in schema["properties"] | |
| assert "response" in schema["properties"] | |
| assert schema["properties"]["request"]["properties"]["name"]["type"] == "string" | |
| assert schema["properties"]["response"]["properties"]["id"]["type"] == "integer" | |
| class TestOpenAPIImportEndpoint: | |
| """Tests for the OpenAPI import API endpoint.""" | |
| def sample_openapi_spec(self) -> dict: | |
| """Sample OpenAPI spec for testing.""" | |
| return { | |
| "openapi": "3.0.0", | |
| "info": { | |
| "title": "Sample API", | |
| "version": "1.0.0", | |
| }, | |
| "paths": { | |
| "/users": { | |
| "get": { | |
| "operationId": "listUsers", | |
| "summary": "List users", | |
| "responses": { | |
| "200": { | |
| "content": { | |
| "application/json": { | |
| "schema": { | |
| "type": "array", | |
| "items": { | |
| "type": "object", | |
| "properties": { | |
| "id": {"type": "integer"}, | |
| "name": {"type": "string"}, | |
| }, | |
| }, | |
| } | |
| } | |
| } | |
| } | |
| }, | |
| }, | |
| "post": { | |
| "operationId": "createUser", | |
| "summary": "Create user", | |
| "requestBody": { | |
| "content": { | |
| "application/json": { | |
| "schema": { | |
| "type": "object", | |
| "properties": {"name": {"type": "string"}}, | |
| "required": ["name"], | |
| } | |
| } | |
| } | |
| }, | |
| "responses": { | |
| "201": { | |
| "content": { | |
| "application/json": { | |
| "schema": { | |
| "type": "object", | |
| "properties": { | |
| "id": {"type": "integer"}, | |
| "name": {"type": "string"}, | |
| }, | |
| } | |
| } | |
| } | |
| } | |
| }, | |
| }, | |
| }, | |
| }, | |
| } | |
| async def test_import_openapi_dry_run(self, client, sample_openapi_spec) -> None: | |
| """Test dry run import of OpenAPI spec.""" | |
| # Create a team first using the API | |
| team_resp = await client.post("/api/v1/teams", json={"name": "API Team"}) | |
| assert team_resp.status_code == 201 | |
| team_id = team_resp.json()["id"] | |
| response = await client.post( | |
| "/api/v1/sync/openapi", | |
| json={ | |
| "spec": sample_openapi_spec, | |
| "owner_team_id": team_id, | |
| "dry_run": True, | |
| }, | |
| ) | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert data["api_title"] == "Sample API" | |
| assert data["api_version"] == "1.0.0" | |
| assert data["endpoints_found"] == 2 | |
| assert data["assets_created"] == 2 | |
| assert data["contracts_published"] == 2 | |
| # All actions should be "would_create" in dry run | |
| for endpoint in data["endpoints"]: | |
| assert endpoint["action"] == "would_create" | |
| async def test_import_openapi_creates_assets(self, client, sample_openapi_spec) -> None: | |
| """Test that OpenAPI import creates assets and contracts.""" | |
| # Create a team first using the API | |
| team_resp = await client.post("/api/v1/teams", json={"name": "API Team 2"}) | |
| assert team_resp.status_code == 201 | |
| team_id = team_resp.json()["id"] | |
| response = await client.post( | |
| "/api/v1/sync/openapi", | |
| json={ | |
| "spec": sample_openapi_spec, | |
| "owner_team_id": team_id, | |
| "auto_publish_contracts": True, | |
| }, | |
| ) | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert data["assets_created"] == 2 | |
| assert data["contracts_published"] == 2 | |
| # Verify assets via API | |
| assets_resp = await client.get(f"/api/v1/assets?owner={team_id}") | |
| assert assets_resp.status_code == 200 | |
| assets = assets_resp.json()["results"] | |
| assert len(assets) == 2 | |
| # Verify contracts via API | |
| for asset in assets: | |
| assert asset["resource_type"] == "api_endpoint" | |
| contracts_resp = await client.get(f"/api/v1/assets/{asset['id']}/contracts") | |
| assert contracts_resp.status_code == 200 | |
| contracts = contracts_resp.json()["results"] | |
| assert len(contracts) == 1 | |
| assert contracts[0]["version"] == "1.0.0" | |
| async def test_import_openapi_without_contracts(self, client, sample_openapi_spec) -> None: | |
| """Test OpenAPI import without auto-publishing contracts.""" | |
| # Create a team first using the API | |
| team_resp = await client.post("/api/v1/teams", json={"name": "API Team 3"}) | |
| assert team_resp.status_code == 201 | |
| team_id = team_resp.json()["id"] | |
| response = await client.post( | |
| "/api/v1/sync/openapi", | |
| json={ | |
| "spec": sample_openapi_spec, | |
| "owner_team_id": team_id, | |
| "auto_publish_contracts": False, | |
| }, | |
| ) | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert data["assets_created"] == 2 | |
| assert data["contracts_published"] == 0 | |
| # Verify assets were created but no contracts | |
| assets_resp = await client.get(f"/api/v1/assets?owner={team_id}") | |
| assert assets_resp.status_code == 200 | |
| assets = assets_resp.json()["results"] | |
| assert len(assets) == 2 | |
| for asset in assets: | |
| contracts_resp = await client.get(f"/api/v1/assets/{asset['id']}/contracts") | |
| assert contracts_resp.status_code == 200 | |
| contracts = contracts_resp.json()["results"] | |
| assert len(contracts) == 0 | |
| async def test_import_openapi_team_not_found(self, client, sample_openapi_spec) -> None: | |
| """Test that import fails if team doesn't exist.""" | |
| response = await client.post( | |
| "/api/v1/sync/openapi", | |
| json={ | |
| "spec": sample_openapi_spec, | |
| "owner_team_id": str(uuid4()), | |
| }, | |
| ) | |
| assert response.status_code == 404 | |
| async def test_import_openapi_invalid_spec(self, client) -> None: | |
| """Test that import fails with invalid OpenAPI spec.""" | |
| # Create a team first using the API | |
| team_resp = await client.post("/api/v1/teams", json={"name": "API Team 4"}) | |
| assert team_resp.status_code == 201 | |
| team_id = team_resp.json()["id"] | |
| response = await client.post( | |
| "/api/v1/sync/openapi", | |
| json={ | |
| "spec": { | |
| "swagger": "2.0", | |
| "info": {"title": "Old", "version": "1.0"}, | |
| "paths": {}, | |
| }, | |
| "owner_team_id": team_id, | |
| }, | |
| ) | |
| assert response.status_code == 400 | |
| data = response.json() | |
| # Error structure is: {"error": {"code": ..., "details": {"errors": [...]}}} | |
| assert "error" in data | |
| assert data["error"]["code"] == "INVALID_OPENAPI_SPEC" | |
| assert "errors" in data["error"]["details"] | |
| class TestOpenAPIGuarantees: | |
| """Tests for OpenAPI guarantee extraction and merging.""" | |
| def test_extract_tessera_guarantees_with_all_fields(self) -> None: | |
| """Test extracting all guarantee types from x-tessera extension.""" | |
| operation = { | |
| "x-tessera": { | |
| "freshness": {"max_staleness_minutes": 60}, | |
| "volume": {"max_requests_per_minute": 1000}, | |
| "nullability": {"user_id": "never"}, | |
| "accepted_values": {"status": ["active", "inactive"]}, | |
| "custom": [{"type": "latency_p99_ms", "value": 200}], | |
| } | |
| } | |
| guarantees = _extract_tessera_guarantees(operation) | |
| assert guarantees is not None | |
| assert guarantees["freshness"] == {"max_staleness_minutes": 60} | |
| assert guarantees["volume"] == {"max_requests_per_minute": 1000} | |
| assert guarantees["nullability"] == {"user_id": "never"} | |
| assert guarantees["accepted_values"] == {"status": ["active", "inactive"]} | |
| assert guarantees["custom"] == [{"type": "latency_p99_ms", "value": 200}] | |
| def test_extract_tessera_guarantees_partial(self) -> None: | |
| """Test extracting only some guarantee types.""" | |
| operation = { | |
| "x-tessera": { | |
| "freshness": {"max_staleness_minutes": 30}, | |
| } | |
| } | |
| guarantees = _extract_tessera_guarantees(operation) | |
| assert guarantees is not None | |
| assert guarantees == {"freshness": {"max_staleness_minutes": 30}} | |
| def test_extract_tessera_guarantees_none(self) -> None: | |
| """Test that no x-tessera returns None.""" | |
| operation = {"operationId": "test"} | |
| guarantees = _extract_tessera_guarantees(operation) | |
| assert guarantees is None | |
| def test_extract_tessera_guarantees_empty(self) -> None: | |
| """Test that empty x-tessera returns None.""" | |
| operation = {"x-tessera": {}} | |
| guarantees = _extract_tessera_guarantees(operation) | |
| assert guarantees is None | |
| def test_infer_nullability_from_required_fields(self) -> None: | |
| """Test inferring nullability from required fields.""" | |
| schema = { | |
| "type": "object", | |
| "properties": { | |
| "id": {"type": "integer"}, | |
| "name": {"type": "string"}, | |
| "email": {"type": "string"}, | |
| }, | |
| "required": ["id", "name"], | |
| } | |
| nullability = _infer_nullability_from_schema(schema) | |
| assert nullability == {"id": "never", "name": "never"} | |
| def test_infer_nullability_respects_nullable_true(self) -> None: | |
| """Test that nullable: true prevents 'never' inference.""" | |
| schema = { | |
| "type": "object", | |
| "properties": { | |
| "id": {"type": "integer"}, | |
| "name": {"type": "string", "nullable": True}, | |
| }, | |
| "required": ["id", "name"], | |
| } | |
| nullability = _infer_nullability_from_schema(schema) | |
| # 'name' is required but nullable, so only 'id' is never-null | |
| assert nullability == {"id": "never"} | |
| def test_infer_nullability_from_nullable_false(self) -> None: | |
| """Test that nullable: false implies 'never' even without required.""" | |
| schema = { | |
| "type": "object", | |
| "properties": { | |
| "id": {"type": "integer", "nullable": False}, | |
| "name": {"type": "string"}, | |
| }, | |
| } | |
| nullability = _infer_nullability_from_schema(schema) | |
| assert nullability == {"id": "never"} | |
| def test_infer_nullability_empty_schema(self) -> None: | |
| """Test inferring nullability from empty schema.""" | |
| assert _infer_nullability_from_schema({}) == {} | |
| assert _infer_nullability_from_schema(None) == {} | |
| def test_merge_guarantees_basic(self) -> None: | |
| """Test basic guarantee merging.""" | |
| default = {"freshness": {"max_staleness_minutes": 60}} | |
| operation = {"nullability": {"user_id": "never"}} | |
| merged = _merge_guarantees(default, operation) | |
| assert merged == { | |
| "freshness": {"max_staleness_minutes": 60}, | |
| "nullability": {"user_id": "never"}, | |
| } | |
| def test_merge_guarantees_override(self) -> None: | |
| """Test that later guarantees override earlier ones.""" | |
| default = {"freshness": {"max_staleness_minutes": 60}} | |
| operation = {"freshness": {"max_staleness_minutes": 30}} | |
| merged = _merge_guarantees(default, operation) | |
| assert merged == {"freshness": {"max_staleness_minutes": 30}} | |
| def test_merge_guarantees_nullability_deep_merge(self) -> None: | |
| """Test that nullability dicts are deep-merged.""" | |
| default = {"nullability": {"id": "never"}} | |
| operation = {"nullability": {"name": "never"}} | |
| merged = _merge_guarantees(default, operation) | |
| assert merged == {"nullability": {"id": "never", "name": "never"}} | |
| def test_merge_guarantees_custom_appends(self) -> None: | |
| """Test that custom guarantees are appended.""" | |
| default = {"custom": [{"type": "a", "value": 1}]} | |
| operation = {"custom": [{"type": "b", "value": 2}]} | |
| merged = _merge_guarantees(default, operation) | |
| assert merged == {"custom": [{"type": "a", "value": 1}, {"type": "b", "value": 2}]} | |
| def test_merge_guarantees_none_inputs(self) -> None: | |
| """Test merging with None inputs.""" | |
| assert _merge_guarantees(None, None) is None | |
| assert _merge_guarantees({"a": 1}, None) == {"a": 1} | |
| assert _merge_guarantees(None, {"b": 2}) == {"b": 2} | |
| def test_parse_openapi_extracts_guarantees(self) -> None: | |
| """Test that parse_openapi extracts guarantees from operations.""" | |
| spec = { | |
| "openapi": "3.0.0", | |
| "info": {"title": "Test API", "version": "1.0.0"}, | |
| "paths": { | |
| "/users": { | |
| "get": { | |
| "x-tessera": { | |
| "freshness": {"max_staleness_minutes": 60}, | |
| }, | |
| "responses": { | |
| "200": { | |
| "content": { | |
| "application/json": { | |
| "schema": { | |
| "type": "object", | |
| "properties": {"id": {"type": "integer"}}, | |
| "required": ["id"], | |
| } | |
| } | |
| } | |
| } | |
| }, | |
| } | |
| } | |
| }, | |
| } | |
| result = parse_openapi(spec) | |
| assert len(result.endpoints) == 1 | |
| endpoint = result.endpoints[0] | |
| assert endpoint.guarantees is not None | |
| # Should have merged: explicit freshness + inferred nullability | |
| assert "freshness" in endpoint.guarantees | |
| assert endpoint.guarantees["freshness"] == {"max_staleness_minutes": 60} | |
| assert "nullability" in endpoint.guarantees | |
| assert endpoint.guarantees["nullability"] == {"id": "never"} | |
| def test_endpoints_to_assets_includes_guarantees(self) -> None: | |
| """Test that endpoints_to_assets includes guarantees in asset.""" | |
| spec = { | |
| "openapi": "3.0.0", | |
| "info": {"title": "Test API", "version": "1.0.0"}, | |
| "paths": { | |
| "/users": { | |
| "get": { | |
| "x-tessera": {"freshness": {"max_staleness_minutes": 60}}, | |
| "responses": {"200": {"description": "OK"}}, | |
| } | |
| } | |
| }, | |
| } | |
| result = parse_openapi(spec) | |
| assets = endpoints_to_assets(result, uuid4(), "production") | |
| assert len(assets) == 1 | |
| assert assets[0].guarantees is not None | |
| assert assets[0].guarantees["freshness"] == {"max_staleness_minutes": 60} | |
Xet Storage Details
- Size:
- 28.4 kB
- Xet hash:
- c6b223c6896c5e7f5948fbe2fe74acdc60bd7d9f84be5830b4ddc378d78171e9
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.