Skip to content

Commit

Permalink
Merge branch 'SIMSBIOHUB-424' of https://github.com/bcgov/biohubbc-pl…
Browse files Browse the repository at this point in the history
…atform into SIMSBIOHUB-424
  • Loading branch information
NickPhura committed Jan 15, 2024
2 parents 0e15cd4 + 98ce012 commit 7ba312a
Show file tree
Hide file tree
Showing 2 changed files with 201 additions and 0 deletions.
4 changes: 4 additions & 0 deletions api/src/repositories/code-repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,8 @@ describe('CodeRepository', () => {
expect(result).to.be.eql([mockRow]);
});
});

describe('getFeaturePropertyByName', () => {
// @TODO
})
});
197 changes: 197 additions & 0 deletions api/src/repositories/search-index-repository.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from 'chai';
import { QueryResult } from 'pg';
import Sinon from 'sinon';
import { ApiExecuteSQLError } from '../errors/api-error';
import { getMockDBConnection } from '../__mocks__/db';
import { FeaturePropertyRecordWithPropertyTypeName, SearchIndexRepository } from './search-index-respository';

Expand Down Expand Up @@ -385,4 +386,200 @@ describe('SearchIndexRepository', () => {
]);
});
});

describe('insertSearchableDatetimeRecords', () => {
it('should succeed on insert', async () => {
const mockQueryResponse = { rowCount: 1, rows: [{ search_datetime_id: 1 }] } as any as Promise<QueryResult<any>>;

const mockDBConnection = getMockDBConnection({
knex: () => mockQueryResponse
});

const searchIndexRepository = new SearchIndexRepository(mockDBConnection);

const response = await searchIndexRepository.insertSearchableDatetimeRecords([
{
feature_property_id: 1,
submission_feature_id: 1,
value: new Date('2024-01-15').toDateString()
}
]);

expect(response[0].search_datetime_id).to.equal(1);
});

it('should throw an exception if no rows are retured', async () => {
const mockQueryResponse = {
rowCount: 0,
rows: []
} as unknown as Promise<QueryResult<any>>;

const mockDBConnection = getMockDBConnection({ knex: () => mockQueryResponse });

const searchIndexRepository = new SearchIndexRepository(mockDBConnection);

try {
await searchIndexRepository.insertSearchableDatetimeRecords([
{
feature_property_id: 1,
submission_feature_id: 1,
value: new Date('2024-01-15').toDateString()
}
]);
} catch (error) {
expect((error as ApiExecuteSQLError).message).to.equal('Failed to insert searchable datetime records');
}
});
});

describe('insertSearchableNumberRecords', () => {
it('should succeed on insert', async () => {
const mockQueryResponse = { rowCount: 1, rows: [{ search_number_id: 1 }] } as any as Promise<QueryResult<any>>;

const mockDBConnection = getMockDBConnection({
knex: () => mockQueryResponse
});

const searchIndexRepository = new SearchIndexRepository(mockDBConnection);

const response = await searchIndexRepository.insertSearchableNumberRecords([
{
feature_property_id: 1,
submission_feature_id: 1,
value: 100
}
]);

expect(response[0].search_number_id).to.equal(1);
});

it('should throw an exception if no rows are retured', async () => {
const mockQueryResponse = {
rowCount: 0,
rows: []
} as unknown as Promise<QueryResult<any>>;

const mockDBConnection = getMockDBConnection({ knex: () => mockQueryResponse });

const searchIndexRepository = new SearchIndexRepository(mockDBConnection);

try {
await searchIndexRepository.insertSearchableNumberRecords([
{
feature_property_id: 1,
submission_feature_id: 1,
value: 100
}
]);
} catch (error) {
expect((error as ApiExecuteSQLError).message).to.equal('Failed to insert searchable number records');
}
});
});

describe('insertSearchableSpatialRecords', () => {
it('should succeed on insert', async () => {
const mockQueryResponse = { rowCount: 1, rows: [{ search_spatial_id: 1 }] } as any as Promise<QueryResult<any>>;

const mockDBConnection = getMockDBConnection({
sql: () => mockQueryResponse
});

const searchIndexRepository = new SearchIndexRepository(mockDBConnection);

const response = await searchIndexRepository.insertSearchableSpatialRecords([
{
feature_property_id: 1,
submission_feature_id: 1,
value: {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
geometry: { type: 'Point', coordinates: [-127, 49] }
}
]
}
}
]);

expect(response[0].search_spatial_id).to.equal(1);
});

it('should throw an exception if no rows are retured', async () => {
const mockQueryResponse = {
rowCount: 0,
rows: []
} as unknown as Promise<QueryResult<any>>;

const mockDBConnection = getMockDBConnection({ sql: () => mockQueryResponse });

const searchIndexRepository = new SearchIndexRepository(mockDBConnection);

try {
await searchIndexRepository.insertSearchableSpatialRecords([
{
feature_property_id: 1,
submission_feature_id: 1,
value: {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
geometry: { type: 'Point', coordinates: [-127, 49] }
}
]
}
}
]);
} catch (error) {
expect((error as ApiExecuteSQLError).message).to.equal('Failed to insert searchable spatial records');
}
});
});

describe('insertSearchableStringRecords', () => {
it('should succeed on insert', async () => {
const mockQueryResponse = { rowCount: 1, rows: [{ search_string_id: 1 }] } as any as Promise<QueryResult<any>>;

const mockDBConnection = getMockDBConnection({
knex: () => mockQueryResponse
});

const searchIndexRepository = new SearchIndexRepository(mockDBConnection);

const response = await searchIndexRepository.insertSearchableStringRecords([
{
feature_property_id: 1,
submission_feature_id: 1,
value: 'Test'
}
]);

expect(response[0].search_string_id).to.equal(1);
});

it('should throw an exception if no rows are retured', async () => {
const mockQueryResponse = {
rowCount: 0,
rows: []
} as unknown as Promise<QueryResult<any>>;

const mockDBConnection = getMockDBConnection({ knex: () => mockQueryResponse });

const searchIndexRepository = new SearchIndexRepository(mockDBConnection);

try {
await searchIndexRepository.insertSearchableStringRecords([
{
feature_property_id: 1,
submission_feature_id: 1,
value: 'Test'
}
]);
} catch (error) {
expect((error as ApiExecuteSQLError).message).to.equal('Failed to insert searchable string records');
}
});
});
});

0 comments on commit 7ba312a

Please sign in to comment.