|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright Red Hat |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import json |
| 17 | +import os |
| 18 | + |
| 19 | +import yaml |
| 20 | +from django.test import TestCase |
| 21 | + |
| 22 | +_current_dir_path = os.path.dirname(__file__) |
| 23 | + |
| 24 | +X_AI_DESCRIPTION = "x-ai-description" |
| 25 | +X_AI_DESCRIPTION_MIN_LENGTH = 0 |
| 26 | +X_AI_DESCRIPTION_MAX_LENGTH = 300 |
| 27 | + |
| 28 | + |
| 29 | +class XAIDescription(TestCase): |
| 30 | + openapi_schema_path = os.path.join( |
| 31 | + _current_dir_path, "..", "..", "..", "tools", "openapi-schema" |
| 32 | + ) |
| 33 | + |
| 34 | + def setUp(self): |
| 35 | + self.openapi_schema_path_yaml = os.path.join( |
| 36 | + self.openapi_schema_path, "ansible-ai-connect-service.yaml" |
| 37 | + ) |
| 38 | + self.openapi_schema_path_json = os.path.join( |
| 39 | + self.openapi_schema_path, "ansible-ai-connect-service.json" |
| 40 | + ) |
| 41 | + |
| 42 | + def assert_schema_x_ai_description(self, schema): |
| 43 | + schema_paths = schema.get("paths", None) |
| 44 | + self.assertIsNotNone(schema_paths) |
| 45 | + self.assertTrue(len(schema_paths) > 0) |
| 46 | + |
| 47 | + operations_count = 0 |
| 48 | + for path, operations in schema_paths.items(): |
| 49 | + for method, operation in operations.items(): |
| 50 | + if isinstance(operation, dict): |
| 51 | + self.assertIn(X_AI_DESCRIPTION, operation) |
| 52 | + x_ai_description = operation.get(X_AI_DESCRIPTION) |
| 53 | + self.assertIsInstance(x_ai_description, str) |
| 54 | + self.assertTrue( |
| 55 | + X_AI_DESCRIPTION_MIN_LENGTH |
| 56 | + < len(x_ai_description) |
| 57 | + < X_AI_DESCRIPTION_MAX_LENGTH, |
| 58 | + f"wrong string length ({len(x_ai_description)}) for: '{x_ai_description}', " |
| 59 | + f"minimum allowed: {X_AI_DESCRIPTION_MIN_LENGTH}, " |
| 60 | + f"maximum allowed: {X_AI_DESCRIPTION_MAX_LENGTH} ", |
| 61 | + ) |
| 62 | + operations_count += 1 |
| 63 | + |
| 64 | + assert operations_count > 0 |
| 65 | + |
| 66 | + def test_openapi_schema_yaml(self): |
| 67 | + self.assertTrue(os.path.exists(self.openapi_schema_path_yaml)) |
| 68 | + with open(os.path.join(self.openapi_schema_path_yaml)) as f: |
| 69 | + schema = yaml.safe_load(f) |
| 70 | + self.assert_schema_x_ai_description(schema) |
| 71 | + |
| 72 | + def test_openapi_schema_json(self): |
| 73 | + self.assertTrue(os.path.exists(self.openapi_schema_path_json)) |
| 74 | + with open(os.path.join(self.openapi_schema_path_json)) as f: |
| 75 | + schema = json.load(f) |
| 76 | + self.assert_schema_x_ai_description(schema) |
0 commit comments