Skip to content

Commit

Permalink
fix: improve test structure
Browse files Browse the repository at this point in the history
* refactor(test): monogDB

* refactor(test): mysql

* refactor(test): redis

* refactor(test):psql

* refactor(test): kafkajs

* refactor(test): amqp

* refactor(test): basic tests

* test: adding unit test for isKnownServiceType
  • Loading branch information
pacostas authored Oct 24, 2022
1 parent adf8596 commit a9539be
Show file tree
Hide file tree
Showing 23 changed files with 334 additions and 291 deletions.
32 changes: 15 additions & 17 deletions test/basic-tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ const { after, before, describe, it } = require('mocha');
const bindings = require('../../index.js');

const {
errors: { NO_SERVICE_BINDING_ROOT, INVALID_ARGUMENTS }
errors: { NO_SERVICE_BINDING_ROOT, INVALID_ARGUMENTS, UNKNOWN_SERVICE_TYPE, UNKNOWN_CLIENT, NO_BINDING_FOUND }
} = require('../../utils/messages/index.js');

const ERROR_INVALID_ARGUMENTS = `Error: ${INVALID_ARGUMENTS}`;
const ERROR_NO_SERVICE_BINDING_ROOT = `Error: ${NO_SERVICE_BINDING_ROOT}`;
const ERROR_UNKNOWN_SERVICE_TYPE = `Error: ${UNKNOWN_SERVICE_TYPE}`;
const ERROR_UNKNOWN_CLIENT = `Error: ${UNKNOWN_CLIENT}`;
const ERROR_NO_BINDING_FOUND = `Error: ${NO_BINDING_FOUND}`;

