Skip to content

Commit 7646045

Browse files
committed
Cleanup backends unit tests
1 parent db943b8 commit 7646045

File tree

8 files changed

+104
-91
lines changed

8 files changed

+104
-91
lines changed

contextshell/backends/FilesystemTree.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33

44
from ..path import NodePath
55
from ..action import action_from_function, BuiltinExecutor
6-
from ..exceptions import NotSupportedError
76

87

9-
class FilesystemRoot(BuiltinExecutor):
8+
class FilesystemTree(BuiltinExecutor):
109
def __init__(self, root_directory_path: str):
1110
super().__init__()
1211
self.root_directory_path = Path(root_directory_path)

contextshell/backends/NodeTree.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ def get(self):
2323
def set(self, new_value):
2424
"""Store provided value in this node"""
2525
if type(self._value) != type(new_value):
26-
raise TypeError("Cannot assign value with type '{}' to '{}' node".format(type(new_value).__name__, type(self._value).__name__))
26+
raise TypeError("Cannot assign value with type '{}' to '{}' node".format(type(new_value).__name__,
27+
type(self._value).__name__))
2728
self._value = new_value
2829

2930
def list(self):
@@ -36,7 +37,7 @@ def list(self):
3637
index += 1
3738
return indexed_names
3839

39-
def append(self, node, name: str=None):
40+
def append(self, node, name: str = None):
4041
"""Append provided node as a subnode"""
4142
if node is None:
4243
raise ValueError("Cannot append None as node")
@@ -48,7 +49,7 @@ def append(self, node, name: str=None):
4849
node._parent = self
4950
self._subnodes.append((name, node))
5051

51-
def get_node(self, name: str=None, index: int=None) -> Optional['Node']:
52+
def get_node(self, name: str = None, index: int = None) -> Optional['Node']:
5253
"""Return subnode with provided name or index"""
5354
if name is not None:
5455
for p in self._subnodes:
@@ -71,7 +72,7 @@ def __getitem__(self, name_or_index) -> 'Node':
7172
raise KeyError(name_or_index)
7273
return node
7374

74-
def remove(self, name: str=None, index: int=None) -> 'Node':
75+
def remove(self, name: str = None, index: int = None) -> 'Node':
7576
"""Remove subnode with provided name"""
7677
node_to_remove = self.get_node(name=name, index=index)
7778
if node_to_remove is None:
@@ -80,7 +81,7 @@ def remove(self, name: str=None, index: int=None) -> 'Node':
8081
node_to_remove._parent = None
8182
return node_to_remove
8283

83-
def contains(self, name: str=None, index: int=None) -> bool:
84+
def contains(self, name: str = None, index: int = None) -> bool:
8485
"""Checks if there is a subnode with provided name"""
8586
return self.get_node(name=name, index=index) is not None
8687

@@ -91,6 +92,7 @@ def __contains__(self, name: str):
9192
# CHECK: how to implement TemporaryTreeRoot (based on NodeTreeRoot)
9293
class NodeTreeRoot(ActionExecutor):
9394
"""Frontend to the (passive) node-based data storage"""
95+
9496
def __init__(self):
9597
self.root = self.create_node(None)
9698
self.install_default_actions()
@@ -200,7 +202,7 @@ def find_type(self, target: NodePath, type_name: NodePath): # TODO: add type-hi
200202
def create_node(self, value):
201203
return Node(value)
202204

203-
def create(self, path: NodePath, initial_value = None):
205+
def create(self, path: NodePath, initial_value=None):
204206
parent = self._create_path(path.base_path)
205207
new_node = self.create_node(initial_value)
206208
parent.append(new_node, path.base_name)
@@ -232,7 +234,7 @@ def _resolve_path(self, path: NodePath, root: Node = None) -> Node:
232234
raise NameError("'{}' doesn't exists".format(path))
233235
return node
234236

235-
def _resolve_optional_path(self, path: NodePath, root: Node=None) -> Optional[Node]:
237+
def _resolve_optional_path(self, path: NodePath, root: Node = None) -> Optional[Node]:
236238
if root is None:
237239
if path.is_relative:
238240
raise ValueError("Could not resolve relative paths")

tests/functional/FilesystemTests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
from contextshell.action import ActionExecutor
55
from tests.functional.ShellTestsBase import TreeRootTestsBase
66
from tests.functional.TestExecutor import script_test
7-
from contextshell.backends.FilesystemTree import FilesystemRoot
7+
from contextshell.backends.FilesystemTree import FilesystemTree
88

99

1010
class FilesystemTestsBase(TreeRootTestsBase):
1111
test_directory_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'test_data')
1212

1313
def create_tree_root(self) -> ActionExecutor:
14-
return FilesystemRoot(self.test_directory.name)
14+
return FilesystemTree(self.test_directory.name)
1515

1616
def _make_test_path(self, relative_path):
1717
return Path(self.test_directory.name).joinpath(relative_path)

tests/unit/Fakes.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from collections import OrderedDict
2+
13
from contextshell.path import NodePath
2-
from contextshell.action import Action, ActionArgsPack
4+
from contextshell.action import Action, ActionArgsPack, ActionExecutor
35
from typing import Dict
46

57

@@ -30,3 +32,17 @@ def invoke(self, target: NodePath, action: NodePath, arguments: ActionArgsPack):
3032
self.received_action = action
3133
self.received_arguments = arguments
3234
return self.return_value
35+
36+
37+
class FakeActionExecutor(ActionExecutor):
38+
def __init__(self):
39+
self.execute_target = None
40+
self.execute_action = None
41+
self.execute_args = None
42+
self.execute_return = None
43+
44+
def execute(self, target: NodePath, action: NodePath, args: ActionArgsPack = None):
45+
self.execute_target = target
46+
self.execute_action = action
47+
self.execute_args = args if args else OrderedDict()
48+
return self.execute_return
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import unittest
2+
3+
from contextshell.path import NodePath as np
4+
5+
6+
def create_filesystem_tree(*args, **kwargs):
7+
from contextshell.backends.FilesystemTree import FilesystemTree
8+
return FilesystemTree(*args, **kwargs)
9+

0 commit comments

Comments
 (0)