def test_should_query_all_users(self): query_all_users = QueryAllUsers( InMemoryUsersRepository([bob(), maria()])) all_users = query_all_users.execute() assert 2 == len(all_users) assert maria() in all_users assert bob() in all_users
def test_should_get_a_list_of_posts_by_user_id(self): query = PostsByUserID(user_id=maria().ID) posts_by_user_id = QueryPostByUserID( QueryUserByID(InMemoryUsersRepository([maria()])), InMemoryPostsRepository([a_post_by_maria()])) post_list = posts_by_user_id.execute(query) assert 1 == len(post_list) assert a_post_by_maria() in post_list
def test_should_create_a_post(self): command = CreatePost(post_id=PostID(), user_id=maria().ID, text='', created_at=a_perfect_day_and_time()) post_creator = PostCreator(QueryUserByID(InMemoryUsersRepository([maria()]))) created_post, events = post_creator.execute(command) assert command.post_id == created_post.post_id assert command.user_id == created_post.user_id assert command.text == created_post.text assert command.created_at == created_post.created_at assert 1 == len(events) assert command.post_id == events[0].post_id assert command.user_id == events[0].user_id assert command.text == events[0].text assert command.created_at == events[0].created_at
def test_should_save_a_user_and_recover_by_user_id(self, user_repository: UsersRepository): a_user = maria() user_repository.save(a_user) user_from_repo = user_repository.by_id(a_user.ID) assert a_user == user_from_repo
def test_should_throw_an_exception_if_user_does_not_exist(self): query = PostsByUserID(user_id=inexistent_user_id()) posts_by_user_id = QueryPostByUserID( QueryUserByID(InMemoryUsersRepository([maria()])), InMemoryPostsRepository([a_post_by_maria()])) with self.assertRaises(UnknownUser): posts_by_user_id.execute(query)
def test_should_throw_an_exception_on_inexistent_user(self): command = CreatePost(post_id=PostID(), user_id=UserID('fc673127-c5b9-4128-9015-2bbc31163df1'), text='', created_at=a_perfect_day_and_time()) post_creator = PostCreator(QueryUserByID(InMemoryUsersRepository([maria()]))) with self.assertRaises(UnkownUserID): post_creator.execute(command)
def test_should_raise_an_exception_on_wrong_password(self): command = LoginUser(username=UserName('Maria'), password=Password('a_wrong_password')) user_login_by_username_and_password = UserLoginByUserNameAndPassword( QueryUserByUserName(InMemoryUsersRepository([maria()])), FakeClock(a_perfect_day_and_time())) with self.assertRaises(InvalidCredentials): user_login_by_username_and_password.execute(command)
def test_should_raise_an_exception_on_unkown_username(self): command = LoginUser(username=UserName('IDontExist'), password=Password('a_password')) user_login_by_username_and_password = UserLoginByUserNameAndPassword( QueryUserByUserName(InMemoryUsersRepository([maria()])), FakeClock(a_perfect_day_and_time())) with self.assertRaises(UnknownUser): user_login_by_username_and_password.execute(command)
def test_should_create_a_relationsip_from_two_valid_users(self): command = CreateRelationship(follower_id=bob().ID, followee_id=maria().ID) relationship_creator = RelationshipCreator(QueryUserByID(InMemoryUsersRepository([bob(), maria()])), FakeClock(a_perfect_day_and_time())) new_relationship, events = relationship_creator.execute(command) assert command.follower_id == new_relationship.follower_id assert command.followee_id == new_relationship.followee_id assert len(events) == 1 assert events[0].follower_id == command.follower_id assert events[0].followee_id == command.followee_id assert events[0].timestamp == a_perfect_day_and_time()
def test_should_throw_an_exception_if_user_does_not_exist(self): query = WallByUserID(user_id=inexistent_user_id()) posts_by_user_id = QueryWallByUserID( QueryUserByID(InMemoryUsersRepository([maria()])), InMemoryPostsRepository( [a_post_by_maria(), a_post_by_bob(), another_post_by_maria()]), QueryRelationshipsByFollowerID( InMemoryRelationshipRepository([bob_follows_maria()]))) with self.assertRaises(UnknownUser): posts_by_user_id.execute(query)
def test_should_get_a_list_of_posts_by_user_id(self): query = WallByUserID(user_id=bob().ID) posts_by_user_id = QueryWallByUserID( QueryUserByID(InMemoryUsersRepository([maria(), bob()])), InMemoryPostsRepository( [a_post_by_maria(), a_post_by_bob(), another_post_by_maria()]), QueryRelationshipsByFollowerID( InMemoryRelationshipRepository([bob_follows_maria()]))) post_list = posts_by_user_id.execute(query) assert 3 == len(post_list) assert a_post_by_maria() in post_list assert a_post_by_bob() in post_list assert another_post_by_maria() in post_list
def test_should_login_a_user(self): try: command = LoginUser(username=UserName('Maria'), password=Password('a_password')) user_login_by_username_and_password = UserLoginByUserNameAndPassword( QueryUserByUserName(InMemoryUsersRepository([maria()])), FakeClock(a_perfect_day_and_time())) entity, events = user_login_by_username_and_password.execute( command) assert entity is None assert len(events) == 1 assert events[0].username == command.username assert events[0].timestamp == a_perfect_day_and_time() except ValueError as err: self.fail("Unexpected exception raised: %s" % err)
def a_post_by_maria(): return Post(PostID('116c6055-86fe-4335-a332-5a586e995c12'), maria().ID, 'Hi! this is Maria', a_perfect_day_and_time())
def another_post_by_maria(): return Post(PostID('966ff2dd-e839-44c0-adc9-cfc771e3cf13'), maria().ID, 'I\'ve invented something!', a_perfect_day_and_time_ten_minutes_after())
def test_should_throw_an_exception_on_duplicate_username(self): user_registrator = UserRegistrator(QueryUserByUserName(InMemoryUsersRepository([maria()]))) with self.assertRaises(DuplicatedUserName): user_registrator.execute(create_maria())
def test_should_throw_an_exception_on_duplicate_username(self): command = CreateRelationship(follower_id=UserID(inexistent_user_id()), followee_id=maria().ID) relationship_creator = RelationshipCreator(QueryUserByID(InMemoryUsersRepository([bob(), maria()])), FakeClock(a_perfect_day_and_time())) with self.assertRaises(UnknownUser): relationship_creator.execute(command)