Skip to content

Commit

Permalink
Add tests for usageEngine and usage routes
Browse files Browse the repository at this point in the history
  • Loading branch information
MilGard91 committed Aug 23, 2023
1 parent 8dc95d7 commit 91cbc92
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 0 deletions.
2 changes: 2 additions & 0 deletions test/mocks/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const express = require('express');

const quoteRouter = require('../../src/routes/quote');
const capacityRouter = require('../../src/routes/capacity');
const usageRouter = require('../../src/routes/usage');

const mockStore = require('./store');

Expand All @@ -27,6 +28,7 @@ const main = () => {
// initiate routes
app.use('/v2', capacityRouter);
app.use('/v2', quoteRouter);
app.use('/v2', usageRouter);

return app;
};
Expand Down
5 changes: 5 additions & 0 deletions test/mocks/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ const store = {
1: [1, 2],
2: [1, 2],
},
poolProductIds: {
1: [0, 1, 2],
2: [1, 2],
3: [0],
},
products: {
0: {
productType: 0,
Expand Down
1 change: 1 addition & 0 deletions test/unit/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
describe('unit tests', function () {
require('./capacityEngine');
require('./quoteEngine');
require('./usageEngine');
require('./routes');
require('./calculateOptimalPoolAllocation');
});
121 changes: 121 additions & 0 deletions test/unit/responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,128 @@ const quote = {
],
};

const usage = [
{
poolId: '1',
products: [
{
productId: 0,
capacityUsed: [
{
assetId: 0,
amount: '0',
},
{
assetId: 1,
amount: '0',
},
{
assetId: 255,
amount: '0',
},
],
},
{
productId: 1,
capacityUsed: [
{
assetId: 0,
amount: '0',
},
{
assetId: 1,
amount: '0',
},
{
assetId: 255,
amount: '0',
},
],
},
{
productId: 2,
capacityUsed: [
{
assetId: 0,
amount: '0',
},
{
assetId: 1,
amount: '0',
},
{
assetId: 255,
amount: '0',
},
],
},
],
},
{
poolId: '2',
products: [
{
productId: 1,
capacityUsed: [
{
assetId: 0,
amount: '0',
},
{
assetId: 1,
amount: '0',
},
{
assetId: 255,
amount: '0',
},
],
},
{
productId: 2,
capacityUsed: [
{
assetId: 0,
amount: '0',
},
{
assetId: 1,
amount: '0',
},
{
assetId: 255,
amount: '0',
},
],
},
],
},
{
poolId: '3',
products: [
{
productId: 0,
capacityUsed: [
{
assetId: 0,
amount: '3750158703091230720',
},
{
assetId: 1,
amount: '10478675352241508148979',
},
{
assetId: 255,
amount: '364800000000000000000',
},
],
},
],
},
];

module.exports = {
capacities,
quote,
usage,
};
1 change: 1 addition & 0 deletions test/unit/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
describe('routes', function () {
require('./quote');
require('./capcaity');
require('./usage');
});
23 changes: 23 additions & 0 deletions test/unit/routes/usage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { expect } = require('chai');
const supertest = require('supertest');
const initApp = require('../../mocks/server');
const { usage } = require('../responses');

describe('GET /usage', async () => {
let server;
before(() => {
const app = initApp();
server = supertest(app);
});

it('should get all usage for pools', async function () {
const { body: response } = await server.get('/v2/usage').expect('Content-Type', /json/).expect(200);
expect(response).to.be.deep.equal(usage);
});

it('should get all usage for one pool', async function () {
const poolId = 1;
const { body: response } = await server.get(`/v2/usage/${poolId}`).expect('Content-Type', /json/).expect(200);
expect(response).to.be.deep.equal(usage[0]);
});
});
52 changes: 52 additions & 0 deletions test/unit/usageEngine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const sinon = require('sinon');
const { expect } = require('chai');
const { BigNumber } = require('ethers');

const usageEngine = require('../../src/lib/usageEngine');
const mockStore = require('../mocks/store');
const { usage } = require('./responses');
describe.only('Capacity Engine tests', () => {
const store = { getState: () => null };

afterEach(function () {
sinon.restore();
});

it('should return usage for all pools', () => {
sinon.stub(store, 'getState').callsFake(() => mockStore);
const response = usageEngine(store, []);

response.forEach((pool, i) => {
expect(pool.poolId).to.be.equal(usage[i].poolId);
pool.products.forEach(({ productId, capacityUsed }, j) => {
expect(productId).to.be.equal(usage[i].products[j].productId);
capacityUsed.forEach(({ amount }, k) => {
expect(amount.toString()).to.be.equal(usage[i].products[j].capacityUsed[k].amount);
});
});
});
});

it('should return capacity for one product', () => {
sinon.stub(store, 'getState').callsFake(() => mockStore);

const [pool] = usageEngine(store, ['1']);
const [expectedUsage] = usage;

expect(pool.poolId).to.be.equal(expectedUsage.poolId);
pool.products.forEach(({ productId, capacityUsed }, i) => {
expect(productId).to.be.equal(expectedUsage.products[i].productId);
capacityUsed.forEach(({ amount }, j) => {
expect(amount.toString()).to.be.equal(expectedUsage.products[j].capacityUsed[j].amount);
});
});
});

it('should throw non existing product', () => {
sinon.stub(store, 'getState').callsFake(() => mockStore);

const [pool] = usageEngine(store, ['10']);

expect(pool.products.length).to.be.equal(0);
});
});

0 comments on commit 91cbc92

Please sign in to comment.