Skip to content

Commit

Permalink
bigquery builder unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Mar 14, 2024
1 parent f9617d5 commit adcfe67
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 4 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/testing-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,28 @@ jobs:
working-directory: api
run: yarn test:integration

testing-api-unit:
name: Unit Tests
runs-on: ubuntu-22.04
timeout-minutes: 30

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Use Node.js 18.16
uses: actions/setup-node@v3
with:
node-version: '18.16'

- name: Install API dependencies
working-directory: api
run: yarn install

- name: Run API tests + coverage
working-directory: api
run: yarn test:unit

# - name: Generate API coverage artifact
# uses: actions/upload-artifact@v2
# with:
Expand Down
1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"test:cov": "node --expose-gc ./node_modules/.bin/jest --config test/jest-config.json --coverage --forceExit",
"test:debug": "node --inspect-brk --expose-gc -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --config test/jest-config.json --detectOpenHandles --forceExit",
"test:integration": "node --expose-gc ./node_modules/.bin/jest --config test/jest-config.json --forceExit -i test/integration/",
"test:unit": "node --expose-gc ./node_modules/.bin/jest --config test/jest-config.json --forceExit -i test/unit/",
"test:e2e": "NODE_OPTIONS=\"--max-old-space-size=6144\" node --expose-gc ./node_modules/.bin/jest --config test/jest-config.json --logHeapUsage --forceExit -i test/e2e/"
},
"engines": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import {
EUDRDashboard,
EUDRDashBoardFields,
} from 'modules/eudr-alerts/dashboard/dashboard.types';
import { GetEUDRAlertDatesDto } from '../dto/get-alerts.dto';
import { AlertsOutput } from '../dto/alerts-output.dto';
import { GetEUDRAlertDatesDto } from 'modules/eudr-alerts/dto/get-alerts.dto';
import { AlertsOutput } from 'modules/eudr-alerts/dto/alerts-output.dto';
import { GeoRegion } from 'modules/geo-regions/geo-region.entity';
import { EUDRDashBoardDetail } from './dashboard-detail.types';
import { EUDRDashBoardDetail } from 'modules/eudr-alerts/dashboard/dashboard-detail.types';
import { MaterialsService } from 'modules/materials/materials.service';
import { AdminRegionsService } from 'modules/admin-regions/admin-regions.service';

Expand Down
2 changes: 1 addition & 1 deletion api/src/modules/eudr-alerts/dto/get-alerts.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ export class GetEUDRAlertsDto extends GetEUDRAlertDatesDto {
@ApiPropertyOptional()
@IsOptional()
@IsInt()
limit?: number = 1000;
limit?: number;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { DataSource, SelectQueryBuilder } from 'typeorm';
import { BigQueryAlertsQueryBuilder } from '../../../src/modules/eudr-alerts/alerts-query-builder/big-query-alerts-query.builder';
import { typeOrmConfig } from '../../../src/typeorm.config';

describe('BigQueryAlertsQueryBuilder', () => {
let queryBuilder: SelectQueryBuilder<any>;
const dataSource = new DataSource(typeOrmConfig);

beforeEach(() => {
queryBuilder = dataSource.createQueryBuilder().from('falsetable', 'alerts');
});
test('without DTO parameters should return a select with table name and alias', () => {
const bigQueryBuilder = new BigQueryAlertsQueryBuilder(queryBuilder);
const result = bigQueryBuilder.buildQuery();
expect(result.query).toBe('SELECT * FROM falsetable alerts');
expect(result.params).toEqual([]);
});

test('with date range parameters should add a where statement with parsed DATE formats', () => {
const bigQueryBuilder = new BigQueryAlertsQueryBuilder(queryBuilder, {
startAlertDate: new Date('2020-01-01'),
endAlertDate: new Date('2020-01-31'),
});
const result = bigQueryBuilder.buildQuery();
expect(result.query).toContain(
'WHERE DATE(alertdate) BETWEEN DATE(?) AND DATE(?)',
);
});

test('with a single supplier id should add a WHERE IN statement with a single parameter', () => {
const bigQueryBuilder = new BigQueryAlertsQueryBuilder(queryBuilder, {
supplierIds: ['supplier1'],
});
const result = bigQueryBuilder.buildQuery();
expect(result.query).toContain('WHERE supplierid IN (?)');
expect(result.params).toEqual(['supplier1']);
});

test('with 2 supplier id should add a WHERE IN statement with 2 parameters', () => {
const bigQueryBuilder = new BigQueryAlertsQueryBuilder(queryBuilder, {
supplierIds: ['supplier1', 'supplier2'],
});
const result = bigQueryBuilder.buildQuery();
expect(result.query).toContain('WHERE supplierid IN (?, ?)');
expect(result.params).toEqual(['supplier1', 'supplier2']);
});
});

0 comments on commit adcfe67

Please sign in to comment.