Restore test coverage

This commit is contained in:
Vincent Jacques
2012-03-03 15:17:44 +00:00
parent 7d8d380a0b
commit 427b17cc27
5 changed files with 100 additions and 30 deletions
+23
View File
@@ -37,4 +37,27 @@ class TestCase( unittest.TestCase ):
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" ) ) )
def testGist( self ):
self.requester.expect.dataRequest( "GET", "/gists/123456", None, None ).andReturn( { "description": "xxx" } )
g = self.g.get_gist( 123456 )
self.assertEqual( g.description, "xxx" )
self.requester.expect.statusRequest( "GET", "/gists/123456/star", None, None ).andReturn( 404 )
self.assertFalse( g.is_starred() )
self.requester.expect.statusRequest( "PUT", "/gists/123456/star", None, None ).andReturn( 204 )
g.set_starred()
self.requester.expect.statusRequest( "DELETE", "/gists/123456/star", None, None ).andReturn( 204 )
g.reset_starred()
self.requester.expect.dataRequest( "POST", "/gists/123456/fork", None, None ).andReturn( { "description": "yyy" } )
self.assertEqual( g.create_fork().description, "yyy" )
self.requester.expect.dataRequest( "GET", "/gists/starred", None, None ).andReturn( [ { "description": "xxx" }, { "description": "yyy" } ] )
self.assertEqual( len( self.g.get_user().get_starred_gists() ), 2 )
def testRepositoryReference( self ):
self.requester.expect.dataRequest( "GET", "/user", None, None ).andReturn( { "login": "xxx" } )
self.requester.expect.dataRequest( "GET", "/repos/xxx/yyy", None, None ).andReturn( { "name": "yyy", "owner": { "login": "xxx" } } )
r = self.g.get_user().get_repo( "yyy" )
self.requester.expect.dataRequest( "GET", "/repos/xxx/yyy/milestones/1", None, None ).andReturn( { "number": 1 } )
self.requester.expect.dataRequest( "GET", "/repos/xxx/yyy/milestones/1/labels", {}, None ).andReturn( [ { "name": "a" } ] )
self.assertIs( r.get_milestone( 1 ).get_labels()[ 0 ]._repo, r )
unittest.main()
+57
View File
@@ -21,6 +21,7 @@ class TestCaseWithGithubTestObject( unittest.TestCase ):
unittest.TestCase.setUp( self )
self.g = MockMockMock.Mock( "github" )
self.o = self.GithubTestObject( self.g.object, { "a1": 1, "a2": 2 }, lazy = True )
self.GithubTestObject._autoDocument() # Only for coverage
def tearDown( self ):
self.g.tearDown()
@@ -47,6 +48,17 @@ class TestCaseWithGithubTestObject( unittest.TestCase ):
def expectStatusDelete( self, url, data = None ):
return self.g.expect._statusRequest( "DELETE", url, None, data )
class GithubObjectWithDocumentationCoveringSpecialCases( TestCaseWithGithubTestObject ):
GithubTestObject = GithubObject(
"GithubTestObject",
BaseUrl( lambda obj: "/test" ),
InternalSimpleAttributes( "a1", "a2", "_a3" ),
MethodFromCallable( "myMethod", [ "mock", "arg" ], [], lambda obj: 42, ObjectTypePolicy( GithubObject ) )
)
def testNothing( self ):
pass
class GithubObjectWithOnlyInternalSimpleAttributes( TestCaseWithGithubTestObject ):
GithubTestObject = GithubObject(
"GithubTestObject",
@@ -232,6 +244,51 @@ class GithubObjectWithListGetableExternalListOfObjects( TestCaseWithGithubTestOb
a3s = self.o.get_a3s( "foobar" )
self.assertEqual( len( a3s ), 3 )
class GithubObjectWithListGetableExternalListOfObjectsWithOtherUrl( TestCaseWithGithubTestObject ):
ContainedObject = GithubObject(
"ContainedObject",
BaseUrl( lambda obj: "/other/" + obj.id ),
InternalSimpleAttributes( "id", "name" )
)
GithubTestObject = GithubObject(
"GithubTestObject",
BaseUrl( lambda obj: "/test" ),
InternalSimpleAttributes( "a1", "a2" ),
ExternalListOfObjects( "a3s", "a3", ContainedObject, ListGetable( [], [ "type" ] ), url = "/other" )
)
def testGetList( self ):
self.expectDataGet( "/other", {} ).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( "/other/id1" ).andReturn( { "name": "name1" } )
self.assertEqual( a3s[ 0 ].name, "name1" )
class GithubObjectWithListGetableExternalListOfObjectsWithAttributeModifier( TestCaseWithGithubTestObject ):
ContainedObject = GithubObject(
"ContainedObject",
BaseUrl( lambda obj: "/test/a3s/" + obj.id ),
InternalSimpleAttributes( "id", "name", "_a" )
)
GithubTestObject = GithubObject(
"GithubTestObject",
BaseUrl( lambda obj: "/test" ),
InternalSimpleAttributes( "a1", "a2" ),
ExternalListOfObjects( "a3s", "a3", ContainedObject, ListGetable( [], [ "type" ], { "_a": lambda obj: 42 } ) )
)
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.assertEqual( a3s[ 0 ]._a, 42 )
self.expectDataGet( "/test/a3s/id1" ).andReturn( { "name": "name1" } )
self.assertEqual( a3s[ 0 ].name, "name1" )
class GithubObjectWithElementAddableExternalListOfObjects( TestCaseWithGithubTestObject ):
ContainedObject = GithubObject(
"ContainedObject",
-1
View File
@@ -24,7 +24,6 @@ class AttributeFromCallable:
def autoDocument( self ):
return ""
return "* `" + self.__name + "`\n"
### @todo include the ArgumentsChecker
class MethodFromCallable:
+19 -25
View File
@@ -65,18 +65,27 @@ class ElementHasable( ListCapacity ):
### @todo `bool` -> bool
return "* `has_in_" + self.safeAttributeName + "( " + self.singularName + " )`: `bool`\n * `" + self.singularName + "`: " + self.typePolicy.documentTypeName() + "\n"
class ElementCreatable( ListCapacity ):
def __init__( self, mandatoryParameters, optionalParameters, attributeModifiers = {} ):
self.__argumentsChecker = ArgumentsChecker( mandatoryParameters, optionalParameters )
class ListCapacityWithModifier( ListCapacity ):
def __init__( self, attributeModifiers ):
self.__attributeModifiers = attributeModifiers
def _modifyAttributes( self, obj, attributes ):
for attributeName, attributeModifier in self.__attributeModifiers.iteritems():
attributes[ attributeName ] = attributeModifier( obj )
return attributes
class ElementCreatable( ListCapacityWithModifier ):
def __init__( self, mandatoryParameters, optionalParameters, attributeModifiers = {} ):
ListCapacityWithModifier.__init__( self, attributeModifiers )
self.__argumentsChecker = ArgumentsChecker( mandatoryParameters, optionalParameters )
def apply( self, cls ):
cls._addMethod( "create_" + self.singularName, self.__execute )
def __execute( self, obj, *args, **kwds ):
return self.typePolicy.createLazy(
obj,
self.__modifyAttributes(
self._modifyAttributes(
obj,
obj._github._dataRequest(
"POST",
@@ -87,18 +96,13 @@ class ElementCreatable( ListCapacity ):
)
)
def __modifyAttributes( self, obj, attributes ):
for attributeName, attributeModifier in self.__attributeModifiers.iteritems():
attributes[ attributeName ] = attributeModifier( obj )
return attributes
def autoDocument( self ):
return "* `create_" + self.singularName + "(" + self.__argumentsChecker.documentParameters() + ")`: " + self.typePolicy.documentTypeName() + "\n"
class ElementGetable( ListCapacity ):
class ElementGetable( ListCapacityWithModifier ):
def __init__( self, mandatoryParameters, optionalParameters, attributeModifiers = {} ):
ListCapacityWithModifier.__init__( self, attributeModifiers )
self.__argumentsChecker = ArgumentsChecker( mandatoryParameters, optionalParameters )
self.__attributeModifiers = attributeModifiers
def apply( self, cls ):
cls._addMethod( "get_" + self.singularName, self.__execute )
@@ -106,17 +110,12 @@ class ElementGetable( ListCapacity ):
def __execute( self, obj, *args, **kwds ):
return self.typePolicy.createNonLazy(
obj,
self.__modifyAttributes(
self._modifyAttributes(
obj,
self.__argumentsChecker.check( args, kwds )
)
)
def __modifyAttributes( self, obj, attributes ):
for attributeName, attributeModifier in self.__attributeModifiers.iteritems():
attributes[ attributeName ] = attributeModifier( obj )
return attributes
def autoDocument( self ):
return "* `get_" + self.singularName + "(" + self.__argumentsChecker.documentParameters() + ")`: " + self.typePolicy.documentTypeName() + "\n"
@@ -156,10 +155,10 @@ class SeveralElementsRemovable( ListCapacity ):
def autoDocument( self ):
return "* `remove_from_" + self.safeAttributeName + "( " + self.singularName + ", ... )`\n * `" + self.singularName + "`: " + self.typePolicy.documentTypeName() + "\n"
class ListGetable( ListCapacity ):
class ListGetable( ListCapacityWithModifier ):
def __init__( self, mandatoryParameters, optionalParameters, attributeModifiers = {} ):
ListCapacityWithModifier.__init__( self, attributeModifiers )
self.__argumentsChecker = ArgumentsChecker( mandatoryParameters, optionalParameters )
self.__attributeModifiers = attributeModifiers
def apply( self, cls ):
cls._addMethod( "get_" + self.safeAttributeName, self.__execute )
@@ -169,7 +168,7 @@ class ListGetable( ListCapacity ):
return [
self.typePolicy.createLazy(
obj,
self.__modifyAttributes( obj, attributes )
self._modifyAttributes( obj, attributes )
)
for attributes in obj._github._dataRequest(
"GET",
@@ -182,11 +181,6 @@ class ListGetable( ListCapacity ):
def autoDocument( self ):
return "* `get_" + self.safeAttributeName + "(" + self.__argumentsChecker.documentParameters() + ")`: list of " + self.typePolicy.documentTypeName() + "\n"
def __modifyAttributes( self, obj, attributes ):
for attributeName, attributeModifier in self.__attributeModifiers.iteritems():
attributes[ attributeName ] = attributeModifier( obj )
return attributes
class ListSetable( ListCapacity ):
def apply( self, cls ):
cls._addMethod( "set_" + self.safeAttributeName, self.__execute )
+1 -4
View File
@@ -19,10 +19,7 @@ class ObjectTypePolicy:
self.__type = type
def createLazy( self, obj, attributes ):
if isinstance( attributes, self.__type ):
return attributes
else:
return self.__type( obj._github, attributes, lazy = True )
return self.__type( obj._github, attributes, lazy = True )
def createNonLazy( self, obj, attributes ):
return self.__type( obj._github, attributes, lazy = False )