1
0
mirror of https://github.com/immich-app/immich.git synced 2024-12-22 01:47:08 +02:00

Fix test instance cannot clear database after each test" (#354)

* Update test

* Fixed test cannot initialize database

* Added a separate network to test containers group to run test while in development mode
This commit is contained in:
Alex 2022-07-16 23:43:31 -05:00 committed by GitHub
parent d8b26c6da8
commit 5d03e9bda8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 35 additions and 24 deletions

View File

@ -15,4 +15,4 @@ jobs:
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Run Immich Server 2E2 Test - name: Run Immich Server 2E2 Test
run: docker-compose -f ./docker/docker-compose.test.yml --env-file ./docker/.env.test up --abort-on-container-exit --exit-code-from immich_server_test run: docker-compose -f ./docker/docker-compose.test.yml --env-file ./docker/.env.test up --abort-on-container-exit --exit-code-from immich-server-test

View File

@ -11,7 +11,7 @@ stage:
docker-compose -f ./docker/docker-compose.staging.yml up --build -V --remove-orphans docker-compose -f ./docker/docker-compose.staging.yml up --build -V --remove-orphans
test-e2e: test-e2e:
docker-compose -f ./docker/docker-compose.test.yml --env-file ./docker/.env.test up --renew-anon-volumes --abort-on-container-exit --exit-code-from immich_server_test --remove-orphans docker-compose -f ./docker/docker-compose.test.yml --env-file ./docker/.env.test -p immich-test-e2e up --renew-anon-volumes --abort-on-container-exit --exit-code-from immich-server-test --remove-orphans --build
prod: prod:
docker-compose -f ./docker/docker-compose.yml up --build -V --remove-orphans docker-compose -f ./docker/docker-compose.yml up --build -V --remove-orphans

View File

@ -1,5 +1,5 @@
# Database # Database
DB_HOSTNAME=immich_postgres_test DB_HOSTNAME=immich-database-test
DB_USERNAME=postgres DB_USERNAME=postgres
DB_PASSWORD=postgres DB_PASSWORD=postgres
DB_DATABASE_NAME=e2e_test DB_DATABASE_NAME=e2e_test

View File

@ -1,8 +1,8 @@
version: "3.8" version: "3.8"
services: services:
immich_server_test: immich-server-test:
image: immich-server-dev:latest image: immich-server-test
build: build:
context: ../server context: ../server
dockerfile: Dockerfile dockerfile: Dockerfile
@ -17,15 +17,17 @@ services:
environment: environment:
- NODE_ENV=development - NODE_ENV=development
depends_on: depends_on:
- redis - immich-redis-test
- database - immich-database-test
networks:
redis: - immich-test-network
container_name: immich_redis_test immich-redis-test:
container_name: immich-redis-test
image: redis:6.2 image: redis:6.2
networks:
database: - immich-test-network
container_name: immich_postgres_test immich-database-test:
container_name: immich-database-test
image: postgres:14 image: postgres:14
env_file: env_file:
- .env.test - .env.test
@ -36,5 +38,8 @@ services:
PG_DATA: /var/lib/postgresql/data PG_DATA: /var/lib/postgresql/data
volumes: volumes:
- /var/lib/postgresql/data - /var/lib/postgresql/data
ports: networks:
- 5432:5432 - immich-test-network
networks:
immich-test-network:

View File

