diff --git a/github/GithubObject.UnitTest.py b/github/GithubObject.UnitTest.py index 0dc91eb2..32b89ef1 100644 --- a/github/GithubObject.UnitTest.py +++ b/github/GithubObject.UnitTest.py @@ -34,6 +34,9 @@ class TestCaseWithGithubTestObject( unittest.TestCase ): def expectDataPatch( self, url, data ): return self.g.expect._dataRequest( "PATCH", url, data ) + def expectDataPost( self, url, data ): + return self.g.expect._dataRequest( "POST", url, data ) + def expectStatusDelete( self, url ): return self.g.expect._statusRequest( "DELETE", url ) @@ -205,6 +208,28 @@ class GithubObjectWithListOfReferences( TestCaseWithGithubTestObject ): self.expectDataGet( "/test/a3s/id1" ).andReturn( { "name": "name1" } ) self.assertEqual( a3s[ 0 ].name, "name1" ) +class GithubObjectWithListOfObjects( TestCaseWithGithubTestObject ): + ContainedObject = GithubObject( + "ContainedObject", + BaseUrl( lambda obj: "/test/a3s/" + obj.id ), + BasicAttributes( "id", "name" ) + ) + + GithubTestObject = GithubObject( + "GithubTestObject", + BaseUrl( lambda obj: "/test" ), + BasicAttributes( "a1", "a2" ), + ListOfObjects( "a3s", ContainedObject ) + ) + + def testGetList( self ): + self.expectDataGet( "/test/a3s" ).andReturn( [ { "id": "id1" }, { "id": "id2" }, { "id": "id3" } ] ) + a3s = self.o.get_a3s() + self.assertEqual( len( a3s ), 3 ) + self.assertEqual( a3s[ 0 ].id, "id1" ) + self.expectDataGet( "/test/a3s/id1" ).andReturn( { "name": "name1" } ) + self.assertEqual( a3s[ 0 ].name, "name1" ) + class GithubObjectWithModifiableListOfReferences( TestCaseWithGithubTestObject ): ContainedObject = GithubObject( "ContainedObject", @@ -237,6 +262,25 @@ class GithubObjectWithModifiableListOfReferences( TestCaseWithGithubTestObject ) self.expectStatusGet( "/test/a3s/idQuery" ).andReturn( 404 ) self.assertFalse( self.o.has_in_a3s( a3ToQuery ) ) +class GithubObjectWithModifiableListOfObjects( TestCaseWithGithubTestObject ): + ContainedObject = GithubObject( + "ContainedObject", + BaseUrl( lambda obj: "/test/a3s/" + obj.id ), + Identity( lambda obj: obj.id ), + BasicAttributes( "id", "name" ), + ) + + GithubTestObject = GithubObject( + "GithubTestObject", + BaseUrl( lambda obj: "/test" ), + BasicAttributes( "a1", "a2" ), + ListOfObjects( "a3s", ContainedObject, creatable = True ) + ) + + def testCreate( self ): + self.expectDataPost( "/test/a3s", { "name": "nameCreate" } ).andReturn( { "id": "idCreate" } ) + self.assertEqual( self.o.create_a3s( name = "nameCreate" ).id, "idCreate" ) + def myCallable( obj, mock, arg ): return mock.call( arg ) diff --git a/github/GithubObject.py b/github/GithubObject.py index 6d5263a5..fe10f727 100644 --- a/github/GithubObject.py +++ b/github/GithubObject.py @@ -103,6 +103,12 @@ class ListOfReferences: if self.__hasName is not None: cls._addMethod( self.__hasName, self.__executeHas ) + def __executeGet( self, obj ): + return [ + self.__type( obj._github, attributes, lazy = True ) + for attributes in obj._github._dataRequest( "GET", obj._baseUrl + "/" + self.__attributeName ) + ] + def __executeAdd( self, obj, toBeAdded ): assert( isinstance( toBeAdded, self.__type ) ) obj._github._statusRequest( "PUT", obj._baseUrl + "/" + self.__attributeName + "/" + toBeAdded._identity ) @@ -115,12 +121,30 @@ class ListOfReferences: assert( isinstance( toBeQueried, self.__type ) ) return obj._github._statusRequest( "GET", obj._baseUrl + "/" + self.__attributeName + "/" + toBeQueried._identity ) == 204 +class ListOfObjects: + def __init__( self, attributeName, type, creatable = False ): + self.__attributeName = attributeName + self.__type = type + self.__getName = "get_" + attributeName + if creatable: + self.__createName = "create_" + attributeName + else: + self.__createName = None + + def apply( self, cls ): + cls._addMethod( self.__getName, self.__executeGet ) + if self.__createName is not None: + cls._addMethod( self.__createName, self.__executeCreate ) + def __executeGet( self, obj ): return [ self.__type( obj._github, attributes, lazy = True ) for attributes in obj._github._dataRequest( "GET", obj._baseUrl + "/" + self.__attributeName ) ] + def __executeCreate( self, obj, **data ): + return self.__type( obj._github, obj._github._dataRequest( "POST", obj._baseUrl + "/" + self.__attributeName, data ), lazy = True ) + class Editable: def __init__( self, mandatoryParameters, optionalParameters ): self.__mandatoryParameters = mandatoryParameters