Coverage for tests/test_clustered_quantum_graph.py: 42%

84 statements  

« prev     ^ index     » next       coverage.py v7.3.0, created at 2023-08-23 10:45 +0000

1# This file is part of ctrl_bps. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <https://www.gnu.org/licenses/>. 

21"""Unit tests for the clustering methods. 

22""" 

23 

24# Turn off "doesn't conform to snake_case naming style" because matching 

25# the unittest casing. 

26# pylint: disable=invalid-name 

27 

28import os 

29import tempfile 

30import unittest 

31from collections import Counter 

32from pathlib import Path 

33 

34from cqg_test_utils import make_test_clustered_quantum_graph 

35from lsst.ctrl.bps import ClusteredQuantumGraph, QuantaCluster 

36from qg_test_utils import make_test_quantum_graph 

37 

38TESTDIR = os.path.abspath(os.path.dirname(__file__)) 

39 

40 

41class TestQuantaCluster(unittest.TestCase): 

42 """Tests for clustering.""" 

43 

44 def setUp(self): 

45 self.qgraph = make_test_quantum_graph() 

46 nodes = list(self.qgraph.getNodesForTask(self.qgraph.findTaskDefByLabel("T1"))) 

47 self.qnode1 = nodes[0] 

48 self.qnode2 = nodes[1] 

49 

50 def tearDown(self): 

51 pass 

52 

53 def testQgraphNodeIds(self): 

54 qc = QuantaCluster.from_quantum_node(self.qnode1, "{node_number}") 

55 self.assertEqual(qc.qgraph_node_ids, frozenset([self.qnode1.nodeId])) 

56 

57 def testQuantaCountsNone(self): 

58 qc = QuantaCluster("NoQuanta", "the_label") 

59 self.assertEqual(qc.quanta_counts, Counter()) 

60 

61 def testQuantaCounts(self): 

62 qc = QuantaCluster.from_quantum_node(self.qnode1, "{node_number}") 

63 self.assertEqual(qc.quanta_counts, Counter({"T1": 1})) 

64 

65 def testAddQuantumNode(self): 

66 qc = QuantaCluster.from_quantum_node(self.qnode1, "{node_number}") 

67 qc.add_quantum_node(self.qnode2) 

68 self.assertEqual(qc.quanta_counts, Counter({"T1": 2})) 

69 

70 def testAddQuantum(self): 

71 qc = QuantaCluster.from_quantum_node(self.qnode1, "{node_number}") 

72 qc.add_quantum(self.qnode2.quantum, self.qnode2.taskDef.label) 

73 self.assertEqual(qc.quanta_counts, Counter({"T1": 2})) 

74 

75 def testStr(self): 

76 qc = QuantaCluster.from_quantum_node(self.qnode1, "{node_number}") 

77 self.assertIn(qc.name, str(qc)) 

78 self.assertIn("T1", str(qc)) 

79 self.assertIn("tags", str(qc)) 

80 

81 def testEqual(self): 

82 qc1 = QuantaCluster.from_quantum_node(self.qnode1, "{node_number}") 

83 qc2 = QuantaCluster.from_quantum_node(self.qnode1, "{node_number}") 

84 self.assertEqual(qc1, qc2) 

85 

86 def testNotEqual(self): 

87 qc1 = QuantaCluster.from_quantum_node(self.qnode1, "{node_number}") 

88 qc2 = QuantaCluster.from_quantum_node(self.qnode2, "{node_number}") 

89 self.assertNotEqual(qc1, qc2) 

90 

91 def testHash(self): 

92 qc1 = QuantaCluster.from_quantum_node(self.qnode1, "{node_number}") 

93 qc2 = QuantaCluster.from_quantum_node(self.qnode2, "{node_number}") 

94 self.assertNotEqual(hash(qc1), hash(qc2)) 

95 

96 

97class TestClusteredQuantumGraph(unittest.TestCase): 

98 """Tests for single_quantum_clustering method.""" 

99 

100 def setUp(self): 

101 self.tmpdir = tempfile.mkdtemp() 

102 self.qgraph, self.cqg1 = make_test_clustered_quantum_graph(self.tmpdir) 

103 

104 def tearDown(self): 

105 pass 

106 # if self.tmpdir is not None and os.path.exists(self.tmpdir): 

107 # shutil.rmtree(self.tmppath, ignore_errors=True) 

108 

109 def testName(self): 

110 self.assertEqual(self.cqg1.name, "cqg1") 

111 

112 def testQgraph(self): 

113 """Test qgraph method""" 

114 self.assertEqual(self.cqg1.qgraph, self.qgraph) 

115 

116 # def testAddClusterSingle(self): 

117 # """Test add_cluster method for single new cluster.""" 

118 

119 def testGetClusterExists(self): 

120 """Test get_cluster method where cluster exists.""" 

121 self.assertEqual("T1_1_2", self.cqg1.get_cluster("T1_1_2").name) 

122 

123 def testGetClusterMissing(self): 

124 """Test get_cluster method where cluster doesn't exist.""" 

125 with self.assertRaises(KeyError): 

126 _ = self.cqg1.get_cluster("Not_There") 

127 

128 # def testGetQuantumNodeExists(self): 

129 # """Test get_quantum_node method where node exists.""" 

130 # 

131 # def testGetQuantumNodeMissing(self): 

132 # """Test get_quantum_node method where node doesn't exist.""" 

133 

134 def testClusters(self): 

135 """Test clusters method returns in correct order""" 

136 

137 def testSuccessorsExisting(self): 

138 """Test successors method returns existing successors.""" 

139 self.assertEqual(list(self.cqg1.successors("T1_1_2")), ["T23_1_2"]) 

140 

141 def testSuccessorsNone(self): 

142 """Test successors method handles no successors.""" 

143 # check iterable and empty 

144 self.assertEqual(len(list(self.cqg1.successors("T4_1_2"))), 0) 

145 

146 def testPredecessorsExisting(self): 

147 """Test predecessors method returns existing predecessors.""" 

148 self.assertEqual(list(self.cqg1.predecessors("T23_1_2")), ["T1_1_2"]) 

149 

150 def testPredecessorsNone(self): 

151 """Test predecessors method handles no predecessors.""" 

152 # check iterable and empty 

153 self.assertEqual(len(list(self.cqg1.predecessors("T1_1_2"))), 0) 

154 

155 # def testAddDependency(self): 

156 

157 def testSaveAndLoad(self): 

158 path = Path(f"{self.tmpdir}/save_1.pickle") 

159 self.cqg1.save(path) 

160 self.assertTrue(path.is_file() and path.stat().st_size) 

161 test_cqg = ClusteredQuantumGraph.load(path) 

162 self.assertEqual(self.cqg1, test_cqg) 

163 

164 

165if __name__ == "__main__": 165 ↛ 166line 165 didn't jump to line 166, because the condition on line 165 was never true

166 unittest.main()