I just read "Test Driven Development: By Example" by Kent Beck, and am trying to apply it on my current project.
Given an API response I need to extract information from it. API response is a JSON array:
[
{"1789484079": "event1", "1531059415": "event2"},
{},
{},
{"1234256612": "event3"}
]
Each object in that array may have multiple keys, each key is a timestamp. Object may be empty.
The task is to find a most recent "event" according to given timestamp. For example:
- if given timestamp is 1889484079 "event1" shall be returned;
- if given timestamp is 1689484079 an "event2" is a right choice.
So, following the book's guidance, a test and a naive implementation which returns a constant:
def parse_response(response, timestamp):
return "event1"
def test_api_response_parsing_returns_correct_events():
response = [{1789484079: "event1", 1531059415: "event2"}]
timestamp = 1889484079
assert parse_response(response, timestamp) == "event1"
Now I need to make the test fail, so I have room for improvements.
def test_api_response_parsing_returns_correct_events():
response = [{1789484079: "event1", 1531059415: "event2"}]
timestamp = 1889484079
assert parse_response(response, timestamp) == "event1"
timestamp = 1689484079
assert parse_response(response, timestamp) == "event2"
What could the next step have been if we want to keep moving by little steps, as Kent Beck suggests in his book?
Introduce 2 more constants?
def parse_response(response, timestamp):
if timestamp == 1889484079:
return "event1"
else:
return "event2"
This starts to seem like duplication, which we must get rid of. And how would we do it? My thought for the next step is that we have timestamps in response, so in order to remove the duplication we have to iterate over response while checking timestamps according to rules which function have to follow, but isn't it a big leap and not a small step?
How would you develop this function if you were following TDD principles?