Esempio n. 1
0
 def __init__(self):
     self.persons = []
     self.profiles = []
     self.experiences = []
     self.saves = []
     self.search_repo = Mock()
     self.repo = ExperienceRepo(self.search_repo)
Esempio n. 2
0
        def given_an_experience_from_blocked_person(self,
                                                    title='',
                                                    description='',
                                                    saves_count=0):
            experience = Experience(id=str(len(self.experiences) + 1),
                                    title=title,
                                    description=description,
                                    author_id=self.orm_blocked_person.id,
                                    author_profile=Profile(
                                        person_id=self.orm_blocked_person.id,
                                        username='******',
                                        bio='cked',
                                        picture=None,
                                        is_me=False),
                                    saves_count=saves_count,
                                    is_mine=False)

            db_experience = ExperienceRepo().create_experience(experience)
            ORMExperience.objects.filter(id=db_experience.id).update(
                saves_count=saves_count)

            experience = experience.builder().id(db_experience.id).build()
            self.experiences.append(experience)

            return self
 def when_unsave_that_experience(self):
     try:
         self.result = ExperienceRepo().unsave_experience(
             person_id=self.orm_person.id,
             experience_id=self.orm_experience_a.id)
     except Exception as e:
         self.error = e
     return self
Esempio n. 4
0
    def test_get_all_experiences_returns_all_experiences(self):
        orm_exp_a = ORMExperience.objects.create(
            title='Exp a', description='some description')
        orm_exp_b = ORMExperience.objects.create(
            title='Exp b', description='other description')

        result = ExperienceRepo().get_all_experiences()

        exp_a = Experience(id=orm_exp_a.id,
                           title='Exp a',
                           description='some description',
                           picture=None)
        exp_b = Experience(id=orm_exp_b.id,
                           title='Exp b',
                           description='other description',
                           picture=None)
        assert result == [exp_a, exp_b]
Esempio n. 5
0
    def test_saved_experiences_returns_only_saved_scenes(self):
        orm_person = ORMPerson.objects.create()
        ORMProfile.objects.create(person_id=orm_person.id, username='******')
        orm_person_b = ORMPerson.objects.create()
        ORMProfile.objects.create(person_id=orm_person_b.id,
                                  username='******',
                                  bio='bs')
        orm_auth_token = ORMAuthToken.objects.create(person=orm_person)
        exp_a = ORMExperience.objects.create(title='Exp a',
                                             description='some description',
                                             author=orm_person_b)
        ORMExperience.objects.create(title='Exp b',
                                     description='other description',
                                     author=orm_person_b)
        ExperienceRepo().save_experience(orm_person.id, exp_a.id)

        client = Client()
        auth_headers = {
            'HTTP_AUTHORIZATION':
            'Token {}'.format(orm_auth_token.access_token),
        }
        response = client.get("{}?saved=true".format(reverse('experiences')),
                              **auth_headers)

        assert response.status_code == 200
        body = json.loads(response.content)
        assert body == {
            'results': [{
                'id': str(exp_a.id),
                'title': 'Exp a',
                'description': 'some description',
                'picture': None,
                'author_profile': {
                    'username': '******',
                    'bio': 'bs',
                    'picture': None,
                    'is_me': False,
                },
                'is_mine': False,
                'is_saved': True,
                'saves_count': 1
            }],
            'next_url':
            None
        }
Esempio n. 6
0
 def when_create_this_experience(self):
     self._result = ExperienceRepo().create_experience(
         self._experience_to_create)
     return self
Esempio n. 7
0
 def when_get_unexistent_experience(self):
     try:
         ExperienceRepo().get_experience(0)
     except EntityDoesNotExistException as e:
         self._entity_does_not_exist_error = e
     return self
Esempio n. 8
0
 def when_get_experience_with_its_id(self):
     self._result = ExperienceRepo().get_experience(
         self._orm_experience_a.id)
     return self
Esempio n. 9
0
 def when_get_all_experiences(self):
     self._result = ExperienceRepo().get_all_experiences()
     return self
Esempio n. 10
0
 def then_should_save_this_experience_to_db(self):
     exp = ExperienceRepo().get_experience(self._result.id)
     assert exp.title == self._experience_to_create.title
     assert exp.description == self._experience_to_create.description
     return self
Esempio n. 11
0
 def when_update_first_experience(self):
     self._result = ExperienceRepo().update_experience(
         self._updated_experience)
     return self
