Skip to content

Commit

Permalink
test: verify that using nvar/char parameters to assign var/char colum…
Browse files Browse the repository at this point in the history
…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.

Validates typeorm#7932

test: verify that storing strings with characters that are too long 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 too-long characters

Validates typeorm#7932
  • Loading branch information
Ceshion committed Jul 22, 2021
1 parent 9212df4 commit 1b22bbe
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
22 changes: 22 additions & 0 deletions test/github-issues/7932/entity/Example.ts
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 = '';
}
56 changes: 56 additions & 0 deletions test/github-issues/7932/issue-7932.ts
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');
})));
});

0 comments on commit 1b22bbe

Please sign in to comment.