Skip to content

Commit

Permalink
New: basic formatting functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
pustovitDmytro committed Aug 6, 2019
2 parents 7637b67 + e195b58 commit 273d6ae
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 7 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"babel-eslint": "^10.0.1",
"babel-plugin-module-resolver": "^3.2.0",
"chai": "^4.2.0",
"chance": "^1.0.18",
"conventional-changelog-eslint": "^3.0.1",
"coveralls": "^3.0.3",
"eslint": "^5.16.0",
Expand All @@ -53,9 +54,11 @@
"eslint-plugin-no-require-lodash": "^1.1.0",
"eslint-plugin-prefer-spread": "^1.0.3",
"eslint-plugin-react": "^7.12.4",
"fs-extra": "^8.1.0",
"mocha": "^6.1.4",
"nyc": "^14.1.1",
"semantic-release": "^15.13.19"
"semantic-release": "^15.13.19",
"uuid": "^3.3.2"
},
"dependencies": {
"winston": "^3.2.1"
Expand Down
2 changes: 1 addition & 1 deletion src/Format.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default function CSVFormatter(fields, options={}){
const delim = options.delimiter || ';';
return format.printf(logObject => {
const data = fields.map(key => {
const value = info[key];
const value = logObject[key];

return value === undefined ? '' : value;
});
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import CSVFormatter from './CSVFormatter';
import CSVFormatter from './Format';
export default CSVFormatter;
39 changes: 35 additions & 4 deletions tests/package/configurations.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
import { assert } from 'chai';
import logger from '../entry';
import { verifyStdout } from '../utils';
import CSV from '../entry';
import { testFormatter } from '../utils';
import Factory from '../Test';

const factory = new Factory();

suite('Configurations');

test('Default configuration', function () {
before(async () => {
await factory.cleanTmpFolder();
await factory.setTmpFolder();
});

test('Positive: default configuration', async function () {
const formatter = CSV([ 'user', 'action' ]);
const data = [
{ user: 'e3c0b365-26fd-54fc-afd7-d844c1d47a95', action: 'CREATE_POST' },
{ user: '', action: 'UPDATE_POST' },
{ user: 'e3c0b365-26fd-54fc-afd7-d844c1d47a95' },
{ },
{ user: 'e3c0b365-26fd-54fc-afd7-d844c1d47a95', time: '2019-08-06T11:46:46.434Z' },
{ action: 'DELETE_POST', user: 'e595b156-f526-5b94-ab76-c9fb610e286b' }
];

const expected = `e3c0b365-26fd-54fc-afd7-d844c1d47a95;CREATE_POST
;UPDATE_POST
e3c0b365-26fd-54fc-afd7-d844c1d47a95;
;
e3c0b365-26fd-54fc-afd7-d844c1d47a95;
e595b156-f526-5b94-ab76-c9fb610e286b;DELETE_POST
`;

await testFormatter(formatter, data, expected);
});


after(async () => {
await factory.cleanTmpFolder();
});
33 changes: 33 additions & 0 deletions tests/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import path from 'path';
import uuid from 'uuid';
import fse from 'fs-extra';
import { assert } from 'chai';
import { createLogger, transports } from 'winston';
import { tmpFolder } from './constants';

export const pause = time => new Promise(resolve => setTimeout(resolve, time));

export async function testFormatter(formatter, data, expected) {
const fileId = uuid.v4();
const filePath = path.join(tmpFolder, `formatter-${fileId}.csv`);
const logger = createLogger({
level : 'info',
format : formatter,
transports : [
new transports.Console(),
new transports.File({
filename : filePath
})
]
});

data.forEach(item => {
logger.log('info', item);
});

await pause(1000);

const content = await fse.readFile(filePath);

assert.equal(content.toString(), expected);
}

0 comments on commit 273d6ae

Please sign in to comment.