Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
setup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Seb-sti1 authored and a-thansen committed Sep 12, 2023
1 parent 8a8c730 commit fd2be31
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ jobs:
run: npm run build
- name: Tests
run: npm run test
- name: Tests end-to-end
run: npm run test:e2e

frontend:
runs-on: ubuntu-22.04
Expand Down
26 changes: 22 additions & 4 deletions backend/src/app.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,39 @@ import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';

import { KnexModule } from 'nestjs-knex';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { DB_LIRAMAP_CONFIG, POSTGIS_DB_CONFIG } from './database';

const database = (config: any, name: string) => {
return KnexModule.forRootAsync(
{
useFactory: () => ({ config }),
},
name,
);
};

describe('AppController', () => {
let appController: AppController;

beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
imports: [
ConfigModule.forRoot(),
database(POSTGIS_DB_CONFIG, 'postgis'),
database(DB_LIRAMAP_CONFIG, 'lira-map'),
],
controllers: [AppController],
providers: [AppService],
providers: [AppService, ConfigService],
}).compile();

appController = app.get<AppController>(AppController);
});

describe('root', () => {
it('should return "Hello World!"', () => {
expect(appController.getHello()).toBe('Hello World!');
describe('GET /', () => {
it('should return "OK"', () => {
expect(appController.getStatus()).toBe('OK');
});
});
});
41 changes: 41 additions & 0 deletions backend/src/conditions/rc.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Test, TestingModule } from '@nestjs/testing';

import { RCController } from './rc.controller';
import { RCService } from './rc.service';

import { KnexModule } from 'nestjs-knex';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { DB_LIRAMAP_CONFIG, POSTGIS_DB_CONFIG } from '../database';

const database = (config: any, name: string) => {
return KnexModule.forRootAsync(
{
useFactory: () => ({ config }),
},
name,
);
};

describe('Road Condition Controller', () => {
let rcController: RCController;

beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
imports: [
ConfigModule.forRoot(),
database(POSTGIS_DB_CONFIG, 'postgis'),
database(DB_LIRAMAP_CONFIG, 'lira-map'),
],
controllers: [RCController],
providers: [RCService, ConfigService],
}).compile();

rcController = app.get<RCController>(RCController);
});

describe('RoadCondition Controller', () => {
it('Should be defined', () => {
expect(rcController).toBeDefined();
});
});
});
11 changes: 4 additions & 7 deletions backend/test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
import { AppModule } from '../src/app.module';

describe('AppController (e2e)', () => {
describe('App', () => {
let app: INestApplication;

beforeEach(async () => {
Expand All @@ -15,10 +15,7 @@ describe('AppController (e2e)', () => {
await app.init();
});

it('/ (GET)', () => {
return request(app.getHttpServer())
.get('/')
.expect(200)
.expect('Hello World!');
it('GET /', async () => {
return request(app.getHttpServer()).get('/').expect(200).expect('OK');
});
});
48 changes: 48 additions & 0 deletions backend/test/rc.e2e-spec.disable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// TODO: Need database to work
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from '../src/app.module';
import testJSON from './utils';

describe('RoadConditions', () => {
let app: INestApplication;

beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();

app = moduleFixture.createNestApplication();
await app.init();
});

test('GET /conditions should return JSON', async () => {
const response = await request(app.getHttpServer()).get('/conditions');

expect(response.status).toBe(200);
expect(testJSON(response.body)).not.toThrow();
});

describe('GET /conditions/ways', () => {
it('Should return JSON', async () => {
const response = await request(app.getHttpServer()).get(
'/conditions/ways?type=IRI&zoom=0',
);

expect(response.status).toBe(200);
expect(testJSON(response.body)).not.toThrow();
});
});

describe('GET /conditions/way', () => {
it('Should return JSON', async () => {
const response = await request(app.getHttpServer()).get(
'/conditions/way?wayId=263681425&type=IRI',
);

expect(response.status).toBe(200);
expect(testJSON(response.body)).not.toThrow();
});
});
});

0 comments on commit fd2be31

Please sign in to comment.