From 0ad1a2345394db738435a0c99ee3c9bd555eb250 Mon Sep 17 00:00:00 2001 From: Nora Date: Fri, 21 Jun 2019 10:03:13 -0400 Subject: [PATCH] fixup! improve integration tests setup --- examples/todo-list/package.json | 1 + examples/todo-list/src/__tests__/helpers.ts | 28 ++++++++++++++++ .../todo-list-image.repository.integration.ts | 32 +++++++++++-------- .../todo-list.repository.integration.ts | 31 +++++++++--------- .../todo.repository.integration.ts | 31 +++++++++--------- 5 files changed, 79 insertions(+), 44 deletions(-) diff --git a/examples/todo-list/package.json b/examples/todo-list/package.json index 31ac987f0013..c81135c744c6 100644 --- a/examples/todo-list/package.json +++ b/examples/todo-list/package.json @@ -50,6 +50,7 @@ "@loopback/build": "^2.0.1", "@loopback/eslint-config": "^1.1.2", "@loopback/http-caching-proxy": "^1.1.3", + "@loopback/repository": "^1.8.0", "@loopback/testlab": "^1.6.1", "@types/lodash": "^4.14.134", "@types/node": "^10.14.9", diff --git a/examples/todo-list/src/__tests__/helpers.ts b/examples/todo-list/src/__tests__/helpers.ts index 29f003a0ea85..9b733d7424b4 100644 --- a/examples/todo-list/src/__tests__/helpers.ts +++ b/examples/todo-list/src/__tests__/helpers.ts @@ -3,6 +3,7 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +import {juggler} from '@loopback/repository'; import {givenHttpServerConfig} from '@loopback/testlab'; import {TodoListApplication} from '../application'; import {Todo, TodoList, TodoListImage} from '../models'; @@ -130,3 +131,30 @@ export async function givenTodoListImageInstance( ) { return await todoListImageRepo.create(givenTodoListImage(data)); } + +export async function givenEmptyDatabase() { + const todoRepo: TodoRepository = new TodoRepository( + testdb, + async () => todoListRepo, + ); + + const todoListRepo: TodoListRepository = new TodoListRepository( + testdb, + async () => todoRepo, + async () => todoListImageRepo, + ); + + const todoListImageRepo: TodoListImageRepository = new TodoListImageRepository( + testdb, + async () => todoListRepo, + ); + + await todoRepo.deleteAll(); + await todoListRepo.deleteAll(); + await todoListImageRepo.deleteAll(); +} + +export const testdb: juggler.DataSource = new juggler.DataSource({ + name: 'db', + connector: 'memory', +}); diff --git a/examples/todo-list/src/__tests__/integration/todo-list-image.repository.integration.ts b/examples/todo-list/src/__tests__/integration/todo-list-image.repository.integration.ts index 4e70a755268e..dfcfca861e8e 100644 --- a/examples/todo-list/src/__tests__/integration/todo-list-image.repository.integration.ts +++ b/examples/todo-list/src/__tests__/integration/todo-list-image.repository.integration.ts @@ -1,30 +1,34 @@ import {expect, toJSON} from '@loopback/testlab'; -import {TodoListApplication} from '../../application'; -import {TodoListImageRepository, TodoListRepository} from '../../repositories'; import { - givenRunningApplicationWithCustomConfiguration, + TodoListImageRepository, + TodoListRepository, + TodoRepository, +} from '../../repositories'; +import { + givenEmptyDatabase, givenTodoListImageInstance, givenTodoListInstance, - givenTodoListRepositories, + testdb, } from '../helpers'; describe('TodoListImageRepository', () => { - let app: TodoListApplication; let todoListImageRepo: TodoListImageRepository; let todoListRepo: TodoListRepository; + let todoRepo: TodoRepository; before(async () => { - app = await givenRunningApplicationWithCustomConfiguration(); + todoListRepo = new TodoListRepository( + testdb, + async () => todoRepo, + async () => todoListImageRepo, + ); + todoListImageRepo = new TodoListImageRepository( + testdb, + async () => todoListRepo, + ); }); - after(() => app.stop()); - before(async () => { - ({todoListRepo, todoListImageRepo} = await givenTodoListRepositories(app)); - }); - - beforeEach(async () => { - await todoListImageRepo.deleteAll(); - }); + beforeEach(givenEmptyDatabase); it('includes TodoList in find method result', async () => { const list = await givenTodoListInstance(todoListRepo); diff --git a/examples/todo-list/src/__tests__/integration/todo-list.repository.integration.ts b/examples/todo-list/src/__tests__/integration/todo-list.repository.integration.ts index 779fdc1198a0..471e06c13d23 100644 --- a/examples/todo-list/src/__tests__/integration/todo-list.repository.integration.ts +++ b/examples/todo-list/src/__tests__/integration/todo-list.repository.integration.ts @@ -1,30 +1,31 @@ import {expect, toJSON} from '@loopback/testlab'; -import {TodoListApplication} from '../../application'; -import {TodoListRepository, TodoRepository} from '../../repositories'; import { - givenRunningApplicationWithCustomConfiguration, + TodoListImageRepository, + TodoListRepository, + TodoRepository, +} from '../../repositories'; +import { + givenEmptyDatabase, givenTodoInstance, givenTodoListInstance, - givenTodoRepositories, + testdb, } from '../helpers'; describe('TodoListRepository', () => { - let app: TodoListApplication; - let todoRepo: TodoRepository; + let todoListImageRepo: TodoListImageRepository; let todoListRepo: TodoListRepository; + let todoRepo: TodoRepository; before(async () => { - app = await givenRunningApplicationWithCustomConfiguration(); - }); - after(() => app.stop()); - - before(async () => { - ({todoRepo, todoListRepo} = await givenTodoRepositories(app)); + todoListRepo = new TodoListRepository( + testdb, + async () => todoRepo, + async () => todoListImageRepo, + ); + todoRepo = new TodoRepository(testdb, async () => todoListRepo); }); - beforeEach(async () => { - await todoRepo.deleteAll(); - }); + beforeEach(givenEmptyDatabase); it('includes Todos in find method result', async () => { const list = await givenTodoListInstance(todoListRepo); diff --git a/examples/todo-list/src/__tests__/integration/todo.repository.integration.ts b/examples/todo-list/src/__tests__/integration/todo.repository.integration.ts index c0c7425662c8..ca16f4965d0a 100644 --- a/examples/todo-list/src/__tests__/integration/todo.repository.integration.ts +++ b/examples/todo-list/src/__tests__/integration/todo.repository.integration.ts @@ -1,30 +1,31 @@ import {expect, toJSON} from '@loopback/testlab'; -import {TodoListApplication} from '../../application'; -import {TodoListRepository, TodoRepository} from '../../repositories'; import { - givenRunningApplicationWithCustomConfiguration, + TodoListImageRepository, + TodoListRepository, + TodoRepository, +} from '../../repositories'; +import { + givenEmptyDatabase, givenTodoInstance, givenTodoListInstance, - givenTodoRepositories, + testdb, } from '../helpers'; describe('TodoRepository', () => { - let app: TodoListApplication; - let todoRepo: TodoRepository; + let todoListImageRepo: TodoListImageRepository; let todoListRepo: TodoListRepository; + let todoRepo: TodoRepository; before(async () => { - app = await givenRunningApplicationWithCustomConfiguration(); - }); - after(() => app.stop()); - - before(async () => { - ({todoRepo, todoListRepo} = await givenTodoRepositories(app)); + todoListRepo = new TodoListRepository( + testdb, + async () => todoRepo, + async () => todoListImageRepo, + ); + todoRepo = new TodoRepository(testdb, async () => todoListRepo); }); - beforeEach(async () => { - await todoRepo.deleteAll(); - }); + beforeEach(givenEmptyDatabase); it('includes TodoList in find method result', async () => { const list = await givenTodoListInstance(todoListRepo);