Exemplo n.º 1
0
Arquivo: tests.py Projeto: hnb2/ideas
    def test_get_number_of_votes(self):
        '''
        Testing the method get_number_of_votes
        '''

        # Exising idea and user
        number_of_votes = IdeaService.get_number_of_votes(
            self.user.id,
            self.idea.id
        )
        self.assertEqual(number_of_votes, 1)

        # User does not exist
        number_of_votes = IdeaService.get_number_of_votes(0, self.idea.id)
        self.assertEqual(number_of_votes, 0)

        # Idea does not exist
        number_of_votes = IdeaService.get_number_of_votes(self.user.id, 0)
        self.assertEqual(number_of_votes, 0)

        # Both idea and user do not exist
        number_of_votes = IdeaService.get_number_of_votes(0, 0)
        self.assertEqual(number_of_votes, 0)

        # Using None
        number_of_votes = IdeaService.get_number_of_votes(None, None)
        self.assertEqual(number_of_votes, 0)
Exemplo n.º 2
0
    def test_get_ideas_by_category(self):
        '''
        Testing the method get_ideas_by_category
        '''

        # Existing category
        category_a_ideas = IdeaService.get_ideas_by_category(self.category.id)
        self.assertEqual(len(category_a_ideas), 2)

        # Make sure it contains only the desired fields
        idea = category_a_ideas[0]
        self.assertIn('id', idea)
        self.assertIn('title', idea)
        self.assertIn('user__first_name', idea)
        self.assertIn('created_date', idea)
        self.assertIn('excerpt', idea)
        self.assertIn('votes', idea)

        # Category without idea
        category_a_ideas = IdeaService.get_ideas_by_category(2)
        self.assertEqual(len(category_a_ideas), 0)

        # Category does not exist
        category_a_ideas = IdeaService.get_ideas_by_category(999)
        self.assertEqual(len(category_a_ideas), 0)

        # Using none
        category_a_ideas = IdeaService.get_ideas_by_category(None)
        self.assertEqual(len(category_a_ideas), 0)
Exemplo n.º 3
0
Arquivo: tests.py Projeto: hnb2/ideas
    def test_get_latest_ideas(self):
        '''
        Testing the method get_latest_ideas
        '''

        # Retrieving all the ideas
        ideas = IdeaService.get_latest_ideas(10)
        self.assertEqual(len(ideas), 2)

        # Make sure it contains only the desired fields
        idea = ideas[0]
        self.assertIn('id', idea)
        self.assertIn('title', idea)
        self.assertIn('user__first_name', idea)
        self.assertIn('created_date', idea)
        self.assertIn('excerpt', idea)
        self.assertIn('votes', idea)

        # Make sure it is sorted by created date DESC
        self.assertGreater(
            ideas[0]['created_date'],
            ideas[1]['created_date']
        )

        # Using None
        ideas = IdeaService.get_latest_ideas(None)
        self.assertEqual(len(ideas), 2)
Exemplo n.º 4
0
Arquivo: tests.py Projeto: hnb2/ideas
    def test_get_ideas_by_category(self):
        '''
        Testing the method get_ideas_by_category
        '''

        # Existing category
        category_a_ideas = IdeaService.get_ideas_by_category(
            self.category.id
        )
        self.assertEqual(len(category_a_ideas), 2)

        # Make sure it contains only the desired fields
        idea = category_a_ideas[0]
        self.assertIn('id', idea)
        self.assertIn('title', idea)
        self.assertIn('user__first_name', idea)
        self.assertIn('created_date', idea)
        self.assertIn('excerpt', idea)
        self.assertIn('votes', idea)

        # Category without idea
        category_a_ideas = IdeaService.get_ideas_by_category(2)
        self.assertEqual(len(category_a_ideas), 0)

        # Category does not exist
        category_a_ideas = IdeaService.get_ideas_by_category(999)
        self.assertEqual(len(category_a_ideas), 0)

        # Using none
        category_a_ideas = IdeaService.get_ideas_by_category(None)
        self.assertEqual(len(category_a_ideas), 0)
