def test_create_from_entity(self):
        star_entity = self.fixture_with_new_uuid('star')
        new_star = yield StarStore().create_from_entity(star_entity)
        self.assertEquals(new_star.name, star_entity.name)

        star = yield StarStore().get(new_star.uuid)
        self.assertEquals(star.name, star_entity.name)
class StarService(BaseService):
    def __init__(self):
        super(StarService, self).__init__()
        self.store = StarStore()

    @coroutine
    def get_by_name(self, star_name):
        log_info = dict(
            name=star_name,
            method='get_by_name',
        )

        star = yield self.store.get_by_name(star_name)
        if not star:
            log_info.update({'result': 'star not found'})
            logger.info(log_info)
            raise Return(None)

        logger.info(log_info)
        raise Return(star)

    @coroutine
    def check_duplicates(self, entity):
        star = yield self.get_by_name(entity.name)
        raise Return(star is not None)
    def test_update_not_exists_entity(self, mock_get):
        mock_get.return_value = None

        fake_uuid = 'c736b780-11b6-4190-8529-4d89504b76a0'

        with self.assertRaises(ResourceNotFoundError):
            yield StarStore().update(fake_uuid, {})

        mock_get.assert_called_once_with(fake_uuid)
 def __init__(self):
     super(UserService, self).__init__()
     self.store = UserStore()
     self.title_store = TitleStore()
     self.star_store = StarStore()
class UserService(BaseService):
    def __init__(self):
        super(UserService, self).__init__()
        self.store = UserStore()
        self.title_store = TitleStore()
        self.star_store = StarStore()

    @coroutine
    def get_by_name(self, user_name):
        user = yield self.store.get_by_name(user_name)

        logger.info(dict(
            user_name=user_name,
            method='get_by_name',
            result_uuid=None if not user else user.uuid,
        ))
        raise Return(user)

    @coroutine
    def check_duplicates(self, entity):
        user = yield self.get_by_name(entity.user_name)
        raise Return(user is not None)

    @coroutine
    def like_title(self, user_uuid, title_uuid, like):
        user_uuid = is_valid_uuid_string(user_uuid)
        title_uuid = is_valid_uuid_string(title_uuid)

        update_dict = {'liked_titles': {}}
        update_dict['liked_titles'][title_uuid] = like
        self.store.update(user_uuid, update_dict)

        logger.info(dict(
            user_uuid=user_uuid,
            title_uuid=title_uuid,
            like=like,
            method='like_title',
        ))
        raise Return()

    @coroutine
    def like_star(self, user_uuid, star_uuid, like):
        user_uuid = is_valid_uuid_string(user_uuid)
        star_uuid = is_valid_uuid_string(star_uuid)

        update_dict = {'liked_stars': {}}
        update_dict['liked_stars'][star_uuid] = like
        self.store.update(user_uuid, update_dict)

        logger.info(dict(
            user_uuid=user_uuid,
            star_uuid=star_uuid,
            like=like,
            method='like_star',
        ))
        raise Return()

    @coroutine
    def get_all_liked_titles(self, user_uuid):
        log_info = dict(
            user_uuid=user_uuid,
            method='get_all_liked_titles',
        )

        user_uuid = is_valid_uuid_string(user_uuid)

        user = yield self.store.get(user_uuid)
        if not user:
            log_info.update({'error': 'user not found'})
            logger.exception(log_info)
            raise ResourceNotFoundError(log_info.get('error'))

        if not user.liked_titles:
            title_uuids = []
        else:
            title_uuids = list(filter(lambda uuid: user.liked_titles[uuid], user.liked_titles))

        titles = yield self.title_store.get_all_by_uuids(title_uuids)

        logger.info(log_info)
        raise Return(titles)

    @coroutine
    def get_all_liked_stars(self, user_uuid):
        log_info = dict(
            user_uuid=user_uuid,
            method='get_all_liked_stars',
        )

        user_uuid = is_valid_uuid_string(user_uuid)

        user = yield self.store.get(user_uuid)
        if not user:
            log_info.update({'error': 'user not found'})
            logger.exception(log_info)
            raise ResourceNotFoundError(log_info.get('error'))

        if not user.liked_stars:
            star_uuids = []
        else:
            star_uuids = list(filter(lambda uuid: user.liked_stars[uuid], user.liked_stars))

        stars = yield self.star_store.get_all_by_uuids(star_uuids)

        logger.info(log_info)
        raise Return(stars)
 def test_get_all_by_uuids(self):
     stars = yield StarStore().get_all_by_uuids([])
     self.assertEquals(stars, [])
 def test_get_by_name(self):
     star = yield StarStore().get_by_name('not found')
     self.assertIsNone(star)
 def test_get(self):
     fake_uuid = uuid.uuid4()
     star = yield StarStore().get(str(fake_uuid))
     self.assertIsNone(star)
 def test_get_all(self):
     stars = yield StarStore().get_all()
     self.assertEquals(stars, [])
 def __init__(self):
     super(StarService, self).__init__()
     self.store = StarStore()