testgithub.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import unittest
  2. from mock import Mock, patch, MagicMock
  3. from django.conf import settings
  4. from hackathon.scripts.github import getUserData, getUserRepositories, getForkedRepositories, getTopContributedRepositories, getStarGazerCount, filterStarGazerCount, filterCommits
  5. class GithubTests(unittest.TestCase):
  6. def setUp(self):
  7. self.API_BASE_URL = 'https://api.github.com/users/DrkSephy'
  8. self.clientID = 'client_id=2404a1e21aebd902f6db'
  9. self.clientSecret = 'client_secret=3da44769d4b7c9465fa4c812669148a163607c23'
  10. def testGetUserData(self):
  11. '''Test for github.py getUserData method'''
  12. # Client and Secret ID
  13. clientID = self.clientID
  14. clientSecret = self.clientSecret
  15. user = 'DrkSephy'
  16. # Construct the URL
  17. self.url = self.API_BASE_URL + '?' + clientID + '&' + clientSecret
  18. # Instantiate data collection objects
  19. jsonList = []
  20. parsedData = []
  21. userData = {}
  22. with patch('hackathon.scripts.github.getUserData') as mock_getUserData:
  23. # Mock the return value of this method
  24. mock_getUserData.return_value = getUserData(user, clientID, clientSecret)
  25. jsonList.append(mock_getUserData.return_value)
  26. for data in jsonList:
  27. userData['name'] = mock_getUserData.return_value['name']
  28. userData['blog'] = mock_getUserData.return_value['blog']
  29. userData['email'] = mock_getUserData.return_value['email']
  30. userData['public_gists'] = mock_getUserData.return_value['public_gists']
  31. userData['public_repos'] = mock_getUserData.return_value['public_repos']
  32. userData['avatar_url'] = mock_getUserData.return_value['avatar_url']
  33. userData['followers'] = mock_getUserData.return_value['followers']
  34. userData['following'] = mock_getUserData.return_value['following']
  35. parsedData.append(userData)
  36. self.assertEqual(getUserData(user, clientID, clientSecret), parsedData)
  37. def testGetUserRepositories(self):
  38. '''Test for github.py getUserRepositories'''
  39. # Client and Secret ID
  40. clientID = self.clientID
  41. clientSecret = self.clientSecret
  42. user = 'DrkSephy'
  43. pageNumber = 1
  44. jsonList = []
  45. repositories = []
  46. while True:
  47. with patch('hackathon.scripts.github.getUserRepositories') as mock_getUserRepositories:
  48. mock_getUserRepositories.return_value = { "id": 22388667, "name": "ACM-Game-Presentation" }
  49. jsonList.append(mock_getUserRepositories.return_value)
  50. if len(mock_getUserRepositories.return_value) < 30:
  51. break
  52. elif len(mock_getUserRepositories.return_value) >= 30:
  53. pageNumber += 1
  54. repositories = getUserRepositories(user, clientID, clientSecret)
  55. self.assertEqual(getUserRepositories(user, clientID, clientSecret), repositories)
  56. def testGetForkedRepositories(self):
  57. '''Test for github.py getForkedRepositories'''
  58. # Client and Secret ID
  59. clientID = self.clientID
  60. clientSecret = self.clientSecret
  61. user = 'DrkSephy'
  62. pageNumber = 1
  63. jsonList = []
  64. forkedRepositories = []
  65. while True:
  66. with patch('hackathon.scripts.github.getUserRepositories') as mock_getForkedRepositories:
  67. mock_getForkedRepositories.return_value = { "id": 22388667, "name": "ACM-Game-Presentation" }
  68. jsonList.append(mock_getForkedRepositories.return_value)
  69. if len(mock_getForkedRepositories.return_value) < 30:
  70. break
  71. elif len(mock_getForkedRepositories.return_value) >= 30:
  72. pageNumber += 1
  73. forkedRepositories = [{'name': 'async'}, {'name': 'FizzBuzz-Test-1'}, {'name': 'hackathon-starter'}, {'name': 'historicalWeather'}, {'name': 'jsrecipes'}, {'name': 'node'}, {'name': 'rst2pdf'}, {'name': 'rust-by-example'}, {'name': 'satellizer'}, {'name': 'vitanao'}, {'name': 'WaterEmblem'}, {'name': 'webauth-via-ssh'}]
  74. self.assertEqual(getForkedRepositories(user, clientID, clientSecret), forkedRepositories)
  75. def testGetTopContributedRepositories(self):
  76. '''Test for github.py getTopContributedRepositories'''
  77. # Client and Secret ID
  78. clientID = self.clientID
  79. clientSecret = self.clientSecret
  80. repos = ['async']
  81. user = 'DrkSephy'
  82. jsonList = []
  83. for repo in repos:
  84. with patch('hackathon.scripts.github.getTopContributedRepositories') as mock_getTopContributedRepositories:
  85. mock_getTopContributedRepositories.return_value = [{'total': 85, 'repo_name': 'ACM-Game-Presentation', 'author': 'DrkSephy'},
  86. {'total': 16, 'repo_name': 'ACM-Portfolio-Presentation', 'author': 'DrkSephy'},
  87. {'total': 17, 'repo_name': 'angular-nhl', 'author': 'DrkSephy'},
  88. {'total': 1, 'repo_name': 'async', 'author': 'DrkSephy'},
  89. {'total': 55, 'repo_name': 'd3-sandbox', 'author': 'DrkSephy'},
  90. {'total': 7, 'repo_name': 'Deep-Learning', 'author': 'DrkSephy'},
  91. {'total': 11, 'repo_name': 'Django-Hackathon-Starter', 'author': 'DrkSephy'},
  92. {'total': 433, 'repo_name': 'drksephy.github.io', 'author': 'DrkSephy'},
  93. {'total': 3, 'repo_name': 'el-gamal-attack', 'author': 'DrkSephy'},
  94. {'total': 1, 'repo_name': 'FizzBuzz-Test-1', 'author': 'DrkSephy'},
  95. {'total': 44, 'repo_name': 'flux-reactJS', 'author': 'DrkSephy'},
  96. {'total': 4, 'repo_name': 'fractals', 'author': 'DrkSephy'}]
  97. jsonList.append(mock_getTopContributedRepositories.return_value)
  98. parsedData = []
  99. indexNumber = -1
  100. for entry in jsonList:
  101. indexNumber += 1
  102. commits = {}
  103. for item in entry:
  104. if item['author'] == 'DrkSephy':
  105. commits['author'] = item['author']
  106. commits['total'] = item['total']
  107. commits['repo_name'] = item['repo_name']
  108. parsedData.append(commits)
  109. parsedData = [{'total': 1, 'repo_name': 'async', 'author': 'DrkSephy'}]
  110. self.assertEqual(getTopContributedRepositories(user, repos, clientID, clientSecret), parsedData)
  111. def testGetStarGazerCount(self):
  112. '''Test for github.py getStarGazerCount'''
  113. # Client and Secret ID
  114. clientID = self.clientID
  115. clientSecret = self.clientSecret
  116. user = 'DrkSephy'
  117. pageNumber = 1
  118. jsonList = []
  119. stargazers = []
  120. while True:
  121. with patch('hackathon.scripts.github.getStarGazerCount') as mock_getStarGazerCount:
  122. mock_getStarGazerCount.return_value = { "id": 22388667, "name": "ACM-Game-Presentation" }
  123. jsonList.append(mock_getStarGazerCount.return_value)
  124. if len(mock_getStarGazerCount.return_value) < 30:
  125. break
  126. elif len(mock_getStarGazerCount.return_value) >= 30:
  127. pageNumber += 1
  128. stargazers = getStarGazerCount(user, clientID, clientSecret)
  129. self.assertEqual(getStarGazerCount(user, clientID, clientSecret), stargazers)
  130. def testFilterStarGazerCount(self):
  131. '''Test for github.py filterStarGazerCount'''
  132. maxStars = []
  133. data = [{'stargazers_count': 0, 'name': 'ACM-Game-Presentation'}, {'stargazers_count': 1, 'name': 'ACM-Portfolio-Presentation'}, {'stargazers_count': 2, 'name': 'angular-nhl'}, {'stargazers_count': 0, 'name': 'async'}, {'stargazers_count': 0, 'name': 'd3-sandbox'}, {'stargazers_count': 3, 'name': 'Deep-Learning'}, {'stargazers_count': 0, 'name': 'Django-Hackathon-Starter'}, {'stargazers_count': 0, 'name': 'drksephy.github.io'}, {'stargazers_count': 0, 'name': 'el-gamal-attack'}, {'stargazers_count': 0, 'name': 'FizzBuzz-Test-1'}, {'stargazers_count': 0, 'name': 'flux-reactJS'}, {'stargazers_count': 0, 'name': 'fractals'}, {'stargazers_count': 0, 'name': 'git-api'}, {'stargazers_count': 1, 'name': 'git-technetium'}, {'stargazers_count': 0, 'name': 'hackathon-starter'}, {'stargazers_count': 0, 'name': 'hackcity'}, {'stargazers_count': 0, 'name': 'hackcity.github.io'}, {'stargazers_count': 0, 'name': 'historicalWeather'}, {'stargazers_count': 0, 'name': 'I4330'}, {'stargazers_count': 1, 'name': 'integrated-chinese'}, {'stargazers_count': 0, 'name': 'jsrecipes'}, {'stargazers_count': 0, 'name': 'learn-angularJS'}, {'stargazers_count': 1, 'name': 'legionJS'}, {'stargazers_count': 0, 'name': 'lehman-hackathon'}, {'stargazers_count': 0, 'name': 'mean-sandbox'}, {'stargazers_count': 0, 'name': 'mean-stack-talk'}, {'stargazers_count': 2, 'name': 'NOAA-Projects'}, {'stargazers_count': 0, 'name': 'node'}, {'stargazers_count': 0, 'name': 'nodeapps'}, {'stargazers_count': 1, 'name': 'pascal-compiler'}, {'stargazers_count': 0, 'name': 'pascal-js'}, {'stargazers_count': 0, 'name': 'Project-Euler'}, {'stargazers_count': 0, 'name': 'python-imp-interpreter'}, {'stargazers_count': 0, 'name': 'rst2pdf'}, {'stargazers_count': 0, 'name': 'rust-by-example'}, {'stargazers_count': 1, 'name': 'rust-sandbox'}, {'stargazers_count': 0, 'name': 'satellizer'}, {'stargazers_count': 0, 'name': 'smw-asm'}, {'stargazers_count': 2, 'name': 'swift-sandbox'}, {'stargazers_count': 1, 'name': 'Tales-of-Kratos'}, {'stargazers_count': 0, 'name': 'theano-sandbox'}, {'stargazers_count': 0, 'name': 'todo'}, {'stargazers_count': 0, 'name': 'TV-Show-Premieres'}, {'stargazers_count': 0, 'name': 'tv-show-tracker'}, {'stargazers_count': 0, 'name': 'vitanao'}, {'stargazers_count': 0, 'name': 'WaterEmblem'}, {'stargazers_count': 0, 'name': 'webauth-ssh-authentication'}, {'stargazers_count': 0, 'name': 'webauth-via-ssh'}, {'stargazers_count': 0, 'name': 'WebRing'}, {'stargazers_count': 0, 'name': 'yabe'}]
  134. with patch('hackathon.scripts.github.filterStarGazerCount') as mock_filterStarGazerCount:
  135. mock_filterStarGazerCount.return_value = [{'stargazers_count': 3, 'name': 'Deep-Learning'}, {'stargazers_count': 2, 'name': 'angular-nhl'}, {'stargazers_count': 2, 'name': 'NOAA-Projects'}, {'stargazers_count': 2, 'name': 'swift-sandbox'}, {'stargazers_count': 1, 'name': 'ACM-Portfolio-Presentation'}, {'stargazers_count': 1, 'name': 'git-technetium'}, {'stargazers_count': 1, 'name': 'integrated-chinese'}, {'stargazers_count': 1, 'name': 'legionJS'}, {'stargazers_count': 1, 'name': 'pascal-compiler'}]
  136. maxStars = [{'stargazers_count': 3, 'name': 'Deep-Learning'}, {'stargazers_count': 2, 'name': 'angular-nhl'}, {'stargazers_count': 2, 'name': 'NOAA-Projects'}, {'stargazers_count': 2, 'name': 'swift-sandbox'}, {'stargazers_count': 1, 'name': 'ACM-Portfolio-Presentation'}, {'stargazers_count': 1, 'name': 'git-technetium'}, {'stargazers_count': 1, 'name': 'integrated-chinese'}, {'stargazers_count': 1, 'name': 'legionJS'}, {'stargazers_count': 1, 'name': 'pascal-compiler'}]
  137. self.assertEqual(filterStarGazerCount(data), maxStars )
  138. def testFilterCommits(self):
  139. '''Test for github.py filtercommits'''
  140. maxCommits = []
  141. data = filterCommits(data)
  142. with patch('hackathon.scripts.github.filterCommits') as mock_filterCommits:
  143. mock_filterCommits.return_value = filterCommits(data)
  144. maxCommits = filterCommits(data)
  145. self.assertEqual(filterCommits(data), maxCommits)