Skip to content

Commit

Permalink
Add return type validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fornellas committed Apr 9, 2020
1 parent e2d020e commit 286a0a7
Showing 1 changed file with 66 additions and 11 deletions.
77 changes: 66 additions & 11 deletions tests/strict_mock_testslide.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,26 @@ def __init__(self):
self.runtime_attr_from_init = True
self.attr = None

def instance_method(self, message: str):
def instance_method(self, message: str) -> str:
return "instance_method: {}".format(message)

async def async_instance_method(self, message: str):
async def async_instance_method(self, message: str) -> str:
return "async_instance_method: {}".format(message)

@staticmethod
def static_method(message: str):
def static_method(message: str) -> str:
return "static_method: {}".format(message)

@staticmethod
async def async_static_method(message: str):
async def async_static_method(message: str) -> str:
return "async_static_method: {}".format(message)

@classmethod
def class_method(cls, message: str):
def class_method(cls, message: str) -> str:
return "class_method: {}".format(message)

@classmethod
async def async_class_method(cls, message: str):
async def async_class_method(cls, message: str) -> str:
return "async_class_method: {}".format(message)

@extra_arg_with_wraps
Expand Down Expand Up @@ -432,7 +432,6 @@ def allows_setting_valid_type(self):

@context.example
def allows_setting_valid_type_with_templated_mock(self):
print(unittest.mock.Mock(spec=str).__class__)
self.strict_mock.non_callable = unittest.mock.Mock(spec=str)
self.strict_mock.non_callable = StrictMock(template=str)

Expand Down Expand Up @@ -527,7 +526,7 @@ def common_examples(
if signature_validation:

@context.example
def fails_on_wrong_signature_call(self):
def fails_on_invalid_signature_call(self):
setattr(
self.strict_mock,
self.test_method_name,
Expand All @@ -542,7 +541,7 @@ def fails_on_wrong_signature_call(self):
else:

@context.example
def passes_on_wrong_signature_call(self):
def passes_on_invalid_signature_call(self):
setattr(
self.strict_mock,
self.test_method_name,
Expand All @@ -559,7 +558,7 @@ def passes_on_wrong_signature_call(self):
if type_validation:

@context.example
def fails_on_wrong_type_call(self):
def fails_on_invalid_argument_type_call(self):
setattr(
self.strict_mock,
self.test_method_name,
Expand All @@ -571,10 +570,23 @@ def fails_on_wrong_type_call(self):
self.test_method_name,
)(1234)

@context.example
def fails_on_invalid_return_type(self):
setattr(
self.strict_mock,
self.test_method_name,
lambda message: 1234,
)
with self.assertRaises(TypeError):
getattr(
self.strict_mock,
self.test_method_name,
)("message")

else:

@context.example
def passes_on_wrong_type_call(self):
def passes_on_invalid_argument_type_call(self):
setattr(
self.strict_mock,
self.test_method_name,
Expand All @@ -588,6 +600,21 @@ def passes_on_wrong_type_call(self):
"mock",
)

@context.example
def passes_on_invalid_return_type(self):
setattr(
self.strict_mock,
self.test_method_name,
lambda message: 1234,
)
self.assertEqual(
getattr(
self.strict_mock,
self.test_method_name,
)("message"),
1234,
)

@context.sub_context(
"with signature_validation=True, type_validation=True"
)
Expand Down Expand Up @@ -950,6 +977,19 @@ async def mock(msg):
self.strict_mock, self.method_name
)(1)

@context.example
async def fails_on_invalid_return_type(self):
async def mock(message):
return 1234

setattr(
self.strict_mock, self.method_name, mock,
)
with self.assertRaises(TypeError):
await getattr(
self.strict_mock, self.method_name,
)("message")

else:

@context.example
Expand All @@ -960,6 +1000,21 @@ async def mock(msg):
setattr(self.strict_mock, self.method_name, mock)
await getattr(self.strict_mock, self.method_name)(1)

@context.example
async def passes_on_invalid_return_type(self):
async def mock(message):
return 1234

setattr(
self.strict_mock, self.method_name, mock,
)
self.assertEqual(
await getattr(
self.strict_mock, self.method_name,
)("message"),
1234,
)

@context.sub_context(
"with signature_validation=True, type_validation=True"
)
Expand Down

0 comments on commit 286a0a7

Please sign in to comment.