Skip to content

Commit

Permalink
Merge pull request #1 from Comfy-Org/rh-add-tests
Browse files Browse the repository at this point in the history
Add tests for validating RETURN_TYPES.
  • Loading branch information
robinjhuang authored Dec 31, 2024
2 parents 3f51b80 + 8718712 commit 7fe9a9e
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 31 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Unit Tests

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-dev.txt
- name: Run tests with pytest
run: |
pytest tests/
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
__pycache__/
*.py[cod]
*$py.class
.coverage
coverage.xml
.pytest_cache/
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12
Empty file added README.md
Empty file.
9 changes: 9 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[project]
name = "node-diff"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
"pytest>=8.3.4",
]
4 changes: 4 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[pytest]
pythonpath = .
testpaths = tests
python_files = test_*.py
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest
42 changes: 11 additions & 31 deletions src/validate_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

class BreakingChangeType(Enum):
RETURN_TYPES_CHANGED = "Return types changed"
RETURN_TYPES_REORDERED = "Return types reordered"
INPUT_REMOVED = "Required input removed"
INPUT_TYPE_CHANGED = "Input type changed"
NODE_REMOVED = "Node removed"
Expand Down Expand Up @@ -91,36 +90,17 @@ def compare_return_types(node_name: str, base_class: Type, pr_class: Type) -> li
base_types = getattr(base_class, "RETURN_TYPES", tuple())
pr_types = getattr(pr_class, "RETURN_TYPES", tuple())

if len(base_types) != len(pr_types):
changes.append(BreakingChange(
node_name=node_name,
change_type=BreakingChangeType.RETURN_TYPES_CHANGED,
details=f"Number of return types changed from {len(base_types)} to {len(pr_types)}",
base_value=base_types,
pr_value=pr_types
))
return changes

# Check for type changes and reordering
base_types_set = set(base_types)
pr_types_set = set(pr_types)

if base_types_set != pr_types_set:
changes.append(BreakingChange(
node_name=node_name,
change_type=BreakingChangeType.RETURN_TYPES_CHANGED,
details="Return types changed",
base_value=base_types,
pr_value=pr_types
))
elif base_types != pr_types:
changes.append(BreakingChange(
node_name=node_name,
change_type=BreakingChangeType.RETURN_TYPES_REORDERED,
details="Return types were reordered",
base_value=base_types,
pr_value=pr_types
))
# Check if all base return types are preserved in PR
for i, base_type in enumerate(base_types):
if i >= len(pr_types) or pr_types[i] != base_type:
changes.append(BreakingChange(
node_name=node_name,
change_type=BreakingChangeType.RETURN_TYPES_CHANGED,
details="Return types changed or removed. This is a breaking change.",
base_value=base_types,
pr_value=pr_types
))
return changes

return changes

Expand Down
60 changes: 60 additions & 0 deletions tests/test_validate_nodes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import pytest
from src.validate_nodes import compare_return_types, BreakingChangeType

class BaseNode:
RETURN_TYPES = ("STRING", "INT", "FLOAT")

class PRNode:
RETURN_TYPES = ("STRING", "INT", "FLOAT")

class ReorderedNode:
RETURN_TYPES = ("INT", "STRING", "FLOAT")

class IncompatibleNode:
RETURN_TYPES = ("STRING", "BOOL", "FLOAT")

class AddTypeNode:
RETURN_TYPES = ("STRING", "INT", "FLOAT", "BOOL")

class RemoveTypeNode:
RETURN_TYPES = ("STRING", "INT")

class NoReturnTypesNode:
pass

def test_compare_return_types_no_changes():
changes = compare_return_types("TestNode", BaseNode, PRNode)
assert len(changes) == 0


def test_compare_return_types_add_type():
changes = compare_return_types("TestNode", BaseNode, AddTypeNode)
assert len(changes) == 0

def test_compare_return_types_remove_type():
changes = compare_return_types("TestNode", BaseNode, RemoveTypeNode)
assert len(changes) == 1
assert changes[0].change_type == BreakingChangeType.RETURN_TYPES_CHANGED
assert changes[0].base_value == ("STRING", "INT", "FLOAT")
assert changes[0].pr_value == ("STRING", "INT")

def test_compare_return_types_reordered():
changes = compare_return_types("TestNode", BaseNode, ReorderedNode)
assert len(changes) == 1
assert changes[0].change_type == BreakingChangeType.RETURN_TYPES_CHANGED
assert changes[0].base_value == ("STRING", "INT", "FLOAT")
assert changes[0].pr_value == ("INT", "STRING", "FLOAT")

def test_compare_return_types_incompatible():
changes = compare_return_types("TestNode", BaseNode, IncompatibleNode)
assert len(changes) == 1
assert changes[0].change_type == BreakingChangeType.RETURN_TYPES_CHANGED
assert changes[0].base_value == ("STRING", "INT", "FLOAT")
assert changes[0].pr_value == ("STRING", "BOOL", "FLOAT")

def test_compare_return_types_missing():
changes = compare_return_types("TestNode", BaseNode, NoReturnTypesNode)
assert len(changes) == 1
assert changes[0].change_type == BreakingChangeType.RETURN_TYPES_CHANGED
assert changes[0].base_value == ("STRING", "INT", "FLOAT")
assert changes[0].pr_value == tuple()
64 changes: 64 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7fe9a9e

Please sign in to comment.