Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic Testing for Discojs #697

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
75 changes: 72 additions & 3 deletions discojs/src/aggregator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from "chai";
import { Map, Range, Set } from "immutable";

import { mock, instance, when, anything } from 'ts-mockito';

Check failure on line 3 in discojs/src/aggregator.spec.ts

View workflow job for this annotation

GitHub Actions / lint-lib

'when' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 3 in discojs/src/aggregator.spec.ts

View workflow job for this annotation

GitHub Actions / lint-lib

'anything' is defined but never used. Allowed unused vars must match /^_/u
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hum, discojs is still using mocha/chai but if we want new features not present in theses, we will rather move to vitest (already used in the webapp & discojs-node) which provides mocking, parallel testing, good typescript support, …
that said, after the other comments, I don't think you'll even need "ts-mockito" anymore

import { Model, WeightsContainer } from "./index.js";
import {
Aggregator,
Expand All @@ -20,10 +20,53 @@
describe(`${name} implements Aggregator contract`, () => {
it("starts at round zero", () => {
const aggregator = new Aggregator();

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's not needed (and is being shown as very red in my git diff).

we don't enforce a formatter yet but I'm all for slowly moving to prettier (that is, when you add changes, prettify it around, but not the whole file if you only changed a line). I'm hopeful that the transition will be smoother this way.

expect(aggregator.round).to.equal(0);
});

it("starts with no contributions", () => {
const aggregator = new Aggregator();

expect(aggregator.size).to.equal(0);
})

it("model correctly initialized", async () => {

Check failure on line 33 in discojs/src/aggregator.spec.ts

View workflow job for this annotation

GitHub Actions / lint-lib

Async arrow function has no 'await' expression
const aggregator = new Aggregator();

const model = mock(Model);

aggregator.setModel(instance(model));
expect(aggregator.model).to.equal(instance(model));
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, no need to test for that, aggregator would need to be redone at some point and avoiding to have any reference to the task or the model will be the first thing

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you. It's the first time I work on GitHub with PRs that are not made by cloning for a PR. Should I make a new branch from this branch for the PR?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that I follow you: this is already a PR, you can added changes by pushing new commits to the same branch ("test_components") and it'll show up here.


it("is full when created with no nodes", () => {
const aggregator = new Aggregator();

expect(aggregator.isFull());
});

it("is not full when created with more than no nodes and empty", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it("is not full when created with more than no nodes and empty", () => {
it("is empty when without contributions", () => {

const aggregator = new Aggregator();
aggregator.setNodes(Set.of("client 0"));

expect(aggregator.isFull()).to.be.false;
});


Comment on lines +54 to +55
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

too much whitespaces

Suggested change


it("is full when enough contributions", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it("is full when enough contributions", () => {
it("is full with enough contributions", () => {

const aggregator = new Aggregator();
aggregator.setNodes(Set.of("client 0", "client 1", "client 2"));

for (let i = 0; i < 3; i++)
for (let r = 0; r < aggregator.communicationRounds; r++)
aggregator.add(`client ${i}`, WeightsContainer.of([i]), 0, r);

expect(aggregator.isFull()).to.be.true;
});


Comment on lines +67 to +68
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whitespaces

Suggested change


it("moves forward with enough contributions", async () => {
const aggregator = new Aggregator();
aggregator.setNodes(Set.of("client 0", "client 1", "client 2"));
Expand All @@ -35,10 +78,36 @@
aggregator.add(`client ${i}`, WeightsContainer.of([i]), 0, r);

await results; // nothing to test

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ws

expect(aggregator.round).to.equal(1);
});

it("does not move forward with not enough contributions", async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double negation are hard on my brain. it also feels as a duplicate of the previous one. you can merge the two and check that it didn't advance with only two clients contributions but does with three.

const aggregator = new Aggregator();
aggregator.setNodes(Set.of("client 0", "client 1", "client 2"));

const results = aggregator.receiveResult();

for (let i = 0; i < 2; i++)
for (let r = 0; r < aggregator.communicationRounds; r++)
aggregator.add(`client ${i}`, WeightsContainer.of([i]), 0, r);

await results;

expect(aggregator.round).to.equal(0);
});

it("Adding at the wrong round does not count", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reads more nicely. also, you can keep the test itself smaller by not having three clients but only one. less code == faster to understand => less bug.

Suggested change
it("Adding at the wrong round does not count", () => {
it("drops contributions for futur rounds", () => {

const aggregator = new Aggregator();
aggregator.setNodes(Set.of("client 0", "client 1", "client 2"));

for (let i = 0; i < 3; i++)
for (let r = 0; r < aggregator.communicationRounds; r++)
aggregator.add(`client ${i}`, WeightsContainer.of([i]), aggregator.round+1, r);

expect(aggregator.size).to.equal(0);
});

it("gives same results on each node", async () => {
const network = setupNetwork(Aggregator);

Expand Down
53 changes: 44 additions & 9 deletions discojs/src/weights/aggregation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,68 @@ import { WeightsContainer, aggregation } from './index.js'

describe('weights aggregation', () => {
it('avg of weights with two operands', () => {
const actual = aggregation.avg([
const expected = aggregation.avg([
WeightsContainer.of([1, 2, 3, -1], [-5, 6]),
WeightsContainer.of([2, 3, 7, 1], [-10, 5]),
WeightsContainer.of([3, 1, 5, 3], [-15, 19])
])
const expected = WeightsContainer.of([2, 2, 5, 1], [-10, 10])
const actual = WeightsContainer.of([2, 2, 5, 1], [-10, 10])

assert.isTrue(actual.equals(expected))
assert.isTrue(expected.equals(actual))
})

it('sum of weights with two operands', () => {
const actual = aggregation.sum([
const expected = aggregation.sum([
[[3, -4], [9]],
[[2, 13], [0]]
])
const expected = WeightsContainer.of([5, 9], [9])
const actual = WeightsContainer.of([5, 9], [9])

assert.isTrue(actual.equals(expected))
assert.isTrue(expected.equals(actual))
})

it('diff of weights with two operands', () => {
const actual = aggregation.diff([
const expected = aggregation.diff([
[[3, -4, 5], [9, 1]],
[[2, 13, 4], [0, 1]]
])
const expected = WeightsContainer.of([1, -17, 1], [9, 0])
const actual = WeightsContainer.of([1, -17, 1], [9, 0])

assert.isTrue(actual.equals(expected))
assert.isTrue(expected.equals(actual))
})

it('avg of weights with no operands throws an error', () => {
assert.throws(() => aggregation.avg([]))
})

it('sum of weights with no operands throws an error', () => {
assert.throws(() => aggregation.sum([]))
})

it('diff of weights with no operands throws an error', () => {
assert.throws(() => aggregation.diff([]))
})

it('avg of weights with different dimensions throws an error', () => {
assert.throws(() => aggregation.avg([
[[3, -4], [9]],
[[2, 13, 4], [0, 1]]
]))
})

it('sum of weights with different dimensions throws an error', () => {
assert.throws(() => aggregation.sum([
[[3, -4], [9]],
[[2, 13, 4], [0, 1]]
]))
})

it('diff of weights with different dimensions throws an error', () => {
assert.throws(() => aggregation.diff([
[[3, -4], [9]],
[[2, 13, 4], [0, 1]]
]))
})
Comment on lines +37 to +68
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bench test, you can simplify it by using a Set(…).forEach((op) => …)

while at it, should it fail? an addition without value can be zero, a multiplication can be one; why is this test needed?

Suggested change
it('avg of weights with no operands throws an error', () => {
assert.throws(() => aggregation.avg([]))
})
it('sum of weights with no operands throws an error', () => {
assert.throws(() => aggregation.sum([]))
})
it('diff of weights with no operands throws an error', () => {
assert.throws(() => aggregation.diff([]))
})
it('avg of weights with different dimensions throws an error', () => {
assert.throws(() => aggregation.avg([
[[3, -4], [9]],
[[2, 13, 4], [0, 1]]
]))
})
it('sum of weights with different dimensions throws an error', () => {
assert.throws(() => aggregation.sum([
[[3, -4], [9]],
[[2, 13, 4], [0, 1]]
]))
})
it('diff of weights with different dimensions throws an error', () => {
assert.throws(() => aggregation.diff([
[[3, -4], [9]],
[[2, 13, 4], [0, 1]]
]))
})
Set.of(aggregation.avg, aggregation.sum, aggregation.diff).forEach((op) => {
it(`${op.name} of weights with no operands throws`, () =>
expect(() => op([])).to.throw());
it(`${op.name} of weights with different dimensions throws`, () =>
expect(() => op([[[1, 2]], [[3, 4, 5]]])).to.throw());
});



})
12 changes: 11 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"webapp"
],
"dependencies": {
"immutable": "4"
"immutable": "4",
"ts-mockito": "^2.6.1"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the root package.json is hardly ever changed, especially for dev deps used in a single package. I use it for syncing version of dependencies that are needed across many packages.
please modify discojs/package.json if you want to add a package for discojs. (which you probably don't need as stated previously)

},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "7",
Expand Down
Loading