@ -10,6 +10,7 @@ import { ImmichJwtModule } from '../src/modules/immich-jwt/immich-jwt.module';
import { AuthUserDto } from '../src/decorators/auth-user.decorator'; import { AuthUserDto } from '../src/decorators/auth-user.decorator';
import { UserService } from '../src/api-v1/user/user.service'; import { UserService } from '../src/api-v1/user/user.service';
import { UserModule } from '../src/api-v1/user/user.module'; import { UserModule } from '../src/api-v1/user/user.module';
import { DataSource } from 'typeorm';
function _createAlbum(app: INestApplication, data: CreateAlbumDto) { function _createAlbum(app: INestApplication, data: CreateAlbumDto) {
return request(app.getHttpServer()).post('/album').send(data); return request(app.getHttpServer()).post('/album').send(data);
@ -17,9 +18,10 @@ function _createAlbum(app: INestApplication, data: CreateAlbumDto) {
describe('Album', () => { describe('Album', () => {
let app: INestApplication; let app: INestApplication;
let database: DataSource;
afterAll(async () => { afterAll(async () => {
await clearDb(); await clearDb(database);
await app.close(); await app.close();
}); });
@ -30,6 +32,7 @@ describe('Album', () => {
}).compile(); }).compile();
app = moduleFixture.createNestApplication(); app = moduleFixture.createNestApplication();
database = app.get(DataSource);
await app.init(); await app.init();
}); });
@ -56,12 +59,14 @@ describe('Album', () => {
app = moduleFixture.createNestApplication(); app = moduleFixture.createNestApplication();
userService = app.get(UserService); userService = app.get(UserService);
database = app.get(DataSource);
await app.init(); await app.init();
}); });
describe('with empty DB', () => { describe('with empty DB', () => {
afterEach(async () => { afterEach(async () => {
await clearDb(); await clearDb(database);
}); });
it('creates an album', async () => { it('creates an album', async () => {
@ -91,13 +96,13 @@ describe('Album', () => {
// setup users // setup users
const result = await Promise.all([ const result = await Promise.all([
userService.createUser({ userService.createUser({
email: 'one1@test.com', email: 'one@test.com',
password: '1234', password: '1234',
firstName: 'one', firstName: 'one',
lastName: 'test', lastName: 'test',
}), }),
userService.createUser({ userService.createUser({
email: 'two2@test.com', email: 'two@test.com',
password: '1234', password: '1234',
firstName: 'two', firstName: 'two',
lastName: 'test', lastName: 'test',

View File

@ -3,13 +3,10 @@ import { CanActivate, ExecutionContext } from '@nestjs/common';
import { TestingModuleBuilder } from '@nestjs/testing'; import { TestingModuleBuilder } from '@nestjs/testing';
import { AuthUserDto } from '../src/decorators/auth-user.decorator'; import { AuthUserDto } from '../src/decorators/auth-user.decorator';
import { JwtAuthGuard } from '../src/modules/immich-jwt/guards/jwt-auth.guard'; import { JwtAuthGuard } from '../src/modules/immich-jwt/guards/jwt-auth.guard';
import { databaseConfig } from '@app/database/config/database.config';
type CustomAuthCallback = () => AuthUserDto; type CustomAuthCallback = () => AuthUserDto;
export async function clearDb() { export async function clearDb(db: DataSource) {
const db = new DataSource(databaseConfig);
const entities = db.entityMetadatas; const entities = db.entityMetadatas;
for (const entity of entities) { for (const entity of entities) {
const repository = db.getRepository(entity.name); const repository = db.getRepository(entity.name);

View File

@ -9,6 +9,7 @@ import { ImmichJwtModule } from '../src/modules/immich-jwt/immich-jwt.module';
import { UserService } from '../src/api-v1/user/user.service'; import { UserService } from '../src/api-v1/user/user.service';
import { CreateUserDto } from '../src/api-v1/user/dto/create-user.dto'; import { CreateUserDto } from '../src/api-v1/user/dto/create-user.dto';
import { UserResponseDto } from '../src/api-v1/user/response-dto/user-response.dto'; import { UserResponseDto } from '../src/api-v1/user/response-dto/user-response.dto';
import { DataSource } from 'typeorm';
function _createUser(userService: UserService, data: CreateUserDto) { function _createUser(userService: UserService, data: CreateUserDto) {
return userService.createUser(data); return userService.createUser(data);
@ -16,9 +17,10 @@ function _createUser(userService: UserService, data: CreateUserDto) {
describe('User', () => { describe('User', () => {
let app: INestApplication; let app: INestApplication;
let database: DataSource;
afterAll(async () => { afterAll(async () => {
await clearDb(); await clearDb(database);
await app.close(); await app.close();
}); });
@ -29,6 +31,7 @@ describe('User', () => {
}).compile(); }).compile();
app = moduleFixture.createNestApplication(); app = moduleFixture.createNestApplication();
database = app.get(DataSource);
await app.init(); await app.init();
}); });
@ -54,6 +57,7 @@ describe('User', () => {
app = moduleFixture.createNestApplication(); app = moduleFixture.createNestApplication();
userService = app.get(UserService); userService = app.get(UserService);
database = app.get(DataSource);
await app.init(); await app.init();
}); });