Coverage for tests/test_clustered_quantum_graph.py: 42%

84 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-22 11:03 +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 software is dual licensed under the GNU General Public License and also 

10# under a 3-clause BSD license. Recipients may choose which of these licenses 

11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt, 

12# respectively. If you choose the GPL option then the following text applies 

13# (but note that there is still no warranty even if you opt for BSD instead): 

14# 

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

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

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

18# (at your option) any later version. 

19# 

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

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

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

23# GNU General Public License for more details. 

24# 

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

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

27"""Unit tests for the clustering methods. 

28""" 

29 

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

31# the unittest casing. 

32# pylint: disable=invalid-name 

33 

34import os 

35import tempfile 

36import unittest 

37from collections import Counter 

38from pathlib import Path 

39 

40from cqg_test_utils import make_test_clustered_quantum_graph 

41from lsst.ctrl.bps import ClusteredQuantumGraph, QuantaCluster 

42from qg_test_utils import make_test_quantum_graph 

43 

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

45 

46 

47class TestQuantaCluster(unittest.TestCase): 

48 """Tests for clustering.""" 

49 

50 def setUp(self): 

51 self.qgraph = make_test_quantum_graph() 

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

53 self.qnode1 = nodes[0] 

54 self.qnode2 = nodes[1] 

55 

56 def tearDown(self): 

57 pass 

58 

59 def testQgraphNodeIds(self): 

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

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

62 

63 def testQuantaCountsNone(self): 

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

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

66 

67 def testQuantaCounts(self): 

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

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

70 

71 def testAddQuantumNode(self): 

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

73 qc.add_quantum_node(self.qnode2) 

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

75 

76 def testAddQuantum(self): 

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

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

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

80 

81 def testStr(self): 

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

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

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

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

86 

87 def testEqual(self): 

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

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

90 self.assertEqual(qc1, qc2) 

91 

92 def testNotEqual(self): 

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

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

95 self.assertNotEqual(qc1, qc2) 

96 

97 def testHash(self): 

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

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

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

101 

102 

103class TestClusteredQuantumGraph(unittest.TestCase): 

104 """Tests for single_quantum_clustering method.""" 

105 

106 def setUp(self): 

107 self.tmpdir = tempfile.mkdtemp() 

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

109 

110 def tearDown(self): 

111 pass 

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

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

114 

115 def testName(self): 

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

117 

118 def testQgraph(self): 

119 """Test qgraph method""" 

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

121 

122 # def testAddClusterSingle(self): 

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

124 

125 def testGetClusterExists(self): 

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

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

128 

129 def testGetClusterMissing(self): 

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

131 with self.assertRaises(KeyError): 

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

133 

134 # def testGetQuantumNodeExists(self): 

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

136 # 

137 # def testGetQuantumNodeMissing(self): 

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

139 

140 def testClusters(self): 

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

142 

143 def testSuccessorsExisting(self): 

144 """Test successors method returns existing successors.""" 

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

146 

147 def testSuccessorsNone(self): 

148 """Test successors method handles no successors.""" 

149 # check iterable and empty 

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

151 

152 def testPredecessorsExisting(self): 

153 """Test predecessors method returns existing predecessors.""" 

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

155 

156 def testPredecessorsNone(self): 

157 """Test predecessors method handles no predecessors.""" 

158 # check iterable and empty 

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

160 

161 # def testAddDependency(self): 

162 

163 def testSaveAndLoad(self): 

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

165 self.cqg1.save(path) 

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

167 test_cqg = ClusteredQuantumGraph.load(path) 

168 self.assertEqual(self.cqg1, test_cqg) 

169 

170 

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

172 unittest.main()