Skip to content

Commit

Permalink
Merge pull request #13 from fhuertas/pre-1.0.7
Browse files Browse the repository at this point in the history
FunctionMockCheckCall return a value
  • Loading branch information
fhuertas authored Nov 15, 2016
2 parents 1197204 + 3cc67b7 commit 19332ad
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 21 deletions.
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ where <python-version> is the version to test. Note: you must be installed that
* Added mock for check if a function is called
* FIX: `make test-versions` makes tests for python 2 and 3

**1.0.7**
* FunctionMockCheckCall can return a value instead invoke the function

## Installation

This module can be installed using the following options:
Expand All @@ -46,7 +49,7 @@ The tests contain examples to use this library but here there are the more usefu

### FunctionMock

This mock replace a function for other. [Example](https://github.com/fhuertas/mock_decorators/blob/1.0.6/tests/mock_decorators/test_function_mock.py#L18)
This mock replace a function for other. [Example](https://github.com/fhuertas/mock_decorators/blob/1.0.7/tests/mock_decorators/test_function_mock.py#L18)

The parameters are the following:

Expand All @@ -57,7 +60,7 @@ The parameters are the following:

### FunctionMockResult

This mock replace a function for a result value. [Example](https://github.com/fhuertas/mock_decorators/blob/1.0.6/tests/mock_decorators/test_function_mock.py#L167)
This mock replace a function for a result value. [Example](https://github.com/fhuertas/mock_decorators/blob/1.0.7/tests/mock_decorators/test_function_mock.py#L167)

The parameters are the following:

Expand All @@ -69,7 +72,7 @@ The parameters are the following:
### FunctionMockChangeResult

This mock modify the result of a function. i.e. if Add 2 to a result of a function.
[Example](https://github.com/fhuertas/mock_decorators/blob/1.0.6/tests/mock_decorators/test_function_mock.py#L220)
[Example](https://github.com/fhuertas/mock_decorators/blob/1.0.7/tests/mock_decorators/test_function_mock.py#L220)

The parameters are the following:
* **entity**: The module, class or object where the function is.
Expand All @@ -79,16 +82,17 @@ The parameters are the following:
### FunctionMockCheckCall

This mock modify the result of a function. i.e. if Add 2 to a result of a function.
[Example](https://github.com/fhuertas/mock_decorators/blob/1.0.6/tests/mock_decorators/test_function_mock.py#L18)
[Example](https://github.com/fhuertas/mock_decorators/blob/1.0.7/tests/mock_decorators/test_function_mock.py#L18)

The parameters are the following:
* **entity**: The module, class or object where the function is.
* **function_name**: The function where you can modify the result
* **times** (*optional, 0 by default*): Expected number times that the function should be called. 0 not check it
* **expected_times** (*optional, 0 by default*): Expected number times that the function should be called. 0 not check it
* **return_value** (*optional, None by default*): Return a value instead call to the function, if None, the function is called

## AttributeMock

This mock change the value of a attribute for other. [Example](https://github.com/fhuertas/mock_decorators/blob/1.0.6/tests/mock_decorators/test_attribute_mock.py#L8)
This mock change the value of a attribute for other. [Example](https://github.com/fhuertas/mock_decorators/blob/1.0.7/tests/mock_decorators/test_attribute_mock.py#L8)

The parameters are the following:

Expand All @@ -99,7 +103,7 @@ The parameters are the following:

## ClassMock

This mock only work in python 3 and higher. This mock replace a class or object by other. [Example](https://github.com/fhuertas/mock_decorators/blob/1.0.6/tests/mock_decorators/test_class_mock.py#L24)
This mock only work in python 3 and higher. This mock replace a class or object by other. [Example](https://github.com/fhuertas/mock_decorators/blob/1.0.7/tests/mock_decorators/test_class_mock.py#L24)

The parameters are the following:
* **old_class_name**: The original object/class that you can replace
Expand All @@ -108,7 +112,7 @@ The parameters are the following:

## DictMock

This mock replace a value in a map by other value. [Example](https://github.com/fhuertas/mock_decorators/blob/1.0.6/tests/mock_decorators/test_dict_mock.py#L7)
This mock replace a value in a map by other value. [Example](https://github.com/fhuertas/mock_decorators/blob/1.0.7/tests/mock_decorators/test_dict_mock.py#L7)

The parameters are the following:
* **entity_dict**: The original dictionary
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.6
1.0.7
16 changes: 9 additions & 7 deletions coverage.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0" ?>
<coverage branch-rate="0" line-rate="0.9886" timestamp="1479199217687" version="4.1">
<coverage branch-rate="0" line-rate="0.9887" timestamp="1479203014684" version="4.1">
<!-- Generated by coverage.py: https://coverage.readthedocs.io -->
<!-- Based on https://raw.githubusercontent.com/cobertura/web/f0366e5e2cf18f111cbd61fc34ef720a6584ba02/htdocs/xml/coverage-03.dtd -->
<sources>
<source>/home/fhuertas/Projects/personal/mock_decorators</source>
</sources>
<packages>
<package branch-rate="0" complexity="0" line-rate="0.9886" name="mock_decorators">
<package branch-rate="0" complexity="0" line-rate="0.9887" name="mock_decorators">
<classes>
<class branch-rate="0" complexity="0" filename="mock_decorators/__init__.py" line-rate="1" name="__init__.py">
<methods/>
Expand Down Expand Up @@ -175,22 +175,24 @@
<line hits="1" number="147"/>
<line hits="1" number="148"/>
<line hits="1" number="149"/>
<line hits="1" number="150"/>
<line hits="1" number="151"/>
<line hits="1" number="153"/>
<line hits="1" number="160"/>
<line hits="1" number="155"/>
<line hits="1" number="162"/>
<line hits="1" number="163"/>
<line hits="1" number="164"/>
<line hits="1" number="165"/>
<line hits="1" number="166"/>
<line hits="1" number="167"/>
<line hits="1" number="168"/>
<line hits="1" number="169"/>
<line hits="1" number="170"/>
<line hits="1" number="171"/>
<line hits="1" number="172"/>
<line hits="1" number="173"/>
<line hits="1" number="176"/>
<line hits="1" number="174"/>
<line hits="1" number="175"/>
<line hits="1" number="178"/>
<line hits="1" number="180"/>
<line hits="1" number="182"/>
</lines>
</class>
<class branch-rate="0" complexity="0" filename="mock_decorators/object_mock.py" line-rate="0" name="object_mock.py">
Expand Down
7 changes: 5 additions & 2 deletions mock_decorators/function_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ class FunctionMockCheckCall(object):
CALLED = 'called'
TIMES = 'times'

def __init__(self, entity, function_name, expected_times=0):
def __init__(self, entity, function_name, expected_times=0, return_value=None):
"""
TODO
This function checks if a function is invoked and the times that it is called. Optionally, the mock can return
a value instead invoke the function
"""
self.entity = entity
self.function_name = function_name
Expand All @@ -146,6 +147,8 @@ def __init__(self, entity, function_name, expected_times=0):

def wrapped_mock(*args, **kwargs):
self.times += 1
if return_value:
return return_value
return self.old_function(*args, **kwargs)

self.new_function = wrapped_mock
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read()
return open(os.path.join(os.path.dirname(__file__), fname)).read().strip()


required = []
Expand Down
16 changes: 14 additions & 2 deletions tests/mock_decorators/test_function_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,13 @@ def call_test():

class TestFunctionMockCheckCall(unittest.TestCase):
def test_no_called(self):

def inner_test():
@FunctionMockCheckCall(module_test, 'function_sum')
def call_test():
pass

call_test()

self.assertRaises(ValueError, inner_test)

def test_called(self):
Expand Down Expand Up @@ -249,6 +250,17 @@ def inner_test():
module_test.function_sum(2, 2)
module_test.function_sum(2, 2)
return module_test.function_sum(2, 2)

self.assertRaises(ValueError, inner_test)
result = module_test.function_sum(2, 2)
self.assertEqual(result, 4, "The function result has been modified")
self.assertEqual(result, 4, "The function result has been modified")

def test_call_change_return(self):
@FunctionMockCheckCall(module_test, 'function_sum', return_value=3)
def inner_test():
return module_test.function_sum(2, 2)

result_change = inner_test()
result_no_change = module_test.function_sum(2, 2)
self.assertEqual(result_change, 3, "The function result has been modified")
self.assertEqual(result_no_change, 4, "The function result has been modified")

0 comments on commit 19332ad

Please sign in to comment.