UNPKG

@kenniy/godeye-data-contracts

Version:

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

319 lines (318 loc) 9.18 kB
"use strict"; /** * Entity Test Data Factories * Provides consistent test data generation for domain entities */ Object.defineProperty(exports, "__esModule", { value: true }); exports.EntityScenarios = exports.UserBuilder = exports.EntityFactory = void 0; /** * Entity Factory - Creates consistent test entities with sensible defaults */ class EntityFactory { static generateId() { return `test_${this.idCounter++}`; } static createUser(overrides = {}) { const id = this.generateId(); const now = new Date(); return { _id: id, id: id, name: 'Test User', firstName: 'Test', lastName: 'User', email: 'test.user@example.com', status: 'active', userType: 'individual', verified: true, phone: '+1234567890', createdAt: now, updatedAt: now, ...overrides, }; } static createBusinessUser(overrides = {}) { return this.createUser({ name: 'Business User', firstName: 'Business', lastName: 'Owner', email: 'business@example.com', userType: 'business', ...overrides, }); } static createAdminUser(overrides = {}) { return this.createUser({ name: 'Admin User', firstName: 'Admin', lastName: 'Super', email: 'admin@example.com', userType: 'admin', ...overrides, }); } static createProfile(userId, overrides = {}) { const id = this.generateId(); return { _id: id, id: id, bio: 'Test profile bio', userId: userId || this.generateId(), profileKind: 'patient', ...overrides, }; } static createDoctorProfile(userId, overrides = {}) { return this.createProfile(userId, { bio: 'Experienced doctor specializing in general medicine', profileKind: 'doctor', ...overrides, }); } static createBusiness(ownerId, overrides = {}) { const id = this.generateId(); return { _id: id, id: id, name: 'Test Business Corp', type: 'healthcare', ownerId: ownerId || this.generateId(), ...overrides, }; } static createPost(authorId, overrides = {}) { const id = this.generateId(); return { _id: id, id: id, title: 'Test Post Title', content: 'This is a test post content.', authorId: authorId || this.generateId(), ...overrides, }; } static createComment(authorId, overrides = {}) { const id = this.generateId(); return { _id: id, id: id, content: 'This is a test comment.', authorId: authorId || this.generateId(), ...overrides, }; } static createFile(userId, overrides = {}) { const id = this.generateId(); const now = new Date(); return { _id: id, id: id, name: `test-file-${id}.jpg`, originalName: 'test-image.jpg', mimeType: 'image/jpeg', size: 1024000, // 1MB fileType: 'image', userId: userId || this.generateId(), tags: ['test', 'image'], createdAt: now, updatedAt: now, ...overrides, }; } static createFolder(userId, overrides = {}) { const id = this.generateId(); return { _id: id, id: id, name: 'Test Folder', userId: userId || this.generateId(), ...overrides, }; } /** * Creates entities with relationships pre-populated */ static createUserWithRelations(overrides = {}) { const user = this.createUser(overrides); const profile = this.createProfile(user._id); const business = this.createBusiness(user._id); const posts = [ this.createPost(user._id, { title: 'First Post' }), this.createPost(user._id, { title: 'Second Post' }), ]; return { ...user, profile, business, posts, }; } /** * Creates arrays of entities for list/pagination scenarios */ static createUsers(count, overrides = {}) { return Array.from({ length: count }, (_, index) => this.createUser({ name: `Test User ${index + 1}`, email: `user${index + 1}@example.com`, ...overrides, })); } static createFiles(count, userId, overrides = {}) { return Array.from({ length: count }, (_, index) => this.createFile(userId, { name: `test-file-${index + 1}.jpg`, originalName: `file-${index + 1}.jpg`, ...overrides, })); } /** * Creates entities for specific test scenarios */ static createInactiveUser(overrides = {}) { return this.createUser({ status: 'inactive', verified: false, ...overrides, }); } static createDeletedUser(overrides = {}) { return this.createUser({ status: 'deleted', ...overrides, }); } static createLargeFile(userId, overrides = {}) { return this.createFile(userId, { size: 50 * 1024 * 1024, // 50MB fileType: 'video', mimeType: 'video/mp4', name: 'large-video.mp4', originalName: 'large-video.mp4', ...overrides, }); } /** * Reset the ID counter for consistent test runs */ static resetIdCounter() { this.idCounter = 1000; } } exports.EntityFactory = EntityFactory; EntityFactory.idCounter = 1000; /** * Builder Pattern for Complex Entity Creation */ class UserBuilder { constructor() { this.user = {}; } static create() { return new UserBuilder(); } withBasicInfo(name, email) { this.user.name = name; this.user.email = email; const nameParts = name.split(' '); this.user.firstName = nameParts[0] || name; this.user.lastName = nameParts.slice(1).join(' ') || 'User'; return this; } withStatus(status) { this.user.status = status; return this; } withType(userType) { this.user.userType = userType; return this; } withVerification(verified) { this.user.verified = verified; return this; } withPhone(phone) { this.user.phone = phone; return this; } withProfile(profile) { this.user.profile = profile; return this; } withBusiness(business) { this.user.business = business; return this; } withPosts(posts) { this.user.posts = posts; return this; } build() { return EntityFactory.createUser(this.user); } } exports.UserBuilder = UserBuilder; /** * Scenario-based entity creation */ class EntityScenarios { /** * Doctor with complete profile and business */ static doctorWithClinic() { const doctor = EntityFactory.createUser({ userType: 'business', name: 'Dr. John Smith', firstName: 'Dr. John', lastName: 'Smith', email: 'dr.smith@clinic.com', }); const profile = EntityFactory.createDoctorProfile(doctor._id, { bio: 'Experienced cardiologist with 15 years of practice', }); const clinic = EntityFactory.createBusiness(doctor._id, { name: 'Smith Cardiology Clinic', type: 'medical_practice', }); return { ...doctor, profile, business: clinic, }; } /** * Patient with medical files */ static patientWithFiles() { const patient = EntityFactory.createUser({ userType: 'individual', name: 'Jane Doe', email: 'jane.doe@email.com', }); const profile = EntityFactory.createProfile(patient._id, { bio: 'Patient seeking healthcare services', profileKind: 'patient', }); const medicalFiles = EntityFactory.createFiles(3, patient._id, { fileType: 'document', mimeType: 'application/pdf', tags: ['medical', 'records'], }); return { ...patient, profile, files: medicalFiles, }; } /** * Admin user with system access */ static systemAdmin() { return EntityFactory.createAdminUser({ name: 'System Administrator', firstName: 'System', lastName: 'Administrator', email: 'admin@system.com', verified: true, }); } } exports.EntityScenarios = EntityScenarios;