elliotf / mocha-mongoose

test helpers for using mongodb with mocha
MIT License
107 stars 21 forks source link

Remove collection indexes when clearDB #6

Closed ericsaboia closed 11 years ago

elliotf commented 11 years ago

Note that this is related (or a duplicate) of #5.

elliotf commented 11 years ago

I agree that having to clear out old indexes is a problem, but in other databases, modifying the schema (which clearing the unique index is an example of) is not done by the testing framework, but instead by a database migration.

rspec, for example, will clear the contents of the database, but will not modify the schema. That would be up to the database migrations. I think that if you remove a unique index from your code, you should be responsible for removing it via a database migration of some sort, not the testing framework. You're going to need to do that anyway, because you don't run your specs against your production instance.

Consider the following scenario related to yours above, but adding a unique key instead of removing it:

  1. I create a model with a unique index
  2. I have ensure indexes turned on
  3. I start a spec run
  4. The model is initialized
  5. The unique index is created in MongoDB
  6. The beforeEach is triggered for the first spec
  7. The beforeEach clears the database and clears unique indexes
  8. The first test of the spec run executes
  9. The unique index is no longer in place, so tests may pass/fail incorrectly.

Does that make sense? The more I think about it, the more I am sure that clearing indexes should not be part of clearing the contents of the database.

I think that if you wrote a test for this, you would see the problem. For example, write a test that has a model with a unique index, and test to see if inserting a duplicate record will still fail after the database has been cleared. I am confident that the datastore will be in a bad state.

Nothing is re-adding indexes after they are cleared. Mongoose does that once when it starts.

ericsaboia commented 11 years ago

Yeah, you are right. Sorry about that!

elliotf commented 11 years ago

You have no need to apologize! This conversation helped me form a solid opinion about whether or not this should be added. Thank you for contributing!