From 66fede0ee1d3b23ba2f990b709f42dc06c8e9657 Mon Sep 17 00:00:00 2001 From: Vincent Jacques Date: Sun, 12 Feb 2012 14:56:18 +0100 Subject: [PATCH] On the way to edit-able objects --- github/GithubObject.UnitTest.py | 25 ++++++++++++++++--------- github/GithubObject.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/github/GithubObject.UnitTest.py b/github/GithubObject.UnitTest.py index 36f1add8..a6a17747 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 +from GithubObject import BadGithubObjectException, GithubObject, SimpleScalarAttributes, Editable class GithubObjectTestCase( unittest.TestCase ): def testDuplicatedAttribute( self ): @@ -23,14 +23,15 @@ class TestCaseWithGithubTestObject( unittest.TestCase ): def expectGet( self, url ): return self.g.expect.rawRequest( "GET", url ) -class GithubObjectWithOnlySimpleAttributes( TestCaseWithGithubTestObject ): + def expectPatch( self, url, data ): + return self.g.expect.rawRequest( "PATCH", url, data ) + +class GithubObjectWithOnlySimpleScalarAttributes( TestCaseWithGithubTestObject ): GithubTestObject = GithubObject( "GithubTestObject", SimpleScalarAttributes( "a1", "a2", "a3", "a4" ) ) - def testConstruction( self ): - pass # Everything is done in setUp/tearDown def testCompletion( self ): # A GithubObject: @@ -47,11 +48,6 @@ class GithubObjectWithOnlySimpleAttributes( TestCaseWithGithubTestObject ): # - remembers that some attributes are absent even after an update self.assertEqual( self.o.a4, None ) - def testEdit( self ): - # A GithubObject: - # - does not have an 'edit' method - self.assertRaises( AttributeError, lambda: self.o.edit ) - def testUnknownAttribute( self ): # A GithubObject: # - does not have silly attributes @@ -66,4 +62,15 @@ class GithubObjectWithOnlySimpleAttributes( TestCaseWithGithubTestObject ): self.assertEqual( o.a3, 3 ) self.assertEqual( o.a4, None ) +class EditableGithubObject( TestCaseWithGithubTestObject ): + GithubTestObject = GithubObject( + "GithubTestObject", + SimpleScalarAttributes( "a1", "a2", "a3", "a4" ), + Editable( [ "a1" ], [ "a2", "a4" ] ), + ) + + def testEdit( self ): + self.expectPatch( "/test", { "a1": 11, "a2": 22 } ).andReturn( {} ) + self.o.edit( a1 = 11, a2 = 22 ) + unittest.main() diff --git a/github/GithubObject.py b/github/GithubObject.py index 9d4afd68..64b64962 100644 --- a/github/GithubObject.py +++ b/github/GithubObject.py @@ -26,6 +26,34 @@ class SimpleScalarAttributes: for attributeName in self.__attributeNames ] +class Editable: + class Editor: + def __init__( self, obj, mandatoryParamters, optionalParameters ): + self.__obj = obj + self.__mandatoryParamters = mandatoryParamters + self.__optionalParameters = optionalParameters + + def __call__( self, *args, **kwds ): + self.__obj._github.rawRequest( "PATCH", "/test", kwds ) + + class AttributeDefinition: + def __init__( self, mandatoryParamters, optionalParameters ): + self.__mandatoryParamters = mandatoryParamters + self.__optionalParameters = optionalParameters + + def getValueFromRawValue( self, obj, rawValue ): + return rawValue + + def fetchRawValues( self, obj ): + return { "edit": Editable.Editor( obj, self.__mandatoryParamters, self.__optionalParameters ) } + + def __init__( self, mandatoryParamters, optionalParameters ): + self.__mandatoryParamters = mandatoryParamters + self.__optionalParameters = optionalParameters + + def getAttributeDefinitions( self ): + yield "edit", Editable.AttributeDefinition( self.__mandatoryParamters, self.__optionalParameters ) + def GithubObject( className, *attributePolicies ): attributeDefinitions = dict() for attributePolicy in attributePolicies: