|
| 1 | +// Common Identifier Application |
| 2 | +// Copyright (C) 2024 World Food Programme |
| 3 | + |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | + |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | + |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +import { existsSync } from 'node:fs'; |
| 18 | +import { join, dirname } from 'node:path'; |
| 19 | +import { fileURLToPath } from 'node:url'; |
| 20 | + |
| 21 | +import { preprocessFile, processFile } from '../../src/processing'; |
| 22 | +import { SUPPORTED_VALIDATORS } from '../../src/validation/Validation'; |
| 23 | + |
| 24 | +import type { Config } from '../../src/config/Config'; |
| 25 | + |
| 26 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 27 | + |
| 28 | +const CONFIG: Config.FileConfiguration = { |
| 29 | + meta: { id: '', version: '', signature: '' }, |
| 30 | + source: { |
| 31 | + columns: [ |
| 32 | + { name: 'A', alias: 'col_a' }, |
| 33 | + { name: 'B', alias: 'col_b' }, |
| 34 | + ], |
| 35 | + }, |
| 36 | + algorithm: { |
| 37 | + hash: { strategy: 'SHA256' }, |
| 38 | + salt: { source: 'STRING', value: 'TEST' }, |
| 39 | + columns: { |
| 40 | + static: ['col_a'], |
| 41 | + process: [], |
| 42 | + reference: [], |
| 43 | + }, |
| 44 | + }, |
| 45 | + validations: { |
| 46 | + col_a: [{ op: SUPPORTED_VALIDATORS.MAX_FIELD_LENGTH, value: 2 }], |
| 47 | + }, |
| 48 | + destination: { |
| 49 | + columns: [ |
| 50 | + { name: 'A', alias: 'col_a' }, |
| 51 | + { name: 'Test', alias: 'test' }, |
| 52 | + ], |
| 53 | + postfix: '_OUTPUT', |
| 54 | + }, |
| 55 | + destination_errors: { |
| 56 | + columns: [ |
| 57 | + { name: 'Errors', alias: 'errors' }, |
| 58 | + { name: 'A', alias: 'col_a' }, |
| 59 | + ], |
| 60 | + postfix: '_ERRORS', |
| 61 | + }, |
| 62 | + destination_map: { |
| 63 | + columns: [ |
| 64 | + { name: 'A', alias: 'col_a' }, |
| 65 | + { name: 'Test', alias: 'test' }, |
| 66 | + ], |
| 67 | + postfix: '_MAPPING', |
| 68 | + }, |
| 69 | +}; |
| 70 | + |
| 71 | +test('preprocessFile invalid', async () => { |
| 72 | + const fn = async () => |
| 73 | + await processFile({ |
| 74 | + config: CONFIG, |
| 75 | + inputFilePath: '', |
| 76 | + outputPath: '', |
| 77 | + // @ts-ignore |
| 78 | + format: null, |
| 79 | + limit: 10, |
| 80 | + }); |
| 81 | + await expect(fn).rejects.toThrow(); |
| 82 | +}); |
| 83 | + |
| 84 | +test('preprocessFile', async () => { |
| 85 | + const filePath = join(__dirname, 'files', 'input_ok.csv'); |
| 86 | + const results = await preprocessFile({ |
| 87 | + config: CONFIG, |
| 88 | + inputFilePath: filePath, |
| 89 | + limit: 10, |
| 90 | + }); |
| 91 | + |
| 92 | + expect(results.document.data[0]).toEqual({ col_a: 'A0', col_b: 'B0' }); |
| 93 | + expect(results.isValid).toEqual(true); |
| 94 | + expect(results.isMappingDocument).toEqual(false); |
| 95 | + expect(results.inputFilePath).toEqual(filePath); |
| 96 | + expect(results.errorFilePath).toEqual(undefined); |
| 97 | +}); |
| 98 | + |
| 99 | +test('preprocessFile::args', async () => { |
| 100 | + const filePath = join(__dirname, 'files', 'input_ok.csv'); |
| 101 | + let results = await preprocessFile({ |
| 102 | + config: CONFIG, |
| 103 | + inputFilePath: filePath, |
| 104 | + limit: 1, |
| 105 | + }); |
| 106 | + |
| 107 | + expect(results.document.data.length).toEqual(1); |
| 108 | + |
| 109 | + results = await preprocessFile({ config: CONFIG, inputFilePath: filePath }); |
| 110 | + expect(results.document.data.length).toEqual(2); |
| 111 | +}); |
| 112 | + |
| 113 | +test('preprocessFile mapping', async () => { |
| 114 | + const filePath = join(__dirname, 'files', 'input_mapping_ok.csv'); |
| 115 | + const results = await preprocessFile({ |
| 116 | + config: CONFIG, |
| 117 | + inputFilePath: filePath, |
| 118 | + limit: 10, |
| 119 | + }); |
| 120 | + |
| 121 | + expect(results.document.data[0]).toEqual({ col_a: 'A0' }); |
| 122 | + |
| 123 | + expect(results.isValid).toEqual(true); |
| 124 | + expect(results.isMappingDocument).toEqual(true); |
| 125 | + expect(results.inputFilePath).toEqual(filePath); |
| 126 | + expect(results.errorFilePath).toEqual(undefined); |
| 127 | +}); |
| 128 | + |
| 129 | +test('preprocessFile mapping invalid', async () => { |
| 130 | + const filePath = join(__dirname, 'files', 'input_mapping_ok.csv'); |
| 131 | + |
| 132 | + const newConfig = JSON.parse(JSON.stringify(CONFIG)); |
| 133 | + newConfig.validations.col_a.push({ op: 'options', value: ['NO', 'WAY'] }); |
| 134 | + |
| 135 | + const results = await preprocessFile({ |
| 136 | + config: newConfig, |
| 137 | + inputFilePath: filePath, |
| 138 | + limit: 10, |
| 139 | + }); |
| 140 | + |
| 141 | + expect(results.isMappingDocument).toEqual(true); |
| 142 | + expect(results.isValid).toEqual(false); |
| 143 | +}); |
| 144 | + |
| 145 | +test('preprocessFile invalid', async () => { |
| 146 | + const filePath = join(__dirname, 'files', 'input_invalid.csv'); |
| 147 | + |
| 148 | + const results = await preprocessFile({ |
| 149 | + config: CONFIG, |
| 150 | + inputFilePath: filePath, |
| 151 | + limit: 10, |
| 152 | + }); |
| 153 | + |
| 154 | + expect(results.isMappingDocument).toEqual(false); |
| 155 | + expect(results.document.data[0]).toEqual({ |
| 156 | + col_a: 'A0', |
| 157 | + col_b: 'B0', |
| 158 | + errors: '', |
| 159 | + row_number: 2, |
| 160 | + }); |
| 161 | + expect(results.document.data[1]).toEqual({ |
| 162 | + col_a: 'A1 TOO LONG', |
| 163 | + col_b: 'B1', |
| 164 | + errors: 'A must be shorter than 2 characters;', |
| 165 | + row_number: 3, |
| 166 | + }); |
| 167 | + |
| 168 | + expect(results.isValid).toEqual(false); |
| 169 | + |
| 170 | + const errorFile = results.errorFilePath; |
| 171 | + expect(errorFile).not.toEqual(undefined); |
| 172 | + expect(existsSync(errorFile as string)).toEqual(true); |
| 173 | +}); |
0 commit comments