testgithub.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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
  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': 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'}
  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 = ['ACM-Game-Presentation', 'ACM-Portfolio-Presentation', 'angular-nhl', 'async', 'd3-sandbox', 'Deep-Learning', 'Django-Hackathon-Starter',
  53. 'drksephy.github.io', 'el-gamal-attack', 'FizzBuzz-Test-1', 'flux-reactJS', 'fractals', 'git-api', 'git-technetium', 'hackathon-starter',
  54. 'hackcity', 'hackcity.github.io', 'historicalWeather', 'I4330', 'integrated-chinese', 'jsrecipes', 'learn-angularJS', 'legionJS', 'lehman-hackathon',
  55. 'mean-sandbox', 'mean-stack-talk', 'NOAA-Projects', 'node', 'nodeapps', 'pascal-compiler', 'pascal-js', 'Project-Euler', 'python-imp-interpreter',
  56. 'rst2pdf', 'rust-by-example', 'rust-sandbox', 'satellizer', 'smw-asm', 'swift-sandbox', 'Tales-of-Kratos', 'theano-sandbox', 'todo', 'TV-Show-Premieres',
  57. 'tv-show-tracker', 'vitanao', 'WaterEmblem', 'webauth-ssh-authentication', 'webauth-via-ssh', 'WebRing', 'yabe']
  58. self.assertEqual(getUserRepositories(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. pageNumber = 1
  65. jsonList = []
  66. forkedRepositories = []
  67. while True:
  68. with patch('hackathon.scripts.github.getUserRepositories') as mock_getForkedRepositories:
  69. mock_getForkedRepositories.return_value = { "id": 22388667, "name": "ACM-Game-Presentation" }
  70. jsonList.append(mock_getForkedRepositories.return_value)
  71. if len(mock_getForkedRepositories.return_value) < 30:
  72. break
  73. elif len(mock_getForkedRepositories.return_value) >= 30:
  74. pageNumber += 1
  75. 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'}]
  76. self.assertEqual(getForkedRepositories(clientID, clientSecret), forkedRepositories)
  77. def testGetTopContributedRepositories(self):
  78. '''Test for github.py testGetTopContributedRepositories'''
  79. # Client and Secret ID
  80. clientID = self.clientID
  81. clientSecret = self.clientSecret
  82. repos = ['async']
  83. jsonList = []
  84. for repo in repos:
  85. with patch('hackathon.scripts.github.getTopContributedRepositories') as mock_getTopContributedRepositories:
  86. mock_getTopContributedRepositories.return_value = [{'total': 85, 'repo_name': 'ACM-Game-Presentation', 'author': 'DrkSephy'},
  87. {'total': 16, 'repo_name': 'ACM-Portfolio-Presentation', 'author': 'DrkSephy'},
  88. {'total': 17, 'repo_name': 'angular-nhl', 'author': 'DrkSephy'},
  89. {'total': 1, 'repo_name': 'async', 'author': 'DrkSephy'},
  90. {'total': 55, 'repo_name': 'd3-sandbox', 'author': 'DrkSephy'},
  91. {'total': 7, 'repo_name': 'Deep-Learning', 'author': 'DrkSephy'},
  92. {'total': 11, 'repo_name': 'Django-Hackathon-Starter', 'author': 'DrkSephy'},
  93. {'total': 433, 'repo_name': 'drksephy.github.io', 'author': 'DrkSephy'},
  94. {'total': 3, 'repo_name': 'el-gamal-attack', 'author': 'DrkSephy'},
  95. {'total': 1, 'repo_name': 'FizzBuzz-Test-1', 'author': 'DrkSephy'},
  96. {'total': 44, 'repo_name': 'flux-reactJS', 'author': 'DrkSephy'},
  97. {'total': 4, 'repo_name': 'fractals', 'author': 'DrkSephy'}]
  98. jsonList.append(mock_getTopContributedRepositories.return_value)
  99. parsedData = []
  100. indexNumber = -1
  101. for entry in jsonList:
  102. indexNumber += 1
  103. commits = {}
  104. for item in entry:
  105. if item['author'] == 'DrkSephy':
  106. commits['author'] = item['author']
  107. commits['total'] = item['total']
  108. commits['repo_name'] = item['repo_name']
  109. parsedData.append(commits)
  110. parsedData = [{'total': 1, 'repo_name': 'async', 'author': 'DrkSephy'}]
  111. self.assertEqual(getTopContributedRepositories(repos, clientID, clientSecret), parsedData)