django-tdd

Lookup the time in a city

The final feature we’d like to add is the ability to get the time in a given city / country. We can continue to use the same endpoint and provide city and country in the request.

You’ll need to add the Cities to your database by creating a model. Models are interesting to write with TDD, here’s a pattern that I like:

def test_can_create_person(self):
    person = Person.objects.create(
        name="Brenton Cleeland",
        country="AU",
        twitter="@sesh",
    )

The problem with that is that the test isn’t failing by rather the test is broken. We can fix that by catching that exception and providing a proper message using self.fail().

def test_can_create_person(self):
    try:
        person = Person.objects.create(
            name="Brenton Cleeland",
            country="AU",
            twitter="@sesh",
        )
    except:
        self.fail('Creating model caused an exception')

Now, on to the feature we’d like to add:

As a user I want to provide the name of a city and country and get the current time in that location


Again, here are the tests that I started with (spoiler alert!):

Can you think of some tests that are missing? What about some edge cases and error states? How would you use TDD to test validation of the country or timezone?