diff --git a/github/GithubObject.UnitTest.py b/github/GithubObject.UnitTest.py index 9841558d..05a503ed 100644 --- a/github/GithubObject.UnitTest.py +++ b/github/GithubObject.UnitTest.py @@ -28,6 +28,9 @@ class TestCaseWithGithubTestObject( unittest.TestCase ): def expectStatusPut( self, url ): return self.g.expect._statusRequest( "PUT", url ) + def expectStatusGet( self, url ): + return self.g.expect._statusRequest( "GET", url ) + def expectDataPatch( self, url, data ): return self.g.expect._dataRequest( "PATCH", url, data ) @@ -214,7 +217,7 @@ class GithubObjectWithModifiableExtendedListAttribute( TestCaseWithGithubTestObj "GithubTestObject", BaseUrl( lambda obj: "/test" ), SimpleScalarAttributes( "a1", "a2" ), - ExtendedListAttribute( "a3s", ContainedObject, addable = True, removable = True ) + ExtendedListAttribute( "a3s", ContainedObject, addable = True, removable = True, hasable = True ) ) def testAddToList( self ): @@ -227,4 +230,11 @@ class GithubObjectWithModifiableExtendedListAttribute( TestCaseWithGithubTestObj self.expectStatusDelete( "/test/a3s/idRemove" ).andReturn( 204 ) self.o.remove_from_a3s( a3ToRemove ) + def testHasInList( self ): + a3ToQuery = self.ContainedObject( self.g.object, { "id": "idQuery", "name": "nameQuery" }, lazy = True ) + self.expectStatusGet( "/test/a3s/idQuery" ).andReturn( 204 ) + self.assertTrue( self.o.has_in_a3s( a3ToQuery ) ) + self.expectStatusGet( "/test/a3s/idQuery" ).andReturn( 404 ) + self.assertFalse( self.o.has_in_a3s( a3ToQuery ) ) + unittest.main() diff --git a/github/GithubObject.py b/github/GithubObject.py index 69db6e82..a57c3399 100644 --- a/github/GithubObject.py +++ b/github/GithubObject.py @@ -99,7 +99,29 @@ class ExtendedListAttribute: def updateAttributes( self, obj ): obj._updateAttributes( { self.__addName: ExtendedListAttribute.Adder( obj, self.__attributeName, self.__type ) } ) - def __init__( self, attributeName, type, addable = False, removable = False ): + class Haser: + def __init__( self, obj, attributeName, type ): + self.__obj = obj + self.__attributeName = attributeName + self.__type = type + + def __call__( self, toBeQueried ): + assert( isinstance( toBeQueried, self.__type ) ) + return self.__obj._github._statusRequest( "GET", self.__obj._baseUrl + "/" + self.__attributeName + "/" + toBeQueried._identity ) == 204 + + class HasDefinition: + def __init__( self, attributeName, hasName, type ): + self.__hasName = hasName + self.__attributeName = attributeName + self.__type = type + + def getValueFromRawValue( self, obj, rawValue ): + return rawValue + + def updateAttributes( self, obj ): + obj._updateAttributes( { self.__hasName: ExtendedListAttribute.Haser( obj, self.__attributeName, self.__type ) } ) + + def __init__( self, attributeName, type, addable = False, removable = False, hasable = False ): self.__attributeName = attributeName self.__type = type self.__getName = "get_" + attributeName @@ -111,6 +133,10 @@ class ExtendedListAttribute: self.__removeName = "remove_from_" + attributeName else: self.__removeName = None + if hasable: + self.__hasName = "has_in_" + attributeName + else: + self.__hasName = None def getAttributeDefinitions( self ): yield self.__getName, ExtendedListAttribute.GetDefinition( self.__attributeName, self.__getName, self.__type ) @@ -118,6 +144,8 @@ class ExtendedListAttribute: yield self.__addName, ExtendedListAttribute.AddDefinition( self.__attributeName, self.__addName, self.__type ) if self.__removeName is not None: yield self.__removeName, ExtendedListAttribute.RemoveDefinition( self.__attributeName, self.__removeName, self.__type ) + if self.__hasName is not None: + yield self.__hasName, ExtendedListAttribute.HasDefinition( self.__attributeName, self.__hasName, self.__type ) class ExtendedScalarAttribute: class AttributeDefinition: