Spaces:
Sleeping
Sleeping
Add test script for schema registry and query/search agent tools
Browse files- test_schema_tools.py +88 -0
test_schema_tools.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Test script for schema registry and query/search agent tools
|
| 2 |
+
|
| 3 |
+
Tests mapping, edge cases, error handling, and rollback/versioning with logs.
|
| 4 |
+
"""
|
| 5 |
+
import unittest
|
| 6 |
+
from schema_registry import SchemaRegistry
|
| 7 |
+
from query_search_agent import QuerySearchAgent
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
def clean_files(registry_path, log_path):
|
| 11 |
+
for p in [registry_path, log_path]:
|
| 12 |
+
if os.path.exists(p):
|
| 13 |
+
os.remove(p)
|
| 14 |
+
|
| 15 |
+
class TestSchemaTools(unittest.TestCase):
|
| 16 |
+
def setUp(self):
|
| 17 |
+
clean_files('schema_registry.json', 'schema_audit.log')
|
| 18 |
+
self.registry = SchemaRegistry('schema_registry.json', 'schema_audit.log')
|
| 19 |
+
self.agent = QuerySearchAgent(self.registry)
|
| 20 |
+
self.user_schema = {
|
| 21 |
+
'id': {'type': 'int', 'required': True},
|
| 22 |
+
'username': {'type': 'string', 'required': True},
|
| 23 |
+
'email': {'type': 'string', 'required': True},
|
| 24 |
+
'age': {'type': 'int', 'required': False}
|
| 25 |
+
}
|
| 26 |
+
self.registry.register_schema('user', self.user_schema, 'User schema for test')
|
| 27 |
+
self.short_user_schema = {
|
| 28 |
+
'username': {'type': 'string', 'required': True},
|
| 29 |
+
'age': {'type': 'int', 'required': False}
|
| 30 |
+
}
|
| 31 |
+
self.registry.register_schema('short_user', self.short_user_schema, 'Short user')
|
| 32 |
+
self.registry.register_mapping('user', 'short_user', {
|
| 33 |
+
'username': 'username',
|
| 34 |
+
'age': 'age'
|
| 35 |
+
})
|
| 36 |
+
|
| 37 |
+
def tearDown(self):
|
| 38 |
+
clean_files('schema_registry.json', 'schema_audit.log')
|
| 39 |
+
|
| 40 |
+
def test_schema_registration_and_mapping(self):
|
| 41 |
+
self.assertIn('user', self.registry.list_schemas())
|
| 42 |
+
self.assertIn('user->short_user', self.registry.list_mappings())
|
| 43 |
+
|
| 44 |
+
def test_validation(self):
|
| 45 |
+
valid_user = {'id': 1, 'username': 'alice', 'email': 'alice@email.com', 'age': 23}
|
| 46 |
+
res, errs = self.registry.validate_data('user', valid_user)
|
| 47 |
+
self.assertTrue(res)
|
| 48 |
+
self.assertEqual(errs, [])
|
| 49 |
+
missing_field = {'username': 'bob', 'email': 'bob@email.com'}
|
| 50 |
+
res, errs = self.registry.validate_data('user', missing_field)
|
| 51 |
+
self.assertFalse(res)
|
| 52 |
+
self.assertTrue(any('Required field' in err for err in errs))
|
| 53 |
+
|
| 54 |
+
def test_transform_data(self):
|
| 55 |
+
user = {'id': 1, 'username': 'alice', 'email': 'alice@email.com', 'age': 23}
|
| 56 |
+
transformed, errs = self.registry.transform_data(user, 'user', 'short_user')
|
| 57 |
+
self.assertIsNotNone(transformed)
|
| 58 |
+
self.assertEqual(set(transformed.keys()), set(['username', 'age']))
|
| 59 |
+
|
| 60 |
+
def test_search_and_query(self):
|
| 61 |
+
users = [
|
| 62 |
+
{'id': 1, 'username': 'alice', 'email': 'alice@email.com', 'age': 23},
|
| 63 |
+
{'id': 2, 'username': 'bob', 'email': 'bob@email.com', 'age': 28}
|
| 64 |
+
]
|
| 65 |
+
results, errors = self.agent.search('user', {'username': 'alice'}, users)
|
| 66 |
+
self.assertEqual(len(results), 1)
|
| 67 |
+
self.assertEqual(results[0]['username'], 'alice')
|
| 68 |
+
|
| 69 |
+
def test_advanced_query_and_errors(self):
|
| 70 |
+
users = [
|
| 71 |
+
{'id': 1, 'username': 'alice', 'email': 'alice@email.com', 'age': 23},
|
| 72 |
+
{'id': 2, 'username': 'bob', 'email': 'bob@email.com'}
|
| 73 |
+
]
|
| 74 |
+
adv_results, adv_errors = self.agent.advanced_query('user', 'short_user', {'username': 'bob'}, users)
|
| 75 |
+
# Should have error because of missing age but it's optional in short_user
|
| 76 |
+
self.assertEqual(len(adv_results), 1)
|
| 77 |
+
self.assertEqual(adv_results[0]['username'], 'bob')
|
| 78 |
+
# Error handling for invalid mapping
|
| 79 |
+
adv_results, adv_errors = self.agent.advanced_query('user', 'short_user', {'username': 'notfound'}, users)
|
| 80 |
+
self.assertEqual(len(adv_results), 0)
|
| 81 |
+
|
| 82 |
+
def test_json_round_trip(self):
|
| 83 |
+
obj = {'id': 42, 'name': 'Test'}
|
| 84 |
+
restored = self.agent.round_trip_json(obj)
|
| 85 |
+
self.assertEqual(obj, restored)
|
| 86 |
+
|
| 87 |
+
if __name__ == '__main__':
|
| 88 |
+
unittest.main()
|