|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { BulkImportService } from 'src/modules/bulk-actions/bulk-import.service'; |
| 3 | +import { DatabaseConnectionService } from 'src/modules/database/database-connection.service'; |
| 4 | +import { |
| 5 | + mockClientMetadata, |
| 6 | + mockDatabaseConnectionService, |
| 7 | + mockIORedisClient, |
| 8 | + mockIORedisCluster, MockType |
| 9 | +} from 'src/__mocks__'; |
| 10 | +import { MemoryStoredFile } from 'nestjs-form-data'; |
| 11 | +import { BulkActionSummary } from 'src/modules/bulk-actions/models/bulk-action-summary'; |
| 12 | +import { IBulkActionOverview } from 'src/modules/bulk-actions/interfaces/bulk-action-overview.interface'; |
| 13 | +import { BulkActionStatus, BulkActionType } from 'src/modules/bulk-actions/constants'; |
| 14 | +import { NotFoundException } from '@nestjs/common'; |
| 15 | +import { BulkActionsAnalyticsService } from 'src/modules/bulk-actions/bulk-actions-analytics.service'; |
| 16 | + |
| 17 | +const generateNCommandsBuffer = (n: number) => Buffer.from( |
| 18 | + (new Array(n)).fill(1).map(() => ['set', ['foo', 'bar']]).join('\n'), |
| 19 | +); |
| 20 | +const generateNBatchCommands = (n: number) => (new Array(n)).fill(1).map(() => ['set', ['foo', 'bar']]); |
| 21 | +const generateNBatchCommandsResults = (n: number) => (new Array(n)).fill(1).map(() => [null, 'OK']); |
| 22 | +const mockBatchCommands = generateNBatchCommands(100); |
| 23 | +const mockBatchCommandsResult = generateNBatchCommandsResults(100); |
| 24 | +const mockBatchCommandsResultWithErrors = [...(new Array(99)).fill(1).map(() => [null, 'OK']), ['ReplyError']]; |
| 25 | +const mockSummary: BulkActionSummary = Object.assign(new BulkActionSummary(), { |
| 26 | + processed: 100, |
| 27 | + succeed: 100, |
| 28 | + failed: 0, |
| 29 | + errors: [], |
| 30 | +}); |
| 31 | + |
| 32 | +const mockSummaryWithErrors = Object.assign(new BulkActionSummary(), { |
| 33 | + processed: 100, |
| 34 | + succeed: 99, |
| 35 | + failed: 1, |
| 36 | + errors: [], |
| 37 | +}); |
| 38 | + |
| 39 | +const mockImportResult: IBulkActionOverview = { |
| 40 | + id: 'empty', |
| 41 | + databaseId: mockClientMetadata.databaseId, |
| 42 | + type: BulkActionType.Import, |
| 43 | + summary: mockSummary.getOverview(), |
| 44 | + progress: null, |
| 45 | + filter: null, |
| 46 | + status: BulkActionStatus.Completed, |
| 47 | + duration: 100, |
| 48 | +}; |
| 49 | + |
| 50 | +const mockUploadImportFileDto = { |
| 51 | + file: { |
| 52 | + originalname: 'filename', |
| 53 | + size: 1, |
| 54 | + buffer: Buffer.from('SET foo bar'), |
| 55 | + } as unknown as MemoryStoredFile, |
| 56 | +}; |
| 57 | + |
| 58 | +describe('BulkImportService', () => { |
| 59 | + let service: BulkImportService; |
| 60 | + let databaseConnectionService: MockType<DatabaseConnectionService>; |
| 61 | + let analytics: MockType<BulkActionsAnalyticsService>; |
| 62 | + |
| 63 | + beforeEach(async () => { |
| 64 | + jest.clearAllMocks(); |
| 65 | + |
| 66 | + const module: TestingModule = await Test.createTestingModule({ |
| 67 | + providers: [ |
| 68 | + BulkImportService, |
| 69 | + { |
| 70 | + provide: DatabaseConnectionService, |
| 71 | + useFactory: mockDatabaseConnectionService, |
| 72 | + }, |
| 73 | + { |
| 74 | + provide: BulkActionsAnalyticsService, |
| 75 | + useFactory: () => ({ |
| 76 | + sendActionStarted: jest.fn(), |
| 77 | + sendActionStopped: jest.fn(), |
| 78 | + }), |
| 79 | + }, |
| 80 | + ], |
| 81 | + }).compile(); |
| 82 | + |
| 83 | + service = module.get(BulkImportService); |
| 84 | + databaseConnectionService = module.get(DatabaseConnectionService); |
| 85 | + analytics = module.get(BulkActionsAnalyticsService); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('executeBatch', () => { |
| 89 | + it('should execute batch in pipeline for standalone', async () => { |
| 90 | + mockIORedisClient.exec.mockResolvedValueOnce(mockBatchCommandsResult); |
| 91 | + expect(await service['executeBatch'](mockIORedisClient, mockBatchCommands)).toEqual(mockSummary); |
| 92 | + }); |
| 93 | + it('should execute batch in pipeline for standalone with errors', async () => { |
| 94 | + mockIORedisClient.exec.mockResolvedValueOnce(mockBatchCommandsResultWithErrors); |
| 95 | + expect(await service['executeBatch'](mockIORedisClient, mockBatchCommands)).toEqual(mockSummaryWithErrors); |
| 96 | + }); |
| 97 | + it('should return all failed in case of global error', async () => { |
| 98 | + mockIORedisClient.exec.mockRejectedValueOnce(new Error()); |
| 99 | + expect(await service['executeBatch'](mockIORedisClient, mockBatchCommands)).toEqual({ |
| 100 | + ...mockSummary.getOverview(), |
| 101 | + succeed: 0, |
| 102 | + failed: mockSummary.getOverview().processed, |
| 103 | + }); |
| 104 | + }); |
| 105 | + it('should execute batch of commands without pipeline for cluster', async () => { |
| 106 | + mockIORedisCluster.call.mockRejectedValueOnce(new Error()); |
| 107 | + mockIORedisCluster.call.mockResolvedValue('OK'); |
| 108 | + expect(await service['executeBatch'](mockIORedisCluster, mockBatchCommands)).toEqual(mockSummaryWithErrors); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe('import', () => { |
| 113 | + let spy; |
| 114 | + |
| 115 | + beforeEach(() => { |
| 116 | + spy = jest.spyOn(service as any, 'executeBatch'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should import data', async () => { |
| 120 | + spy.mockResolvedValue(mockSummary); |
| 121 | + expect(await service.import(mockClientMetadata, mockUploadImportFileDto)).toEqual({ |
| 122 | + ...mockImportResult, |
| 123 | + duration: jasmine.anything(), |
| 124 | + }); |
| 125 | + expect(analytics.sendActionStopped).toHaveBeenCalledWith({ |
| 126 | + ...mockImportResult, |
| 127 | + duration: jasmine.anything(), |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should import data (100K) from file in batches 10K each', async () => { |
| 132 | + spy.mockResolvedValue(Object.assign(new BulkActionSummary(), { |
| 133 | + processed: 10_000, |
| 134 | + succeed: 10_000, |
| 135 | + failed: 0, |
| 136 | + })); |
| 137 | + expect(await service.import(mockClientMetadata, { |
| 138 | + file: { |
| 139 | + ...mockUploadImportFileDto.file, |
| 140 | + buffer: generateNCommandsBuffer(100_000), |
| 141 | + } as unknown as MemoryStoredFile, |
| 142 | + })).toEqual({ |
| 143 | + ...mockImportResult, |
| 144 | + summary: { |
| 145 | + processed: 100_000, |
| 146 | + succeed: 100_000, |
| 147 | + failed: 0, |
| 148 | + errors: [], |
| 149 | + }, |
| 150 | + duration: jasmine.anything(), |
| 151 | + }); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should import data (10K) from file in batches 10K each', async () => { |
| 155 | + spy.mockResolvedValue(Object.assign(new BulkActionSummary(), { |
| 156 | + processed: 10_000, |
| 157 | + succeed: 10_000, |
| 158 | + failed: 0, |
| 159 | + })); |
| 160 | + expect(await service.import(mockClientMetadata, { |
| 161 | + file: { |
| 162 | + ...mockUploadImportFileDto.file, |
| 163 | + buffer: generateNCommandsBuffer(10_000), |
| 164 | + } as unknown as MemoryStoredFile, |
| 165 | + })).toEqual({ |
| 166 | + ...mockImportResult, |
| 167 | + summary: { |
| 168 | + processed: 10_000, |
| 169 | + succeed: 10_000, |
| 170 | + failed: 0, |
| 171 | + errors: [], |
| 172 | + }, |
| 173 | + duration: jasmine.anything(), |
| 174 | + }); |
| 175 | + }); |
| 176 | + |
| 177 | + it('should not import any data due to parse error', async () => { |
| 178 | + spy.mockResolvedValue(Object.assign(new BulkActionSummary(), { |
| 179 | + processed: 0, |
| 180 | + succeed: 0, |
| 181 | + failed: 0, |
| 182 | + })); |
| 183 | + expect(await service.import(mockClientMetadata, { |
| 184 | + file: { |
| 185 | + ...mockUploadImportFileDto.file, |
| 186 | + buffer: Buffer.from('{"incorrectdata"}\n{"incorrectdata"}'), |
| 187 | + } as unknown as MemoryStoredFile, |
| 188 | + })).toEqual({ |
| 189 | + ...mockImportResult, |
| 190 | + summary: { |
| 191 | + processed: 2, |
| 192 | + succeed: 0, |
| 193 | + failed: 2, |
| 194 | + errors: [], |
| 195 | + }, |
| 196 | + duration: jasmine.anything(), |
| 197 | + }); |
| 198 | + }); |
| 199 | + |
| 200 | + it('should throw an error in case of global error', async () => { |
| 201 | + try { |
| 202 | + databaseConnectionService.createClient.mockRejectedValueOnce(new NotFoundException()); |
| 203 | + |
| 204 | + await service.import(mockClientMetadata, mockUploadImportFileDto); |
| 205 | + |
| 206 | + fail(); |
| 207 | + } catch (e) { |
| 208 | + expect(e).toBeInstanceOf(NotFoundException); |
| 209 | + } |
| 210 | + }); |
| 211 | + }); |
| 212 | +}); |
0 commit comments