[Answered ]-How do I use TDD to create a database representation of existing objects?

2👍

First, ask yourself if you will be testing your code or the Django ORM. Storing and reading will be fine most of the time.

The things you will need to test are validating the data and any properties that are not model fields. I think you should end up with a good schema through writing tests on the next layer up from the database.

Also, use South or some other means of migration to reduce the cost of schema change. That will give you some peace of mind.

If you have several levels of tests (eg. integration tests), it makes sense to check that the database configuration is intact. Most of the tests do not need to hit the database. You can do this by mocking the model or some database operations (at least save()). Having said that, you can check database writes and reads with this simple test:

def test_db_access(self):
    input = Element(title = 'foo')
    input.save()

    output = Element.objects.get(title='foo')

    self.assertEquals(input, output)

Mocking save:

def save(obj):
    obj.id = 1
👤ferrix

Leave a comment