Skip to content

Commit

Permalink
test: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh committed Oct 23, 2023
1 parent ce69144 commit d4f3723
Showing 1 changed file with 103 additions and 4 deletions.
107 changes: 103 additions & 4 deletions test/tests/unit/adopted-probes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('AdoptedProbes', () => {
sandbox.restore();
});

it('startSync method should sync the data and start regular syncs', async () => {
it('syncDashboardData method should sync the data', async () => {
const adoptedProbes = new AdoptedProbes(sqlStub as unknown as Knex, fetchSocketsStub);
selectStub.resolves([{ ip: '1.1.1.1', uuid: '1-1-1-1-1', lastSyncDate: '1970-01-01' }]);

Expand All @@ -39,7 +39,8 @@ describe('AdoptedProbes', () => {
expect(sqlStub.callCount).to.equal(1);
expect(sqlStub.args[0]).deep.equal([ 'adopted_probes' ]);
expect(selectStub.callCount).to.equal(1);
expect(selectStub.args[0]).deep.equal([ 'ip', 'uuid', 'lastSyncDate' ]);

expect(selectStub.args[0]).deep.equal([ 'ip', 'uuid', 'lastSyncDate', 'status', 'version', 'country', 'city', 'latitude', 'longitude', 'asn', 'network' ]);
});

it('class should update uuid if it is wrong', async () => {
Expand Down Expand Up @@ -80,7 +81,7 @@ describe('AdoptedProbes', () => {
expect(deleteStub.callCount).to.equal(0);
});

it('class should do nothing if adopted probe was not found and lastSyncDate > 30 days away', async () => {
it('class should delete adoption if adopted probe was not found and lastSyncDate > 30 days away', async () => {
const adoptedProbes = new AdoptedProbes(sqlStub as unknown as Knex, fetchSocketsStub);
selectStub.resolves([{ ip: '1.1.1.1', uuid: '1-1-1-1-1', lastSyncDate: '1969-11-15' }]);
fetchSocketsStub.resolves([]);
Expand All @@ -91,7 +92,6 @@ describe('AdoptedProbes', () => {
expect(whereStub.args[0]).to.deep.equal([{ ip: '1.1.1.1' }]);
expect(updateStub.callCount).to.equal(0);
expect(deleteStub.callCount).to.equal(1);
expect(deleteStub.callCount).to.equal(1);
});

it('class should do nothing if probe data is actual', async () => {
Expand Down Expand Up @@ -144,4 +144,103 @@ describe('AdoptedProbes', () => {
expect(updateStub.callCount).to.equal(0);
expect(deleteStub.callCount).to.equal(0);
});

it('class should update probe meta info if it is outdated', async () => {
const adoptedProbes = new AdoptedProbes(sqlStub as unknown as Knex, fetchSocketsStub);
selectStub.resolves([{
ip: '1.1.1.1',
uuid: '1-1-1-1-1',
lastSyncDate: '1970-01-01',
status: 'ready',
version: '0.26.0',
country: 'IE',
city: 'Dublin',
latitude: 53.3331,
longitude: -6.2489,
asn: 16509,
network: 'Amazon.com, Inc.',
}]);

fetchSocketsStub.resolves([{
data: {
probe: {
ipAddress: '1.1.1.1',
uuid: '1-1-1-1-1',
status: 'initializing',
version: '0.27.0',
nodeVersion: 'v18.17.0',
location: {
continent: 'EU',
region: 'Northern Europe',
country: 'GB',
city: 'London',
asn: 20473,
latitude: 53.3331,
longitude: -6.2489,
network: 'The Constant Company, LLC',
},
},
},
}]);

await adoptedProbes.syncDashboardData();

expect(whereStub.callCount).to.equal(1);
expect(whereStub.args[0]).to.deep.equal([{ ip: '1.1.1.1' }]);
expect(updateStub.callCount).to.equal(1);

expect(updateStub.args[0]).to.deep.equal([{
status: 'initializing',
version: '0.27.0',
country: 'GB',
city: 'London',
asn: 20473,
network: 'The Constant Company, LLC',
}]);
});

it('class should update probe meta info if it is outdated', async () => {
const adoptedProbes = new AdoptedProbes(sqlStub as unknown as Knex, fetchSocketsStub);
selectStub.resolves([{
ip: '1.1.1.1',
uuid: '1-1-1-1-1',
lastSyncDate: '1970-01-01',
status: 'ready',
version: '0.26.0',
country: 'IE',
city: 'Dublin',
latitude: 53.3331,
longitude: -6.2489,
asn: 16509,
network: 'Amazon.com, Inc.',
}]);

fetchSocketsStub.resolves([{
data: {
probe: {
ipAddress: '1.1.1.1',
uuid: '1-1-1-1-1',
status: 'ready',
version: '0.26.0',
nodeVersion: 'v18.17.0',
location: {
continent: 'EU',
region: 'Northern Europe',
country: 'IE',
city: 'Dublin',
asn: 16509,
latitude: 53.3331,
longitude: -6.2489,
network: 'Amazon.com, Inc.',
},
},
},
}]);

await adoptedProbes.syncDashboardData();

expect(whereStub.callCount).to.equal(0);
expect(updateStub.callCount).to.equal(0);
expect(deleteStub.callCount).to.equal(0);
});
});

0 comments on commit d4f3723

Please sign in to comment.