TDD for DB integration

7,957 views

Published on

TDD style proved itself as very reliable and quick way of business tasks solving with code. But most of examples on trainings and in the internet show how to apply TDD to simple input/output code or interface based dependencies with mocking techniques. What about other areas of application development like database related code? Could it be developed with TDD style? What does TDD bring to developer? I will try to answer these questions in my talk and show on practical examples how helpful TDD is for database code, how it reduces risks and opens the door for refactoring techniques.

Published in: Technology

TDD for DB integration

  1. 1. TDD for DB related code Mikalai Alimenkou http://xpinjection.com @xpinjection
  2. 2. Data is very important
  3. 3. What to test, master? Everything!
  4. 4. DB testing checklist O/R mappings Data flow (incoming and outgoing data) Stored procedures, functions, triggers Database schema View definitions Referential integrity Default values for a column Data invariants for a single column Data invariants involving several columns Database performance
  5. 5. Are you crazy?
  6. 6. Lets talk about techniques
  7. 7. Small quiz: How to test DB code? Mock direct DB access API (JDBC in Java) Mock high level DB access API (ORM and data access frameworks) Mock DB server (SQL level) Test on real DB
  8. 8. Simplest testing technique Provide input data (params + DB state) Invoke tested behavior Assert returned value + DB state
  9. 9. Rollback transactions We rollback the uncommitted test transaction as part of the teardown.
  10. 10. Each test is a hero Each test knows what data is needed and prepares affected tables.
  11. 11. Constraints and validations Test suites Referential integrity Domain integrity Main suite Integration suite Validation suite Each test may manage constraints in runtime
  12. 12. Database structure management Incremental updates for all environments
  13. 13. Simple tools data sets management integration code and regular testing DB operations database management and versioning
  14. 14. TDD benefits No need to have checklists for DB related code Minimal amount of code and DB changes Full control under data access code Open the door for DB refactoring
  15. 15. Show me the code!
  16. 16. @xpinjection http://xpinjection.com mikalai.alimenkou@xpinjection.com

×