Coverage for tests / unit / views / test_cytoscape.py: 100%

60 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-16 21:32 +0000

1from json import load, loads 

2 

3from graphable.graph import Graph 

4from graphable.graphable import Graphable 

5from graphable.registry import EXPORTERS 

6from graphable.views.cytoscape import ( 

7 CytoscapeStylingConfig, 

8 create_topology_cytoscape, 

9 export_topology_cytoscape, 

10) 

11 

12 

13def test_cytoscape_registration(): 

14 """Verify that .cy.json is registered as an exporter.""" 

15 assert ".cy.json" in EXPORTERS 

16 

17 

18def test_cytoscape_config_defaults(): 

19 """Verify default configuration values for Cytoscape.""" 

20 config = CytoscapeStylingConfig() 

21 assert config.node_data_fnc is None 

22 assert config.edge_data_fnc is None 

23 assert config.indent == 2 

24 

25 node = Graphable("A") 

26 assert config.reference_fnc(node) == "A" 

27 

28 

29def test_create_topology_cytoscape_simple(): 

30 """Verify simple graph conversion to Cytoscape JSON.""" 

31 g = Graph() 

32 a = Graphable("A") 

33 b = Graphable("B") 

34 g.add_edge(a, b, weight=10) 

35 

36 output = create_topology_cytoscape(g) 

37 data = loads(output) 

38 

39 # Expected: 2 nodes, 1 edge 

40 assert len(data) == 3 

41 

42 nodes = [ 

43 item for item in data if "source" not in item["data"] and "id" in item["data"] 

44 ] 

45 edges = [item for item in data if "source" in item["data"]] 

46 

47 assert len(nodes) == 2 

48 assert len(edges) == 1 

49 

50 # Nodes might be in any order depending on topo sort 

51 node_ids = {n["data"]["id"] for n in nodes} 

52 assert node_ids == {"A", "B"} 

53 

54 assert edges[0]["data"]["source"] == "A" 

55 assert edges[0]["data"]["target"] == "B" 

56 assert edges[0]["data"]["weight"] == 10 

57 

58 

59def test_create_topology_cytoscape_with_tags(): 

60 """Verify node tags are included in Cytoscape JSON.""" 

61 g = Graph() 

62 a = Graphable("A") 

63 a.add_tag("v1") 

64 g.add_node(a) 

65 

66 output = create_topology_cytoscape(g) 

67 data = loads(output) 

68 

69 assert data[0]["data"]["tags"] == ["v1"] 

70 

71 

72def test_export_topology_cytoscape(tmp_path): 

73 """Verify file writing functionality for Cytoscape.""" 

74 g = Graph() 

75 g.add_node(Graphable("A")) 

76 

77 output_file = tmp_path / "graph.cy.json" 

78 export_topology_cytoscape(g, output_file) 

79 

80 assert output_file.exists() 

81 with open(output_file, "r") as f: 

82 data = load(f) 

83 assert data[0]["data"]["id"] == "A" 

84 

85 

86def test_create_topology_cytoscape_custom_data(): 

87 """Verify custom data mapping functions in Cytoscape JSON.""" 

88 g = Graph() 

89 a = Graphable("A") 

90 b = Graphable("B") 

91 g.add_edge(a, b) 

92 

93 config = CytoscapeStylingConfig( 

94 node_data_fnc=lambda n: {"extra_node": True}, 

95 edge_data_fnc=lambda u, v: {"extra_edge": True}, 

96 ) 

97 

98 output = create_topology_cytoscape(g, config) 

99 data = loads(output) 

100 

101 nodes = [item for item in data if "source" not in item["data"]] 

102 edges = [item for item in data if "source" in item["data"]] 

103 

104 assert all(n["data"]["extra_node"] is True for n in nodes) 

105 assert all(e["data"]["extra_edge"] is True for e in edges)