testgithub.py 14 KB

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