Skip to content

Commit

Permalink
add formatter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bmesuere committed Jul 30, 2024
1 parent 5c9e17f commit e0b691a
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/formatters/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ export abstract class Formatter {

abstract header(sampleData: object, fastaMapper?: boolean): string;
abstract footer(): string;
abstract convert(data: { [key: string]: string }[], first?: boolean): string;
abstract convert(data: object[], first?: boolean): string;

format(data: { [key: string]: string }[], fastaMapper?: boolean, first?: boolean): string {
format(data: object[], fastaMapper?: boolean, first?: boolean): string {
if (fastaMapper) {
data = this.integrateFastaHeaders(data, fastaMapper);
}
return this.convert(data, first);
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
integrateFastaHeaders(data: { [key: string]: string }[], fastaMapper: boolean): { [key: string]: string }[] {
integrateFastaHeaders(data: object[], fastaMapper: boolean): object[] {
return data;
}
}
2 changes: 1 addition & 1 deletion tests/commands/unipept.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Unipept } from '../../lib/commands/unipept';

test('test single argument', async () => {
test('test if all commands are available', async () => {
const command = new Unipept();
const commandNames = command.program.commands.map(c => c.name());
expect(commandNames).toContain("pept2lca");
Expand Down
30 changes: 30 additions & 0 deletions tests/formatters/csv_formatter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { FormatterFactory } from "../../lib/formatters/formatter_factory";
import { TestObject } from "./test_object";

const formatter = FormatterFactory.getFormatter("csv");

test('test header', () => {
const fasta = [["peptide", ">test"]];

Check failure on line 7 in tests/formatters/csv_formatter.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'fasta' is assigned a value but never used
const object = [TestObject.testObject(), TestObject.testObject()];
expect(formatter.header(object)).toBe(TestObject.asCsvHeader());
//expect(formatter.header(object, fasta)).toBe(`fasta_header,${TestObject.asCsvHeader()}`);
});

test('test footer', () => {
expect(formatter.footer()).toBe("");
});

test('test convert', () => {
const object = [TestObject.testObject(), TestObject.testObject()];
const csv = [TestObject.asCsv(), TestObject.asCsv(), ""].join("\n");

expect(formatter.convert(object, true)).toBe(csv);
expect(formatter.convert(object, false)).toBe(csv);
});

test('test format with fasta', () => {
const fasta = [['>test', '5']];

Check failure on line 26 in tests/formatters/csv_formatter.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'fasta' is assigned a value but never used
const object = [TestObject.testObject(), TestObject.testObject()];

Check failure on line 27 in tests/formatters/csv_formatter.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'object' is assigned a value but never used
const csv = [`>test,${TestObject.asCsv()}`, TestObject.asCsv(), ""].join("\n");

Check failure on line 28 in tests/formatters/csv_formatter.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'csv' is assigned a value but never used
//expect(formatter.format(object, fasta, false)).toBe(csv);
});
11 changes: 11 additions & 0 deletions tests/formatters/formatter_factory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { FormatterFactory } from "../../lib/formatters/formatter_factory";

test('test if default formatter is csv', async () => {
const formatter = FormatterFactory.getFormatter("foo");
expect(formatter.constructor.name).toBe("CSVFormatter");
});

test('test if csv formatter is csv', async () => {
const formatter = FormatterFactory.getFormatter("csv");
expect(formatter.constructor.name).toBe("CSVFormatter");
});
21 changes: 21 additions & 0 deletions tests/formatters/test_object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export class TestObject {
static testObject() {
return { "integer": 5, "string": "string", "list": ["a", 2, false] };
}

static asJson() {
return '{"integer":5,"string":"string","list":["a",2,false]}';
}

static asXml() {
return '<integer>5</integer><string>string</string><list><item>a</item><item>2</item><item>false</item></list>';
}

static asCsv() {
return '5,string,"[""a"",2,false]"';
}

static asCsvHeader() {
return "integer,string,list\n";
}
}

0 comments on commit e0b691a

Please sign in to comment.