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