Exemplo n.º 5
0
    def test_get_number_of_votes(self):
        '''
        Testing the method get_number_of_votes
        '''

        # Exising idea and user
        number_of_votes = IdeaService.get_number_of_votes(
            self.user.id, self.idea.id)
        self.assertEqual(number_of_votes, 1)

        # User does not exist
        number_of_votes = IdeaService.get_number_of_votes(0, self.idea.id)
        self.assertEqual(number_of_votes, 0)

        # Idea does not exist
        number_of_votes = IdeaService.get_number_of_votes(self.user.id, 0)
        self.assertEqual(number_of_votes, 0)

        # Both idea and user do not exist
        number_of_votes = IdeaService.get_number_of_votes(0, 0)
        self.assertEqual(number_of_votes, 0)

        # Using None
        number_of_votes = IdeaService.get_number_of_votes(None, None)
        self.assertEqual(number_of_votes, 0)
Exemplo n.º 6
0
    def test_get_categories(self):
        '''
        Testing the method get_categories
        '''

        # Retrieving one category
        categories = IdeaService.get_categories(1)
        self.assertEqual(len(categories), 1)

        # Retrieving all the categories
        categories = IdeaService.get_categories(10)
        self.assertEqual(len(categories), 3)

        # Using None
        categories = IdeaService.get_categories(None)
        self.assertEqual(len(categories), 3)
Exemplo n.º 7
0
Arquivo: tests.py Projeto: hnb2/ideas
    def test_get_categories(self):
        '''
        Testing the method get_categories
        '''

        # Retrieving one category
        categories = IdeaService.get_categories(1)
        self.assertEqual(len(categories), 1)

        # Retrieving all the categories
        categories = IdeaService.get_categories(10)
        self.assertEqual(len(categories), 3)

        # Using None
        categories = IdeaService.get_categories(None)
        self.assertEqual(len(categories), 3)
Exemplo n.º 8
0
Arquivo: ideas.py Projeto: hnb2/ideas
 def get_queryset(self):
     '''
     Return a dictionnary of fields, it will not be objects.
     '''
     return IdeaService.get_ideas_by_category(
         self.kwargs['category_id']
     )
Exemplo n.º 9
0
    def test_get_safe_limit(self):
        '''
        Test the method get_safe_limit
        '''

        # Valid limit
        limit = IdeaService.get_safe_limit(8)
        self.assertEqual(limit, 8)

        # Negative limit
        limit = IdeaService.get_safe_limit(-1)
        self.assertEqual(limit, IdeaService.LIMIT)

        # Limit above max
        limit = IdeaService.get_safe_limit(IdeaService.LIMIT + 1)
        self.assertEqual(limit, IdeaService.LIMIT)

        # None
        limit = IdeaService.get_safe_limit(None)
        self.assertEqual(limit, IdeaService.LIMIT)

        # Parsable string
        limit = IdeaService.get_safe_limit('8')
        self.assertEqual(limit, 8)

        # Un-parsable string
        limit = IdeaService.get_safe_limit('qwerty')
        self.assertEqual(limit, IdeaService.LIMIT)
Exemplo n.º 10
0
Arquivo: tests.py Projeto: hnb2/ideas
    def test_get_safe_limit(self):
        '''
        Test the method get_safe_limit
        '''

        # Valid limit
        limit = IdeaService.get_safe_limit(8)
        self.assertEqual(limit, 8)

        # Negative limit
        limit = IdeaService.get_safe_limit(-1)
        self.assertEqual(limit, IdeaService.LIMIT)

        # Limit above max
        limit = IdeaService.get_safe_limit(IdeaService.LIMIT + 1)
        self.assertEqual(limit, IdeaService.LIMIT)

        # None
        limit = IdeaService.get_safe_limit(None)
        self.assertEqual(limit, IdeaService.LIMIT)

        # Parsable string
        limit = IdeaService.get_safe_limit('8')
        self.assertEqual(limit, 8)

        # Un-parsable string
        limit = IdeaService.get_safe_limit('qwerty')
        self.assertEqual(limit, IdeaService.LIMIT)
