Repository: opensistemas-hub/osbrain
Pull request #135 (2017-06-14 08:48:36)

Improve helper module
Check all conditions when calling wait_agent_attr.

codecov[bot]: # [Codecov](https://codecov.io/gh/opensistemas-hub/osbrain/pull/135?src=pr&el=h1) Report
> Merging [#135](https://codecov.io/gh/opensistemas-hub/osbrain/pull/135?src=pr&el=desc) into [master](https://codecov.io/gh/opensistemas-hub/osbrain/commit/d8ab57dc83aab9be8d2399b9ca811f01420f6a00?src=pr&el=desc) will **decrease** coverage by `0.41%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/opensistemas-hub/osbrain/pull/135/graphs/tree.svg?width=650&token=LfvMoNOrOc&height=150&src=pr)](https://codecov.io/gh/opensistemas-hub/osbrain/pull/135?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #135      +/-   ##
==========================================
- Coverage   97.67%   97.25%   -0.42%     
==========================================
  Files           8        8              
  Lines        1202     1202              
  Branches      181      181              
==========================================
- Hits         1174     1169       -5     
- Misses         20       23       +3     
- Partials        8       10       +2
```


| [Impacted Files](https://codecov.io/gh/opensistemas-hub/osbrain/pull/135?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [osbrain/helper.py](https://codecov.io/gh/opensistemas-hub/osbrain/pull/135?src=pr&el=tree#diff-b3NicmFpbi9oZWxwZXIucHk=) | `91.22% <100%> (-8.78%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/opensistemas-hub/osbrain/pull/135?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/opensistemas-hub/osbrain/pull/135?src=pr&el=footer). Last update [d8ab57d...e80cd46](https://codecov.io/gh/opensistemas-hub/osbrain/pull/135?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

codecov[bot]: # [Codecov](https://codecov.io/gh/opensistemas-hub/osbrain/pull/135?src=pr&el=h1) Report
> Merging [#135](https://codecov.io/gh/opensistemas-hub/osbrain/pull/135?src=pr&el=desc) into [master](https://codecov.io/gh/opensistemas-hub/osbrain/commit/d8ab57dc83aab9be8d2399b9ca811f01420f6a00?src=pr&el=desc) will **decrease** coverage by `0.41%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/opensistemas-hub/osbrain/pull/135/graphs/tree.svg?width=650&height=150&src=pr&token=LfvMoNOrOc)](https://codecov.io/gh/opensistemas-hub/osbrain/pull/135?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #135      +/-   ##
==========================================
- Coverage   97.67%   97.25%   -0.42%     
==========================================
  Files           8        8              
  Lines        1202     1202              
  Branches      181      181              
==========================================
- Hits         1174     1169       -5     
- Misses         20       23       +3     
- Partials        8       10       +2
```


| [Impacted Files](https://codecov.io/gh/opensistemas-hub/osbrain/pull/135?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [osbrain/helper.py](https://codecov.io/gh/opensistemas-hub/osbrain/pull/135?src=pr&el=tree#diff-b3NicmFpbi9oZWxwZXIucHk=) | `91.22% <100%> (-8.78%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/opensistemas-hub/osbrain/pull/135?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/opensistemas-hub/osbrain/pull/135?src=pr&el=footer). Last update [d8ab57d...e80cd46](https://codecov.io/gh/opensistemas-hub/osbrain/pull/135?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/osbrain/helper.py b/osbrain/helper.py
index bcc6526..7b0a289 100644
--- a/osbrain/helper.py
+++ b/osbrain/helper.py
@@ -113,24 +113,26 @@ def agent_dies(agent, nsproxy, timeout=1.):
     return True
 
 
-def attribute_match(attribute, length=None, data=None, value=None):
+def attribute_match_all(attribute, length=None, data=None, value=None):
     """
-    Check if an attribute matches any condition:
+    Check if an attribute matches all of the following specified conditions:
 
     - Minimum length.
     - Contains an item.
     - Is exactly a value.
 
+    Note that only those conditions explicitly passed will be checked.
+
     Parameters
     ----------
     attribute : anything
         The attribute to match against.
     length : int, default is None
-        Return True if the attribute has this minimum length.
+        If specified, the attribute must reach this length (or higher).
     data : anything, default is None
-        Return True if the attribute contains this value.
+        If specified, the attribute must contain this value.
     value : anything, default is None
-        Return True if the attribute contains this value.
+        If specified, the attribute must be this value.
 
     Returns
     -------
@@ -138,25 +140,28 @@ def attribute_match(attribute, length=None, data=None, value=None):
         Whether the attribute matches any condition.
     """
     assert length is not None or data is not None or value is not None, \
-        'No condition passed! will return False always...'
-    if length is not None and len(attribute) >= length:
-        return True
-    if data is not None and data in attribute:
-        return True
-    if value is not None and attribute == value:
-        return True
-    return False
+        'No condition passed! Will return True always...'
+    if length is not None and len(attribute) < length:
+        return False
+    if data is not None and data not in attribute:
+        return False
+    if value is not None and attribute != value:
+        return False
+    return True
 
 
 def wait_agent_attr(agent, name='received', length=None, data=None, value=None,
                     timeout=3):
     """
-    Wait for an agent's attribute, to:
+    Wait for an agent's attribute to match all of the following specified
+    conditions:
 
     - Reach a minimum length.
     - Contain a particular item.
     - Become a given value.
 
+    Note that only those conditions explicitly passed will be checked.
+
     Parameters
     ----------
     agent : Proxy
@@ -164,18 +169,24 @@ def wait_agent_attr(agent, name='received', length=None, data=None, value=None,
     name : str, default is `'received'`
         Name of the agent's attribute to look for (should be a list).
     length : int, default is None
-        If specified, wait until the attribute reaches this length.
+        If specified, wait until the attribute reaches this length or higher.
     data : anything, default is None
         If specified, wait until the attribute contains this element.
     value : anything, default is None
         If specified, wait until the attribute becomes this value.
     timeout : float, default is 3
         After this number of seconds the function will return `False`.
+
+    Returns
+    -------
+    bool
+        Whether the specified conditions where met within the given time.
     """
     t0 = time.time()
     while True:
         attribute = agent.get_attr(name)
-        if attribute_match(attribute, length=length, data=data, value=value):
+        if attribute_match_all(attribute, length=length, data=data,
+                               value=value):
             return True
         if time.time() - t0 > timeout:
             break


Test patch:
diff --git a/osbrain/tests/test_helper.py b/osbrain/tests/test_helper.py
index eae2a0f..fb0670b 100644
--- a/osbrain/tests/test_helper.py
+++ b/osbrain/tests/test_helper.py
@@ -7,7 +7,7 @@ from osbrain import run_agent
 from osbrain import Agent
 from osbrain.helper import logger_received
 from osbrain.helper import agent_dies
-from osbrain.helper import attribute_match
+from osbrain.helper import attribute_match_all
 from osbrain.helper import wait_agent_attr
 
 from common import nsproxy  # pragma: no flakes
@@ -102,17 +102,21 @@ def test_logger_received_count(agent_logger):
     ([], 1, None, None, False),
     ([], None, 1, None, False),
     ([], None, None, [], True),
+    ([1, 2], 2, 2, None, True),
+    ([1, 2], 2, 3, None, False),
+    ([1, 2], 3, 2, None, False),
     ({'foo'}, 1, None, None, True),
     ({'foo'}, None, 'foo', None, True),
     ({'foo'}, None, None, {'foo'}, True),
     (42, None, None, 14, False),
     (42, None, None, 42, True),
 ])
-def test_attribute_match(attribute, length, data, value, match):
+def test_attribute_match_all(attribute, length, data, value, match):
     """
-    Test `attribute_match` function.
+    Test `attribute_match_all` function.
     """
-    result = attribute_match(attribute, length=length, data=data, value=value)
+    result = attribute_match_all(attribute, length=length, data=data,
+                                 value=value)
     assert result == match