diff --git a/github/Github.UnitTest.py b/github/Github.UnitTest.py new file mode 100644 index 00000000..e3ddfb09 --- /dev/null +++ b/github/Github.UnitTest.py @@ -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() diff --git a/github/GithubObject.UnitTest.py b/github/GithubObject.UnitTest.py index 71685a79..50244c5e 100644 --- a/github/GithubObject.UnitTest.py +++ b/github/GithubObject.UnitTest.py @@ -298,4 +298,43 @@ class GithubObjectWithMethodFromCallable( TestCaseWithGithubTestObject ): 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() diff --git a/github/GithubObject.py b/github/GithubObject.py index dfcba1fb..061e6265 100644 --- a/github/GithubObject.py +++ b/github/GithubObject.py @@ -13,10 +13,11 @@ class BasicAttributes: 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 @@ -37,10 +38,11 @@ class ComplexAttribute: 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 @@ -61,6 +63,9 @@ class AttributeFromCallable: 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 @@ -230,11 +235,12 @@ def GithubObject( className, *attributePolicies ): 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() diff --git a/github/GithubObjects.py b/github/GithubObjects.py index f6fe9a74..7b380200 100644 --- a/github/GithubObjects.py +++ b/github/GithubObjects.py @@ -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 ) )