Skip to content
Snippets Groups Projects
Commit b6323d09 authored by Simon Malesys's avatar Simon Malesys
Browse files

Add unit tests to the streams

parent b72f5e63
Branches
No related tags found
No related merge requests found
Pipeline #156221 passed
// ABSD
// Copyright (C) 2025 Institut Pasteur
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
import { describe, it } from "node:test"
import { BuildAntibodiesStream } from "./BuildAntibodiesStream.js"
import { PassThrough } from "node:stream"
import assert from "node:assert"
describe('BuildAntibodiesStream', () => {
it('should be in object mode', () => {
const buildAntibodies = new BuildAntibodiesStream()
assert(
buildAntibodies.readableObjectMode === true,
'The objectMode parameter should be true'
)
})
it('should accept a species as argument', () => {
const buildAntibodies = new BuildAntibodiesStream({ species: 'Homo sapiens' })
assert(
buildAntibodies.species === 'Homo sapiens',
'The species property should be set by passing an argument'
)
})
it('should have a lightChain property, set to null', () => {
const buildAntibodies = new BuildAntibodiesStream()
assert(
buildAntibodies.lightChain === null,
'The lightChain property needs to be set'
)
})
it('should build an antibody object from two fasta objects, light and heavy chains', (test, done) => {
const inputLightChain = {
header: 'lightID|||firstLightHeader;firstLightID;firstLightSource;firstLightSegment|||secondLightHeader;secondLightID;secondLightSource;secondLightSegment',
sequence: 'LIGHTCHAINSEQUENCE'
}
const inputHeavyChain = {
header: 'heavyID|||firstHeavyHeader;firstHeavyID;firstHeavySource;firstHeavySegment|||secondHeavyHeader;secondHeavyID;secondHeavySource;secondHeavySegment',
sequence: 'HEAVYCHAINSEQUENCE'
}
const inputSpecies = 'Homo sapiens'
const expected = {
id: 'lightID',
species: inputSpecies,
lightChain: {
headers: [
{
header: 'firstLightHeader',
id: 'firstLightID',
source: 'firstLightSegment',
vGeneSegment: 'firstLightSource'
},
{
header: 'secondLightHeader',
id: 'secondLightID',
source: 'secondLightSegment',
vGeneSegment: 'secondLightSource'
}
],
rawFasta: inputLightChain.header,
sequence: 'LIGHTCHAINSEQUENCE',
},
heavyChain: {
headers: [
{
header: 'firstHeavyHeader',
id: 'firstHeavyID',
source: 'firstHeavySegment',
vGeneSegment: 'firstHeavySource'
},
{
header: 'secondHeavyHeader',
id: 'secondHeavyID',
source: 'secondHeavySegment',
vGeneSegment: 'secondHeavySource'
}
],
rawFasta: inputHeavyChain.header,
sequence: 'HEAVYCHAINSEQUENCE'
}
}
// Setup a test pipeline with mock-up streams
const inputStream = new PassThrough({ objectMode: true })
const outputStream = new PassThrough({ objectMode: true })
const buildAntibodies = new BuildAntibodiesStream({ species: inputSpecies })
inputStream.pipe(buildAntibodies).pipe(outputStream)
// Handle processed data on the output stream
let outputData = null
outputStream.on('data', (chunk) => {
outputData = chunk
})
// Assert at the end of the stream
outputStream.on('end', () => {
assert.deepStrictEqual(outputData, expected)
assert(
buildAntibodies.lightChain === null,
'The lightChain property should be reset to null after an antibody is emitted'
)
done()
})
// Start the process
inputStream.write(inputLightChain)
inputStream.write(inputHeavyChain)
inputStream.end()
})
})
// ABSD
// Copyright (C) 2025 Institut Pasteur
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
import { describe, it } from "node:test"
import { GenerateHashIdStream } from "./GenerateHashIdStream.js"
import { PassThrough } from "node:stream"
import assert from "node:assert"
describe('GenerateHashIdStream', () => {
it('should be in object mode', () => {
const parser = new GenerateHashIdStream()
assert(
parser.readableObjectMode === true,
'The objectMode parameter should be true'
)
})
it('should remove extra spaces and tabs from a fasta object', (test, done) => {
const inputAntibody = {
species: 'Homo sapiens',
lightChain: {
sequence: 'LIGHTCHAINSEQUENCE',
},
heavyChain: {
sequence: 'HEAVYCHAINSEQUENCE'
}
}
const expected = {
hashId: "164a3b9b60621d13a0f25ed56c2932e4aca79208a4a01d6d886507636ae059ad",
species: 'Homo sapiens',
lightChain: {
sequence: 'LIGHTCHAINSEQUENCE',
},
heavyChain: {
sequence: 'HEAVYCHAINSEQUENCE'
}
}
// Setup a test pipeline with mock-up streams
const inputStream = new PassThrough({ objectMode: true })
const outputStream = new PassThrough({ objectMode: true })
const parser = new GenerateHashIdStream()
inputStream.pipe(parser).pipe(outputStream)
// Handle processed data on the output stream
let outputData = ''
outputStream.on('data', (chunk) => {
outputData = chunk
})
// Assert at the end of the stream
outputStream.on('end', () => {
assert.deepStrictEqual(outputData, expected)
done()
})
// Start the process
inputStream.write(inputAntibody)
inputStream.end()
})
})
// ABSD
// Copyright (C) 2025 Institut Pasteur
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
import { describe, it } from "node:test"
import { GroupAntibodiesStream } from "./GroupAntibodiesStream.js"
import { PassThrough } from "node:stream"
import assert from "node:assert"
describe('GroupAntibodiesStream', () => {
it('should be in object mode', () => {
const groupAntibodies = new GroupAntibodiesStream()
assert.ok(groupAntibodies.readableObjectMode)
})
it('should have a group property set to an empty array', () => {
const groupAntibodies = new GroupAntibodiesStream()
assert.deepStrictEqual(groupAntibodies.group, [])
})
it('should accept a group size as an argument', () => {
const groupSize = 500
const groupAntibodies = new GroupAntibodiesStream({ groupSize })
assert.strictEqual(groupAntibodies.groupSize, groupSize)
})
it('should group antibodies and emit an array when the given group size is reached', (test, done) => {
const antibodiesNumber = 1357
const groupSize = 500
// Setup a test pipeline with mock-up streams
const inputStream = new PassThrough({ objectMode: true })
const outputStream = new PassThrough({ objectMode: true })
const groupAntibodies = new GroupAntibodiesStream({ groupSize })
inputStream.pipe(groupAntibodies).pipe(outputStream)
// Handle processed data on the output stream
let outputGroups = []
outputStream.on('data', (group) => {
outputGroups.push(group)
})
// Assert at the end of the stream
outputStream.on('end', () => {
assert.strictEqual(outputGroups[0].length, 500)
assert.strictEqual(outputGroups[1].length, 500)
assert.strictEqual(outputGroups[2].length, 357, "The last group should contain the remaining antibodies")
done()
})
// Start the process
for (let i = 0; i < antibodiesNumber; i++) {
inputStream.write({})
}
inputStream.end()
})
})
......@@ -20,11 +20,11 @@ import { PassThrough } from "node:stream"
import assert from "node:assert"
describe('ParseFastaStream', () => {
it('should be in object mode by default', () => {
it('should be in object mode', () => {
const parser = new ParseFastaStream()
assert(
parser.readableObjectMode === true,
'The objectMode parameter should be true by default'
'The objectMode parameter should be true'
)
})
......
......@@ -20,11 +20,11 @@ import { PassThrough } from "node:stream"
import assert from "node:assert"
describe('SanitizeFastaStream', () => {
it('should be in object mode by default', () => {
it('should be in object mode', () => {
const parser = new SanitizeFastaStream()
assert(
parser.readableObjectMode === true,
'The objectMode parameter should be true by default'
'The objectMode parameter should be true'
)
})
......@@ -33,6 +33,7 @@ describe('SanitizeFastaStream', () => {
header: 'this is \t the header\t',
sequence: ' THISISTHESEQUENCE '
}
const expected = {
header: 'this is the header',
sequence: 'THISISTHESEQUENCE'
......
......@@ -20,15 +20,15 @@ import { PassThrough } from "node:stream"
import assert from "node:assert"
describe('SplitFastaStream', () => {
it('should be in object mode by default', () => {
it('should be in object mode', () => {
const splitFasta = new SplitFastaStream()
assert(
splitFasta.readableObjectMode === true,
'The objectMode parameter should be true by default'
'The objectMode parameter should be true'
)
})
it('should have a buffer property, intialized as an empty string', () => {
it('should have a buffer property, set as an empty string', () => {
const splitFasta = new SplitFastaStream()
assert(
splitFasta.buffer === '',
......@@ -36,7 +36,7 @@ describe('SplitFastaStream', () => {
)
})
it('should have a firstLine property, intialized to true', () => {
it('should have a firstLine property, set to true', () => {
const splitFasta = new SplitFastaStream()
assert(
splitFasta.firstLine === true,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment