@wearesage/schema
Version:
A flexible schema definition and validation system for TypeScript with multi-database support
207 lines (167 loc) • 7.3 kB
text/typescript
import { MetadataRegistry } from "../../core/MetadataRegistry";
import "reflect-metadata";
describe("MetadataRegistry", () => {
let registry: MetadataRegistry;
beforeEach(() => {
registry = new MetadataRegistry();
});
test("should register and retrieve entity metadata", () => {
// Define a mock entity class
class TestEntity {}
// Register entity with options
const entityOptions = { name: "test_entity", description: "A test entity" };
registry.registerEntity(TestEntity, entityOptions);
// Retrieve entity metadata
const retrievedOptions = registry.getEntityMetadata(TestEntity);
// Verify registration worked
expect(retrievedOptions).toBeDefined();
expect(retrievedOptions).toEqual(entityOptions);
});
test("should register and retrieve property metadata", () => {
// Define a mock entity class
class TestEntity {}
// Register property with options
const propertyOptions = { required: true, unique: true };
registry.registerProperty(TestEntity, "testProperty", propertyOptions);
// Retrieve property metadata
const retrievedOptions = registry.getPropertyMetadata(TestEntity, "testProperty");
// Verify registration worked
expect(retrievedOptions).toBeDefined();
expect(retrievedOptions).toEqual(propertyOptions);
});
test("should register and retrieve ID properties", () => {
// Define a mock entity class
class TestEntity {}
// Register ID property
registry.registerIdProperty(TestEntity, "id");
// Retrieve ID properties
const idProperties = registry.getIdProperties(TestEntity);
// Verify registration worked
expect(idProperties).toBeDefined();
expect(idProperties?.has("id")).toBe(true);
});
test("should register and retrieve relationship metadata", () => {
// Define mock entity classes
class TestEntity {}
class RelatedEntity {}
// Register relationship with options
const relationshipOptions = {
name: "TEST_REL",
target: RelatedEntity,
inverse: "testEntities",
cardinality: "many" as const
};
registry.registerRelationship(TestEntity, "relatedEntities", relationshipOptions);
// Retrieve relationship metadata
const retrievedOptions = registry.getRelationshipMetadata(TestEntity, "relatedEntities");
// Verify registration worked
expect(retrievedOptions).toBeDefined();
expect(retrievedOptions).toEqual(relationshipOptions);
});
test("should get all properties for an entity", () => {
// Define a mock entity class
class TestEntity {}
// Register multiple properties
registry.registerProperty(TestEntity, "prop1", { required: true });
registry.registerProperty(TestEntity, "prop2", { unique: true });
registry.registerProperty(TestEntity, "prop3", { default: "test" });
// Get all properties
const allProperties = registry.getAllProperties(TestEntity);
// Verify we got all properties
expect(allProperties).toBeDefined();
expect(allProperties?.size).toBe(3);
expect(allProperties?.get("prop1")).toEqual({ required: true });
expect(allProperties?.get("prop2")).toEqual({ unique: true });
expect(allProperties?.get("prop3")).toEqual({ default: "test" });
});
test("should get all relationships for an entity", () => {
// Define mock entity classes
class TestEntity {}
class RelatedEntity1 {}
class RelatedEntity2 {}
// Register multiple relationships
registry.registerRelationship(TestEntity, "rel1", {
name: "REL_ONE",
target: RelatedEntity1,
cardinality: "one" as const
});
registry.registerRelationship(TestEntity, "rel2", {
name: "REL_TWO",
target: RelatedEntity2,
cardinality: "many" as const
});
// Get all relationships
const allRelationships = registry.getAllRelationships(TestEntity);
// Verify we got all relationships
expect(allRelationships).toBeDefined();
expect(allRelationships?.size).toBe(2);
expect(allRelationships?.get("rel1")?.name).toBe("REL_ONE");
expect(allRelationships?.get("rel2")?.name).toBe("REL_TWO");
});
test("should get all entities", () => {
// Define mock entity classes
class TestEntity1 {}
class TestEntity2 {}
class TestEntity3 {}
// Register multiple entities
registry.registerEntity(TestEntity1, { name: "entity1" });
registry.registerEntity(TestEntity2, { name: "entity2" });
registry.registerEntity(TestEntity3, { name: "entity3" });
// Get all entities
const allEntities = registry.getAllEntities();
// Verify we got all entities
expect(allEntities.size).toBe(3);
expect(allEntities.get(TestEntity1)).toEqual({ name: "entity1" });
expect(allEntities.get(TestEntity2)).toEqual({ name: "entity2" });
expect(allEntities.get(TestEntity3)).toEqual({ name: "entity3" });
});
test("should clone registry with all metadata", () => {
// Define mock entity classes
class TestEntity {}
class RelatedEntity {}
// Set up original registry with various metadata
registry.registerEntity(TestEntity, { name: "test_entity" });
registry.registerProperty(TestEntity, "prop", { required: true });
registry.registerIdProperty(TestEntity, "id");
registry.registerRelationship(TestEntity, "related", {
name: "RELATES_TO",
target: RelatedEntity,
cardinality: "many" as const
});
// Clone the registry
const clonedRegistry = registry.clone();
// Verify all metadata was cloned correctly
expect(clonedRegistry.getEntityMetadata(TestEntity)).toEqual({ name: "test_entity" });
expect(clonedRegistry.getPropertyMetadata(TestEntity, "prop")).toEqual({ required: true });
expect(clonedRegistry.getIdProperties(TestEntity)?.has("id")).toBe(true);
expect(clonedRegistry.getRelationshipMetadata(TestEntity, "related")?.name).toBe("RELATES_TO");
// Verify that modifying the clone doesn't affect the original
clonedRegistry.registerProperty(TestEntity, "newProp", { unique: true });
expect(clonedRegistry.getPropertyMetadata(TestEntity, "newProp")).toBeDefined();
expect(registry.getPropertyMetadata(TestEntity, "newProp")).toBeUndefined();
});
test("should clear all metadata", () => {
// Define mock entity classes
class TestEntity {}
class RelatedEntity {}
// Set up registry with various metadata
registry.registerEntity(TestEntity, { name: "test_entity" });
registry.registerProperty(TestEntity, "prop", { required: true });
registry.registerIdProperty(TestEntity, "id");
registry.registerRelationship(TestEntity, "related", {
name: "RELATES_TO",
target: RelatedEntity,
cardinality: "many" as const
});
// Verify registry has data
expect(registry.getAllEntities().size).toBe(1);
// Clear registry
registry.clear();
// Verify all metadata was cleared
expect(registry.getAllEntities().size).toBe(0);
expect(registry.getEntityMetadata(TestEntity)).toBeUndefined();
expect(registry.getPropertyMetadata(TestEntity, "prop")).toBeUndefined();
expect(registry.getIdProperties(TestEntity)).toBeUndefined();
expect(registry.getRelationshipMetadata(TestEntity, "related")).toBeUndefined();
});
});