Skip to content

Commit

Permalink
Merge pull request #39 from skyforest/improve-test-coverage
Browse files Browse the repository at this point in the history
This is part of #11.

* Testing if namify converts class name and module into desired format

* defining test for draw_connections

* classes to test relationships

* assertions showing if relationships are true within graph connections (includes leading spaces which was throwing me off)

* reverting .gitignore to original

* running lint

* formatting to make ruff happy
  • Loading branch information
pydanny committed Oct 2, 2023
2 parents e29e976 + 01a8695 commit 4b02cf1
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions tests/test_dj_notebook.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,95 @@
from dj_notebook import activate
from dj_notebook.shell_plus import DiagramClass


def test_thing():
plus = activate("test_harness")
# TODO capture STDOUT and assert on it
assert plus.print() is None


def test_namify():
"""
Test the `namify` method of the `DiagramClass`.
Checks if the `namify` method correctly converts the class
name and its module into a format that replaces dots with underscores.
Test covers three scenarios:
1. Built-in classes (e.g., `str`).
2. Custom classes (e.g., `DiagramClass`).
3. Nested classes (e.g., `OuterClass.InnerClass`).
TODO: Maybe add scenarios of periods included in naming to
test conversion
"""
# Create an instance of DiagramClass - include a sample class
diagram = DiagramClass(str)

# Test built-in class
assert diagram.namify(str) == "builtins_str"

# Test custom class
assert diagram.namify(DiagramClass) == "dj_notebook_shell_plus_DiagramClass"

# Test nested class
class OuterClass:
class InnerClass:
pass

assert diagram.namify(OuterClass.InnerClass) == "tests_test_dj_notebook_InnerClass"


def test_draw_connections():
"""
Test the `draw_connections` functionality of the `DiagramClass`.
Verifies that the graph generated by `DiagramClass` correctly
reflects the relationships between a sample class (`SampleClass`) and its
direct base classes (`TestClassA` and `TestClassB`).
TODO: There is an oddity with needing to add 2 leading spaces to the
assertions... look on line 47 in the `draw_connections` definition
which it needs to match.
"""

# Define base classes
class TestClassA:
pass

class TestClassB:
pass

# Create a sample class that inherits from the base classes
class SampleClass(TestClassA, TestClassB):
pass

diagram = DiagramClass(SampleClass)

# Check if the graph has nodes for the SampleClass and its ancestors
sample_class_node = (
f" class {diagram.namify(SampleClass)}"
f'["{SampleClass.__module__}::{SampleClass.__name__}"]'
)
assert sample_class_node in diagram.graph

test_class_a_node = (
f" class {diagram.namify(TestClassA)}"
f'["{TestClassA.__module__}::{TestClassA.__name__}"]'
)
assert test_class_a_node in diagram.graph

test_class_b_node = (
f" class {diagram.namify(TestClassB)}"
f'["{TestClassB.__module__}::{TestClassB.__name__}"]'
)
assert test_class_b_node in diagram.graph

# Check if the graph has connections between the SampleClass and its ancestors
assert (
f" {diagram.namify(TestClassA)} <|-- {diagram.namify(SampleClass)}"
in diagram.graph
)
assert (
f" {diagram.namify(TestClassB)} <|-- {diagram.namify(SampleClass)}"
in diagram.graph
)

0 comments on commit 4b02cf1

Please sign in to comment.