UNPKG

@wearesage/schema

Version:

A flexible schema definition and validation system for TypeScript with multi-database support

142 lines (116 loc) 4.39 kB
import "reflect-metadata"; import { UniversalEntityService, EntityContext } from "../../services/UniversalEntityService"; import { Entity, Property, Id, Auth } from "../../core/decorators"; // Base entity with @Id @Entity() @Auth({ permissions: ['user'] }) class BaseEntity { @Id() id!: string; @Property({ required: true }) name!: string; @Property() createdAt!: Date; @Property() updatedAt!: Date; } // Extended entity that should inherit the @Id decorator @Entity() @Auth({ permissions: ['user'] }) class ExtendedEntity extends BaseEntity { @Property({ required: true }) description!: string; @Property() status!: string; } describe("UniversalEntityService Inheritance", () => { let service: UniversalEntityService; let userContext: EntityContext; beforeEach(() => { service = UniversalEntityService.getInstance(); service.registerEntities([BaseEntity, ExtendedEntity]); userContext = { user: { id: 'user123', username: 'testuser', email: 'test@example.com', role: 0, permissions: ['user'] } }; }); afterAll(async () => { if ((service as any).adapter && (service as any).adapter.close) { await (service as any).adapter.close(); } }); describe("Base Entity", () => { test("should create base entity successfully", async () => { const baseData = { name: 'Base Entity Test' }; const entity = await service.create(BaseEntity, baseData, userContext); expect(entity).toBeDefined(); expect(entity.id).toBeDefined(); expect(entity.name).toBe('Base Entity Test'); expect(entity.createdAt).toBeInstanceOf(Date); expect(entity.updatedAt).toBeInstanceOf(Date); }); }); describe("Extended Entity with Inheritance", () => { test("should create extended entity successfully", async () => { const extendedData = { name: 'Extended Entity Test', description: 'Test description', status: 'active' }; const entity = await service.create(ExtendedEntity, extendedData, userContext); expect(entity).toBeDefined(); expect(entity.id).toBeDefined(); // Should inherit @Id from BaseEntity expect(entity.name).toBe('Extended Entity Test'); // Should inherit name field expect(entity.description).toBe('Test description'); // Own property expect(entity.status).toBe('active'); // Own property expect(entity.createdAt).toBeInstanceOf(Date); // Should inherit from BaseEntity expect(entity.updatedAt).toBeInstanceOf(Date); // Should inherit from BaseEntity }); test("should find extended entity by ID", async () => { const extendedData = { name: 'Extended Entity Test', description: 'Test description' }; const createdEntity = await service.create(ExtendedEntity, extendedData, userContext); const foundEntity = await service.findById(ExtendedEntity, createdEntity.id, userContext); expect(foundEntity).toBeDefined(); expect(foundEntity?.id).toBe(createdEntity.id); expect(foundEntity?.name).toBe('Extended Entity Test'); expect(foundEntity?.description).toBe('Test description'); }); test("should enforce required fields from both base and extended classes", async () => { // Missing required 'name' field from base class const invalidData1 = { description: 'Test description' }; await expect( service.create(ExtendedEntity, invalidData1, userContext) ).rejects.toThrow("Required field 'name' is missing"); // Missing required 'description' field from extended class const invalidData2 = { name: 'Test Name' }; await expect( service.create(ExtendedEntity, invalidData2, userContext) ).rejects.toThrow("Required field 'description' is missing"); }); }); describe("Instance Types", () => { test("should create proper instance types for inherited entities", async () => { const extendedData = { name: 'Extended Entity Test', description: 'Test description' }; const entity = await service.create(ExtendedEntity, extendedData, userContext); expect(entity).toBeInstanceOf(ExtendedEntity); expect(entity.constructor.name).toBe('ExtendedEntity'); }); }); });