Coverage for tests/test_clustered_quantum_graph.py: 38%

84 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-30 09:27 +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 def setUp(self): 

43 self.qgraph = make_test_quantum_graph() 

44 nodes = [n for n in self.qgraph.getNodesForTask(self.qgraph.findTaskDefByLabel("T1"))] 

45 self.qnode1 = nodes[0] 

46 self.qnode2 = nodes[1] 

47 

48 def tearDown(self): 

49 pass 

50 

51 def testQgraphNodeIds(self): 

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

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

54 

55 def testQuantaCountsNone(self): 

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

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

58 

59 def testQuantaCounts(self): 

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

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

62 

63 def testAddQuantumNode(self): 

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

65 qc.add_quantum_node(self.qnode2) 

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

67 

68 def testAddQuantum(self): 

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

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

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

72 

73 def testStr(self): 

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

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

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

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

78 

79 def testEqual(self): 

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

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

82 self.assertEqual(qc1, qc2) 

83 

84 def testNotEqual(self): 

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

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

87 self.assertNotEqual(qc1, qc2) 

88 

89 def testHash(self): 

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

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

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

93 

94 

95class TestClusteredQuantumGraph(unittest.TestCase): 

96 """Tests for single_quantum_clustering method.""" 

97 

98 def setUp(self): 

99 self.tmpdir = tempfile.mkdtemp() 

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

101 

102 def tearDown(self): 

103 pass 

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

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

106 

107 def testName(self): 

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

109 

110 def testQgraph(self): 

111 """Test qgraph method""" 

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

113 

114 # def testAddClusterSingle(self): 

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

116 

117 def testGetClusterExists(self): 

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

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

120 

121 def testGetClusterMissing(self): 

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

123 with self.assertRaises(KeyError): 

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

125 

126 # def testGetQuantumNodeExists(self): 

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

128 # 

129 # def testGetQuantumNodeMissing(self): 

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

131 

132 def testClusters(self): 

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

134 

135 def testSuccessorsExisting(self): 

136 """Test successors method returns existing successors.""" 

137 self.assertEqual([x for x in self.cqg1.successors("T1_1_2")], ["T23_1_2"]) 

138 

139 def testSuccessorsNone(self): 

140 """Test successors method handles no successors.""" 

141 # check iterable and empty 

142 self.assertEqual(len([x for x in self.cqg1.successors("T4_1_2")]), 0) 

143 

144 def testPredecessorsExisting(self): 

145 """Test predecessors method returns existing predecessors.""" 

146 self.assertEqual([x for x in self.cqg1.predecessors("T23_1_2")], ["T1_1_2"]) 

147 

148 def testPredecessorsNone(self): 

149 """Test predecessors method handles no predecessors.""" 

150 # check iterable and empty 

151 self.assertEqual(len([x for x in self.cqg1.predecessors("T1_1_2")]), 0) 

152 

153 # def testAddDependency(self): 

154 

155 def testSaveAndLoad(self): 

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

157 self.cqg1.save(path) 

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

159 test_cqg = ClusteredQuantumGraph.load(path) 

160 self.assertEqual(self.cqg1, test_cqg) 

161 

162 

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

164 unittest.main()