Skip to content

Commit

Permalink
implement add file from buffer (#196)
Browse files Browse the repository at this point in the history
* implement add file from buffer
  • Loading branch information
AlinaKul committed Oct 24, 2021
1 parent c7bac21 commit d68a772
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
9 changes: 7 additions & 2 deletions packages/xlsx-import/src/ImporterFactory.ts
@@ -1,12 +1,17 @@
import { Workbook } from 'exceljs';
import IImporterLegacy, { IImporter } from './IImporter';
import { Buffer, Workbook } from 'exceljs';
import { IImporter } from './IImporter';
import ImporterLegacy, { Importer } from './Importer';

export class ImporterFactory {
public async from(path: string): Promise<IImporter> {
const wb = new Workbook();
await wb.xlsx.readFile(path);
return new Importer(wb);
}

public async fromBuffer(buffer: Buffer): Promise<IImporter> {
const wb = new Workbook();
await wb.xlsx.load(buffer);
return new Importer(wb);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/xlsx-import/src/mappers/dateMapper.ts
@@ -1,4 +1,4 @@
import { ValueMapper } from '../abstracts/ValueMapper';

// noinspection SuspiciousTypeOfGuard
export const dateMapper: ValueMapper<Date> = value => new Date(typeof value === "string" ? value : "invalid");
export const dateMapper: ValueMapper<Date> = value => new Date(typeof value === 'string' ? value : 'invalid');
@@ -1,7 +1,8 @@
import * as chai from 'chai';
import { ImportType } from '../../src/config/ImportType';

import { ImporterFactory } from '../../src/ImporterFactory';
import path from 'path';
import { readFileSync } from 'fs';

describe('reading sigle items (objects)', () => {
const configs = {
Expand All @@ -26,7 +27,18 @@ describe('reading sigle items (objects)', () => {
chai.expect(result).eql(expected);
chai.expect(result.length).equals(1);
});
it('getAllItems should return one correct object from buffer', async () => {
const factory = new ImporterFactory();
const filePath = path.resolve(__dirname, '../data/', 'marsjanie-db.xlsx');
const buffer = readFileSync(filePath);
const importer = await factory.fromBuffer(buffer);
const result = importer.getAllItems(configs.author);

const expected = [{ firstName: 'Marian', secondName: 'Marianacki', age: 123, city: 'Pila-wojenna' }];

chai.expect(result).eql(expected);
chai.expect(result.length).equals(1);
});
const definedTypesAsString = ['object', 'single', 'singleton'];
definedTypesAsString.forEach(type => {
it(`for type (as string) '${type}' getAllItems should return one correct object`, async () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/xlsx-import/tests/unit/mappers/dateMapper.ts
Expand Up @@ -5,7 +5,10 @@ describe('UNIT TEST: src/mappers/', () => {
describe('dateMapper', () => {
const dataProvider = [
// data time
{ inValue: 'Thu Oct 08 2020 02:00:00 GMT+0200 (Central European Summer Time)', expectedResult: 1602115200000 },
{
inValue: 'Thu Oct 08 2020 02:00:00 GMT+0200 (Central European Summer Time)',
expectedResult: 1602115200000,
},
{ inValue: 'Thu Jan 30 1750 02:00:00 GMT-0900', expectedResult: -6939954000000 },
{ inValue: 'Thu Jan 30 3125', expectedResult: 36450774000000 },

Expand Down

0 comments on commit d68a772

Please sign in to comment.