Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NickPhura committed Dec 17, 2024
1 parent 67022fe commit 5d5d170
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
50 changes: 31 additions & 19 deletions api/src/repositories/technique-vantage-repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ import { VantagePostData } from './vantage-mode-repository';
chai.use(sinonChai);

describe('TechniqueVantageRepository', () => {
let dbConnection: any;
let repository: TechniqueVantageRepository;

beforeEach(() => {
dbConnection = getMockDBConnection();
repository = new TechniqueVantageRepository(dbConnection);
});

afterEach(() => {
sinon.restore();
});
Expand All @@ -33,20 +25,25 @@ describe('TechniqueVantageRepository', () => {
description: 'test description'
}
];

const mockResponse = { rows: mockRecord } as QueryResult<any>;
const knexStub = sinon.stub(dbConnection, 'knex').resolves(mockResponse);
const dbConnection = getMockDBConnection({ knex: sinon.stub().resolves(mockResponse) });

const repository = new TechniqueVantageRepository(dbConnection);

const surveyId = 1;
const methodTechniqueId = 2;

const result = await repository.getVantagesForTechnique(surveyId, methodTechniqueId);

expect(knexStub).to.have.been.calledOnce;
expect(dbConnection.knex).to.have.been.calledOnce;
expect(result).to.deep.equal(mockRecord);
});

it('should throw an error if the query fails', async () => {
const dbConnection = getMockDBConnection();

const repository = new TechniqueVantageRepository(dbConnection);

sinon.stub(dbConnection, 'knex').throws(new Error('Query error'));

try {
Expand All @@ -61,22 +58,27 @@ describe('TechniqueVantageRepository', () => {
describe('insertVantagesForTechnique', () => {
it('should insert the vantages successfully', async () => {
const mockRecord = [{ method_technique_vantage_id: 1 }];
const mockResponse = { rows: mockRecord } as QueryResult<any>;
const dbConnection = getMockDBConnection({ knex: sinon.stub().resolves(mockResponse) });

const vantageMethods: VantagePostData[] = [{ vantage_method_id: 3 }];
const repository = new TechniqueVantageRepository(dbConnection);

const mockResponse = { rows: mockRecord } as QueryResult<any>;
const knexStub = sinon.stub(dbConnection, 'knex').resolves(mockResponse);
const vantageMethods: VantagePostData[] = [{ vantage_method_id: 3 }];

const surveyId = 1;
const methodTechniqueId = 2;

const result = await repository.insertVantagesForTechnique(surveyId, methodTechniqueId, vantageMethods);

expect(knexStub).to.have.been.calledOnce;
expect(dbConnection.knex).to.have.been.calledOnce;
expect(result).to.deep.equal(mockRecord);
});

it('should throw an error if insertion fails', async () => {
const dbConnection = getMockDBConnection();

const repository = new TechniqueVantageRepository(dbConnection);

const vantageMethods: VantagePostData[] = [{ vantage_method_id: 3 }];

sinon.stub(dbConnection, 'knex').throws(new Error('Insert error'));
Expand All @@ -92,20 +94,26 @@ describe('TechniqueVantageRepository', () => {

describe('deleteVantagesForTechnique', () => {
it('should delete the vantages successfully', async () => {
const vantageMethods: VantagePostData[] = [{ vantage_method_id: 3 }];

const mockResponse = { rows: [], rowCount: 0 } as any as Promise<QueryResult<any>>;
const knexStub = sinon.stub(dbConnection, 'knex').resolves(mockResponse);
const dbConnection = getMockDBConnection({ knex: sinon.stub().resolves(mockResponse) });

const repository = new TechniqueVantageRepository(dbConnection);

const vantageMethods: VantagePostData[] = [{ vantage_method_id: 3 }];

const surveyId = 1;
const methodTechniqueId = 2;

await repository.deleteVantagesForTechnique(surveyId, methodTechniqueId, vantageMethods);

expect(knexStub).to.have.been.calledOnce;
expect(dbConnection.knex).to.have.been.calledOnce;
});

it('should throw an error if deletion fails', async () => {
const dbConnection = getMockDBConnection();

const repository = new TechniqueVantageRepository(dbConnection);

const vantageMethods: VantagePostData[] = [{ vantage_method_id: 3 }];

sinon.stub(dbConnection, 'knex').throws(new Error('Delete error'));
Expand All @@ -119,6 +127,10 @@ describe('TechniqueVantageRepository', () => {
});

it('should do nothing if no vantage methods are provided', async () => {
const dbConnection = getMockDBConnection();

const repository = new TechniqueVantageRepository(dbConnection);

const knexStub = sinon.stub(dbConnection, 'knex');

await repository.deleteVantagesForTechnique(1, 2, []);
Expand Down
20 changes: 17 additions & 3 deletions api/src/services/technique-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ describe('TechniqueService', () => {
const insertQuantitativeAttributesForTechniqueStub = sinon
.stub(TechniqueAttributeService.prototype, 'insertQuantitativeAttributesForTechnique')
.resolves();
const insertVantagesForTechniqueStub = sinon
.stub(TechniqueVantageService.prototype, 'insertVantagesForTechnique')
.resolves();

const dbConnection = getMockDBConnection();

Expand All @@ -136,20 +139,24 @@ describe('TechniqueService', () => {
attributes: {
quantitative_attributes: [
{
method_technique_attribute_quantitative_id: 44,
method_lookup_attribute_quantitative_id: '123-456-55',
value: 66
}
],
qualitative_attributes: [
{
method_technique_attribute_qualitative_id: 77,
method_lookup_attribute_qualitative_id: '123-456-88',
method_lookup_attribute_qualitative_option_id: '123-456-99'
}
]
},
vantage_methods: []
vantage_methods: [
{
method_technique_vantage_id: 1,
vantage_method_id: 2,
vantage_category_id: 3
}
]
}
];

Expand All @@ -170,6 +177,13 @@ describe('TechniqueService', () => {
11,
techniques[0].attributes.quantitative_attributes
);
expect(insertVantagesForTechniqueStub).to.have.been.calledOnceWith(surveyId, 11, [
{
method_technique_vantage_id: 1,
vantage_method_id: 2,
vantage_category_id: 3
}
]);

expect(response).to.eql([mockRecord]);
});
Expand Down

0 comments on commit 5d5d170

Please sign in to comment.