working on sql data types tests;

This commit is contained in:
Zotov Dmitry 2017-06-21 17:55:46 +05:00
parent d0ab9416a0
commit 96d6125cc5
7 changed files with 239 additions and 19 deletions

View File

@ -283,6 +283,8 @@ export class MysqlQueryRunner implements QueryRunner {
columnSchema.isUnique = dbColumn["COLUMN_KEY"].indexOf("UNI") !== -1;
columnSchema.isGenerated = dbColumn["EXTRA"].indexOf("auto_increment") !== -1;
columnSchema.comment = dbColumn["COLUMN_COMMENT"];
columnSchema.precision = dbColumn["NUMERIC_PRECISION"];
columnSchema.scale = dbColumn["NUMERIC_SCALE"];
return columnSchema;
});
@ -677,6 +679,8 @@ export class MysqlQueryRunner implements QueryRunner {
*/
protected buildCreateColumnSql(column: ColumnSchema, skipPrimary: boolean) {
let c = "`" + column.name + "` " + column.type;
if (column.enum)
c += "(" + column.enum.map(value => "'" + value + "'").join(", ") + ")";
if (column.isNullable !== true)
c += " NOT NULL";
if (column.isUnique === true)

View File

@ -28,8 +28,7 @@ export type WithLengthColumnType = "int" // mysql, postgres, mssql, oracle, sqli
|"char" // mysql, postgres, mssql, oracle
|"nchar" // mssql, oracle, sqlite
|"varchar2" // oracle
|"nvarchar2" // oracle, sqlite
|"year"; // mysql
|"nvarchar2"; // oracle, sqlite
/**
* All other regular column types.
@ -97,6 +96,7 @@ export type SimpleColumnType =
|"interval year" // oracle
|"interval day" // oracle
|"interval" // postgres
|"year" // mysql
// geometric types
|"point" // postgres

View File

@ -49,6 +49,23 @@ export class ColumnSchema {
*/
comment: string|undefined;
/**
* The precision for a decimal (exact numeric) column (applies only for decimal column), which is the maximum
* number of digits that are stored for the values.
*/
precision: number|undefined;
/**
* The scale for a decimal (exact numeric) column (applies only for decimal column), which represents the number
* of digits to the right of the decimal point and must not be greater than precision.
*/
scale: number|undefined;
/**
* Array of possible enumerated values.
*/
enum?: any[];
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
@ -61,7 +78,8 @@ export class ColumnSchema {
isGenerated?: boolean,
isPrimary?: boolean,
isUnique?: boolean,
comment?: string
comment?: string,
enum?: any[]
}) {
if (options) {
this.name = options.name || "";
@ -72,6 +90,7 @@ export class ColumnSchema {
this.isPrimary = options.isPrimary || false;
this.isUnique = options.isUnique || false;
this.comment = options.comment;
this.enum = options.enum;
}
}
@ -112,6 +131,7 @@ export class ColumnSchema {
columnSchema.type = normalizedType;
columnSchema.isPrimary = columnMetadata.isPrimary;
columnSchema.isUnique = columnMetadata.isUnique;
columnSchema.enum = columnMetadata.enum;
return columnSchema;
}

View File

@ -3,7 +3,6 @@ import {IndexSchema} from "./IndexSchema";
import {ForeignKeySchema} from "./ForeignKeySchema";
import {PrimaryKeySchema} from "./PrimaryKeySchema";
import {ColumnMetadata} from "../../metadata/ColumnMetadata";
import {QueryRunner} from "../../query-runner/QueryRunner";
import {ObjectLiteral} from "../../common/ObjectLiteral";
import {EntityMetadata} from "../../metadata/EntityMetadata";
import {Driver} from "../../driver/Driver";
@ -228,6 +227,10 @@ export class TableSchema {
});
}
findColumnByName(name: string): ColumnSchema|undefined {
return this.columns.find(column => column.name === name);
}
// -------------------------------------------------------------------------
// Protected Methods
// -------------------------------------------------------------------------

View File

