mirror of
https://github.com/status-im/PyGithub.git
synced 2026-08-31 10:51:14 +00:00
Add unit tests for requests
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import unittest
|
||||
import MockMockMock
|
||||
import httplib
|
||||
import base64
|
||||
|
||||
from Github import Github
|
||||
|
||||
class TestCase( unittest.TestCase ):
|
||||
def setUp( self ):
|
||||
unittest.TestCase.setUp( self )
|
||||
|
||||
self.g = Github( "login", "password" )
|
||||
self.b64_userpass = base64.b64encode( "login:password" )
|
||||
self.b64_userpass = self.b64_userpass.replace( '\n', '' )
|
||||
|
||||
self.connectionFactory = MockMockMock.Mock( "httplib.HTTPSConnection" )
|
||||
self.connection = MockMockMock.Mock( "connection", self.connectionFactory )
|
||||
self.response = MockMockMock.Mock( "response", self.connectionFactory )
|
||||
|
||||
httplib.HTTPSConnection = self.connectionFactory.object
|
||||
|
||||
def tearDown( self ):
|
||||
self.connectionFactory.tearDown()
|
||||
unittest.TestCase.tearDown( self )
|
||||
|
||||
def expect( self, verb, url, input, status, responseHeaders, output ):
|
||||
self.connectionFactory.expect( "api.github.com", strict = True ).andReturn( self.connection.object )
|
||||
self.connection.expect.request( verb, url, input, { "Authorization" : "Basic " + self.b64_userpass } )
|
||||
self.connection.expect.getresponse().andReturn( self.response.object )
|
||||
self.response.expect.status.andReturn( status )
|
||||
self.response.expect.getheaders().andReturn( responseHeaders )
|
||||
self.response.expect.read().andReturn( output )
|
||||
self.connection.expect.close()
|
||||
|
||||
def testSimpleStatus( self ):
|
||||
self.expect( "GET", "/test", "null", 200, [], "" )
|
||||
self.assertEqual( self.g._statusRequest( "GET", "/test", None, None ), 200 )
|
||||
|
||||
def testSimpleData( self ):
|
||||
self.expect( "GET", "/test", "null", 200, [], '{ "foo": "bar" }' )
|
||||
self.assertEqual( self.g._dataRequest( "GET", "/test", None, None ), { "foo" : "bar" } )
|
||||
|
||||
unittest.main()
|
||||
+6
-6
@@ -9,15 +9,15 @@ class Github:
|
||||
self.__login = login
|
||||
self.__password = password
|
||||
|
||||
def _dataRequest( self, verb, url, data = None ):
|
||||
status, headers, data = self.__rawRequest( verb, url, data )
|
||||
def _dataRequest( self, verb, url, parameters, data ):
|
||||
status, headers, data = self.__rawRequest( verb, url, parameters, data )
|
||||
return data
|
||||
|
||||
def _statusRequest( self, verb, url, data = None ):
|
||||
status, headers, data = self.__rawRequest( verb, url, data )
|
||||
def _statusRequest( self, verb, url, parameters, data ):
|
||||
status, headers, data = self.__rawRequest( verb, url, parameters, data )
|
||||
return status
|
||||
|
||||
def __rawRequest( self, verb, url, input ):
|
||||
def __rawRequest( self, verb, url, parameters, input ):
|
||||
assert verb in [ "HEAD", "GET", "POST", "PATCH", "PUT", "DELETE" ]
|
||||
|
||||
b64_userpass = base64.b64encode( '%s:%s' % ( self.__login, self.__password ) )
|
||||
@@ -30,7 +30,7 @@ class Github:
|
||||
response = cnx.getresponse()
|
||||
|
||||
status = response.status
|
||||
headers = response.getheaders()
|
||||
headers = dict( response.getheaders() )
|
||||
output = response.read()
|
||||
if len( output ) == 0:
|
||||
output = None
|
||||
|
||||
@@ -23,22 +23,22 @@ class TestCaseWithGithubTestObject( unittest.TestCase ):
|
||||
unittest.TestCase.tearDown( self )
|
||||
|
||||
def expectDataGet( self, url ):
|
||||
return self.g.expect._dataRequest( "GET", url )
|
||||
return self.g.expect._dataRequest( "GET", url, None, None )
|
||||
|
||||
def expectStatusPut( self, url ):
|
||||
return self.g.expect._statusRequest( "PUT", url )
|
||||
return self.g.expect._statusRequest( "PUT", url, None, None )
|
||||
|
||||
def expectStatusGet( self, url ):
|
||||
return self.g.expect._statusRequest( "GET", url )
|
||||
return self.g.expect._statusRequest( "GET", url, None, None )
|
||||
|
||||
def expectDataPatch( self, url, data ):
|
||||
return self.g.expect._dataRequest( "PATCH", url, data )
|
||||
return self.g.expect._dataRequest( "PATCH", url, None, data )
|
||||
|
||||
def expectDataPost( self, url, data ):
|
||||
return self.g.expect._dataRequest( "POST", url, data )
|
||||
return self.g.expect._dataRequest( "POST", url, None, data )
|
||||
|
||||
def expectStatusDelete( self, url ):
|
||||
return self.g.expect._statusRequest( "DELETE", url )
|
||||
return self.g.expect._statusRequest( "DELETE", url, None, None )
|
||||
|
||||
class GithubObjectWithOnlyBasicAttributes( TestCaseWithGithubTestObject ):
|
||||
GithubTestObject = GithubObject(
|
||||
|
||||
+10
-10
@@ -12,7 +12,7 @@ class BasicAttributes:
|
||||
return rawValue
|
||||
|
||||
def updateAttributes( self, obj ):
|
||||
attributes = obj._github._dataRequest( "GET", obj._baseUrl )
|
||||
attributes = obj._github._dataRequest( "GET", obj._baseUrl, None, None )
|
||||
for attributeName in self.__attributeNames:
|
||||
if attributeName not in attributes:
|
||||
attributes[ attributeName ] = None
|
||||
@@ -36,7 +36,7 @@ class ComplexAttribute:
|
||||
return self.__type( obj._github, rawValue, lazy = True )
|
||||
|
||||
def updateAttributes( self, obj ):
|
||||
attributes = obj._github._dataRequest( "GET", obj._baseUrl )
|
||||
attributes = obj._github._dataRequest( "GET", obj._baseUrl, None, None )
|
||||
# for attributeName in self.__attributeNames:
|
||||
# if attributeName not in attributes:
|
||||
# attributes[ attributeName ] = None
|
||||
@@ -106,20 +106,20 @@ class ListOfReferences:
|
||||
def __executeGet( self, obj ):
|
||||
return [
|
||||
self.__type( obj._github, attributes, lazy = True )
|
||||
for attributes in obj._github._dataRequest( "GET", obj._baseUrl + "/" + self.__attributeName )
|
||||
for attributes in obj._github._dataRequest( "GET", obj._baseUrl + "/" + self.__attributeName, None, None )
|
||||
]
|
||||
|
||||
def __executeAdd( self, obj, toBeAdded ):
|
||||
assert isinstance( toBeAdded, self.__type )
|
||||
obj._github._statusRequest( "PUT", obj._baseUrl + "/" + self.__attributeName + "/" + toBeAdded._identity )
|
||||
obj._github._statusRequest( "PUT", obj._baseUrl + "/" + self.__attributeName + "/" + toBeAdded._identity, None, None )
|
||||
|
||||
def __executeRemove( self, obj, toBeDeleted ):
|
||||
assert isinstance( toBeDeleted, self.__type )
|
||||
obj._github._statusRequest( "DELETE", obj._baseUrl + "/" + self.__attributeName + "/" + toBeDeleted._identity )
|
||||
obj._github._statusRequest( "DELETE", obj._baseUrl + "/" + self.__attributeName + "/" + toBeDeleted._identity, None, None )
|
||||
|
||||
def __executeHas( self, obj, toBeQueried ):
|
||||
assert isinstance( toBeQueried, self.__type )
|
||||
return obj._github._statusRequest( "GET", obj._baseUrl + "/" + self.__attributeName + "/" + toBeQueried._identity ) == 204
|
||||
return obj._github._statusRequest( "GET", obj._baseUrl + "/" + self.__attributeName + "/" + toBeQueried._identity, None, None ) == 204
|
||||
|
||||
class ListOfObjects:
|
||||
def __init__( self, attributeName, type, creatable = False, singularName = None ):
|
||||
@@ -139,11 +139,11 @@ class ListOfObjects:
|
||||
def __executeGet( self, obj ):
|
||||
return [
|
||||
self.__type( obj._github, attributes, lazy = True )
|
||||
for attributes in obj._github._dataRequest( "GET", obj._baseUrl + "/" + self.__attributeName )
|
||||
for attributes in obj._github._dataRequest( "GET", obj._baseUrl + "/" + self.__attributeName, None, None )
|
||||
]
|
||||
|
||||
def __executeCreate( self, obj, **data ):
|
||||
return self.__type( obj._github, obj._github._dataRequest( "POST", obj._baseUrl + "/" + self.__attributeName, data ), lazy = True )
|
||||
return self.__type( obj._github, obj._github._dataRequest( "POST", obj._baseUrl + "/" + self.__attributeName, None, data ), lazy = True )
|
||||
|
||||
class Editable:
|
||||
def __init__( self, mandatoryParameters, optionalParameters ):
|
||||
@@ -161,7 +161,7 @@ class Editable:
|
||||
for argumentName in kwds:
|
||||
if argumentName not in itertools.chain( self.__mandatoryParameters, self.__optionalParameters ):
|
||||
raise TypeError()
|
||||
attributes = obj._github._dataRequest( "PATCH", obj._baseUrl, kwds )
|
||||
attributes = obj._github._dataRequest( "PATCH", obj._baseUrl, None, kwds )
|
||||
obj._updateAttributes( attributes )
|
||||
|
||||
class Deletable:
|
||||
@@ -169,7 +169,7 @@ class Deletable:
|
||||
cls._addMethod( "delete", self.__execute )
|
||||
|
||||
def __execute( self, obj, *args, **kwds ):
|
||||
obj._github._statusRequest( "DELETE", obj._baseUrl )
|
||||
obj._github._statusRequest( "DELETE", obj._baseUrl, None, None )
|
||||
|
||||
class MethodFromCallable:
|
||||
def __init__( self, name, callable ):
|
||||
|
||||
@@ -95,9 +95,9 @@ NamedUser._addAttributePolicy( ListOfReferences( "watched", Repository ) )
|
||||
|
||||
def __createForkForUser( user, repo ):
|
||||
assert isinstance( repo, Repository )
|
||||
return Repository( user._github, user._github._dataRequest( "POST", repo._baseUrl + "/forks" ), lazy = True )
|
||||
return Repository( user._github, user._github._dataRequest( "POST", repo._baseUrl + "/forks", None, None ), lazy = True )
|
||||
AuthenticatedUser._addAttributePolicy( MethodFromCallable( "create_fork", __createForkForUser ) )
|
||||
def __createForkForOrg( org, repo ):
|
||||
assert isinstance( repo, Repository )
|
||||
return Repository( org._github, org._github._dataRequest( "POST", repo._baseUrl + "/forks?org=" + org.login ), lazy = True )
|
||||
return Repository( org._github, org._github._dataRequest( "POST", repo._baseUrl + "/forks", { "org=": org.login }, None ), lazy = True )
|
||||
Organization._addAttributePolicy( MethodFromCallable( "create_fork", __createForkForOrg ) )
|
||||
|
||||
Reference in New Issue
Block a user