Skip to content

Commit e850040

Browse files
Robin BuschmannRobin Buschmann
authored andcommitted
tests + example added for #3
1 parent f7d0693 commit e850040

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

example/models/Book.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import {Author} from "./Author";
1010
@Table
1111
export default class Book extends Model<Book> {
1212

13-
@Column
13+
@Column({
14+
type: DataType.STRING(50)
15+
})
1416
title: string;
1517

1618
@BelongsToMany(() => Author, 'AuthorBook', 'bookId', 'authorId')

test/models/User.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ export class User extends Model<User> {
2323
@Column
2424
username: string;
2525

26+
@Column(DataType.STRING(5))
27+
username2: string;
28+
2629
@Column
2730
aNumber: number;
2831

test/specs/table_column.spec.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import {expect} from 'chai';
1+
import {expect, use} from 'chai';
2+
import * as chaiAsPromised from 'chai-as-promised';
23
import {DefineAttributes} from 'sequelize';
34
import {Model, Table, Column, DataType} from "../../index";
45
import {createSequelize} from "../utils/sequelize";
56
import {User} from "../models/User";
67
import {getOptions, getAttributes} from "../../lib/services/models";
78
import {Shoe, SHOE_TABLE_NAME} from "../models/Shoe";
9+
import * as _ from 'lodash';
10+
11+
use(chaiAsPromised);
812

913
/* tslint:disable:max-classes-per-file */
1014

@@ -29,6 +33,9 @@ describe('table_column', () => {
2933
username: {
3034
type: DataType.STRING
3135
},
36+
username2: {
37+
type: DataType.STRING(5)
38+
},
3239
aNumber: {
3340
type: DataType.INTEGER
3441
},
@@ -147,7 +154,7 @@ describe('table_column', () => {
147154

148155
try {
149156

150-
expect(attributes[key]).to.have.property(attrOptionKey, expectedUserAttributes[key][attrOptionKey]);
157+
expect(attributes[key]).to.have.property(attrOptionKey).that.eql(expectedUserAttributes[key][attrOptionKey]);
151158
} catch (e) {
152159

153160
e.message += ` for property "${key}"`;
@@ -197,6 +204,38 @@ describe('table_column', () => {
197204

198205
});
199206

207+
describe('column', () => {
208+
209+
it('should create appropriate sql query', () => {
210+
211+
const seq = createSequelize(false);
212+
213+
@Table
214+
class Bottle extends Model<Bottle> {
215+
216+
@Column(DataType.STRING(5))
217+
brand: string;
218+
219+
@Column(DataType.CHAR(2))
220+
key: string;
221+
222+
@Column(DataType.INTEGER(100))
223+
num: number;
224+
}
225+
226+
seq.addModels([Bottle]);
227+
228+
return Bottle.sync({
229+
force: true, logging: _.after(2, _.once((sql) => {
230+
231+
// tslint:disable:max-line-length
232+
expect(sql).to.match(/CREATE TABLE IF NOT EXISTS `Bottle` \(`id` INTEGER PRIMARY KEY AUTOINCREMENT, `brand` VARCHAR\(5\), `key` CHAR\(2\), `num` INTEGER\(100\)\)/);
233+
}))
234+
});
235+
});
236+
237+
});
238+
200239
describe('accessors', () => {
201240

202241
describe('get', () => {

0 commit comments

Comments
 (0)