test: Add test to prove that issue #2331 is resolved (#9688)

This commit is contained in:
Lukas Spiss 2023-01-28 11:07:55 +00:00 committed by GitHub
parent de84014509
commit b937ae4afd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 72 additions and 0 deletions

View File

@ -0,0 +1,13 @@
import { BaseEntity, Entity, Column, PrimaryColumn } from "../../../../src"
@Entity()
export class Post extends BaseEntity {
@PrimaryColumn()
id: number
@Column({ type: "varchar" })
title: string
@Column({ type: "varchar", nullable: true })
author: string | null
}

View File

@ -0,0 +1,59 @@
import "reflect-metadata"
import { expect } from "chai"
import { DataSource, Repository } from "../../../src"
import { Post } from "./entity/Post"
import {
reloadTestingDatabases,
closeTestingConnections,
setupSingleTestingConnection,
} from "../../utils/test-utils"
describe("github issues > #2331 undefined value is nulling column on update", () => {
let dataSource: DataSource
let repository: Repository<Post>
before(async () => {
const options = setupSingleTestingConnection("postgres", {
entities: [__dirname + "/entity/*{.js,.ts}"],
schemaCreate: true,
dropSchema: true,
})
if (!options) return
dataSource = new DataSource(options)
await dataSource.initialize()
})
beforeEach(async () => {
if (!dataSource) return
await reloadTestingDatabases([dataSource])
repository = dataSource.getRepository(Post)
})
after(() => closeTestingConnections([dataSource]))
it("should not overwrite column with null when passing undefined", async () => {
if (!dataSource) return
const post = new Post()
post.id = 1
post.title = "Some post"
post.author = "Some author"
await repository.save(post)
await repository.update(
{
id: post.id,
},
{
title: "Updated post",
author: undefined,
},
)
const postReloaded = await repository.findOne({
where: { id: post.id },
})
expect(postReloaded).to.exist
expect(postReloaded!.author).to.be.equal("Some author")
})
})