-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(repository): revert hasOne target FK as PK implementation
Remove attempt at guaranteeing the foreign key of a has one relation to be unique by using it as the primary key of the target model. This allow users to set up their own unique contstraints.
- Loading branch information
Showing
13 changed files
with
170 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
examples/todo-list/test/acceptance/todo-list-image.acceptance.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// Copyright IBM Corp. 2018. All Rights Reserved. | ||
// Node module: @loopback/example-todo-list | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import { | ||
Client, | ||
createRestAppClient, | ||
expect, | ||
givenHttpServerConfig, | ||
toJSON, | ||
} from '@loopback/testlab'; | ||
import {TodoListApplication} from '../../src/application'; | ||
import {TodoList, TodoListImage} from '../../src/models/'; | ||
import { | ||
TodoListRepository, | ||
TodoListImageRepository, | ||
} from '../../src/repositories/'; | ||
import {givenTodoListImage, givenTodoList} from '../helpers'; | ||
|
||
describe('TodoListApplication', () => { | ||
let app: TodoListApplication; | ||
let client: Client; | ||
let todoListImageRepo: TodoListImageRepository; | ||
let todoListRepo: TodoListRepository; | ||
|
||
let persistedTodoList: TodoList; | ||
|
||
before(givenRunningApplicationWithCustomConfiguration); | ||
after(() => app.stop()); | ||
|
||
before(givenTodoListImageRepository); | ||
before(givenTodoListRepository); | ||
before(() => { | ||
client = createRestAppClient(app); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await todoListImageRepo.deleteAll(); | ||
await todoListRepo.deleteAll(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
persistedTodoList = await givenTodoListInstance(); | ||
}); | ||
|
||
it('creates image for a todoList', async () => { | ||
const todoListImage = givenTodoListImage(); | ||
delete todoListImage.todoListId; | ||
const response = await client | ||
.post(`/todo-lists/${persistedTodoList.id}/image`) | ||
.send(todoListImage) | ||
.expect(200); | ||
|
||
const expected = {...todoListImage, todoListId: persistedTodoList.id}; | ||
expect(response.body).to.containEql(expected); | ||
|
||
const created = await todoListImageRepo.findById(response.body.id); | ||
expect(toJSON(created)).to.deepEqual({id: response.body.id, ...expected}); | ||
}); | ||
|
||
it('finds images for a todoList', async () => { | ||
const todoListImage = await givenTodoListImageInstanceOfTodoList( | ||
persistedTodoList.id, | ||
{ | ||
value: 'A picture of a green checkmark', | ||
}, | ||
); | ||
|
||
const response = await client | ||
.get(`/todo-lists/${persistedTodoList.id}/image`) | ||
.send() | ||
.expect(200); | ||
|
||
expect(response.body).to.containDeep(todoListImage); | ||
}); | ||
|
||
/* | ||
============================================================================ | ||
TEST HELPERS | ||
These functions help simplify setup of your test fixtures so that your tests | ||
can: | ||
- operate on a "clean" environment each time (a fresh in-memory database) | ||
- avoid polluting the test with large quantities of setup logic to keep | ||
them clear and easy to read | ||
- keep them DRY (who wants to write the same stuff over and over?) | ||
============================================================================ | ||
*/ | ||
|
||
async function givenRunningApplicationWithCustomConfiguration() { | ||
app = new TodoListApplication({ | ||
rest: givenHttpServerConfig(), | ||
}); | ||
|
||
await app.boot(); | ||
|
||
/** | ||
* Override default config for DataSource for testing so we don't write | ||
* test data to file when using the memory connector. | ||
*/ | ||
app.bind('datasources.config.db').to({ | ||
name: 'db', | ||
connector: 'memory', | ||
}); | ||
|
||
// Start Application | ||
await app.start(); | ||
} | ||
|
||
async function givenTodoListImageRepository() { | ||
todoListImageRepo = await app.getRepository(TodoListImageRepository); | ||
} | ||
|
||
async function givenTodoListRepository() { | ||
todoListRepo = await app.getRepository(TodoListRepository); | ||
} | ||
|
||
async function givenTodoListImageInstanceOfTodoList( | ||
id: typeof TodoList.prototype.id, | ||
todoListImage?: Partial<TodoListImage>, | ||
) { | ||
const data = givenTodoListImage(todoListImage); | ||
delete data.todoListId; | ||
return await todoListRepo.image(id).create(data); | ||
} | ||
|
||
async function givenTodoListInstance(todoList?: Partial<TodoList>) { | ||
return await todoListRepo.create(givenTodoList(todoList)); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.