Skip to content

Commit

Permalink
unit tests for dumpFileFormatter class
Browse files Browse the repository at this point in the history
  • Loading branch information
Knijus committed Feb 20, 2023
1 parent 5377746 commit e44502d
Show file tree
Hide file tree
Showing 9 changed files with 7,783 additions and 2,603 deletions.
6 changes: 6 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
presets: [
["@babel/preset-env", { targets: { node: "current" } }],
"@babel/preset-typescript",
],
};
13 changes: 13 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* eslint-disable @typescript-eslint/naming-convention */
const path = require("path");
/** @type {import('jest').Config} */
const config = {
roots: [
"<rootDir>/src/test/nodeTest"
],
globals: {
__DEV__: true
},
};

module.exports = config;
10,111 changes: 7,517 additions & 2,594 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,13 @@
"compile:views": "webpack --mode development",
"watch:extension": "tsc -watch -p ./",
"watch:views": "webpack --watch --mode development",
"test": "npm run compile && node ./node_modules/vscode/bin/test"
"test": "npm run compile && node ./node_modules/vscode/bin/test",
"jestTest": "jest"
},
"devDependencies": {
"@babel/core": "^7.21.0",
"@babel/preset-env": "^7.20.2",
"@babel/preset-typescript": "^7.21.0",
"@types/glob": "^7.2.0",
"@types/mocha": "^9.1.1",
"@types/node": "14.x",
Expand All @@ -224,9 +228,11 @@
"@typescript-eslint/eslint-plugin": "^5.21.0",
"@typescript-eslint/parser": "^5.21.0",
"@vscode/test-electron": "^2.1.3",
"babel-jest": "^29.4.3",
"css-loader": "^6.7.1",
"eslint": "^8.14.0",
"glob": "^8.0.1",
"jest": "^29.4.3",
"mocha": "^9.2.2",
"npm-run-all": "^4.1.5",
"style-loader": "^3.3.1",
Expand Down
2 changes: 1 addition & 1 deletion src/db/Oe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ interface IOeColumn {
type: string,
format: string | null
}
interface IOePsc {
export interface IOePsc {
cpstream: string,
dateformat: string,
numformat: string,
Expand Down
25 changes: 25 additions & 0 deletions src/test/nodeTest/DumpFileFormatterTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { IExportDumpData } from "../../db/oe";
import { DumpFileFormatter } from "../../webview/DumpFileFormatter";

export class DumpFileFormatterTest extends DumpFileFormatter {
public dumpData: string = super.dumpData;
public trailerInfo: string = super.trailerInfo;
public dumpFile: string = super.dumpFile;

public combineDumpFile() {
return super.combineDumpFile();
}

public formatDumpData(data: IExportDumpData) {
return super.formatDumpData(data);
}

public formatTrailerInfo(
data: any,
fileName: string,
dbName: string,
recordNum: number
) {
return super.formatTrailerInfo(data, fileName, dbName, recordNum);
}
}
58 changes: 58 additions & 0 deletions src/test/nodeTest/dumpFileFormatter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { expect, jest, test } from "@jest/globals";
import { assert } from "console";
import {
testObjInput,
testOutputDumpData,
testOutputTrailerInfo,
} from "./testObjects";
import { DumpFileFormatterTest } from "./DumpFileFormatterTest";

afterEach(() => {
jest.restoreAllMocks();
});

test("formatDumpFile calls other methods ", () => {
const dumpFileFormatterTest = new DumpFileFormatterTest();
let spyDumpData = jest.spyOn(dumpFileFormatterTest, "formatDumpData");
let spyTrailerInfo = jest.spyOn(dumpFileFormatterTest, "formatTrailerInfo");
let spyDumpFile = jest.spyOn(dumpFileFormatterTest, "combineDumpFile");

dumpFileFormatterTest.formatDumpFile(testObjInput, "test", "test");
expect(spyDumpData).toHaveBeenCalled();
expect(spyTrailerInfo).toHaveBeenCalled();
expect(spyDumpFile).toHaveBeenCalled();
});

test("getDumpFile method returns dumpFile", () => {
const dumpFileFormatterTest = new DumpFileFormatterTest();
dumpFileFormatterTest.dumpFile = "dumpFile";
assert("dumpFile", dumpFileFormatterTest.getDumpFile());
});

test("combineDumpFile combines data to dump file in right format", () => {
const dumpFileFormatterTest = new DumpFileFormatterTest();
dumpFileFormatterTest.dumpData = "test dump Data";
dumpFileFormatterTest.trailerInfo = "test trailer info";
dumpFileFormatterTest.combineDumpFile();

const testReturnValue =
"test dump Data\r\n" +
".\r\n" +
"test trailer info\r\n" +
".\r\n" +
"0000000021";

assert(testReturnValue, dumpFileFormatterTest.dumpFile);
});

test("formatDumpData formatted correctly with add data types", () => {
const dumpFileFormatterTest = new DumpFileFormatterTest();
dumpFileFormatterTest.formatDumpData(testObjInput);
assert(testOutputDumpData, dumpFileFormatterTest.dumpData);
});

test("formatTrailerInfo formatted correctly", () => {
const dumpFileFormatterTest = new DumpFileFormatterTest();
dumpFileFormatterTest.formatTrailerInfo(testObjInput.psc, "testing", "testDb", 4);
assert(testOutputTrailerInfo, dumpFileFormatterTest.trailerInfo);
});
149 changes: 149 additions & 0 deletions src/test/nodeTest/testObjects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { IExportDumpData } from "../../db/oe";

export const testObjInput: IExportDumpData = {
columns: [
{
name: "ROWID",
key: "ROWID",
label: "ROWID",
type: "ROWID",
format: null,
},
{
name: "testCharacter",
key: "testCharacter",
label: "testCharacter",
type: "character",
format: "x(10)",
},
{
name: "testDecimal",
key: "testDecimal",
label: "testDecimal",
type: "decimal",
format: "->,>>>,>>9.99",
},
{
name: "testInt",
key: "testInt",
label: "testInt",
type: "integer",
format: ">>9",
},
{
name: "testIntPercent",
key: "testIntPercent",
label: "testIntPercent",
type: "integer",
format: ">>9%",
},
{
name: "testInt64",
key: "testInt64",
label: "testInt64",
type: "int64",
format: ">>>>>>>>>>>>>>>>>>9",
},
{
name: "testRaw",
key: "testRaw",
label: "testRaw",
type: "raw",
format: "x(8)",
},
{
name: "testDate",
key: "testDate",
label: "testDate",
type: "date",
format: "99/99/9999",
},
{
name: "testDatetime",
key: "testDatetime",
label: "testDatetime",
type: "datetime",
format: "99/99/9999 HH:MM:SS",
},
{
name: "testLogical",
key: "testLogical",
label: "testLogical",
type: "logical",
format: "TRUE/FALSE",
},
],
psc: {
cpstream: "UTF-8",
dateformat: "mdy-1950",
numformat: "44,46",
timestamp: "2023/02/20-15:36:36",
},
rawData: [
{
ROWID: "0x0000000000002c01",
testCharacter: "record no.1",
testDecimal: 0.01,
testInt: 123,
testIntPercent: 15,
testInt64: 12345678901234567890,
testRaw: "AAMBAQEBAQEBAQEBAQA=",
testDate: "2023-02-20",
testDatetime: "2023-02-20T11:11:11.111",
testLogical: true,
},
{
ROWID: "0x0000000000002c02",
testCharacter: "record no.2",
testDecimal: 0.11,
testInt: 123,
testIntPercent: 15,
testInt64: 12345678901234567890,
testRaw: "AAMBAQEBAQEBAQEBAQA=",
testDate: "2023-02-20",
testDatetime: "2023-02-20T11:11:11.111",
testLogical: false,
},
{
ROWID: "0x0000000000002c03",
testCharacter: "record no.3",
testDecimal: 0.22,
testInt: -123,
testIntPercent: 15,
testInt64: 12345678901234567890,
testRaw: "AAMBAQEBAQEBAQEBAQA=",
testDate: "2023-02-20",
testDatetime: "2023-02-20T11:11:11.111",
testLogical: true,
},
{
ROWID: "0x0000000000002c04",
testCharacter: "record no.4",
testDecimal: 0.33,
testInt: 123,
testIntPercent: 15,
testInt64: 12345678901234567890,
testRaw: "AAMBAQEBAQEBAQEBAQA=",
testDate: "2023-02-20",
testDatetime: "2023-02-20T11:11:11.111",
testLogical: false,
},
],
};

export const testOutputDumpData: string =
'"record no.1" .01 123 15 12345678901234567000 "AAMBAQEBAQEBAQEBAQA=" 02/20/23 2023-02-20T11:11:11.111 yes\r\n' +
'"record no.2" .11 123 0.15 12345678901234567000 "AAMBAQEBAQEBAQEBAQA=" 02/20/23 2023-02-20T11:11:11.111 no\r\n' +
'"record no.3" .22 -123 15 12345678901234567000 "AAMBAQEBAQEBAQEBAQA=" 02/20/23 2023-02-20T11:11:11.111 yes\r\n' +
'"record no.4" .33 123 15 12345678901234567000 "AAMBAQEBAQEBAQEBAQA=" 02/20/23 2023-02-20T11:11:11.111 no\r\n';

export const testOutputTrailerInfo: string =
"PSC\r\n" +
"filename=testing\r\n" +
"records=0000000000004\r\n" +
"ldbname=testDb\r\n" +
"timestamp=2023/02/20-15:36:36\r\n" +
"numformat=44,46\r\n" +
"dateformat=mdy-1950\r\n" +
"map=NO-MAP\r\n" +
"cpstream=UTF-8\r\n";
14 changes: 7 additions & 7 deletions src/webview/DumpFileFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { IExportDumpData } from "../db/Oe";
import { IExportDumpData, IOePsc } from "../db/Oe";

export class DumpFileFormatter {

private dumpData: string = "";
private trailerInfo: string = "";
private dumpFile: string = "";
protected dumpData: string = "";
protected trailerInfo: string = "";
protected dumpFile: string = "";

constructor() {
}
Expand All @@ -19,15 +19,15 @@ export class DumpFileFormatter {
return this.dumpFile;
}

private combineDumpFile (): void {
protected combineDumpFile (): void {
this.dumpFile = this.dumpData
+ `.\r\n`
+ this.trailerInfo
+ `.\r\n`
+ `${String(this.dumpData.length + 3).padStart(10, "0")}\r\n`;
}

private formatDumpData (data: IExportDumpData): void {
protected formatDumpData (data: IExportDumpData): void {
this.dumpData = data.rawData.reduce((accumulator: string, row: any) => {
return (
accumulator +
Expand Down Expand Up @@ -85,7 +85,7 @@ export class DumpFileFormatter {
}, "");
}

private formatTrailerInfo (data: any, fileName: string, dbName: string, recordNum: number): void {
protected formatTrailerInfo (data: IOePsc, fileName: string, dbName: string, recordNum: number): void {
this.trailerInfo = `PSC\r\n`
+ `filename=${fileName}\r\n`
+ `records=${String(recordNum).padStart(13, "0")}\r\n`
Expand Down

0 comments on commit e44502d

Please sign in to comment.