forked from typeorm/typeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: verify that using nvar/char parameters to assign var/char colum…
…ns retains data add tests to ensure that using nchar and nvarchar parameters will correctly store data, and that the actual underlying data type has not changed. test: verify that storing strings with characters that are too wide for var/char throws ensure that we will fail loudly when an attempt is made to store a string that cannot be converted to the underlying type because it has characters that are not in the codepage used by the database/table/column Validates typeorm#7932
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { | ||
Entity, | ||
PrimaryGeneratedColumn, | ||
Column, | ||
CreateDateColumn | ||
} from '../../../../src'; | ||
|
||
@Entity() | ||
export class Example { | ||
|
||
@PrimaryGeneratedColumn('uuid') | ||
id?: string; | ||
|
||
@CreateDateColumn({ type: 'datetime' }) | ||
created?: Date; | ||
|
||
@Column('varchar', { length: 10 }) | ||
content: string = ''; | ||
|
||
@Column('char', { length: 10 }) | ||
fixedLengthContent: string = ''; | ||
} |
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,56 @@ | ||
import "reflect-metadata"; | ||
import { expect } from "chai"; | ||
import { Connection } from "../../../src"; | ||
import { closeTestingConnections, createTestingConnections, reloadTestingDatabases } from "../../utils/test-utils"; | ||
import { Example } from "./entity/Example"; | ||
|
||
describe("github issues > #7932 non-ascii characters assigned to var/char columns in SQL are truncated to one byte", () => { | ||
|
||
let connections: Connection[]; | ||
before(async () => { | ||
connections = await createTestingConnections({ | ||
enabledDrivers: ["mssql"], | ||
entities: [Example], | ||
schemaCreate: false, | ||
dropSchema: true | ||
}); | ||
}); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should store non-ascii characters in var/char without data loss", () => Promise.all(connections.map(async connection => { | ||
const repo = connection.getRepository(Example); | ||
|
||
const entity = new Example(); | ||
entity.content = '\u2021'; | ||
entity.fixedLengthContent = '\u2022'; | ||
|
||
await repo.save(entity); | ||
const savedEntity = | ||
await repo.findOne({ order: { created: 'DESC' } }); | ||
|
||
expect(savedEntity?.content).to.be.equal(entity.content); | ||
expect(savedEntity?.fixedLengthContent).to.be.equal('\u2022 '); | ||
}))); | ||
|
||
it("should throw an error if characters in a string are too long to store", () => Promise.all(connections.map(async connection => { | ||
const repo = connection.getRepository(Example); | ||
|
||
const entity = new Example(); | ||
entity.content = '💖'; | ||
entity.fixedLengthContent = '🏍'; | ||
|
||
expect(repo.save(entity)).to.eventually.be.rejectedWith(Error); | ||
}))); | ||
|
||
it("should not change char or varchar column types to nchar or nvarchar", () => Promise.all(connections.map(async connection => { | ||
const repo = connection.getRepository(Example); | ||
|
||
const columnMetadata = repo.metadata.ownColumns; | ||
const contentColumnType = columnMetadata.find(m => m.propertyName === 'content')?.type; | ||
const fixedLengthContentColumnType = columnMetadata.find(m => m.propertyName === 'fixedLengthContent')?.type; | ||
|
||
expect(contentColumnType).to.be.equal('varchar'); | ||
expect(fixedLengthContentColumnType).to.be.equal('char'); | ||
}))); | ||
}); |