@ -1,25 +1,171 @@
import "reflect-metadata";
import {expect} from "chai";
import {Post} from "./entity/Post";
import {Connection} from "../../../../../src/connection/Connection";
import {closeTestingConnections, createTestingConnections} from "../../../../utils/test-utils";
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../../../utils/test-utils";
import {PostWithOptions} from "./entity/PostWithOptions";
// todo: finish those tests
describe.skip("database schema > column types > mysql", () => {
describe("database schema > column types > mysql", () => {
let connections: Connection[];
before(async () => {
connections = await createTestingConnections({
entities: [Post],
enabledDrivers: ["mysql"]
entities: [__dirname + "/entity/*{.js,.ts}"],
enabledDrivers: ["mysql"],
schemaCreate: true,
});
});
// beforeEach(() => reloadTestingDatabases(connections));
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));
it("all types should work correctly - persist and hydrate", () => Promise.all(connections.map(async connection => {
const postRepository = connection.getRepository(Post);
const queryRunner = connection.createQueryRunner();
const tableSchema = await queryRunner.loadTableSchema("post");
await queryRunner.release();
const post = new Post();
post.id = "1";
post.name = "Post";
post.int = 2147483647;
post.tinyint = 127;
post.smallint = 32767;
post.mediumint = 8388607;
post.bigint = 8223372036854775807;
post.float = 10.53;
post.double = 10.1234;
post.decimal = 50;
post.date = "2017-06-21";
post.datetime = new Date();
post.datetime.setMilliseconds(0);
post.timestamp = new Date();
post.timestamp.setMilliseconds(0);
post.time = "15:30:00";
post.year = 2017;
post.char = "A";
post.varchar = "This is varchar";
post.blob = new Buffer("This is blob");
post.text = "This is text";
post.tinyblob = new Buffer("This is tinyblob");
post.tinytext = "This is tinytext";
post.mediumblob = new Buffer("This is mediumblob");
post.mediumtext = "This is mediumtext";
post.longblob = new Buffer("This is longblob");
post.longtext = "This is longtext";
post.enum = "A";
post.simpleArray = ["A", "B", "C"];
await postRepository.save(post);
const loadedPost = (await postRepository.findOneById(1))!;
loadedPost.id.should.be.equal(post.id);
loadedPost.name.should.be.equal(post.name);
loadedPost.int.should.be.equal(post.int);
loadedPost.tinyint.should.be.equal(post.tinyint);
loadedPost.smallint.should.be.equal(post.smallint);
loadedPost.mediumint.should.be.equal(post.mediumint);
loadedPost.bigint.should.be.equal(post.bigint);
loadedPost.float.should.be.equal(post.float);
loadedPost.double.should.be.equal(post.double);
loadedPost.decimal.should.be.equal(post.decimal);
loadedPost.date.should.be.equal(post.date);
loadedPost.datetime.getTime().should.be.equal(post.datetime.getTime());
loadedPost.timestamp.getTime().should.be.equal(post.timestamp.getTime());
loadedPost.time.should.be.equal(post.time);
loadedPost.year.should.be.equal(post.year);
loadedPost.char.should.be.equal(post.char);
loadedPost.varchar.should.be.equal(post.varchar);
loadedPost.blob.toString().should.be.equal(post.blob.toString());
loadedPost.text.should.be.equal(post.text);
loadedPost.tinyblob.toString().should.be.equal(post.tinyblob.toString());
loadedPost.tinytext.should.be.equal(post.tinytext);
loadedPost.mediumblob.toString().should.be.equal(post.mediumblob.toString());
loadedPost.mediumtext.should.be.equal(post.mediumtext);
loadedPost.longblob.toString().should.be.equal(post.longblob.toString());
loadedPost.longtext.should.be.equal(post.longtext);
loadedPost.enum.should.be.equal(post.enum);
loadedPost.simpleArray[0].should.be.equal(post.simpleArray[0]);
loadedPost.simpleArray[1].should.be.equal(post.simpleArray[1]);
loadedPost.simpleArray[2].should.be.equal(post.simpleArray[2]);
tableSchema!.findColumnByName("id")!.type.should.be.equal("varchar(255)");
tableSchema!.findColumnByName("name")!.type.should.be.equal("varchar(255)");
tableSchema!.findColumnByName("int")!.type.should.be.equal("int(11)");
tableSchema!.findColumnByName("tinyint")!.type.should.be.equal("tinyint(4)");
tableSchema!.findColumnByName("smallint")!.type.should.be.equal("smallint(5)");
tableSchema!.findColumnByName("mediumint")!.type.should.be.equal("mediumint(9)");
tableSchema!.findColumnByName("bigint")!.type.should.be.equal("bigint(20)");
tableSchema!.findColumnByName("float")!.type.should.be.equal("float");
tableSchema!.findColumnByName("double")!.type.should.be.equal("double");
tableSchema!.findColumnByName("decimal")!.type.should.be.equal("decimal(10,0)");
tableSchema!.findColumnByName("date")!.type.should.be.equal("date");
tableSchema!.findColumnByName("datetime")!.type.should.be.equal("datetime");
tableSchema!.findColumnByName("timestamp")!.type.should.be.equal("timestamp");
tableSchema!.findColumnByName("time")!.type.should.be.equal("time");
tableSchema!.findColumnByName("year")!.type.should.be.equal("year(4)");
tableSchema!.findColumnByName("char")!.type.should.be.equal("char(1)");
tableSchema!.findColumnByName("varchar")!.type.should.be.equal("varchar(255)");
tableSchema!.findColumnByName("blob")!.type.should.be.equal("blob");
tableSchema!.findColumnByName("text")!.type.should.be.equal("text");
tableSchema!.findColumnByName("tinyblob")!.type.should.be.equal("tinyblob");
tableSchema!.findColumnByName("tinytext")!.type.should.be.equal("tinytext");
tableSchema!.findColumnByName("mediumblob")!.type.should.be.equal("mediumblob");
tableSchema!.findColumnByName("mediumtext")!.type.should.be.equal("mediumtext");
tableSchema!.findColumnByName("longblob")!.type.should.be.equal("longblob");
tableSchema!.findColumnByName("longtext")!.type.should.be.equal("longtext");
tableSchema!.findColumnByName("enum")!.type.should.be.equal("enum(\'a\',\'b\',\'c\')");
tableSchema!.findColumnByName("simpleArray")!.type.should.be.equal("text");
})));
it("all types should work correctly - persist and hydrate when options are specified on columns", () => Promise.all(connections.map(async connection => {
const postRepository = connection.getRepository(PostWithOptions);
const queryRunner = connection.createQueryRunner();
const tableSchema = await queryRunner.loadTableSchema("post_with_options");
await queryRunner.release();
const post = new PostWithOptions();
post.id = "1";
post.name = "Post";
post.int = 2147483647;
post.tinyint = 127;
post.smallint = 32767;
post.mediumint = 8388607;
post.bigint = 8223372036854775807;
post.float = 10.53;
post.double = 10.12;
post.decimal = 50;
post.char = "A";
post.varchar = "This is varchar";
await postRepository.save(post);
const loadedPost = (await postRepository.findOneById(1))!;
loadedPost.id.should.be.equal(post.id);
loadedPost.name.should.be.equal(post.name);
loadedPost.int.should.be.equal(post.int);
loadedPost.tinyint.should.be.equal(post.tinyint);
loadedPost.smallint.should.be.equal(post.smallint);
loadedPost.mediumint.should.be.equal(post.mediumint);
loadedPost.bigint.should.be.equal(post.bigint);
loadedPost.float.should.be.equal(post.float);
loadedPost.double.should.be.equal(post.double);
loadedPost.decimal.should.be.equal(post.decimal);
loadedPost.char.should.be.equal(post.char);
loadedPost.varchar.should.be.equal(post.varchar);
tableSchema!.findColumnByName("id")!.type.should.be.equal("varchar(255)");
tableSchema!.findColumnByName("name")!.type.should.be.equal("varchar(10)");
tableSchema!.findColumnByName("int")!.type.should.be.equal("int(3)");
tableSchema!.findColumnByName("tinyint")!.type.should.be.equal("tinyint(3)");
tableSchema!.findColumnByName("smallint")!.type.should.be.equal("smallint(3)");
tableSchema!.findColumnByName("mediumint")!.type.should.be.equal("mediumint(3)");
tableSchema!.findColumnByName("bigint")!.type.should.be.equal("bigint(3)");
tableSchema!.findColumnByName("float")!.type.should.be.equal("float(5,2)");
tableSchema!.findColumnByName("double")!.type.should.be.equal("double(5,2)");
tableSchema!.findColumnByName("decimal")!.type.should.be.equal("decimal(5,2)");
tableSchema!.findColumnByName("char")!.type.should.be.equal("char(5)");
tableSchema!.findColumnByName("varchar")!.type.should.be.equal("varchar(30)");
})));
});

