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.

Tests typeorm#7933
  • Loading branch information
Ceshion committed Jul 19, 2021
1 parent 73816ff commit a32b2d9
Show file tree
Hide file tree
Showing 2 changed files with 68 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 = '';
}
46 changes: 46 additions & 0 deletions test/github-issues/7932/issue-7932.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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(entity.fixedLengthContent);
})));

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 === 'content')?.type;

expect(contentColumnType).to.be.equal('varchar');
expect(fixedLengthContentColumnType).to.be.equal('char');
})));
});

0 comments on commit a32b2d9

Please sign in to comment.