Skip to content

Commit

Permalink
selement f
Browse files Browse the repository at this point in the history
  • Loading branch information
HMiry committed Dec 12, 2023
1 parent 9596834 commit d381289
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
80 changes: 80 additions & 0 deletions mytest/byron/classes/node_view-test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import pytest
import networkx as nx
from byron.classes.node_view import NodeView
from byron.classes.node_reference import NodeReference
from byron.classes.selement import SElement # Replace with actual import

# Assuming a basic implementation of SElement
class MockSElement(SElement):
def __init__(self):
pass

def dump(self, value_bag):
return "MockSElement dump"

# Setup a graph for testing
@pytest.fixture
def setup_graph():
G = nx.MultiDiGraph()
# Add nodes and edges to the graph as necessary for testing
for i in range(1, 4):
G.add_node(i, _type="SomeType", _selement=MockSElement())
G.add_edge(1, 2, _type="FRAMEWORK")
G.add_edge(2, 3, _type="FRAMEWORK")
return G

# Test NodeView creation and basic properties
def test_node_view_creation(setup_graph):
node_ref = NodeReference(graph=setup_graph, node=1)
node_view = NodeView(ref=node_ref)

assert node_view.ref == node_ref
assert node_view.graph is setup_graph
assert node_view.node == 1
assert isinstance(node_view.selement, MockSElement)

# Test NodeView methods and properties
def test_node_view_properties(setup_graph):
node_ref = NodeReference(graph=setup_graph, node=1)
node_view = NodeView(ref=node_ref)

# Test safe_dump property
assert node_view.safe_dump == "MockSElement dump"

# Test node_type property
assert node_view.node_type == "SomeType"

# Test path_string property
assert node_view.path_string == "1.2"

# Test tree property
tree = node_view.tree
assert isinstance(tree, nx.DiGraph)
assert set(tree.nodes) == set(setup_graph.nodes)

# Test parent property
parent_view = node_view.parent
assert isinstance(parent_view, NodeView)
assert parent_view.node == 0 # Assuming 0 is the root node

# Test children property
children_views = node_view.children
assert all(isinstance(child, NodeView) for child in children_views)
assert [child.node for child in children_views] == [2] # Node 1 has one child, Node 2

# Test path property
path = node_view.path
assert all(isinstance(pv, NodeView) for pv in path)
assert [pv.node for pv in path] == [0, 1]

# Test out_degree property
assert node_view.out_degree == 1 # Node 1 has one outgoing edge of type FRAMEWORK

# Add more tests for other properties and methods as needed

# Test NodeView.make static method
def test_node_view_make(setup_graph):
node_view = NodeView.make(setup_graph, 1)
assert isinstance(node_view, NodeView)
assert node_view.node == 1

61 changes: 61 additions & 0 deletions mytest/byron/classes/selement-test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import pytest
from byron.classes.selement import SElement
import logging
class TestSElement:

def test_instantiation(self):
element = SElement()
assert isinstance(element, SElement)

def test_equality(self):
element1 = SElement()
element2 = SElement()
assert element1 != element2
assert element1 == element1


def test_representation(self):
element = SElement()
expected_repr = f"<SElement at {hex(id(element))}>"
assert repr(element) == expected_repr

def test_hash(self):
element = SElement()
assert hash(element) == id(element)

def test_add_node_check(self):
def dummy_check(node):
return True

SElement.add_node_check(dummy_check)
assert dummy_check in SElement.NODE_CHECKS

def test_is_valid(self):
def always_true_check(node):
return True

SElement.add_node_check(always_true_check)
element = SElement()
assert element.is_valid(None) is True


def test_is_valid_debug(self, caplog):
caplog.set_level(logging.DEBUG)

def failing_check(node):
return False

SElement.add_node_check(failing_check)
element = SElement()
element._is_valid_debug(None)
assert "is_valid: Failed check on None" in caplog.text


@pytest.fixture
def cleanup_node_checks():
yield
SElement.NODE_CHECKS = tuple()

@pytest.mark.usefixtures("cleanup_node_checks")
class TestSElementWithCleanup(TestSElement):
pass

0 comments on commit d381289

Please sign in to comment.