Esempio n. 12
0
    def test_others_experiences_returns_others_experiences(self):
        orm_person = ORMPerson.objects.create()
        ORMProfile.objects.create(person_id=orm_person.id, username='******')
        orm_auth_token = ORMAuthToken.objects.create(person=orm_person)
        orm_other_person = ORMPerson.objects.create()
        ORMProfile.objects.create(person_id=orm_other_person.id,
                                  username='******',
                                  bio='c')
        exp_a = ORMExperience.objects.create(title='Exp a',
                                             description='some description',
                                             author=orm_other_person)
        exp_b = ORMExperience.objects.create(title='Exp b',
                                             description='other description',
                                             author=orm_other_person)
        exp_c = ORMExperience.objects.create(title='Exp c',
                                             description='third description',
                                             author=orm_other_person)
        ExperienceRepo().save_experience(person_id=orm_person.id,
                                         experience_id=exp_c.id)

        client = Client()
        auth_headers = {
            'HTTP_AUTHORIZATION':
            'Token {}'.format(orm_auth_token.access_token),
        }
        response = client.get(
            "{}?username=other&limit=2".format(reverse('experiences')),
            **auth_headers)

        assert response.status_code == 200
        body = json.loads(response.content)
        assert body == {
            'results': [
                {
                    'id': str(exp_c.id),
                    'title': 'Exp c',
                    'description': 'third description',
                    'picture': None,
                    'author_profile': {
                        'username': '******',
                        'bio': 'c',
                        'picture': None,
                        'is_me': False,
                    },
                    'is_mine': False,
                    'is_saved': True,
                    'saves_count': 1
                },
                {
                    'id': str(exp_b.id),
                    'title': 'Exp b',
                    'description': 'other description',
                    'picture': None,
                    'author_profile': {
                        'username': '******',
                        'bio': 'c',
                        'picture': None,
                        'is_me': False,
                    },
                    'is_mine': False,
                    'is_saved': False,
                    'saves_count': 0
                },
            ],
            'next_url':
            'https://testserver/experiences/?username=other&limit=2&offset=2'
        }

        auth_headers = {
            'HTTP_AUTHORIZATION':
            'Token {}'.format(orm_auth_token.access_token),
        }
        response = client.get(body['next_url'], **auth_headers)
        assert response.status_code == 200
        body = json.loads(response.content)
        assert body == {
            'results': [
                {
                    'id': str(exp_a.id),
                    'title': 'Exp a',
                    'description': 'some description',
                    'picture': None,
                    'author_profile': {
                        'username': '******',
                        'bio': 'c',
                        'picture': None,
                        'is_me': False,
                    },
                    'is_mine': False,
                    'is_saved': False,
                    'saves_count': 0
                },
            ],
            'next_url':
            None
        }
