From e0b691a7fb0817e7daa57fbbbc8e5b7b7843c21d Mon Sep 17 00:00:00 2001 From: Bart Mesuere Date: Tue, 30 Jul 2024 14:57:33 +0200 Subject: [PATCH] add formatter tests --- lib/formatters/formatter.ts | 6 ++--- tests/commands/unipept.test.ts | 2 +- tests/formatters/csv_formatter.test.ts | 30 ++++++++++++++++++++++ tests/formatters/formatter_factory.test.ts | 11 ++++++++ tests/formatters/test_object.ts | 21 +++++++++++++++ 5 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 tests/formatters/csv_formatter.test.ts create mode 100644 tests/formatters/formatter_factory.test.ts create mode 100644 tests/formatters/test_object.ts diff --git a/lib/formatters/formatter.ts b/lib/formatters/formatter.ts index 39d4202d..1c8ecf02 100644 --- a/lib/formatters/formatter.ts +++ b/lib/formatters/formatter.ts @@ -2,9 +2,9 @@ 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); } @@ -12,7 +12,7 @@ export abstract class Formatter { } // 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; } } diff --git a/tests/commands/unipept.test.ts b/tests/commands/unipept.test.ts index c2b2e340..cdda14dd 100644 --- a/tests/commands/unipept.test.ts +++ b/tests/commands/unipept.test.ts @@ -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"); diff --git a/tests/formatters/csv_formatter.test.ts b/tests/formatters/csv_formatter.test.ts new file mode 100644 index 00000000..847f086b --- /dev/null +++ b/tests/formatters/csv_formatter.test.ts @@ -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"]]; + 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']]; + const object = [TestObject.testObject(), TestObject.testObject()]; + const csv = [`>test,${TestObject.asCsv()}`, TestObject.asCsv(), ""].join("\n"); + //expect(formatter.format(object, fasta, false)).toBe(csv); +}); diff --git a/tests/formatters/formatter_factory.test.ts b/tests/formatters/formatter_factory.test.ts new file mode 100644 index 00000000..05933522 --- /dev/null +++ b/tests/formatters/formatter_factory.test.ts @@ -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"); +}); diff --git a/tests/formatters/test_object.ts b/tests/formatters/test_object.ts new file mode 100644 index 00000000..097ffc57 --- /dev/null +++ b/tests/formatters/test_object.ts @@ -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 '5stringa2false'; + } + + static asCsv() { + return '5,string,"[""a"",2,false]"'; + } + + static asCsvHeader() { + return "integer,string,list\n"; + } +}