Exemplo n.º 11
0
    def test_get_latest_ideas(self):
        '''
        Testing the method get_latest_ideas
        '''

        # Retrieving all the ideas
        ideas = IdeaService.get_latest_ideas(10)
        self.assertEqual(len(ideas), 2)

        # Make sure it contains only the desired fields
        idea = ideas[0]
        self.assertIn('id', idea)
        self.assertIn('title', idea)
        self.assertIn('user__first_name', idea)
        self.assertIn('created_date', idea)
        self.assertIn('excerpt', idea)
        self.assertIn('votes', idea)

        # Make sure it is sorted by created date DESC
        self.assertGreater(ideas[0]['created_date'], ideas[1]['created_date'])

        # Using None
        ideas = IdeaService.get_latest_ideas(None)
        self.assertEqual(len(ideas), 2)
Exemplo n.º 12
0
Arquivo: forms.py Projeto: hnb2/ideas
    def validate(self, value):
        '''
        Validate the vote for an idea from the current user. At this point,
        the idea is valid, and we check whereas the user already voted for
        this idea or not.
        '''
        super(IdeaIdField, self).validate(value)

        if not self.user.id:
            raise forms.ValidationError('You must be logged in to vote.');

        number_of_votes = IdeaService.get_number_of_votes(
            self.user.id,
            value.id
        )
        if number_of_votes > 0:
           raise forms.ValidationError('You already voted for this idea.');
Exemplo n.º 13
0
    def get_context_data(self, **kwargs):
        '''
        This is how we can add extra data to the context when using
        a generic view. We are just adding a flag to check whether
        the user already voted for the idea or not.
        '''
        context = super(DetailView, self).get_context_data(**kwargs)

        #Cannot vote if not logged in
        has_not_voted = False

        #Checking if the user already voted
        if self.request.user.id:
            number_of_votes = IdeaService.get_number_of_votes(
                self.request.user.id, self.kwargs['pk'])

            has_not_voted = (number_of_votes == 0)

        context['has_not_voted'] = has_not_voted
        return context
Exemplo n.º 14
0
    def get_context_data(self, **kwargs):
        '''
        This is how we can add extra data to the context when using
        a generic view. We are just adding a flag to check whether
        the user already voted for the idea or not.
        '''
        context = super(DetailView, self).get_context_data(**kwargs)

        #Cannot vote if not logged in
        has_not_voted = False

        #Checking if the user already voted
        if self.request.user.id:
            number_of_votes = IdeaService.get_number_of_votes(
                self.request.user.id,
                self.kwargs['pk']
            )

            has_not_voted = (number_of_votes == 0)

        context['has_not_voted'] = has_not_voted
        return context
Exemplo n.º 15
0
 def get_queryset(self):
     return IdeaService.get_categories(8)
Exemplo n.º 16
0
Arquivo: index.py Projeto: hnb2/ideas
 def get_queryset(self):
     '''
     Return a dictionnary of fields, it will not be objects.
     '''
     return IdeaService.get_latest_ideas(8)
Exemplo n.º 17
0
 def get_queryset(self):
     '''
     Return a dictionnary of fields, it will not be objects.
     '''
     return IdeaService.get_latest_ideas(8)
Exemplo n.º 18
0
Arquivo: ideas.py Projeto: hnb2/ideas
 def get_queryset(self):
     '''
     Return a dictionnary of fields, it will not be objects.
     '''
     return IdeaService.get_ideas_by_category(self.kwargs['category_id'])