Esempio n. 13
0
    class ScenarioMaker:
        def __init__(self):
            self.persons = []
            self.profiles = []
            self.experiences = []
            self.saves = []
            self.search_repo = Mock()
            self.repo = ExperienceRepo(self.search_repo)

        def given_a_person_in_db(self, username):
            self.persons.append(ORMPerson.objects.create())
            self.profiles.append(
                ORMProfile.objects.create(
                    person=self.persons[len(self.persons) - 1],
                    username=username))
            return self

        def given_an_experience_in_db(self,
                                      created_by_person,
                                      share_id=None,
                                      is_deleted=False):
            author_id = self.persons[created_by_person - 1].id
            self.experiences.append(
                ORMExperience.objects.create(author_id=author_id,
                                             share_id=share_id,
                                             is_deleted=is_deleted))
            return self

        def given_I_save_experience(self, experience):
            experience_id = str(self.experiences[experience - 1].id)
            self.repo.save_experience(person_id=str(self.persons[0].id),
                                      experience_id=experience_id)
            self.saves.append(experience_id)
            return self

        def given_a_word_location_limit_and_offset(self):
            self.word = 'culture'
            self.location = (5.4, -0.8)
            self.offset = 4
            self.limit = 10
            return self

        def given_a_search_repo_that_returns_experience_ids_and_offset(
                self, experiences_positions, offset):
            experiences_ids = [
                self.experiences[i - 1].id for i in experiences_positions
            ]
            self.search_repo.search_experiences.return_value = {
                'results': experiences_ids,
                'next_offset': offset
            }
            return self

        def when_get_saved_experiences(self, offset, limit):
            self.result = self.repo.get_saved_experiences(logged_person_id=str(
                self.persons[0].id),
                                                          offset=offset,
                                                          limit=limit)
            return self

        def when_get_person_experiences(self, target_person, offset, limit):
            self.result = self.repo.get_person_experiences(
                logged_person_id=str(self.persons[0].id),
                target_person_id=str(self.persons[target_person - 1].id),
                offset=offset,
                limit=limit)
            return self

        def when_get_experience(self, position, person=0):
            try:
                self.result = self.repo.get_experience(
                    id=str(self.experiences[position - 1].id),
                    logged_person_id=str(self.persons[person - 1].id))
            except EntityDoesNotExistException as e:
                self.entity_does_not_exist_exception = e
            return self

        def when_get_experience_by_share_id(self, share_id, person=0):
            try:
                self.result = self.repo.get_experience(
                    share_id=share_id,
                    logged_person_id=str(self.persons[person - 1].id))
            except EntityDoesNotExistException as e:
                self.entity_does_not_exist_exception = e
            return self

        def when_get_unexistent_experience(self):
            try:
                self.repo.get_experience(id='0')
            except EntityDoesNotExistException as e:
                self.entity_does_not_exist_exception = e
            return self

        def when_create_experience(self, title, description, author):
            orm_author = self.persons[author - 1]
            experience = Experience(title=title,
                                    description=description,
                                    author_id=str(orm_author.id))
            self.result = self.repo.create_experience(experience)
            return self

        def when_update_experience(self,
                                   experience,
                                   title,
                                   description,
                                   share_id=None):
            experience = self.repo._decode_db_experience(
                ORMExperience.objects.get(id=str(self.experiences[experience -
                                                                  1].id)),
                logged_person_id=str(self.persons[0].id))
            updated_experience = experience.builder().title(title).description(
                description).share_id(share_id).build()
            try:
                self.result = self.repo.update_experience(
                    updated_experience,
                    logged_person_id=str(self.persons[0].id))
            except EntityDoesNotExistException as e:
                self.entity_does_not_exist_exception = e
            except Exception as e:
                self.error = e
            return self

        def when_save_experience(self, position):
            experience = self.experiences[position - 1]
            self.result = self.repo.save_experience(
                person_id=str(self.persons[0].id),
                experience_id=str(experience.id))
            return self

        def when_unsave_experience(self, position):
            experience = self.experiences[position - 1]
            self.result = self.repo.unsave_experience(
                person_id=str(self.persons[0].id),
                experience_id=str(experience.id))
            return self

        def when_flag_experience(self, position, reason):
            self.result = self.repo.flag_experience(
                person_id=str(self.persons[0].id),
                experience_id=str(self.experiences[position - 1].id),
                reason=reason)
            return self

        def when_search_experiences(self):
            self.result = self.repo.search_experiences(str(self.persons[0].id),
                                                       self.word,
                                                       self.location,
                                                       self.offset, self.limit)
            return self

        def then_result_should_be_experiences_and_offset(
                self, experiences_positions, next_offset):
            assert self.result['next_offset'] == next_offset
            assert len(self.result['results']) == len(experiences_positions)
            for i in range(len(experiences_positions)):
                orm_experience = self.experiences[experiences_positions[i] - 1]
                orm_experience.refresh_from_db()
                saved = str(orm_experience.id) in self.saves
                parsed_experience = self.repo._decode_db_experience(
                    orm_experience, str(self.persons[0].id), is_saved=saved)
                assert self.result['results'][i] == parsed_experience

            return self

        def then_repo_should_return_experience(self,
                                               position,
                                               person_logged,
                                               saved=False):
            orm_experience = self.experiences[position - 1]
            parsed_experience = self.repo._decode_db_experience(
                orm_experience,
                str(self.persons[person_logged - 1].id),
                is_saved=saved)
            if saved:
                parsed_experience = parsed_experience.builder().saves_count(
                    1).build()
            assert self.result == parsed_experience
            return self

        def then_entity_does_not_exists_should_be_raised(self):
            assert self.entity_does_not_exist_exception is not None
            return self

        def then_should_return_experience(self,
                                          title,
                                          description,
                                          author,
                                          mine,
                                          share_id=None):
            assert self.result.title == title
            assert self.result.description == description
            assert self.result.author_profile.person_id == str(
                self.profiles[author - 1].person_id)
            assert self.result.author_profile.username == self.profiles[
                author - 1].username
            assert self.result.author_profile.bio == self.profiles[author -
                                                                   1].bio
            assert self.result.author_profile.is_me == mine
            assert self.result.is_mine == mine
            assert self.result.share_id == share_id
            return self

        def then_result_experience_should_be_in_db(self):
            orm_experience = ORMExperience.objects.get(id=self.result.id)
            assert str(orm_experience.id) == self.result.id
            assert orm_experience.title == self.result.title
            assert orm_experience.description == self.result.description
            assert str(orm_experience.author.profile.person_id
                       ) == self.result.author_profile.person_id
            assert orm_experience.author.profile.username == self.result.author_profile.username
            assert orm_experience.author.profile.bio == self.result.author_profile.bio
            assert orm_experience.share_id == self.result.share_id
            return self

        def then_result_should_be_true(self):
            assert self.result is True
            return self

        def then_save_should_be_in_db(self, how_many_saves, person,
                                      experience):
            orm_person = self.persons[person - 1]
            orm_experience = self.experiences[experience - 1]
            assert len(
                ORMSave.objects.filter(
                    experience_id=orm_experience.id,
                    person_id=orm_person.id)) == how_many_saves
            return self

        def then_flag_should_be_in_db(self, person, experience, reason):
            orm_person = self.persons[person - 1]
            orm_experience = self.experiences[experience - 1]
            assert len(
                ORMFlag.objects.filter(experience_id=orm_experience.id,
                                       person_id=orm_person.id,
                                       reason=reason)) == 1
            return self

        def then_experience_saves_count_should_be(self, experience,
                                                  saves_count):
            self.experiences[experience - 1].refresh_from_db()
            assert self.experiences[experience - 1].saves_count == saves_count
            return self

        def then_should_call_search_repo_search_experiences_with_correct_params(
                self):
            self.search_repo.search_experiences.assert_called_once_with(
                self.word, self.location, self.offset, self.limit)
            return self

        def then_should_raise_conflict_exception(self):
            assert type(self.error) == ConflictException
            return self
 def when_get_all_experiences(self, mine=False, saved=False):
     self.result = ExperienceRepo().get_all_experiences(
         logged_person_id=self.logged_person_id, mine=mine, saved=saved)
     return self