不要怂,就是干,撸起袖子干!

Commit 851015a9 by Sascha Depold

added specs for string usage + invalid table name usage

1 parent 3da525fa
Showing with 107 additions and 22 deletions
...@@ -2043,35 +2043,120 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() { ...@@ -2043,35 +2043,120 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
describe('references', function() { describe('references', function() {
before(function() { before(function() {
this.Author = this.sequelize.define('author', { firstName: Sequelize.STRING }) this.Author = this.sequelize.define('author', { firstName: Sequelize.STRING })
this.Post = this.sequelize.define('post', { })
title: Sequelize.STRING,
authorId: { describe("use of existing dao factory", function() {
type: Sequelize.INTEGER, before(function() {
references: this.Author, this.Post = this.sequelize.define('post', {
referencesKey: "id" title: Sequelize.STRING,
} authorId: {
type: Sequelize.INTEGER,
references: this.Author,
referencesKey: "id"
}
})
this.Author.hasMany(this.Post)
this.Post.belongsTo(this.Author)
}) })
this.Author.hasMany(this.Post) it('references the author table', function(done) {
this.Post.belongsTo(this.Author) this.Author.sync({ force: true }).success(function() {
this.Post.sync({ force: true }).on('sql', function(sql) {
if (dialect === 'postgres') {
expect(sql).toMatch(/"authorId" INTEGER REFERENCES "authors" \("id"\)/)
} else if (dialect === 'mysql') {
expect(sql).toMatch(/FOREIGN KEY \(`authorId`\) REFERENCES `authors` \(`id`\)/)
} else if (dialect === 'sqlite') {
expect(sql).toMatch(/`authorId` INTEGER REFERENCES `authors` \(`id`\)/)
} else {
throw new Error('Undefined dialect!')
}
done()
})
}.bind(this))
})
}) })
it('references the author table', function(done) { describe('use of table name as string', function() {
this.Author.sync({ force: true }).success(function() { before(function() {
this.Post.sync({ force: true }).on('sql', function(sql) { this.Post = this.sequelize.define('post', {
if (dialect === 'postgres') { title: Sequelize.STRING,
expect(sql).toMatch(/"authorId" INTEGER REFERENCES "authors" \("id"\)/) authorId: {
} else if (dialect === 'mysql') { type: Sequelize.INTEGER,
expect(sql).toMatch(/FOREIGN KEY \(`authorId`\) REFERENCES `authors` \(`id`\)/) references: 'authors',
} else if (dialect === 'sqlite') { referencesKey: "id"
expect(sql).toMatch(/`authorId` INTEGER REFERENCES `authors` \(`id`\)/)
} else {
throw new Error('Undefined dialect!')
} }
})
done() this.Author.hasMany(this.Post)
this.Post.belongsTo(this.Author)
})
it('references the author table', function(done) {
this.Author.sync({ force: true }).success(function() {
this.Post.sync({ force: true }).on('sql', function(sql) {
if (dialect === 'postgres') {
expect(sql).toMatch(/"authorId" INTEGER REFERENCES "authors" \("id"\)/)
} else if (dialect === 'mysql') {
expect(sql).toMatch(/FOREIGN KEY \(`authorId`\) REFERENCES `authors` \(`id`\)/)
} else if (dialect === 'sqlite') {
expect(sql).toMatch(/`authorId` INTEGER REFERENCES `authors` \(`id`\)/)
} else {
throw new Error('Undefined dialect!')
}
done()
})
}.bind(this))
})
})
describe('use of invalid table name', function() {
before(function() {
this.Post = this.sequelize.define('post', {
title: Sequelize.STRING,
authorId: {
type: Sequelize.INTEGER,
references: '4uth0r5',
referencesKey: "id"
}
}) })
}.bind(this))
this.Author.hasMany(this.Post)
this.Post.belongsTo(this.Author)
})
it("emits the error event as the referenced table name is invalid", function(done) {
this.Author.sync({ force: true }).success(function() {
this.Post
.sync({ force: true })
.success(function() {
if (dialect === 'sqlite') {
// sorry ... but sqlite is too stupid to understand whats going on ...
expect(1).toEqual(1)
done()
} else {
// the parser should not end up here ...
expect(2).toEqual(1)
}
}).error(function(err) {
if (dialect === 'mysql') {
expect(err.message).toMatch(/ER_CANT_CREATE_TABLE/)
} else if (dialect === 'sqlite') {
// the parser should not end up here ... see above
expect(1).toEqual(2)
} else if (dialect === 'postgres') {
expect(err.message).toMatch(/relation "4uth0r5" does not exist/)
} else {
throw new Error('Undefined dialect!')
}
done()
})
}.bind(this))
})
}) })
}) //- describe: references }) //- describe: references
}) })
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!