UNPKG

@kenniy/godeye-data-contracts

Version:

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

450 lines (449 loc) 11.6 kB
"use strict"; /** * DTO Test Factory * Provides factory methods for creating test DTOs with various configurations */ Object.defineProperty(exports, "__esModule", { value: true }); exports.CriteriaFactory = exports.DtoScenarios = exports.DtoBuilder = exports.DtoFactory = void 0; const dto_1 = require("../../core/dto"); /** * DTO Factory - Creates DTO instances with common test configurations */ class DtoFactory { /** * Creates basic query DTO with default pagination */ static createBaseQuery(overrides = {}) { const dto = new dto_1.BaseQueryDto(); dto.page = 1; dto.limit = 20; dto.status = 'active'; return Object.assign(dto, overrides); } /** * Creates query DTO with search configuration */ static createSearchQuery(searchTerm, overrides = {}) { return this.createBaseQuery({ search: searchTerm, ...overrides, }); } /** * Creates query DTO with sorting */ static createSortedQuery(sortConfig = 'createdAt:DESC', overrides = {}) { return this.createBaseQuery({ sort: sortConfig, ...overrides, }); } /** * Creates query DTO with relations/includes */ static createIncludeQuery(include = 'id,name,email', overrides = {}) { return this.createBaseQuery({ include, ...overrides, }); } /** * Creates FindOne DTO */ static createFindOneDto(overrides = {}) { const dto = new dto_1.FindOneDto(); dto.include = 'id,name,email'; return Object.assign(dto, overrides); } /** * Creates FindMany DTO with pagination */ static createFindManyDto(overrides = {}) { const dto = new dto_1.FindManyDto(); dto.page = 1; dto.limit = 50; dto.includeTotalCount = true; return Object.assign(dto, overrides); } /** * Creates User-specific query DTO */ static createUserQuery(overrides = {}) { const dto = new dto_1.UserQueryDto(); dto.page = 1; dto.limit = 20; dto.userType = 'individual'; dto.status = 'active'; dto.verified = true; return Object.assign(dto, overrides); } /** * Creates business user query DTO */ static createBusinessUserQuery(overrides = {}) { return this.createUserQuery({ userType: 'business', include: 'id,name,email,profile,business', ...overrides, }); } /** * Creates admin user query DTO */ static createAdminUserQuery(overrides = {}) { return this.createUserQuery({ userType: 'admin', include: 'id,name,email,profile,permissions', ...overrides, }); } /** * Creates user query with date filtering */ static createUserQueryWithDateRange(createdAfter, createdBefore, overrides = {}) { return this.createUserQuery({ createdAfter, createdBefore, ...overrides, }); } /** * Creates File query DTO */ static createFileQuery(overrides = {}) { const dto = new dto_1.FileQueryDto(); dto.page = 1; dto.limit = 20; dto.fileType = 'image'; return Object.assign(dto, overrides); } /** * Creates file query for specific user */ static createUserFileQuery(userId, overrides = {}) { return this.createFileQuery({ userId, include: 'id,name,originalName,size,user', ...overrides, }); } /** * Creates file query with size filtering */ static createFileSizeQuery(minSize, maxSize, overrides = {}) { return this.createFileQuery({ minSize, maxSize, ...overrides, }); } /** * Creates file query for specific MIME type */ static createFileTypeQuery(mimeType, fileType, overrides = {}) { return this.createFileQuery({ mimeType, fileType, ...overrides, }); } } exports.DtoFactory = DtoFactory; /** * DTO Builder - Fluent interface for complex DTO construction */ class DtoBuilder { constructor(dtoInstance) { this.dto = dtoInstance; } static forBaseQuery() { return new DtoBuilder(new dto_1.BaseQueryDto()); } static forUserQuery() { return new DtoBuilder(new dto_1.UserQueryDto()); } static forFileQuery() { return new DtoBuilder(new dto_1.FileQueryDto()); } static forFindOne() { return new DtoBuilder(new dto_1.FindOneDto()); } static forFindMany() { return new DtoBuilder(new dto_1.FindManyDto()); } /** * Configure pagination */ withPagination(page, limit) { this.dto.page = page; this.dto.limit = limit; return this; } /** * Configure search */ withSearch(searchTerm) { this.dto.search = searchTerm; return this; } /** * Configure sorting */ withSort(sort) { this.dto.sort = sort; return this; } /** * Configure includes/relations */ withInclude(include) { this.dto.include = include; return this; } /** * Configure status filter */ withStatus(status) { this.dto.status = status; return this; } /** * Configure user-specific properties (if T extends UserQueryDto) */ withUserType(userType) { if ('userType' in this.dto) { this.dto.userType = userType; } return this; } withEmail(email) { if ('email' in this.dto) { this.dto.email = email; } return this; } withVerified(verified) { if ('verified' in this.dto) { this.dto.verified = verified; } return this; } withDateRange(createdAfter, createdBefore) { if ('createdAfter' in this.dto) { this.dto.createdAfter = createdAfter; } if ('createdBefore' in this.dto) { this.dto.createdBefore = createdBefore; } return this; } /** * Configure file-specific properties (if T extends FileQueryDto) */ withFileType(fileType) { if ('fileType' in this.dto) { this.dto.fileType = fileType; } return this; } withMimeType(mimeType) { if ('mimeType' in this.dto) { this.dto.mimeType = mimeType; } return this; } withUserId(userId) { if ('userId' in this.dto) { this.dto.userId = userId; } return this; } withSizeRange(minSize, maxSize) { if ('minSize' in this.dto) { this.dto.minSize = minSize; } if ('maxSize' in this.dto) { this.dto.maxSize = maxSize; } return this; } /** * Build the final DTO */ build() { return this.dto; } /** * Build and convert to ICriteria */ buildCriteria(id) { if ('toICriteria' in this.dto && typeof this.dto.toICriteria === 'function') { return this.dto.toICriteria(id); } throw new Error('DTO does not support toICriteria conversion'); } } exports.DtoBuilder = DtoBuilder; /** * Common DTO Test Scenarios */ class DtoScenarios { /** * Basic pagination scenario */ static basicPagination() { return DtoFactory.createBaseQuery({ page: 2, limit: 10, }); } /** * Search with pagination scenario */ static searchWithPagination(searchTerm = 'test') { return DtoFactory.createSearchQuery(searchTerm, { page: 1, limit: 25, sort: 'relevance:DESC', }); } /** * Complex user query scenario */ static complexUserQuery() { return DtoFactory.createUserQuery({ page: 2, limit: 10, userType: 'business', verified: true, search: 'doctor', include: 'id,name,email,profile,business', sort: 'createdAt:DESC,name:ASC', createdAfter: '2023-01-01', }); } /** * File query for images scenario */ static imageFilesQuery() { return DtoFactory.createFileQuery({ fileType: 'image', mimeType: 'image/jpeg', minSize: 1024, maxSize: 5000000, include: 'id,name,size,user,folder', }); } /** * Admin user query scenario */ static adminUsersQuery() { return DtoFactory.createAdminUserQuery({ verified: true, status: 'active', include: 'id,name,email,profile,permissions', sort: 'lastName:ASC', }); } /** * Minimal query scenario */ static minimalQuery() { return new dto_1.BaseQueryDto(); } /** * Large pagination scenario */ static largePagination() { return DtoFactory.createBaseQuery({ page: 10, limit: 100, }); } /** * Multi-sort scenario */ static multiSort() { return DtoFactory.createSortedQuery('status:ASC,createdAt:DESC,name:ASC'); } /** * Full includes scenario */ static fullIncludes() { return DtoFactory.createUserQuery({ include: 'id,name,email,firstName,lastName,phone,profile,business,posts,files', }); } /** * Date range filtering scenario */ static dateRangeQuery() { return DtoFactory.createUserQueryWithDateRange('2023-01-01', '2023-12-31', { sort: 'createdAt:ASC', }); } } exports.DtoScenarios = DtoScenarios; /** * ICriteria Mock Factory - Creates mock ICriteria objects for testing */ class CriteriaFactory { /** * Creates basic ICriteria with where conditions */ static createBasic(where = {}, overrides = {}) { return { where, page: 1, limit: 20, ...overrides, }; } /** * Creates ICriteria with relations */ static withRelations(relations, where = {}) { return this.createBasic(where, { relations }); } /** * Creates ICriteria with sorting */ static withSort(sort, where = {}) { return this.createBasic(where, { sort }); } /** * Creates ICriteria with field selection */ static withSelect(select, where = {}) { return this.createBasic(where, { select }); } /** * Creates ICriteria with search */ static withSearch(term, where = {}) { return this.createBasic(where, { search: { term } }); } /** * Creates ICriteria with pagination */ static withPagination(page, limit, where = {}) { return this.createBasic(where, { page, limit }); } /** * Creates complete ICriteria with all options */ static createComplete() { return { where: { status: 'active' }, page: 2, limit: 10, relations: ['profile', 'business'], select: ['id', 'name', 'email'], sort: { createdAt: 'DESC', name: 'ASC' }, search: { term: 'test search' }, }; } } exports.CriteriaFactory = CriteriaFactory;