Skip to content

Commit

Permalink
added tests for vis styles
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanovaos committed Sep 12, 2024
1 parent c7bf3f8 commit cd69840
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions tests/test_vis_styles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import pytest
from networkcommons.visual._styles import get_styles, set_style_attributes, merge_styles


class MockItem:
"""
A simple mock class to simulate graph items (nodes or edges) for testing.
"""

def __init__(self):
self.attr = {}


def test_get_styles():
styles = get_styles()

# Check if the returned styles are as expected
assert isinstance(styles, dict), "Styles should be a dictionary."
assert 'default' in styles, "'default' should be a key in styles."
assert 'sign_consistent' in styles, "'sign_consistent' should be a key in styles."

# Check structure of a 'nodes' style
assert 'nodes' in styles['default'], "'nodes' should be a key in 'default' style."
assert 'sources' in styles['default']['nodes'], "'sources' should be a key in 'default.nodes' style."
assert 'color' in styles['default']['nodes']['sources'], "'color' should be a key in 'default.nodes.sources'."
assert styles['default']['nodes']['sources']['color'] == '#1f78b4', "Unexpected color for 'default.nodes.sources'."

# Check structure of an 'edges' style
assert 'edges' in styles['default'], "'edges' should be a key in 'default' style."
assert 'positive' in styles['default']['edges'], "'positive' should be a key in 'default.edges'."
assert styles['default']['edges']['positive'][
'color'] == '#33a02c', "Unexpected color for 'default.edges.positive'."


def test_set_style_attributes():
item = MockItem()
base_style = {'color': 'blue', 'shape': 'circle'}
condition_style = {'color': 'red'}

# Apply base and condition styles to the item
result = set_style_attributes(item, base_style, condition_style)

# Check if the attributes were set correctly
assert result.attr['color'] == 'red', "Condition style should override base style for 'color'."
assert result.attr['shape'] == 'circle', "Base style 'shape' should be applied."

# Test when condition_style is None
item2 = MockItem()
result2 = set_style_attributes(item2, base_style)

assert result2.attr['color'] == 'blue', "Base style 'color' should be applied when no condition style."
assert result2.attr['shape'] == 'circle', "Base style 'shape' should be applied when no condition style."


def test_merge_styles():
default_style = {
'nodes': {
'color': 'blue',
'shape': 'circle'
},
'edges': {
'color': 'gray',
'penwidth': 2
}
}

custom_style = {
'nodes': {
'color': 'red'
},
'edges': {
'penwidth': 3
}
}

merged = merge_styles(default_style, custom_style)

# Check if the merged style correctly reflects the custom style
assert merged['nodes']['color'] == 'red', "Custom style should override default 'color' for 'nodes'."
assert merged['nodes']['shape'] == 'circle', "'shape' should remain from default style."
assert merged['edges']['color'] == 'gray', "'color' for 'edges' should remain from default style."
assert merged['edges']['penwidth'] == 3, "Custom style should override 'penwidth' for 'edges'."

# Test missing keys handling (logging is ignored in this test)
custom_style_missing = {
'nodes': {
# Missing 'shape'
'color': 'red'
}
}

merged_missing = merge_styles(default_style, custom_style_missing)

assert merged_missing['nodes'][
'shape'] == 'circle', "'shape' should remain from default style when missing in custom."
assert merged_missing['nodes']['color'] == 'red', "Custom 'color' should still override the default."

0 comments on commit cd69840

Please sign in to comment.