1+ import os
12import unittest
23
3- from pyneuroml .swc .LoadSWC import SWCGraph , SWCNode
4+ from pyneuroml .swc .LoadSWC import SWCGraph , SWCNode , load_swc
45
56
67class TestSWCNode (unittest .TestCase ):
8+ """Test cases for the SWCNode class."""
9+
710 def test_init (self ):
11+ """Test the initialization of an SWCNode object."""
812 node = SWCNode (1 , 1 , 0.0 , 0.0 , 0.0 , 1.0 , - 1 )
913 self .assertEqual (node .id , 1 )
1014 self .assertEqual (node .type , 1 )
@@ -15,12 +19,16 @@ def test_init(self):
1519 self .assertEqual (node .parent_id , - 1 )
1620
1721 def test_invalid_init (self ):
22+ """Test that initializing an SWCNode with invalid data raises a ValueError."""
1823 with self .assertRaises (ValueError ):
1924 SWCNode ("a" , 1 , 0.0 , 0.0 , 0.0 , 1.0 , - 1 )
2025
2126
2227class TestSWCGraph (unittest .TestCase ):
28+ """Test cases for the SWCGraph class."""
29+
2330 def setUp (self ):
31+ """Set up a sample SWCGraph for testing."""
2432 self .tree = SWCGraph ()
2533 self .node1 = SWCNode (1 , 1 , 0.0 , 0.0 , 0.0 , 1.0 , - 1 )
2634 self .node2 = SWCNode (2 , 3 , 1.0 , 0.0 , 0.0 , 0.5 , 1 )
@@ -30,39 +38,95 @@ def setUp(self):
3038 self .tree .add_node (self .node3 )
3139
3240 def test_duplicate_node (self ):
41+ """Test that adding a duplicate node raises a ValueError."""
3342 with self .assertRaises (ValueError ):
3443 self .tree .add_node (SWCNode (1 , 1 , 0.0 , 0.0 , 0.0 , 1.0 , - 1 ))
3544
3645 def test_add_metadata (self ):
46+ """Test adding valid metadata to the SWCGraph."""
3747 self .tree .add_metadata ("ORIGINAL_SOURCE" , "file.swc" )
3848 self .assertEqual (self .tree .metadata ["ORIGINAL_SOURCE" ], "file.swc" )
3949
4050 def test_invalid_metadata (self ):
51+ """Test that adding invalid metadata does not modify the metadata dictionary."""
4152 self .tree .add_metadata ("INVALID_FIELD" , "value" )
4253 self .assertEqual (self .tree .metadata , {})
4354
4455 def test_get_parent (self ):
56+ """Test getting the parent node of a given node."""
4557 self .assertIsNone (self .tree .get_parent (self .node1 .id ))
4658 self .assertEqual (self .tree .get_parent (self .node2 .id ), self .node1 )
4759 self .assertEqual (self .tree .get_parent (self .node3 .id ), self .node2 )
4860 with self .assertRaises (ValueError ):
4961 self .tree .get_parent (4 )
5062
5163 def test_get_children (self ):
64+ """Test getting the children of a given node."""
5265 self .assertEqual (self .tree .get_children (self .node1 .id ), [self .node2 ])
5366 self .assertEqual (self .tree .get_children (self .node2 .id ), [self .node3 ])
5467 with self .assertRaises (ValueError ):
5568 self .tree .get_parent (4 )
5669
5770 def test_get_nodes_with_multiple_children (self ):
71+ """Test getting nodes with multiple children."""
5872 node4 = SWCNode (4 , 3 , 3.0 , 0.0 , 0.0 , 0.5 , 2 )
5973 self .tree .add_node (node4 )
6074 self .assertEqual (self .tree .get_nodes_with_multiple_children (), [self .node2 ])
6175
6276 def test_get_nodes_by_type (self ):
77+ """Test getting nodes by their type."""
6378 self .assertEqual (self .tree .get_nodes_by_type (1 ), [self .node1 ])
6479 self .assertEqual (self .tree .get_nodes_by_type (3 ), [self .node2 , self .node3 ])
6580
6681
82+ class TestSWCExport (unittest .TestCase ):
83+ """Test cases for exporting SWC files."""
84+
85+ def setUp (self ):
86+ """Set up file paths for testing."""
87+ current_dir = os .path .dirname (os .path .abspath (__file__ ))
88+ self .input_file = os .path .join (current_dir , "Case1_new.swc" )
89+ self .output_file = os .path .join (current_dir , "Case1_exported.swc" )
90+
91+ def test_load_export_compare (self ):
92+ """Test loading an SWC file, exporting it, and comparing the results."""
93+ # Load the original file
94+ original_tree = load_swc (self .input_file )
95+
96+ # Export the loaded tree
97+ original_tree .export_to_swc_file (self .output_file )
98+
99+ # Check if the exported file was created
100+ self .assertTrue (os .path .exists (self .output_file ))
101+
102+ # Load the exported file
103+ exported_tree = load_swc (self .output_file )
104+
105+ # Compare the original and exported trees
106+ self .assertEqual (len (original_tree .nodes ), len (exported_tree .nodes ))
107+
108+ # Compare a few key properties of the first and last nodes
109+ self .compare_nodes (original_tree .nodes [0 ], exported_tree .nodes [0 ])
110+ self .compare_nodes (original_tree .nodes [- 1 ], exported_tree .nodes [- 1 ])
111+
112+ # Compare metadata
113+ self .assertEqual (original_tree .metadata , exported_tree .metadata )
114+
115+ def compare_nodes (self , node1 , node2 ):
116+ """Compare two SWCNode objects for equality."""
117+ self .assertEqual (node1 .id , node2 .id )
118+ self .assertEqual (node1 .type , node2 .type )
119+ self .assertEqual (node1 .parent_id , node2 .parent_id )
120+ self .assertAlmostEqual (node1 .x , node2 .x , places = 4 )
121+ self .assertAlmostEqual (node1 .y , node2 .y , places = 4 )
122+ self .assertAlmostEqual (node1 .z , node2 .z , places = 4 )
123+ self .assertAlmostEqual (node1 .radius , node2 .radius , places = 4 )
124+
125+ def tearDown (self ):
126+ """Clean up by removing the exported file if it exists."""
127+ if os .path .exists (self .output_file ):
128+ os .remove (self .output_file )
129+
130+
67131if __name__ == "__main__" :
68132 unittest .main ()
0 commit comments