Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement add file from buffer #196

Merged
merged 3 commits into from Oct 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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