Skip to content

Commit

Permalink
Implement and test setup for feeds
Browse files Browse the repository at this point in the history
  • Loading branch information
danpalmer committed Jan 4, 2018
1 parent 12aeee9 commit f366c6c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion routemaster/feeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

def feeds_for_state_machine(state_machine) -> Dict[str, 'Feed']:
"""Get a mapping of feed prefixes to unfetched feeds."""
pass
return {x.name: Feed(x.url) for x in state_machine.feeds}


class FeedNotFetched(Exception):
Expand Down
71 changes: 71 additions & 0 deletions routemaster/tests/test_feeds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import mock
import pytest
import httpretty

from routemaster.feeds import Feed, FeedNotFetched, feeds_for_state_machine
from routemaster.config import Feed as FeedConfig
from routemaster.config import Gate, NoNextStates, StateMachine
from routemaster.exit_conditions import ExitConditionProgram


def test_feeds_for_state_machine():
state_machine = StateMachine(
name='example',
feeds=[
FeedConfig(name='test_feed', url='http://localhost/<label>'),
],
states=[
Gate(
name='start',
triggers=[],
next_states=NoNextStates(),
exit_condition=ExitConditionProgram('false'),
),
]
)

feeds = feeds_for_state_machine(state_machine)

assert 'test_feed' in feeds
assert feeds['test_feed'].data is None
assert feeds['test_feed'].url == 'http://localhost/<label>'


@httpretty.activate
def test_fetch_only_once():
httpretty.register_uri(
httpretty.GET,
'http://example.com/label1',
body='{"foo": "bar"}',
content_type='application/json',
)

feed = Feed('http://example.com/<label>')

with mock.patch('requests.Response.json') as json:
feed.fetch('label1')
feed.fetch('label1')
feed.fetch('label1')

assert json.call_count == 1


@httpretty.activate
def test_lookup():
httpretty.register_uri(
httpretty.GET,
'http://example.com/label1',
body='{"foo": "bar"}',
content_type='application/json',
)

feed = Feed('http://example.com/<label>')
feed.fetch('label1')

assert feed.lookup(('foo',)) == 'bar'


def test_lookup_fails_on_unfetched_feed():
feed = Feed('http://example.com/<label>')
with pytest.raises(FeedNotFetched):
feed.lookup(('foo',))

0 comments on commit f366c6c

Please sign in to comment.