Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Add exception testing to Python #1141

Merged
merged 1 commit into from
Mar 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ public ImportSectionView generateImportSection(
}

public ImportSectionView generateTestImportSection(SurfaceTransformerContext context) {
ImportSectionView.Builder importSection = ImportSectionView.newBuilder();
importSection.standardImports(generateTestStandardImports());
importSection.appImports(generateTestAppImports(context));
return importSection.build();
return ImportSectionView.newBuilder()
.standardImports(generateTestStandardImports())
.externalImports(generateTestExternalImports())
.appImports(generateTestAppImports(context))
.build();
}

private List<ImportFileView> generateInitCodeAppImports(
Expand Down Expand Up @@ -109,6 +110,10 @@ private List<ImportFileView> generateTestStandardImports() {
return ImmutableList.of(createImport("mock"), createImport("unittest2"));
}

private List<ImportFileView> generateTestExternalImports() {
return ImmutableList.of(createImport("google.gax", "errors"));
}

private List<ImportFileView> generateTestAppImports(SurfaceTransformerContext context) {
Set<ImportFileView> appImports = new TreeSet<>(importFileViewComparator());
for (Map.Entry<String, TypeAlias> entry : context.getTypeTable().getImports().entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ public String getTestCaseName(SymbolTable symbolTable, Method method) {
return publicMethodName(testCaseName);
}

@Override
public String getExceptionTestCaseName(SymbolTable symbolTable, Method method) {
Name testCaseName =
symbolTable.getNewSymbol(Name.upperCamel("Test", method.getSimpleName(), "Exception"));
return publicMethodName(testCaseName);
}

@Override
public String quoted(String text) {
return "'" + text + "'";
Expand Down
100 changes: 68 additions & 32 deletions src/main/resources/com/google/api/codegen/py/test.snip
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
@extends "py/method_sample.snip"

@snippet generate(apiTest)
{@licenseSection(apiTest.fileHeader)}

{@moduleDocstring()}
{@renderImportSection(apiTest.fileHeader.importSection)}
{@header(apiTest.fileHeader)}

class {@apiTest.testClass.name}(unittest2.TestCase):

Expand All @@ -15,6 +12,17 @@
@end
@end

@private header(fileHeader)
{@licenseSection(fileHeader)}

{@moduleDocstring()}
{@renderImportSection(fileHeader.importSection)}

class CustomException(Exception):
pass

@end

@private licenseSection(fileHeader)
@join line : fileHeader.copyrightLines
@# {@line}
Expand Down Expand Up @@ -57,40 +65,68 @@
@end

@private nonStreamingOptionalArrayTestCase(test, moduleName)
@switch test.clientMethodType
@case "OptionalArrayMethod"
{@simpleTestCase(test, moduleName)}
@default
{@unhandledCase()}
@end
@end

@private simpleTestCase(test, moduleName)
@@mock.patch('google.gax.config.create_stub')
def {@test.name}(self, mock_create_stub):
@switch test.clientMethodType
@case "OptionalArrayMethod"
{@simpleTestCase(test, moduleName)}
@default
{@unhandledCase()}
@# Mock gRPC layer
grpc_stub = mock.Mock()
mock_create_stub.return_value = grpc_stub

client = {@moduleName}.{@test.serviceConstructorName}()

@if test.hasRequestParameters
@# Mock request
{@initCode(test.initCode)}

@end
@if test.hasReturnValue
@# Mock response
{@initCode(test.mockResponse.initCode)}
grpc_stub.{@test.grpcStubCallString}.return_value = expected_response

response = client.{@test.clientMethodName}(\
{@fieldSettingArgList(test.initCode.fieldSettings)})
self.assertEqual(expected_response, response)
@else
client.{@test.clientMethodName}(\
{@fieldSettingArgList(test.initCode.fieldSettings)})
@end
@end

@private simpleTestCase(test, moduleName)
@# Mock gRPC layer
grpc_stub = mock.Mock()
mock_create_stub.return_value = grpc_stub
grpc_stub.{@test.grpcStubCallString}.assert_called_once()

client = {@moduleName}.{@test.serviceConstructorName}()
@@mock.patch('google.gax.config.API_ERRORS', (CustomException,))
@@mock.patch('google.gax.config.create_stub')
def {@test.nameWithException}(self, mock_create_stub):
@# Mock gRPC layer
grpc_stub = mock.Mock()
mock_create_stub.return_value = grpc_stub

@if test.hasRequestParameters
@# Mock request
{@initCode(test.initCode)}
client = {@moduleName}.{@test.serviceConstructorName}()

@end
@if test.hasReturnValue
@# Mock response
{@initCode(test.mockResponse.initCode)}
grpc_stub.{@test.grpcStubCallString}.return_value = expected_response

response = client.{@test.clientMethodName}(\
{@fieldSettingArgList(test.initCode.fieldSettings)})
self.assertEqual(expected_response, response)
@else
client.{@test.clientMethodName}(\
{@fieldSettingArgList(test.initCode.fieldSettings)})
@end
@if test.hasRequestParameters
@# Mock request
{@initCode(test.initCode)}

@end
@# Mock exception response
grpc_stub.{@test.grpcStubCallString}.side_effect = CustomException()

self.assertRaises(\
errors.GaxError, \
client.{@test.clientMethodName}\
{@fieldSettingArgListAfterComma(test.initCode.fieldSettings)})
@end

grpc_stub.{@test.grpcStubCallString}.assert_called_once()
@private fieldSettingArgListAfterComma(fieldSettings)
@if fieldSettings
, {@fieldSettingArgList(fieldSettings)}
@end
@end
Loading