Skip to content

Commit

Permalink
Fix API response when there are no actions to return
Browse files Browse the repository at this point in the history
  • Loading branch information
stefankoegl committed Aug 15, 2018
1 parent b622f2f commit f334e3c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
8 changes: 6 additions & 2 deletions mygpo/api/advanced/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,12 @@ def get_episode_changes(user, podcast, device, since, until, aggregated, version
if aggregated:
actions = list(dict( (a['episode'], a) for a in actions ).values())

timestamp = history[-1].timestamp
return {'actions': actions, 'timestamp': get_timestamp(timestamp)}
if history:
ts = get_timestamp(history[-1].timestamp)
else:
ts = get_timestamp(until)

return {'actions': actions, 'timestamp': ts}


def episode_action_json(history, user):
Expand Down
25 changes: 25 additions & 0 deletions mygpo/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,28 @@ def test_limit_actions(self):
get_timestamp(timestamps[9]),
response_obj['timestamp']
)


def test_no_actions(self):
""" Test when there are no actions to return """

t1 = get_timestamp(datetime.utcnow())

url = reverse(episodes, kwargs={
'version': '2',
'username': self.user.username,
})
response = self.client.get(url, {'since': '0'}, **self.extra)
self.assertEqual(response.status_code, 200, response.content)
response_obj = json.loads(response.content.decode('utf-8'))
actions = response_obj['actions']

# 10 actions should be returned
self.assertEqual(len(actions), 0)

returned = response_obj['timestamp']
t2 = get_timestamp(datetime.utcnow())
# the `timestamp` field in the response should be the timestamp of the
# last returned action
self.assertGreaterEqual(returned, t1)
self.assertGreaterEqual(t2, returned)

0 comments on commit f334e3c

Please sign in to comment.