From 9dd9619173a8034f9622579658197ec2eb3c00dd Mon Sep 17 00:00:00 2001 From: Vincent Jacques Date: Sun, 12 Feb 2012 16:19:35 +0100 Subject: [PATCH] Use real base url --- github/GithubObject.UnitTest.py | 18 +++++++++++++++--- github/GithubObject.py | 23 ++++++++++++++++++++--- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/github/GithubObject.UnitTest.py b/github/GithubObject.UnitTest.py index a79a147a..68beae74 100644 --- a/github/GithubObject.UnitTest.py +++ b/github/GithubObject.UnitTest.py @@ -1,7 +1,7 @@ import unittest import MockMockMock -from GithubObject import BadGithubObjectException, GithubObject, SimpleScalarAttributes, Editable, Deletable +from GithubObject import * class GithubObjectTestCase( unittest.TestCase ): def testDuplicatedAttributeInOnePolicy( self ): @@ -34,6 +34,7 @@ class TestCaseWithGithubTestObject( unittest.TestCase ): class GithubObjectWithOnlySimpleScalarAttributes( TestCaseWithGithubTestObject ): GithubTestObject = GithubObject( "GithubTestObject", + BaseUrl( lambda obj: "/test" ), SimpleScalarAttributes( "a1", "a2", "a3", "a4" ) ) @@ -54,8 +55,6 @@ class GithubObjectWithOnlySimpleScalarAttributes( TestCaseWithGithubTestObject ) self.assertEqual( self.o.a4, None ) def testUnknownAttribute( self ): - # A GithubObject: - # - does not have silly attributes self.assertRaises( AttributeError, lambda: self.o.foobar ) def testNonLazyConstruction( self ): @@ -67,9 +66,21 @@ class GithubObjectWithOnlySimpleScalarAttributes( TestCaseWithGithubTestObject ) self.assertEqual( o.a3, 3 ) self.assertEqual( o.a4, None ) +class GithubObjectWithOtherBaseUrl( TestCaseWithGithubTestObject ): + GithubTestObject = GithubObject( + "GithubTestObject", + BaseUrl( lambda obj: "/other/" + str( obj.a1 ) ), + SimpleScalarAttributes( "a1", "a2", "a3", "a4" ) + ) + + def testCompletion( self ): + self.expectGet( "/other/1" ).andReturn( { "a2": 22, "a3": 3 } ) + self.assertEqual( self.o.a3, 3 ) + class EditableGithubObject( TestCaseWithGithubTestObject ): GithubTestObject = GithubObject( "GithubTestObject", + BaseUrl( lambda obj: "/test" ), SimpleScalarAttributes( "a1", "a2", "a3", "a4" ), Editable( [ "a1" ], [ "a2", "a4" ] ), ) @@ -134,6 +145,7 @@ class EditableGithubObject( TestCaseWithGithubTestObject ): class DeletableGithubObject( TestCaseWithGithubTestObject ): GithubTestObject = GithubObject( "GithubTestObject", + BaseUrl( lambda obj: "/test" ), SimpleScalarAttributes( "a1", "a2", "a3", "a4" ), Deletable(), ) diff --git a/github/GithubObject.py b/github/GithubObject.py index a6fe2869..1a605bcb 100644 --- a/github/GithubObject.py +++ b/github/GithubObject.py @@ -12,7 +12,7 @@ class SimpleScalarAttributes: return rawValue def updateAttributes( self, obj ): - attributes = obj._github.rawRequest( "GET", "/test" ) + attributes = obj._github.rawRequest( "GET", obj._baseUrl ) for attributeName in self.__attributeNames: if attributeName not in attributes: attributes[ attributeName ] = None @@ -43,7 +43,7 @@ class Editable: for argumentName in kwds: if argumentName not in itertools.chain( self.__mandatoryParameters, self.__optionalParameters ): raise TypeError() - attributes = self.__obj._github.rawRequest( "PATCH", "/test", kwds ) + attributes = self.__obj._github.rawRequest( "PATCH", self.__obj._baseUrl, kwds ) self.__obj._updateAttributes( attributes ) class AttributeDefinition: @@ -70,7 +70,7 @@ class Deletable: self.__obj = obj def __call__( self ): - self.__obj._github.rawRequest( "DELETE", "/test" ) + self.__obj._github.rawRequest( "DELETE", self.__obj._baseUrl ) class AttributeDefinition: def getValueFromRawValue( self, obj, rawValue ): @@ -82,6 +82,23 @@ class Deletable: def getAttributeDefinitions( self ): yield "delete", Deletable.AttributeDefinition() +class BaseUrl: + class AttributeDefinition: + def __init__( self, baseUrl ): + self.__baseUrl = baseUrl + + def getValueFromRawValue( self, obj, rawValue ): + return rawValue + + def updateAttributes( self, obj ): + obj._updateAttributes( { "_baseUrl": self.__baseUrl( obj ) } ) + + def __init__( self, baseUrl ): + self.__baseUrl = baseUrl + + def getAttributeDefinitions( self ): + yield "_baseUrl", BaseUrl.AttributeDefinition( self.__baseUrl ) + def GithubObject( className, *attributePolicies ): attributeDefinitions = dict() for attributePolicy in attributePolicies: