Blame tests/test_pagure_lib_star_project.py

Vivek Anand 327a4f
# coding=utf-8
Vivek Anand 327a4f
"""
Vivek Anand 327a4f
 (c) 2015-2017 - Copyright Red Hat Inc
Vivek Anand 327a4f
Vivek Anand 327a4f
 Authors:
Vivek Anand 327a4f
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Vivek Anand 327a4f
   Vivek Anand <vivekanand1101@gmail.com></vivekanand1101@gmail.com>
Vivek Anand 327a4f
Vivek Anand 327a4f
"""
Vivek Anand 327a4f
Aurélien Bompard dcf6f6
from __future__ import unicode_literals
Aurélien Bompard dcf6f6
Vivek Anand 327a4f
__requires__ = ['SQLAlchemy >= 0.8']
Vivek Anand 327a4f
import pkg_resources
Vivek Anand 327a4f
Vivek Anand 327a4f
import unittest
Vivek Anand 327a4f
import sys
Vivek Anand 327a4f
import os
Vivek Anand 327a4f
Vivek Anand 327a4f
sys.path.insert(0, os.path.join(os.path.dirname(
Vivek Anand 327a4f
    os.path.abspath(__file__)), '..'))
Vivek Anand 327a4f
Pierre-Yves Chibon 930073
import pagure.lib.model
Pierre-Yves Chibon 930073
import pagure.lib.query
Vivek Anand 327a4f
import tests
Vivek Anand 327a4f
Vivek Anand e28e4b
Vivek Anand 327a4f
class TestStarProjectLib(tests.SimplePagureTest):
Vivek Anand 327a4f
    ''' Test the star project feature of pagure '''
Vivek Anand 327a4f
Vivek Anand 327a4f
    def setUp(self):
Vivek Anand 327a4f
        """ Set up the environnment for running each star project lib tests """
Vivek Anand 327a4f
        super(TestStarProjectLib, self).setUp()
Vivek Anand 327a4f
        tests.create_projects(self.session)
Vivek Anand 327a4f
Vivek Anand 327a4f
    def test_update_star_project(self):
Vivek Anand 327a4f
        ''' Test the update_star_project endpoint of pagure.lib '''
Vivek Anand 327a4f
Pierre-Yves Chibon 930073
        repo_obj = pagure.lib.query._get_project(self.session, 'test')
Vivek Anand 327a4f
        # test with invalud Star object, should return None
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.update_star_project(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand 327a4f
            None,
Vivek Anand 327a4f
            'pingou',
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.session.commit()
Vivek Anand 327a4f
        self.assertEqual(msg, None)
Pierre-Yves Chibon 930073
        user_obj = pagure.lib.query.get_user(self.session, 'pingou')
Vivek Anand e28e4b
        self.assertEqual(len(user_obj.stars), 0)
Vivek Anand 327a4f
Vivek Anand 327a4f
        # test starring the project
Pierre-Yves Chibon 930073
        repo_obj = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.update_star_project(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand 327a4f
            '1',
Vivek Anand 327a4f
            'pingou',
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
Vivek Anand 327a4f
        self.session.commit()
Vivek Anand 327a4f
        self.assertEqual(msg, 'You starred this project')
Pierre-Yves Chibon 930073
        user_obj = pagure.lib.query.get_user(self.session, 'pingou')
Vivek Anand e28e4b
        self.assertEqual(len(user_obj.stars), 1)
Vivek Anand 327a4f
Vivek Anand 327a4f
        # test unstarring the project
Pierre-Yves Chibon 930073
        repo_obj = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.update_star_project(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand 327a4f
            '0',
Vivek Anand 327a4f
            'pingou',
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
Vivek Anand 327a4f
        self.session.commit()
Vivek Anand 327a4f
        self.assertEqual(msg, 'You unstarred this project')
Pierre-Yves Chibon 930073
        user_obj = pagure.lib.query.get_user(self.session, 'pingou')
Vivek Anand e28e4b
        self.assertEqual(len(user_obj.stars), 0)
Vivek Anand 327a4f
Vivek Anand 327a4f
    def test_star_project(self):
Vivek Anand 327a4f
        ''' Test the _star_project endpoint of pagure.lib '''
Vivek Anand 327a4f
Vivek Anand 327a4f
        # test with not all arguments present
Pierre-Yves Chibon 930073
        user_obj = pagure.lib.query.get_user(self.session, 'pingou')
Pierre-Yves Chibon 930073
        repo_obj = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon 930073
        msg = pagure.lib.query._star_project(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand 327a4f
            None
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.session.commit()
Vivek Anand 327a4f
        self.assertEqual(msg, None)
Vivek Anand 327a4f
Pierre-Yves Chibon 930073
        repo_obj = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon 930073
        user_obj = pagure.lib.query.get_user(self.session, 'pingou')
Pierre-Yves Chibon 930073
        msg = pagure.lib.query._star_project(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand e28e4b
            user_obj,
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.session.commit()
Vivek Anand 327a4f
        self.assertEqual(msg, 'You starred this project')
Pierre-Yves Chibon 930073
        user_obj = pagure.lib.query.get_user(self.session, 'pingou')
Vivek Anand e28e4b
        self.assertEqual(len(user_obj.stars), 1)
Vivek Anand 327a4f
Vivek Anand 327a4f
    def test_unstar_project(self):
Vivek Anand 327a4f
        ''' Test the _unstar_project endpoint of pagure.lib '''
Vivek Anand 327a4f
Vivek Anand 327a4f
        # test with not all arguments present
Pierre-Yves Chibon 930073
        user_obj = pagure.lib.query.get_user(self.session, 'pingou')
Pierre-Yves Chibon 930073
        repo_obj = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon 930073
        msg = pagure.lib.query._unstar_project(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand 327a4f
            None
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.session.commit()
Vivek Anand 327a4f
        self.assertEqual(msg, None)
Vivek Anand 327a4f
Vivek Anand 327a4f
        # the user hasn't starred the project before
Pierre-Yves Chibon 930073
        repo_obj = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon 930073
        user_obj = pagure.lib.query.get_user(self.session, 'pingou')
Pierre-Yves Chibon 930073
        msg = pagure.lib.query._unstar_project(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand e28e4b
            user_obj,
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.assertEqual(msg, 'You never starred the project')
Pierre-Yves Chibon 930073
        user_obj = pagure.lib.query.get_user(self.session, 'pingou')
Vivek Anand e28e4b
        self.assertEqual(len(user_obj.stars), 0)
Vivek Anand 327a4f
Vivek Anand 327a4f
        # star it for testing
Pierre-Yves Chibon 930073
        msg = pagure.lib.query._star_project(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand e28e4b
            user_obj,
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.session.commit()
Vivek Anand 327a4f
        self.assertEqual(msg, 'You starred this project')
Pierre-Yves Chibon 930073
        user_obj = pagure.lib.query.get_user(self.session, 'pingou')
Vivek Anand e28e4b
        self.assertEqual(len(user_obj.stars), 1)
Vivek Anand 327a4f
Vivek Anand 327a4f
        # the user starred and wishes to unstar
Pierre-Yves Chibon 930073
        repo_obj = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon 930073
        user_obj = pagure.lib.query.get_user(self.session, 'pingou')
Pierre-Yves Chibon 930073
        msg = pagure.lib.query._unstar_project(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand e28e4b
            user_obj,
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.session.commit()
Vivek Anand 327a4f
        self.assertEqual(msg, 'You unstarred this project')
Pierre-Yves Chibon 930073
        user_obj = pagure.lib.query.get_user(self.session, 'pingou')
Vivek Anand e28e4b
        self.assertEqual(len(user_obj.stars), 0)
Vivek Anand 327a4f
Vivek Anand 327a4f
    def test_get_stargazer_obj(self):
Vivek Anand 327a4f
        ''' Test the _get_stargazer_obj test of pagure.lib '''
Vivek Anand 327a4f
Vivek Anand 327a4f
        # star the project first
Pierre-Yves Chibon 930073
        repo_obj = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon 930073
        user_obj = pagure.lib.query.get_user(self.session, 'pingou')
Pierre-Yves Chibon 930073
        msg = pagure.lib.query._star_project(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand e28e4b
            user_obj,
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.session.commit()
Vivek Anand 327a4f
        self.assertEqual(msg, 'You starred this project')
Pierre-Yves Chibon 930073
        user_obj = pagure.lib.query.get_user(self.session, 'pingou')
Vivek Anand e28e4b
        self.assertEqual(len(user_obj.stars), 1)
Vivek Anand 327a4f
Vivek Anand 327a4f
        # get the object now
Pierre-Yves Chibon 930073
        repo_obj = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon 930073
        star_obj = pagure.lib.query._get_stargazer_obj(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand e28e4b
            user_obj
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.assertEqual(isinstance(star_obj, pagure.lib.model.Star), True)
Vivek Anand 327a4f
Vivek Anand 327a4f
        # unstar it and then try to get the object
Pierre-Yves Chibon 930073
        repo_obj = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon 930073
        user_obj = pagure.lib.query.get_user(self.session, 'pingou')
Pierre-Yves Chibon 930073
        msg = pagure.lib.query._unstar_project(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand e28e4b
            user_obj,
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.session.commit()
Vivek Anand 327a4f
        self.assertEqual(msg, 'You unstarred this project')
Pierre-Yves Chibon 930073
        user_obj = pagure.lib.query.get_user(self.session, 'pingou')
Vivek Anand e28e4b
        self.assertEqual(len(user_obj.stars), 0)
Vivek Anand 327a4f
Vivek Anand 327a4f
        # we don't store if the user has unstarred, we delete the obj
Vivek Anand 327a4f
        # so, we should get anything back in the query
Pierre-Yves Chibon 930073
        repo_obj = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon 930073
        star_obj = pagure.lib.query._get_stargazer_obj(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand e28e4b
            user_obj
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.assertEqual(star_obj is None, True)
Vivek Anand 327a4f
Vivek Anand 327a4f
    def test_has_starred(self):
Vivek Anand 327a4f
        ''' Test the has_starred endpoint of pagure.lib '''
Vivek Anand 327a4f
Vivek Anand 327a4f
        # star the project
Pierre-Yves Chibon 930073
        repo_obj = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon 930073
        user_obj = pagure.lib.query.get_user(self.session, 'pingou')
Pierre-Yves Chibon 930073
        msg = pagure.lib.query._star_project(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand e28e4b
            user_obj,
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.session.commit()
Vivek Anand 327a4f
        self.assertEqual(msg, 'You starred this project')
Pierre-Yves Chibon 930073
        user_obj = pagure.lib.query.get_user(self.session, 'pingou')
Vivek Anand e28e4b
        self.assertEqual(len(user_obj.stars), 1)
Vivek Anand 327a4f
Pierre-Yves Chibon 930073
        has_starred = pagure.lib.query.has_starred(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand 327a4f
            'pingou'
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.assertEqual(has_starred is True, True)
Vivek Anand 327a4f
Vivek Anand 327a4f
        # unstar it and then test for has_starred
Pierre-Yves Chibon 930073
        repo_obj = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon 930073
        user_obj = pagure.lib.query.get_user(self.session, 'pingou')
Pierre-Yves Chibon 930073
        msg = pagure.lib.query._unstar_project(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand e28e4b
            user_obj,
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.session.commit()
Vivek Anand 327a4f
        self.assertEqual(msg, 'You unstarred this project')
Pierre-Yves Chibon 930073
        user_obj = pagure.lib.query.get_user(self.session, 'pingou')
Vivek Anand e28e4b
        self.assertEqual(len(user_obj.stars), 0)
Vivek Anand 327a4f
Vivek Anand 327a4f
        # check now, it should return False
Pierre-Yves Chibon 930073
        has_starred = pagure.lib.query.has_starred(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand 327a4f
            'pingou'
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.assertEqual(has_starred is False, True)
Vivek Anand 327a4f
Vivek Anand 327a4f
Vivek Anand 327a4f
if __name__ == '__main__':
Vivek Anand 327a4f
    unittest.main(verbosity=2)