testgithub.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. # Construct the URL
  16. self.url = self.API_BASE_URL + '?' + clientID + '&' + clientSecret
  17. # Instantiate data collection objects
  18. jsonList = []
  19. parsedData = []
  20. userData = {}
  21. with patch('hackathon.scripts.github.getUserData') as mock_getUserData:
  22. # Mock the return value of this method
  23. mock_getUserData.return_value = {'public_repos': 52, 'public_gists': 5, 'name': 'David Leonard', 'blog': 'http://drksephy.github.io', 'avatar_url': 'https://avatars.githubusercontent.com/u/1226900?v=3', 'followers': 51, 'following': 7, 'email': 'DrkSephy1025@gmail.com'}
  24. jsonList.append(mock_getUserData.return_value)
  25. for data in jsonList:
  26. userData['name'] = mock_getUserData.return_value['name']
  27. userData['blog'] = mock_getUserData.return_value['blog']
  28. userData['email'] = mock_getUserData.return_value['email']
  29. userData['public_gists'] = mock_getUserData.return_value['public_gists']
  30. userData['public_repos'] = mock_getUserData.return_value['public_repos']
  31. userData['avatar_url'] = mock_getUserData.return_value['avatar_url']
  32. userData['followers'] = mock_getUserData.return_value['followers']
  33. userData['following'] = mock_getUserData.return_value['following']
  34. parsedData.append(userData)
  35. self.assertEqual(getUserData(clientID, clientSecret), parsedData)
  36. def testGetUserRepositories(self):
  37. '''Test for github.py getUserRepositories'''
  38. # Client and Secret ID
  39. clientID = self.clientID
  40. clientSecret = self.clientSecret
  41. pageNumber = 1
  42. jsonList = []
  43. repositories = []
  44. while True:
  45. with patch('hackathon.scripts.github.getUserRepositories') as mock_getUserRepositories:
  46. mock_getUserRepositories.return_value = { "id": 22388667, "name": "ACM-Game-Presentation" }
  47. jsonList.append(mock_getUserRepositories.return_value)
  48. if len(mock_getUserRepositories.return_value) < 30:
  49. break
  50. elif len(mock_getUserRepositories.return_value) >= 30:
  51. pageNumber += 1
  52. repositories = getUserRepositories(clientID, clientSecret)
  53. self.assertEqual(getUserRepositories(clientID, clientSecret), repositories)
  54. def testGetForkedRepositories(self):
  55. '''Test for github.py getForkedRepositories'''
  56. # Client and Secret ID
  57. clientID = self.clientID
  58. clientSecret = self.clientSecret
  59. pageNumber = 1
  60. jsonList = []
  61. forkedRepositories = []
  62. while True:
  63. with patch('hackathon.scripts.github.getUserRepositories') as mock_getForkedRepositories:
  64. mock_getForkedRepositories.return_value = { "id": 22388667, "name": "ACM-Game-Presentation" }
  65. jsonList.append(mock_getForkedRepositories.return_value)
  66. if len(mock_getForkedRepositories.return_value) < 30:
  67. break
  68. elif len(mock_getForkedRepositories.return_value) >= 30:
  69. pageNumber += 1
  70. 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'}]
  71. self.assertEqual(getForkedRepositories(clientID, clientSecret), forkedRepositories)
  72. def testGetTopContributedRepositories(self):
  73. '''Test for github.py getTopContributedRepositories'''
  74. # Client and Secret ID
  75. clientID = self.clientID
  76. clientSecret = self.clientSecret
  77. repos = ['async']
  78. jsonList = []
  79. for repo in repos:
  80. with patch('hackathon.scripts.github.getTopContributedRepositories') as mock_getTopContributedRepositories:
  81. mock_getTopContributedRepositories.return_value = [{'total': 85, 'repo_name': 'ACM-Game-Presentation', 'author': 'DrkSephy'},
  82. {'total': 16, 'repo_name': 'ACM-Portfolio-Presentation', 'author': 'DrkSephy'},
  83. {'total': 17, 'repo_name': 'angular-nhl', 'author': 'DrkSephy'},
  84. {'total': 1, 'repo_name': 'async', 'author': 'DrkSephy'},
  85. {'total': 55, 'repo_name': 'd3-sandbox', 'author': 'DrkSephy'},
  86. {'total': 7, 'repo_name': 'Deep-Learning', 'author': 'DrkSephy'},
  87. {'total': 11, 'repo_name': 'Django-Hackathon-Starter', 'author': 'DrkSephy'},
  88. {'total': 433, 'repo_name': 'drksephy.github.io', 'author': 'DrkSephy'},
  89. {'total': 3, 'repo_name': 'el-gamal-attack', 'author': 'DrkSephy'},
  90. {'total': 1, 'repo_name': 'FizzBuzz-Test-1', 'author': 'DrkSephy'},
  91. {'total': 44, 'repo_name': 'flux-reactJS', 'author': 'DrkSephy'},
  92. {'total': 4, 'repo_name': 'fractals', 'author': 'DrkSephy'}]
  93. jsonList.append(mock_getTopContributedRepositories.return_value)
  94. parsedData = []
  95. indexNumber = -1
  96. for entry in jsonList:
  97. indexNumber += 1
  98. commits = {}
  99. for item in entry:
  100. if item['author'] == 'DrkSephy':
  101. commits['author'] = item['author']
  102. commits['total'] = item['total']
  103. commits['repo_name'] = item['repo_name']
  104. parsedData.append(commits)
  105. parsedData = [{'total': 1, 'repo_name': 'async', 'author': 'DrkSephy'}]
  106. self.assertEqual(getTopContributedRepositories(repos, clientID, clientSecret), parsedData)
  107. def testGetStarGazerCount(self):
  108. '''Test for github.py getStarGazerCount'''
  109. # Client and Secret ID
  110. clientID = self.clientID
  111. clientSecret = self.clientSecret
  112. pageNumber = 1
  113. jsonList = []
  114. stargazers = []
  115. while True:
  116. with patch('hackathon.scripts.github.getStarGazerCount') as mock_getStarGazerCount:
  117. mock_getStarGazerCount.return_value = { "id": 22388667, "name": "ACM-Game-Presentation" }
  118. jsonList.append(mock_getStarGazerCount.return_value)
  119. if len(mock_getStarGazerCount.return_value) < 30:
  120. break
  121. elif len(mock_getStarGazerCount.return_value) >= 30:
  122. pageNumber += 1
  123. stargazers = getStarGazerCount(clientID, clientSecret)
  124. self.assertEqual(getStarGazerCount(clientID, clientSecret), stargazers)
  125. def testFilterStarGazerCount(self):
  126. '''Test for github.py filterStarGazerCount'''
  127. maxStars = []
  128. 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'}]
  129. with patch('hackathon.scripts.github.filterStarGazerCount') as mock_filterStarGazerCount:
  130. 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'}]
  131. 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'}]
  132. self.assertEqual(filterStarGazerCount(data), maxStars )
  133. def testFilterCommits(self):
  134. '''Test for github.py filtercommits'''
  135. maxCommits = []
  136. 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'}]
  137. with patch('hackathon.scripts.github.filterCommits') as mock_filterCommits:
  138. 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'}]
  139. 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'}]
  140. self.assertEqual(filterCommits(data), maxCommits)