diff --git a/.env b/.env index c58526f..594d940 100644 --- a/.env +++ b/.env @@ -2,8 +2,9 @@ COMPOSE_FILE=docker-compose.yml;docker/dev.yml COMPOSE_PROJECT_NAME=off-query COMPOSE_PATH_SEPARATOR=; TAG=latest +QUERY_PORT=127.0.0.1:5511 POSTGRES_HOST=localhost -POSTGRES_PORT=5512 +POSTGRES_PORT=127.0.0.1:5512 POSTGRES_DB=query POSTGRES_USER=productopener POSTGRES_PASSWORD=productopener diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4f1e98d --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +# Use this to start both the query service and associated database in Docker +up: + docker-compose up -d --build + +# This task starts a Postgres database in Docker and then prepares the local environment for development +dev: + docker-compose up -d query_postgres + npm install + npm run migration:up + +tests: + npm test + +lint: + npm run lint diff --git a/README.md b/README.md index bd52a42..3b0b113 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ To get started... Run the following: ``` -docker-compose up -d --build +docker-compose up -d query_postgres ``` ### Use an existing Postgres database @@ -28,7 +28,6 @@ Run the following: ``` npm install -npm run build npm run migration:up ``` @@ -94,6 +93,4 @@ The "count" and "aggregate" POST endpoints accept a MongoDB style filter and agg # TODO -- Run tests on PR -- Add branch protection - Configure production deployment diff --git a/docker-compose.yml b/docker-compose.yml index 800fb54..f5d04c6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,6 +17,9 @@ services: - POSTGRES_USER - POSTGRES_PASSWORD - MONGO_URI + # Use a different number so does not clash with locally running instance + ports: + - "${QUERY_PORT:-5511}:5510" depends_on: query_postgres: condition: service_started diff --git a/docker/dev.yml b/docker/dev.yml index 64b8292..ff1d495 100644 --- a/docker/dev.yml +++ b/docker/dev.yml @@ -2,7 +2,7 @@ services: query_postgres: # Expose port locally for testing purposes. ports: - - 5512:5432 + - "${POSTGRES_PORT:-5512}:5432" volumes: - ./data:/mnt/data networks: @@ -14,10 +14,6 @@ services: environment: # Use Product Opener's MongoDB - MONGO_URI=mongodb://mongodb:27017 - # Expose port locally for testing purposes. - # Use a different number so does not clash with locally running instance - ports: - - 5511:5510 volumes: - ./data:/data networks: diff --git a/src/health/health.controller.spec.ts b/src/health/health.controller.spec.ts index 033349a..eb4f7f0 100644 --- a/src/health/health.controller.spec.ts +++ b/src/health/health.controller.spec.ts @@ -2,7 +2,11 @@ import { HealthController } from './health.controller'; import { createTestingModule } from '../../test/test.helper'; import { AppModule } from '../app.module'; import { MongodbHealthIndicator } from './mongodb-health-indicator'; -import { HealthCheckError, HealthIndicatorResult } from '@nestjs/terminus'; +import { + HealthCheckError, + HealthIndicatorResult, + HealthCheckService, +} from '@nestjs/terminus'; import { ServiceUnavailableException } from '@nestjs/common'; describe('HealthController', () => { @@ -27,6 +31,8 @@ describe('HealthController', () => { it('should return unhealthy if mongodb is down', async () => { await createTestingModule([AppModule], async (app) => { const controller = app.get(HealthController); + const errorLog = (app.get(HealthCheckService)['logger'].error = + jest.fn()); const mongoIndicator = app.get(MongodbHealthIndicator); mongoIndicator.isHealthy = jest.fn(async () => { @@ -42,6 +48,7 @@ describe('HealthController', () => { } catch (e) { expect(e).toBeInstanceOf(ServiceUnavailableException); expect(e.response.status).toBe('error'); + expect(errorLog).toHaveBeenCalledTimes(1); } }); });