diff --git a/.github/workflows/testing-api.yml b/.github/workflows/testing-api.yml index 935f8cb69..5323b26bd 100644 --- a/.github/workflows/testing-api.yml +++ b/.github/workflows/testing-api.yml @@ -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: diff --git a/api/package.json b/api/package.json index 9ad724b87..9028353d7 100644 --- a/api/package.json +++ b/api/package.json @@ -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": { diff --git a/api/src/modules/eudr-alerts/dashboard/eudr-dashboard.service.ts b/api/src/modules/eudr-alerts/dashboard/eudr-dashboard.service.ts index ee85b9509..9fc5d782c 100644 --- a/api/src/modules/eudr-alerts/dashboard/eudr-dashboard.service.ts +++ b/api/src/modules/eudr-alerts/dashboard/eudr-dashboard.service.ts @@ -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'; diff --git a/api/src/modules/eudr-alerts/dto/get-alerts.dto.ts b/api/src/modules/eudr-alerts/dto/get-alerts.dto.ts index 77a99f425..0f55caac4 100644 --- a/api/src/modules/eudr-alerts/dto/get-alerts.dto.ts +++ b/api/src/modules/eudr-alerts/dto/get-alerts.dto.ts @@ -53,5 +53,5 @@ export class GetEUDRAlertsDto extends GetEUDRAlertDatesDto { @ApiPropertyOptional() @IsOptional() @IsInt() - limit?: number = 1000; + limit?: number; } diff --git a/api/test/unit/bigquery-query-builder/bigquery-query-builder.spec.ts b/api/test/unit/bigquery-query-builder/bigquery-query-builder.spec.ts new file mode 100644 index 000000000..6c6d32214 --- /dev/null +++ b/api/test/unit/bigquery-query-builder/bigquery-query-builder.spec.ts @@ -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; + 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']); + }); +});