View File

@ -42,7 +42,7 @@ export class Post {
datetime: Date;
@Column("timestamp")
timestamp: number;
timestamp: Date;
@Column("time")
time: string;
@ -57,30 +57,33 @@ export class Post {
varchar: string;
@Column("blob")
blob: string;
blob: Buffer;
@Column("text")
text: string;
@Column("tinyblob")
tinyblob: string;
tinyblob: Buffer;
@Column("tinytext")
tinytext: string;
@Column("mediumblob")
mediumblob: string;
mediumblob: Buffer;
@Column("mediumtext")
mediumtext: string;
@Column("longblob")
longblob: string;
longblob: Buffer;
@Column("longtext")
longtext: string;
@Column("enum")
enum: string[];
@Column("enum", { enum: ["A", "B", "C"] })
enum: string;
@Column("simple-array")
simpleArray: string[];
}

View File

@ -0,0 +1,44 @@
import {Entity} from "../../../../../../src/decorator/entity/Entity";
import {PrimaryColumn} from "../../../../../../src/decorator/columns/PrimaryColumn";
import {Column} from "../../../../../../src/decorator/columns/Column";
@Entity()
export class PostWithOptions {
@PrimaryColumn()
id: string;
@Column({ length: 10 })
name: string;
@Column("int", { length: 3 })
int: number;
@Column("tinyint", { length: 3 })
tinyint: number;
@Column("smallint", { length: 3 })
smallint: number;
@Column("mediumint", { length: 3 })
mediumint: number;
@Column("bigint", { length: 3 })
bigint: number;
@Column("float", { precision: 5, scale: 2 })
float: number;
@Column("double", { precision: 5, scale: 2 })
double: number;
@Column("decimal", { precision: 5, scale: 2 })
decimal: number;
@Column("char", { length: 5 })
char: string;
@Column("varchar", { length: 30 })
varchar: string;
}