开发者

Unit Tests and Foreign Key troubles

Currently our DBA's design our databases without defining foreign keys, this is to let the dev team create unit tests for CRUD targeting each table individually.

We want to let the database team start using foreign keys in the actual DB, but we would also like to keep our ability to test each operation without having to create the parent rows.

Is there a way to disable foreign key constraints without having al开发者_如何学JAVAter table permissions?

How do you handle this situation where you are?


We require that every test sets up the environment it expects. This means that, if we must hit a DB, each test sets up it's environment first. We do use some helper methods to fill in common parts of the database, but we don't mess with the schema itself, since that is part of what is being tested in many cases.

We also make this a bit easier by using in-memory databases for unit testing so each test is even more independent.


   > ... we would also like to keep our ability to test each operation without 
   > having to create the parent rows.
   > Is there a way to disable foreign key constraints without having alter 
   > table permissions?

No, You cannot disable foreign key constraints but maybe you can have Dummy-Parent-Objects in the database with id "-1" that are used in you tests. The child-Items-parents then can referece the "-1" dummy items

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