test_pagure_lib_star_project.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. # coding=utf-8
  2. """
  3. (c) 2015-2017 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. Vivek Anand <vivekanand1101@gmail.com>
  7. """
  8. from __future__ import unicode_literals
  9. __requires__ = ['SQLAlchemy >= 0.8']
  10. import pkg_resources
  11. import unittest
  12. import sys
  13. import os
  14. sys.path.insert(0, os.path.join(os.path.dirname(
  15. os.path.abspath(__file__)), '..'))
  16. import pagure.lib.model
  17. import pagure.lib.query
  18. import tests
  19. class TestStarProjectLib(tests.SimplePagureTest):
  20. ''' Test the star project feature of pagure '''
  21. def setUp(self):
  22. """ Set up the environnment for running each star project lib tests """
  23. super(TestStarProjectLib, self).setUp()
  24. tests.create_projects(self.session)
  25. def test_update_star_project(self):
  26. ''' Test the update_star_project endpoint of pagure.lib '''
  27. repo_obj = pagure.lib.query._get_project(self.session, 'test')
  28. # test with invalud Star object, should return None
  29. msg = pagure.lib.query.update_star_project(
  30. self.session,
  31. repo_obj,
  32. None,
  33. 'pingou',
  34. )
  35. self.session.commit()
  36. self.assertEqual(msg, None)
  37. user_obj = pagure.lib.query.get_user(self.session, 'pingou')
  38. self.assertEqual(len(user_obj.stars), 0)
  39. # test starring the project
  40. repo_obj = pagure.lib.query._get_project(self.session, 'test')
  41. msg = pagure.lib.query.update_star_project(
  42. self.session,
  43. repo_obj,
  44. '1',
  45. 'pingou',
  46. )
  47. self.session.commit()
  48. self.assertEqual(msg, 'You starred this project')
  49. user_obj = pagure.lib.query.get_user(self.session, 'pingou')
  50. self.assertEqual(len(user_obj.stars), 1)
  51. # test unstarring the project
  52. repo_obj = pagure.lib.query._get_project(self.session, 'test')
  53. msg = pagure.lib.query.update_star_project(
  54. self.session,
  55. repo_obj,
  56. '0',
  57. 'pingou',
  58. )
  59. self.session.commit()
  60. self.assertEqual(msg, 'You unstarred this project')
  61. user_obj = pagure.lib.query.get_user(self.session, 'pingou')
  62. self.assertEqual(len(user_obj.stars), 0)
  63. def test_star_project(self):
  64. ''' Test the _star_project endpoint of pagure.lib '''
  65. # test with not all arguments present
  66. user_obj = pagure.lib.query.get_user(self.session, 'pingou')
  67. repo_obj = pagure.lib.query._get_project(self.session, 'test')
  68. msg = pagure.lib.query._star_project(
  69. self.session,
  70. repo_obj,
  71. None
  72. )
  73. self.session.commit()
  74. self.assertEqual(msg, None)
  75. repo_obj = pagure.lib.query._get_project(self.session, 'test')
  76. user_obj = pagure.lib.query.get_user(self.session, 'pingou')
  77. msg = pagure.lib.query._star_project(
  78. self.session,
  79. repo_obj,
  80. user_obj,
  81. )
  82. self.session.commit()
  83. self.assertEqual(msg, 'You starred this project')
  84. user_obj = pagure.lib.query.get_user(self.session, 'pingou')
  85. self.assertEqual(len(user_obj.stars), 1)
  86. def test_unstar_project(self):
  87. ''' Test the _unstar_project endpoint of pagure.lib '''
  88. # test with not all arguments present
  89. user_obj = pagure.lib.query.get_user(self.session, 'pingou')
  90. repo_obj = pagure.lib.query._get_project(self.session, 'test')
  91. msg = pagure.lib.query._unstar_project(
  92. self.session,
  93. repo_obj,
  94. None
  95. )
  96. self.session.commit()
  97. self.assertEqual(msg, None)
  98. # the user hasn't starred the project before
  99. repo_obj = pagure.lib.query._get_project(self.session, 'test')
  100. user_obj = pagure.lib.query.get_user(self.session, 'pingou')
  101. msg = pagure.lib.query._unstar_project(
  102. self.session,
  103. repo_obj,
  104. user_obj,
  105. )
  106. self.assertEqual(msg, 'You never starred the project')
  107. user_obj = pagure.lib.query.get_user(self.session, 'pingou')
  108. self.assertEqual(len(user_obj.stars), 0)
  109. # star it for testing
  110. msg = pagure.lib.query._star_project(
  111. self.session,
  112. repo_obj,
  113. user_obj,
  114. )
  115. self.session.commit()
  116. self.assertEqual(msg, 'You starred this project')
  117. user_obj = pagure.lib.query.get_user(self.session, 'pingou')
  118. self.assertEqual(len(user_obj.stars), 1)
  119. # the user starred and wishes to unstar
  120. repo_obj = pagure.lib.query._get_project(self.session, 'test')
  121. user_obj = pagure.lib.query.get_user(self.session, 'pingou')
  122. msg = pagure.lib.query._unstar_project(
  123. self.session,
  124. repo_obj,
  125. user_obj,
  126. )
  127. self.session.commit()
  128. self.assertEqual(msg, 'You unstarred this project')
  129. user_obj = pagure.lib.query.get_user(self.session, 'pingou')
  130. self.assertEqual(len(user_obj.stars), 0)
  131. def test_get_stargazer_obj(self):
  132. ''' Test the _get_stargazer_obj test of pagure.lib '''
  133. # star the project first
  134. repo_obj = pagure.lib.query._get_project(self.session, 'test')
  135. user_obj = pagure.lib.query.get_user(self.session, 'pingou')
  136. msg = pagure.lib.query._star_project(
  137. self.session,
  138. repo_obj,
  139. user_obj,
  140. )
  141. self.session.commit()
  142. self.assertEqual(msg, 'You starred this project')
  143. user_obj = pagure.lib.query.get_user(self.session, 'pingou')
  144. self.assertEqual(len(user_obj.stars), 1)
  145. # get the object now
  146. repo_obj = pagure.lib.query._get_project(self.session, 'test')
  147. star_obj = pagure.lib.query._get_stargazer_obj(
  148. self.session,
  149. repo_obj,
  150. user_obj
  151. )
  152. self.assertEqual(isinstance(star_obj, pagure.lib.model.Star), True)
  153. # unstar it and then try to get the object
  154. repo_obj = pagure.lib.query._get_project(self.session, 'test')
  155. user_obj = pagure.lib.query.get_user(self.session, 'pingou')
  156. msg = pagure.lib.query._unstar_project(
  157. self.session,
  158. repo_obj,
  159. user_obj,
  160. )
  161. self.session.commit()
  162. self.assertEqual(msg, 'You unstarred this project')
  163. user_obj = pagure.lib.query.get_user(self.session, 'pingou')
  164. self.assertEqual(len(user_obj.stars), 0)
  165. # we don't store if the user has unstarred, we delete the obj
  166. # so, we should get anything back in the query
  167. repo_obj = pagure.lib.query._get_project(self.session, 'test')
  168. star_obj = pagure.lib.query._get_stargazer_obj(
  169. self.session,
  170. repo_obj,
  171. user_obj
  172. )
  173. self.assertEqual(star_obj is None, True)
  174. def test_has_starred(self):
  175. ''' Test the has_starred endpoint of pagure.lib '''
  176. # star the project
  177. repo_obj = pagure.lib.query._get_project(self.session, 'test')
  178. user_obj = pagure.lib.query.get_user(self.session, 'pingou')
  179. msg = pagure.lib.query._star_project(
  180. self.session,
  181. repo_obj,
  182. user_obj,
  183. )
  184. self.session.commit()
  185. self.assertEqual(msg, 'You starred this project')
  186. user_obj = pagure.lib.query.get_user(self.session, 'pingou')
  187. self.assertEqual(len(user_obj.stars), 1)
  188. has_starred = pagure.lib.query.has_starred(
  189. self.session,
  190. repo_obj,
  191. 'pingou'
  192. )
  193. self.assertEqual(has_starred is True, True)
  194. # unstar it and then test for has_starred
  195. repo_obj = pagure.lib.query._get_project(self.session, 'test')
  196. user_obj = pagure.lib.query.get_user(self.session, 'pingou')
  197. msg = pagure.lib.query._unstar_project(
  198. self.session,
  199. repo_obj,
  200. user_obj,
  201. )
  202. self.session.commit()
  203. self.assertEqual(msg, 'You unstarred this project')
  204. user_obj = pagure.lib.query.get_user(self.session, 'pingou')
  205. self.assertEqual(len(user_obj.stars), 0)
  206. # check now, it should return False
  207. has_starred = pagure.lib.query.has_starred(
  208. self.session,
  209. repo_obj,
  210. 'pingou'
  211. )
  212. self.assertEqual(has_starred is False, True)
  213. if __name__ == '__main__':
  214. unittest.main(verbosity=2)