-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #186 from kayjan/optional-assertions
Optional assertions
- Loading branch information
Showing
11 changed files
with
351 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import os | ||
|
||
ASSERTIONS: bool = bool(os.environ.get("BIGTREE_CONF_ASSERTIONS", True)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Remove Tree Checks | ||
|
||
!!! note | ||
|
||
Available from version 0.16.2 onwards | ||
|
||
When constructing trees, there are a few checks done that slow down performance. | ||
This slowness will be more apparent with very large trees. The checks are to | ||
|
||
- Check parent/children data type | ||
- Check for loops (expensive for trees that are deep as it checks the ancestors of node) | ||
|
||
These checks are enabled by default. To turn off these checks, you can set environment variable before importing `bigtree`. | ||
|
||
```python | ||
import os | ||
os.environ["BIGTREE_CONF_ASSERTIONS"] = "" | ||
|
||
import bigtree | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import unittest | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
from bigtree.node.basenode import BaseNode | ||
|
||
|
||
@patch("bigtree.node.basenode.ASSERTIONS", "") | ||
class TestBaseNodeNoAssertions(unittest.TestCase): | ||
def setUp(self): | ||
""" | ||
Tree should have structure | ||
a (age=90) | ||
|-- b (age=65) | ||
| |-- d (age=40) | ||
| +-- e (age=35) | ||
| |-- g (age=10) | ||
| +-- h (age=6) | ||
+-- c (age=60) | ||
+-- f (age=38) | ||
""" | ||
self.a = BaseNode(name="a", age=90) | ||
self.b = BaseNode(name="b", age=65) | ||
self.c = BaseNode(name="c", age=60) | ||
self.d = BaseNode(name="d", age=40) | ||
self.e = BaseNode(name="e", age=35) | ||
self.f = BaseNode(name="f", age=38) | ||
self.g = BaseNode(name="g", age=10) | ||
self.h = BaseNode(name="h", age=6) | ||
|
||
def tearDown(self): | ||
self.a = None | ||
self.b = None | ||
self.c = None | ||
self.d = None | ||
self.e = None | ||
self.f = None | ||
self.g = None | ||
self.h = None | ||
|
||
def test_set_children_none_parent_error(self): | ||
# TypeError: 'NoneType' object is not iterable | ||
children = None | ||
with pytest.raises(TypeError): | ||
self.h.children = children | ||
|
||
def test_set_parent_type_error(self): | ||
# AttributeError: 'int' object has no attribute '_BaseNode__children' | ||
parent = 1 | ||
with pytest.raises(AttributeError): | ||
self.a.parent = parent | ||
|
||
def test_set_parent_loop_error(self): | ||
# No error without assertion | ||
self.a.parent = self.a | ||
|
||
# No error without assertion | ||
self.b.parent = self.a | ||
self.c.parent = self.b | ||
self.a.parent = self.c | ||
|
||
def test_set_children_type_error(self): | ||
# AttributeError: 'int' object has no attribute '_BaseNode__children' | ||
children = 1 | ||
with pytest.raises(AttributeError): | ||
self.a.children = [self.b, children] | ||
|
||
# AttributeError: 'NoneType' object has no attribute 'parent' | ||
children = None | ||
with pytest.raises(AttributeError): | ||
self.a.children = [self.b, children] | ||
|
||
def test_set_children_loop_error(self): | ||
# No error without assertion | ||
self.a.children = [self.b, self.a] | ||
|
||
# No error without assertion | ||
self.a.children = [self.b, self.c] | ||
self.c.children = [self.d, self.e, self.f] | ||
self.f.children = [self.a] | ||
|
||
def test_set_duplicate_children_error(self): | ||
# No error without assertion | ||
self.a.children = [self.b, self.b] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import unittest | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
from bigtree.node.basenode import BaseNode | ||
from bigtree.node.binarynode import BinaryNode | ||
from bigtree.node.node import Node | ||
from bigtree.utils.exceptions import TreeError | ||
|
||
|
||
@patch("bigtree.node.binarynode.ASSERTIONS", "") | ||
class TestBinaryNodeNoAssertions(unittest.TestCase): | ||
def setUp(self): | ||
self.a = BinaryNode(1) | ||
self.b = BinaryNode(2) | ||
self.c = BinaryNode(3) | ||
self.d = BinaryNode(4) | ||
self.e = BinaryNode(5) | ||
self.f = BinaryNode(6) | ||
self.g = BinaryNode(7) | ||
self.h = BinaryNode(8) | ||
|
||
def tearDown(self): | ||
self.a = None | ||
self.b = None | ||
self.c = None | ||
self.d = None | ||
self.e = None | ||
self.f = None | ||
self.g = None | ||
self.h = None | ||
|
||
def test_set_parent_type_error(self): | ||
# AttributeError: 'int' object has no attribute '_BinaryNode__children' | ||
parent = 1 | ||
with pytest.raises(AttributeError): | ||
self.a.parent = parent | ||
|
||
# AttributeError: 'BaseNode' object has no attribute '_BinaryNode__children' | ||
parent = BaseNode() | ||
with pytest.raises(AttributeError): | ||
self.a.parent = parent | ||
|
||
# AttributeError: 'Node' object has no attribute '_BinaryNode__children' | ||
parent = Node("a") | ||
with pytest.raises(AttributeError): | ||
self.a.parent = parent | ||
|
||
def test_set_parent_loop_error(self): | ||
# No error without assertion | ||
self.a.parent = self.a | ||
|
||
# No error without assertion | ||
self.b.parent = self.a | ||
self.c.parent = self.b | ||
self.a.parent = self.c | ||
|
||
def test_set_children_type_error(self): | ||
# AttributeError: 'int' object has no attribute 'parent' | ||
children = 1 | ||
with pytest.raises(AttributeError): | ||
self.a.children = [self.b, children] | ||
|
||
# No error without assertion | ||
children = BaseNode() | ||
self.a.children = [children, None] | ||
|
||
# bigtree.utils.exceptions.TreeError: 'NoneType' object has no attribute '_BinaryNode__children' | ||
children = Node("a") | ||
with pytest.raises(TreeError): | ||
self.a.children = [children, None] | ||
|
||
def test_set_children_loop_error(self): | ||
# No error without assertion | ||
self.a.children = [self.b, self.a] | ||
|
||
# No error without assertion | ||
self.a.children = [self.b, self.c] | ||
self.c.children = [self.d, self.e] | ||
self.e.children = [self.a, self.f] | ||
|
||
def test_set_duplicate_children_error(self): | ||
# No error without assertion | ||
self.a.children = [self.b, self.b] |
Oops, something went wrong.