Skip to content

Instantly share code, notes, and snippets.

@davidkazuhiro
Last active December 18, 2018 12:47
Show Gist options
  • Save davidkazuhiro/bbc850cdb50b95ef67a8404ee5537a99 to your computer and use it in GitHub Desktop.
Save davidkazuhiro/bbc850cdb50b95ef67a8404ee5537a99 to your computer and use it in GitHub Desktop.
import io
import unittest
from unittest.mock import patch
import licenses
import pandas
def mocked_requests_get(*args, **kwargs):
class MockResponse:
def __init__(self, json_data, status_code):
self.json_data = json_data
self.status_code = status_code
def json(self):
return self.json_data
data = {
"seats": 280,
"seats_used": 166,
"seats_available": 114,
"kind": "standard",
"days_until_expiration": 324,
"expire_at": "2019-10-03T23:59:59.000-07:00"
}
return MockResponse(data, 200)
class GitHubLicensesTest(unittest.TestCase):
@patch('requests.get', side_effect=mocked_requests_get)
@patch('pandas.read_csv')
def setUp(self, mock_read_csv, mock_get):
test_data = {'poh': 1,
'david': 2,
'terje': 4}
keys = list(test_data.keys())
values = list(test_data.values())
named = {'name': keys, 'sid': values}
self.test_dataframe = pandas.DataFrame(named)
self.test_series = pandas.Series(test_data)
mock_read_csv.return_value = self.test_dataframe
self.licenses = licenses.GitHubLicenses('fake_user', 'fake_token')
def test_get_percentages_sponsored(self):
# save real methods
real_get_percentages = self.licenses.get_percentages
real_calculate = self.licenses.calculate
# mock methods
self.licenses.calculate = unittest.mock.Mock()
self.licenses.convert_to_excel = unittest.mock.Mock()
# run test
self.licenses.get_percentages(sponsor='sponsor')
self.licenses.calculate.assert_called_with(sponsor='sponsor')
# replace real methods
self.licenses.get_percentages = real_get_percentages
self.licenses.calculate = real_calculate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment