Skip to content

Commit

Permalink
Add test case for the update
Browse files Browse the repository at this point in the history
* Attempt to reproduce the issue

* Attempt to reproduce the issue

* Attempt to reproduce the issue

* Attempt to reproduce the issue

* Test with fixed typeorm

* Test with fixed typeorm

* Test with fixed typeorm

* Merged master

* Checkout master of typeorm for the functional tests

* Checkout master of typeorm for the functional tests

* Checkout master of typeorm for the functional tests

* Checkout master of typeorm for the functional tests

* Checkout master of typeorm for the functional tests

* Checkout master of typeorm for the functional tests
  • Loading branch information
ArsenyYankovsky authored Oct 13, 2019
1 parent 59d7225 commit d1e3e3d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 154 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
command: npm link
- run:
name: checkout typeorm
command: git clone --single-branch --branch fix-update https://github.com/ArsenyYankovsky/typeorm.git
command: git clone --single-branch --branch master https://github.com/typeorm/typeorm.git
- run:
name: build typeorm with current package
command: cd typeorm/ && npm i && npm run package && cd build/package && npm link && rm -rf node_modules/typeorm-aurora-data-api-driver && npm link typeorm-aurora-data-api-driver
Expand Down
95 changes: 64 additions & 31 deletions test/functional/basic/simple-queries.func.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,38 @@ describe('aurora data api > simple queries', () => {
})
})

it('should be able to update a post', async () => {
await useCleanDatabase({ entities: [Post, Category] }, async (connection) => {

const postRepository = connection.getRepository(Post)

const post = new Post()

post.title = 'My First Post'
post.text = 'Post Text'
post.likesCount = 4
post.publishedAt = new Date(2017, 1, 1)

const insertResult = await postRepository.save(post)

const postId = insertResult.id

const dbPost = await postRepository.findOne({ id: postId })

dbPost!.publishedAt = new Date()

await postRepository.save(dbPost!)

const updatedPost = await postRepository.findOne(postId)

expect(updatedPost!.publishedAt > new Date(2017, 1, 1)).toBeTruthy()

expect(dbPost!.title).toBe('My First Post')
expect(dbPost!.text).toBe('Post Text')
expect(dbPost!.likesCount).toBe(4)
})
})

it('should be able to handle dates and multiple inserts', async () => {
await useCleanDatabase({ entities: [Post, Category] }, async (connection) => {
const postRepository = connection.getRepository(Post)
Expand Down Expand Up @@ -73,22 +105,22 @@ describe('aurora data api > simple queries', () => {

it('should be able to create and query a many-to-many relationship', async () => {
await useCleanDatabase({ entities: [Post, Category] }, async (connection) => {
// Create categories
// Create categories
const categoryRepository = connection.getRepository(Category)

const firstCategory = await categoryRepository.save(
categoryRepository.create({
name: 'first',
}),
)
categoryRepository.create({
name: 'first',
}),
)

const secondCategory = await categoryRepository.save(
categoryRepository.create({
name: 'second',
}),
)
categoryRepository.create({
name: 'second',
}),
)

// Create a post and associate with created categories
// Create a post and associate with created categories
const postRepository = connection.getRepository(Post)

const post = postRepository.create({
Expand All @@ -101,9 +133,9 @@ describe('aurora data api > simple queries', () => {

const storedPost = await postRepository.save(post)

// Assert
// Assert
const dbPost = await postRepository.findOne(
storedPost.id, { relations: ['categories'] })
storedPost.id, { relations: ['categories'] })

expect(dbPost).toBeTruthy()
expect(dbPost!.categories).toBeTruthy()
Expand All @@ -113,30 +145,31 @@ describe('aurora data api > simple queries', () => {

it('should be able to update a date field by primary key', async () => {
await useCleanDatabase({ entities: [Post, Category] }, async (connection) => {
// Create a post and associate with created categories
// Create a post and associate with created categories
const postRepository = connection.getRepository(Post)

const storedPost = await postRepository.save(
postRepository.create({
title: 'Post For Update',
text: 'Text',
likesCount: 6,
publishedAt: new Date(),
}),
)

// Retrieve the post and update the date
postRepository.create({
title: 'Post For Update',
text: 'Text',
likesCount: 6,
publishedAt: new Date(),
}),
)

// Retrieve the post and update the date
const getPost = await postRepository.findOne(storedPost.id)
expect(getPost).toBeTruthy()
expect(getPost!.updatedAt).toBeFalsy()

const updatedAt = new Date()
getPost!.updatedAt = updatedAt
await postRepository.save(getPost!)

// Assert
// Assert
const dbPost = await postRepository.findOne(storedPost.id)
expect(dbPost).toBeTruthy()
expect(Math.round(dbPost!.updatedAt!.getTime() / 1000)).toEqual(Math.round(updatedAt.getTime() / 1000))
expect(Math.trunc(dbPost!.updatedAt!.getTime() / 1000)).toEqual(Math.trunc(updatedAt.getTime() / 1000))
})
})

Expand All @@ -146,17 +179,17 @@ describe('aurora data api > simple queries', () => {
const newCategories = categoryNames.map(name => ({ name }))

await connection.createQueryBuilder()
.insert()
.into(Category)
.values(newCategories)
.orIgnore()
.execute()
.insert()
.into(Category)
.values(newCategories)
.orIgnore()
.execute()

// Query back the inserted categories
// Query back the inserted categories
const categoryRepository = connection.getRepository(Category)
const categories = await categoryRepository.find()

// Assert
// Assert
expect(categories.length).toBe(4)
expect(categories[0].name = 'one')
expect(categories[1].name = 'two')
Expand Down
Loading

0 comments on commit d1e3e3d

Please sign in to comment.