UNPKG

@kenniy/godeye-data-contracts

Version:

Enterprise-grade base repository architecture for GOD-EYE microservices with zero overhead and maximum code reuse

203 lines (202 loc) 9.14 kB
"use strict"; /** * Enhanced Aggregation Repository Tests - Simple Version * * Tests basic functionality of enhanced aggregation without complex mocking */ Object.defineProperty(exports, "__esModule", { value: true }); const enhanced_mongoose_aggregate_repository_1 = require("../repositories/enhanced-mongoose-aggregate.repository"); const mongoose_aggregate_repository_1 = require("../repositories/mongoose-aggregate.repository"); const enhanced_aggregation_utils_1 = require("../utils/enhanced-aggregation.utils"); describe("Enhanced Aggregation Repository", () => { describe("Class Structure", () => { it("should extend MongooseAggregateRepository", () => { // Test that inheritance is correct - check prototype chain expect(enhanced_mongoose_aggregate_repository_1.EnhancedMongooseRepository.prototype instanceof mongoose_aggregate_repository_1.MongooseAggregateRepository).toBe(true); // Verify the class can be instantiated (abstract class test) class TestRepo extends enhanced_mongoose_aggregate_repository_1.EnhancedMongooseRepository { constructor() { super({ collection: { name: 'test' }, modelName: 'TestModel' }); } } const testInstance = new TestRepo(); expect(testInstance instanceof enhanced_mongoose_aggregate_repository_1.EnhancedMongooseRepository).toBe(true); expect(testInstance instanceof mongoose_aggregate_repository_1.MongooseAggregateRepository).toBe(true); }); }); describe("Configuration Interfaces", () => { it("should accept enhanced query configuration", () => { const config = { conditions: { status: "ACTIVE" }, strategy: enhanced_aggregation_utils_1.AggregationStrategy.MEMORY_OPTIMIZED, optimization: enhanced_aggregation_utils_1.QueryOptimizationLevel.ADVANCED, enableCaching: true, cacheTTL: 300, maxMemoryMB: 512, enableParallel: false, timeoutMs: 30000, enableMetrics: true, indexHints: ["idx_status"], chunkSize: 1000, }; expect(config.strategy).toBe(enhanced_aggregation_utils_1.AggregationStrategy.MEMORY_OPTIMIZED); expect(config.optimization).toBe(enhanced_aggregation_utils_1.QueryOptimizationLevel.ADVANCED); expect(config.enableCaching).toBe(true); }); it("should handle aggregation strategies enum", () => { expect(enhanced_aggregation_utils_1.AggregationStrategy.MEMORY_OPTIMIZED).toBe("memory_optimized"); expect(enhanced_aggregation_utils_1.AggregationStrategy.HYBRID).toBe("hybrid"); expect(enhanced_aggregation_utils_1.AggregationStrategy.STREAMING).toBe("streaming"); expect(enhanced_aggregation_utils_1.AggregationStrategy.DISTRIBUTED).toBe("distributed"); }); it("should handle optimization levels enum", () => { expect(enhanced_aggregation_utils_1.QueryOptimizationLevel.BASIC).toBe("basic"); expect(enhanced_aggregation_utils_1.QueryOptimizationLevel.ADVANCED).toBe("advanced"); expect(enhanced_aggregation_utils_1.QueryOptimizationLevel.ENTERPRISE).toBe("enterprise"); }); }); describe("Backward Compatibility", () => { it("should maintain all original methods from base class", () => { class TestRepo extends enhanced_mongoose_aggregate_repository_1.EnhancedMongooseRepository { constructor() { super({ aggregate: jest.fn(), collection: { name: "test" }, modelName: "TestModel" }); } } const repo = new TestRepo(); // Verify original methods exist expect(typeof repo.aggregate).toBe("function"); expect(typeof repo.aggregateWithPagination).toBe("function"); expect(typeof repo.complexQuery).toBe("function"); // Verify new enhanced method exists expect(typeof repo.enhancedComplexQuery).toBe("function"); }); }); describe("Method Visibility", () => { it("should expose protected methods for testing", () => { class TestRepo extends enhanced_mongoose_aggregate_repository_1.EnhancedMongooseRepository { constructor() { super({ aggregate: jest.fn(), collection: { name: "test" }, modelName: "TestModel" }); } // These should be accessible since they're protected testBuildOptimizedPipeline(config, strategy) { return this.buildOptimizedPipeline(config, strategy); } testBuildFilterStages(config) { return this.buildFilterStages(config); } testFormatEnhancedResult(items, total, pagination) { return this.formatEnhancedResult(items, total, pagination); } } const repo = new TestRepo(); // Test filter stages const filterStages = repo.testBuildFilterStages({ conditions: { status: "ACTIVE", type: "USER" }, }); expect(Array.isArray(filterStages)).toBe(true); expect(filterStages.length).toBeGreaterThan(0); expect(filterStages[0]).toHaveProperty("$match"); expect(filterStages[0].$match).toEqual({ status: "ACTIVE", type: "USER", }); }); }); describe("Pipeline Building", () => { it("should build filter stages correctly", () => { class TestRepo extends enhanced_mongoose_aggregate_repository_1.EnhancedMongooseRepository { constructor() { super({ aggregate: jest.fn(), collection: { name: "test" }, modelName: "TestModel" }); } buildFilterStagesPublic(config) { return this.buildFilterStages(config); } } const repo = new TestRepo(); // Test simple conditions const stages = repo.buildFilterStagesPublic({ conditions: { status: "ACTIVE", age: { $gte: 18, $lt: 65 }, tags: ["premium", "verified"], }, }); expect(stages).toEqual([ { $match: { status: "ACTIVE", age: { $gte: 18, $lt: 65 }, tags: ["premium", "verified"], }, }, ]); }); it("should handle empty conditions", () => { class TestRepo extends enhanced_mongoose_aggregate_repository_1.EnhancedMongooseRepository { constructor() { super({ aggregate: jest.fn(), collection: { name: "test" }, modelName: "TestModel" }); } buildFilterStagesPublic(config) { return this.buildFilterStages(config); } } const repo = new TestRepo(); const stages = repo.buildFilterStagesPublic({}); expect(stages).toEqual([]); }); }); describe("Result Formatting", () => { it("should format enhanced results correctly", () => { class TestRepo extends enhanced_mongoose_aggregate_repository_1.EnhancedMongooseRepository { constructor() { super({ aggregate: jest.fn(), collection: { name: "test" }, modelName: "TestModel" }); } formatEnhancedResultPublic(items, total, pagination) { return this.formatEnhancedResult(items, total, pagination); } } const repo = new TestRepo(); const items = [ { id: "1", name: "Item 1" }, { id: "2", name: "Item 2" }, ]; const result = repo.formatEnhancedResultPublic(items, 25, { page: 2, limit: 10, }); expect(result).toEqual({ items, total: 25, page: 2, limit: 10, totalPages: 3, hasNext: true, hasPrev: true, metrics: expect.any(Object), }); }); }); });