Skip to content

Commit

Permalink
Completed first scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
tomphp committed Mar 5, 2019
1 parent c704e11 commit e742d17
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 22 deletions.
6 changes: 6 additions & 0 deletions features/environment.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
from flitter.flitter import Flitter
from flitter.in_memory_message_store import InMemoryMessageStore


def before_scenario(context, step):
context.errorMessage = 'Unimplemented step. ' + \
'Please provide the implementation.'

context.app = Flitter(message_store=InMemoryMessageStore())
7 changes: 4 additions & 3 deletions features/steps/message_feed_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

@given('{author} has posted a message "{message}"')
def post_message(context, author, message):
raise NotImplementedError(context.errorMessage)
context.app.post(author=author, message=message)


@given('{follower} follows {followee}')
Expand All @@ -18,12 +18,13 @@ def do_not_follow(context, follower, followee):

@when('{user} views their feed')
def view_feed(context, user):
raise NotImplementedError(context.errorMessage)
context.feed = context.app.get_feed_for(user)


@then('they can see the message "{message}" by {author}')
def assert_can_see_message(context, message, author):
raise NotImplementedError(context.errorMessage)
expected = dict(author=author, message=message)
assert expected in context.feed, f'{expected} is not in {context.feed}'


@then('they cannot see the message "{message}" by {author}')
Expand Down
31 changes: 12 additions & 19 deletions flitter/flitter.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
from flitter.message import Message


class Flitter:
def post(self, author, message):
"""
Post a message
def __init__(self, message_store):
self.message_store = message_store

:param author: The user name of the author
:type author: string
:param message: The message to be posted
:type message: string
:return: nothing
:rtype: void
"""
pass
def post(self, author, message):
self.message_store.save(Message(author=author, text=message))

def get_feed_for(self, user):
"""
Get messages in a users feed.
feed = []

:param user: The user to get messages for
:type user: string
:return: All the message as a list of dicts containing author and message
:rtype: list(dict(author=string, message=string))
"""
pass
for message in self.message_store.fetch_by_author(user):
feed.append(dict(author=message.author, message=message.text))

return feed

def follow(self, follower, followee):
"""
Expand Down
9 changes: 9 additions & 0 deletions flitter/in_memory_message_store.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class InMemoryMessageStore:
def __init__(self):
self.messages = []

def save(self, message):
self.messages.append(message)

def fetch_by_author(self, author):
return self.messages
7 changes: 7 additions & 0 deletions flitter/message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Message:
def __init__(self, author, text):
self.author = author
self.text = text

def __repr__(self):
return f'Message(author={self.author}, text={self.text})'
22 changes: 22 additions & 0 deletions tests/flitter/test_in_memory_message_store.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from flitter.in_memory_message_store import InMemoryMessageStore
from flitter.message import Message


def test_fetch_by_author_returns_a_list():
store = InMemoryMessageStore()

assert store.fetch_by_author('alice') == []


def test_fetch_by_author_returns_messages_by_the_author():
message1 = Message(author='alice', text='hi1')
message2 = Message(author='alice', text='hi2')
store = InMemoryMessageStore()

store.save(message1)
store.save(message2)

messages = store.fetch_by_author('alice')

assert message1 in messages, f'{message1} was not in {messages}'
assert message2 in messages, f'{message2} was not in {messages}'

0 comments on commit e742d17

Please sign in to comment.