Skip to content

Commit

Permalink
Improve coverage, fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquev6 committed Feb 18, 2012
1 parent cbbe08a commit 6f73ad2
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 12 deletions.
40 changes: 40 additions & 0 deletions github/Github.UnitTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import unittest
import MockMockMock

from Github import Github

class TestCase( unittest.TestCase ):
def setUp( self ):
unittest.TestCase.setUp( self )

self.g = Github( "login", "password" )

self.requester = MockMockMock.Mock( "requester" )
self.g._Github__requester = self.requester.object

def tearDown( self ):
self.requester.tearDown()
unittest.TestCase.tearDown( self )

def testCreateForkForUser( self ):
self.requester.expect.dataRequest( "GET", "/users/xxx", None, None ).andReturn( { "login": "xxx" } )
self.requester.expect.dataRequest( "GET", "/repos/xxx/yyy", None, None ).andReturn( { "name": "yyy", "owner": { "login": "xxx" } } )
self.requester.expect.dataRequest( "POST", "/repos/xxx/yyy/forks", None, None ).andReturn( { "name": "yyy", "owner": { "login": "login" } } )
self.g.get_user().create_fork( self.g.get_user( "xxx" ).get_repo( "yyy" ) )

def testCreateForkForOrganization( self ):
self.requester.expect.dataRequest( "GET", "/orgs/ooo", None, None ).andReturn( { "login": "ooo" } )
self.requester.expect.dataRequest( "GET", "/users/xxx", None, None ).andReturn( { "login": "xxx" } )
self.requester.expect.dataRequest( "GET", "/repos/xxx/yyy", None, None ).andReturn( { "name": "yyy", "owner": { "login": "xxx" } } )
self.requester.expect.dataRequest( "POST", "/repos/xxx/yyy/forks", { "org": "ooo" }, None ).andReturn( { "name": "yyy", "owner": { "login": "ooo" } } )
self.g.get_organization( "ooo" ).create_fork( self.g.get_user( "xxx" ).get_repo( "yyy" ) )

def testQueryFollowing( self ):
self.requester.expect.dataRequest( "GET", "/users/xxx", None, None ).andReturn( { "login": "xxx" } )
self.requester.expect.statusRequest( "GET", "/user/following/xxx", None, None ).andReturn( 404 )
self.requester.expect.dataRequest( "GET", "/users/yyy", None, None ).andReturn( { "login": "yyy" } )
self.requester.expect.statusRequest( "GET", "/user/following/yyy", None, None ).andReturn( 204 )
self.assertFalse( self.g.get_user().has_in_following( self.g.get_user( "xxx" ) ) )
self.assertTrue( self.g.get_user().has_in_following( self.g.get_user( "yyy" ) ) )

unittest.main()
39 changes: 39 additions & 0 deletions github/GithubObject.UnitTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,43 @@ def testCallMethod( self ):
self.assertEqual( self.o.myMethod( mock.object, 42 ), 72 )
mock.tearDown()

class GithubObjectWithSeveralBasicAttributesAndComplexAttributes( TestCaseWithGithubTestObject ):
ContainedObject = GithubObject(
"ContainedObject",
BaseUrl( lambda obj: "/test/a3s/" + obj.id ),
BasicAttributes( "id", "name" )
)

GithubTestObject = GithubObject(
"GithubTestObject",
BaseUrl( lambda obj: "/test" ),
BasicAttributes( "a2", "a4" ),
BasicAttributes( "a1", "a3" ),
ComplexAttribute( "a5", ContainedObject ),
)

def testCompletionInOneCall_1( self ):
self.expectDataGet( "/test" ).andReturn( {} )
self.assertIsNone( self.o.a3 )
self.assertIsNone( self.o.a4 )
self.assertIsNone( self.o.a5 )

def testCompletionInOneCall_2( self ):
self.expectDataGet( "/test" ).andReturn( {} )
self.assertIsNone( self.o.a4 )
self.assertIsNone( self.o.a3 )
self.assertIsNone( self.o.a5 )

def testCompletionInOneCall_3( self ):
self.expectDataGet( "/test" ).andReturn( {} )
self.assertIsNone( self.o.a5 )
self.assertIsNone( self.o.a3 )
self.assertIsNone( self.o.a4 )

def testCompletionInOneCall_4( self ):
self.expectDataGet( "/test" ).andReturn( {} )
self.assertIsNone( self.o.a5 )
self.assertIsNone( self.o.a4 )
self.assertIsNone( self.o.a3 )

unittest.main()
28 changes: 17 additions & 11 deletions github/GithubObject.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ def getValueFromRawValue( self, obj, rawValue ):

def updateAttributes( self, obj ):
attributes = obj._github._dataRequest( "GET", obj._baseUrl, None, None )
for attributeName in self.__attributeNames:
if attributeName not in attributes:
attributes[ attributeName ] = None
obj._updateAttributes( attributes )
obj._markAsCompleted()

def isLazy( self ):
return True

def __init__( self, *attributeNames ):
self.__attributeNames = attributeNames
Expand All @@ -37,10 +38,11 @@ def getValueFromRawValue( self, obj, rawValue ):

def updateAttributes( self, obj ):
attributes = obj._github._dataRequest( "GET", obj._baseUrl, None, None )
# for attributeName in self.__attributeNames:
# if attributeName not in attributes:
# attributes[ attributeName ] = None
obj._updateAttributes( attributes )
obj._markAsCompleted()

def isLazy( self ):
return True

def __init__( self, attributeName, type ):
self.__attributeName = attributeName
Expand All @@ -61,6 +63,9 @@ def getValueFromRawValue( self, obj, rawValue ):
def updateAttributes( self, obj ):
obj._updateAttributes( { self.__name: self.__callable( obj ) } )

def isLazy( self ):
return False

def __init__( self, name, callable ):
self.__name = name
self.__callable = callable
Expand Down Expand Up @@ -230,11 +235,12 @@ def __getattr__( self, attributeName ):
def _updateAttributes( self, attributes ):
for attributeName, attributeValue in attributes.iteritems():
attributeDefinition = GithubObject.__attributeDefinitions[ attributeName ]
if attributeValue is None:
if attributeName not in self.__attributes:
self.__attributes[ attributeName ] = None
else:
self.__attributes[ attributeName ] = attributeDefinition.getValueFromRawValue( self, attributeValue )
self.__attributes[ attributeName ] = attributeDefinition.getValueFromRawValue( self, attributeValue )

def _markAsCompleted( self ):
for attributeName, attributeDefinition in GithubObject.__attributeDefinitions.iteritems():
if attributeDefinition.isLazy() and attributeName not in self.__attributes:
self.__attributes[ attributeName ] = None

def __dir__( self ):
return GithubObject.__attributeDefinitions.keys()
Expand Down
2 changes: 1 addition & 1 deletion github/GithubObjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,5 @@ def __createForkForUser( user, repo ):
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 }, None ), 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 ) )

0 comments on commit 6f73ad2

Please sign in to comment.