3

I am new to TDD. I find that the shoulda gem has the ability to test if a column exists for a database entity as well as the ability to test its indexes.

But is it necessary to include testing of columns and indexes in my test suite?

Will I need to be concerned with a potential deletion of any of the columns and indexes during development?

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
donkey
  • 4,285
  • 7
  • 42
  • 68

2 Answers2

3

Don't test database columns; that's just testing implementation. Don't test implementation, test behavior. Do test functionality that uses the attributes that are stored in those columns. Exception: if there are external requirements on your schema (this has never happened to me but I can imagine it), it might be helpful to write tests to show that those requirements are met.

Test indexes? It depends. If you're strictly test-driven, you may want to write a test for each index that you add purely for performance reasons. I'm pretty test-driven, but I don't write tests like this; I just create the indexes. The real test of the indexes is their effect on performance. In any case, there is no point in testing that indexes exist that have to exist anyway to support functionality that you're already testing (primary keys, foreign keys, unique constraints, etc.).

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
  • Do you test indexes performance because of client's requirements or other reasons? – donkey Apr 19 '16 at 18:27
  • There are lots of reasons to be concerned with performance, including client (or product manager) requirements, the need for an application to run efficiently in the data center, desire for one's test suite to run fast, and engineering standards. – Dave Schweisguth Apr 19 '16 at 18:43
0

I rarely test of columns and indexes. If you want to make sure your database migrations or constraint correct, you might include it. Production will be a logical copy of development. So if you really care about all potential risks, you might write the test case.

Linh Nguyen
  • 925
  • 1
  • 10
  • 23