describe('basic tests', () => {
let env;
before(() => {
Expand Down Expand Up @@ -43,17 +49,15 @@ describe('basic tests', () => {
]);
});

it('unknown service', () => {
it(`should throw error: ${ERROR_UNKNOWN_SERVICE_TYPE}`, () => {
try {
bindings.getBinding('DOEST_NOT_EXIST');
assert.fail();
} catch (err) {
assert.equal(err.toString(), 'Error: Unknown service type');
assert.equal(err.toString(), ERROR_UNKNOWN_SERVICE_TYPE);
}
});

const ERROR_INVALID_ARGUMENTS = `Error: ${INVALID_ARGUMENTS}`;

it(`should throw error: ${ERROR_INVALID_ARGUMENTS}`, () => {
try {
bindings.getBinding('KAFKA', { removeUnmapped: true });
Expand All @@ -63,12 +67,12 @@ describe('basic tests', () => {
}
});

it('unknown client', () => {
it(`should throw error: ${ERROR_UNKNOWN_CLIENT}`, () => {
try {
bindings.getBinding('KAFKA', 'does-not-exist');
assert.fail();
} catch (err) {
assert.equal(err.toString(), 'Error: Unknown client');
assert.equal(err.toString(), ERROR_UNKNOWN_CLIENT);
}
});

Expand All @@ -88,17 +92,12 @@ describe('basic tests', () => {
});
});

it('id matches available', () => {
const binding = bindings.getBinding('KAFKA', 'kafkajs', 'kafka');
assert(binding);
});

it('id filters available', () => {
it(`should throw error: ${ERROR_NO_BINDING_FOUND}`, () => {
try {
bindings.getBinding('KAFKA', 'kafkajs', 'does_not_exist');
assert.fail();
} catch (err) {
assert.equal(err.toString(), 'Error: No Binding Found');
assert.equal(err.toString(), ERROR_NO_BINDING_FOUND);
}
});

Expand All @@ -114,12 +113,12 @@ describe('basic tests 2', () => {
process.env = { SERVICE_BINDING_ROOT: path.join(__dirname, 'bindings2') };
});

it('no available binding', () => {
it(`should throw error: ${ERROR_NO_BINDING_FOUND}`, () => {
try {
bindings.getBinding('KAFKA');
assert.fail();
} catch (err) {
assert.equal(err.toString(), 'Error: No Binding Found');
assert.equal(err.toString(), ERROR_NO_BINDING_FOUND);
}
});

Expand All @@ -129,7 +128,6 @@ describe('basic tests 2', () => {
});

describe('No service binding variable provided on process environment.', () => {
const ERROR_NO_SERVICE_BINDING_ROOT = `Error: ${NO_SERVICE_BINDING_ROOT}`;
it(`should throw error: ${NO_SERVICE_BINDING_ROOT}`, () => {
try {
bindings.getBinding('KAFKA');
Expand Down
80 changes: 0 additions & 80 deletions test/test-amqp-bindings-amq/test.js

This file was deleted.

29 changes: 0 additions & 29 deletions test/test-amqp-bindings-rabbitmq/test.js

This file was deleted.

114 changes: 114 additions & 0 deletions test/test-amqp-bindings/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
const assert = require('assert');
const path = require('path');
const { after, before, describe, it } = require('mocha');
const bindings = require('../../index.js');

const amqBindingData = {
host: 'test.domain.com',
password: 'pass1',
port: '1234',
provider: 'amqp',
type: 'amqp',
username: 'user1'
};

const rabbitmqBindingsMappedForRhea = {
host: 'test.domain.com',
password: 'pass1',
port: 1234,
provider: 'rabbitmq',
type: 'rabbitmq',
username: 'user1'
};

const amqBindingsMappedForRhea = {
host: 'test.domain.com',
password: 'pass1',
port: '1234',
provider: 'amqp',
type: 'amqp',
username: 'user1'
};

describe('AMQ', () => {
let env;
before(() => {
env = process.env;
process.env = { SERVICE_BINDING_ROOT: path.join(__dirname, 'bindings') };
});

describe('rhea on amqp-bindings', () => {
const id = 'amqp-bindings';
it('Default behaviour', () => {
const binding = bindings.getBinding('AMQP', 'rhea', { id });
assert(binding);
assert.deepEqual(binding, amqBindingsMappedForRhea);
});
it('Does NOT Remove Unmapped Values', () => {
const binding = bindings.getBinding('AMQP', 'rhea', {
removeUnmapped: false,
id
});
assert(binding);
assert.deepEqual(binding, amqBindingsMappedForRhea);
});
it('Remove Unmapped Values', () => {
const binding = bindings.getBinding('AMQP', 'rhea', {
removeUnmapped: true,
id
});
assert(binding);
assert.deepEqual(binding, amqBindingsMappedForRhea);
});
});
describe('rhea on rabbitmq-bindings', () => {
const id = 'rabbitmq-bindings';
it('Default behaviour', () => {
const binding = bindings.getBinding('AMQP', 'rhea', { id });
assert(binding);
assert.deepEqual(binding, rabbitmqBindingsMappedForRhea);
});
it('Does NOT Remove Unmapped Values', () => {
const binding = bindings.getBinding('AMQP', 'rhea', {
removeUnmapped: false,
id
});
assert(binding);
assert.deepEqual(binding, rabbitmqBindingsMappedForRhea);
});
it('Remove Unmapped Values', () => {
const binding = bindings.getBinding('AMQP', 'rhea', {
removeUnmapped: true,
id
});
assert(binding);
assert.deepEqual(binding, rabbitmqBindingsMappedForRhea);
});
});
describe('rhea passing amq bindng data as parameter.', () => {
it('Default behaviour', () => {
const binding = bindings.getBinding('AMQP', 'rhea', { bindingData: amqBindingData });
assert(binding);
assert.deepEqual(binding, amqBindingsMappedForRhea);
});
it('Do NOT Remove Unmapped Values', () => {
const binding = bindings.getBinding('AMQP', 'rhea', {
bindingData: amqBindingData,
removeUnmapped: false
});
assert(binding);
assert.deepEqual(binding, amqBindingsMappedForRhea);
});
it('Remove Unmapped Values', () => {
const binding = bindings.getBinding('AMQP', 'rhea', {
bindingData: amqBindingData,
removeUnmapped: true
});
assert(binding);
assert.deepEqual(binding, amqBindingsMappedForRhea);
});
});
after(() => {
process.env = env;
});
});
Loading

0 comments on commit a9539be

Please sign in to comment.