Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

# This file is part of verify. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

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

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

# for details of code ownership. 

# 

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

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

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

# (at your option) any later version. 

# 

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

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

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

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

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

 

import os 

import unittest 

 

from lsst.verify import Metric, MetricSet, Name 

 

 

class MetricsPackageTestCase(unittest.TestCase): 

"""Test creating a MetricSet from a mock verification framework 

metric package located at data/ relative test modules. 

 

These tests are coupled to the data/metrics/*.yaml files. 

""" 

 

def setUp(self): 

self.metrics_yaml_dirname = os.path.join( 

os.path.dirname(__file__), 'data') 

 

self.metric_set = MetricSet.load_metrics_package( 

self.metrics_yaml_dirname) 

 

def test_len(self): 

self.assertEqual(len(self.metric_set), 4) 

 

def test_contains(self): 

for key in ['testing.PA1', 

'testing.PF1', 

'testing.PA2', 

'testing.AM1']: 

self.assertIn(key, self.metric_set, msg=key) 

 

def test_iter(self): 

"""Test __iter__ over keys (Name instances of metrics).""" 

keys = [k for k in self.metric_set] 

self.assertEqual(len(keys), 4) 

for k in keys: 

self.assertIsInstance(k, Name) 

 

def test_items(self): 

"""Test the items iterator.""" 

count = 0 

for key, value in self.metric_set.items(): 

count += 1 

self.assertIsInstance(key, Name) 

self.assertIsInstance(value, Metric) 

self.assertEqual(count, 4) 

 

def test_tags(self): 

"""Both single string and tag lists are present in YAML.""" 

# Parsing this metric required putting the single tag inside a list 

self.assertEqual( 

len(self.metric_set['testing.PA1'].tags), 

1) 

self.assertIn( 

'photometric', 

self.metric_set['testing.PA1'].tags) 

 

self.assertEqual( 

len(self.metric_set['testing.AM1'].tags), 

2) 

self.assertIn( 

'astrometric', 

self.metric_set['testing.AM1'].tags) 

self.assertIn( 

'random-tag', 

self.metric_set['testing.AM1'].tags) 

 

def test_setitem_delitem(self): 

"""Test adding and deleting metrics.""" 

m1 = Metric('validate_drp.test', 

'test', '', 

reference_url='example.com', 

reference_doc='Doc', reference_page=1) 

metric_set = MetricSet() 

self.assertEqual(len(metric_set), 0) 

 

metric_set['validate_drp.test'] = m1 

self.assertEqual(len(metric_set), 1) 

self.assertEqual(metric_set['validate_drp.test'], m1) 

 

with self.assertRaises(KeyError): 

# inconsistent metric names 

metric_set['validate_drp.new_test'] = m1 

 

with self.assertRaises(TypeError): 

# Not a metric name 

n = Name('validate_drp') 

m2 = Metric(n, 'test', '') 

metric_set[n] = m2 

 

del metric_set['validate_drp.test'] 

self.assertEqual(len(metric_set), 0) 

 

def test_insert(self): 

"""Test MetricSet.insert.""" 

m1 = Metric('validate_drp.test', 

'test', '', 

reference_url='example.com', 

reference_doc='Doc', reference_page=1) 

metric_set = MetricSet() 

 

metric_set.insert(m1) 

self.assertEqual(m1, metric_set['validate_drp.test']) 

 

def test_update(self): 

"""Test MetricSet.update.""" 

m1 = Metric('validate_drp.test', 

'test', '', 

reference_url='example.com', 

reference_doc='Doc', reference_page=1) 

new_metric_set = MetricSet([m1]) 

 

self.metric_set.update(new_metric_set) 

 

self.assertIn('validate_drp.test', self.metric_set) 

self.assertIn('testing.PA1', self.metric_set) 

self.assertIn('testing.PF1', self.metric_set) 

self.assertIn('testing.PA2', self.metric_set) 

self.assertIn('testing.AM1', self.metric_set) 

 

def test_iadd(self): 

"""Test __iadd__ to merging metric sets.""" 

m1 = Metric('validate_drp.test', 

'test', '', 

reference_url='example.com', 

reference_doc='Doc', reference_page=1) 

new_metric_set = MetricSet([m1]) 

 

self.metric_set += new_metric_set 

 

self.assertIn('validate_drp.test', self.metric_set) 

self.assertIn('testing.PA1', self.metric_set) 

self.assertIn('testing.PF1', self.metric_set) 

self.assertIn('testing.PA2', self.metric_set) 

self.assertIn('testing.AM1', self.metric_set) 

 

 

class VerifyMetricsParsingTestCase(unittest.TestCase): 

"""Test parsing metrics from verify_metrics (an EUPS package).""" 

 

def setUp(self): 

self.metric_set = MetricSet.load_metrics_package('verify_metrics') 

 

def test_len(self): 

"""Just verify that we got metrics without raising an exception""" 

self.assertTrue(len(self.metric_set) > 0) 

 

def test_nonexistent_package(self): 

"""Test handling of non-existing metrics packages/directories.""" 

with self.assertRaises(OSError): 

MetricSet.load_metrics_package('nonexistent_metrics') 

 

 

class MetricSetSubsetTestCase(unittest.TestCase): 

"""Test case for MetricSet.subset.""" 

 

def setUp(self): 

self.m1 = Metric('pkgA.m1', 'In pkgA', '', tags='testing') 

self.m2 = Metric('pkgA.m2', 'In pkgA', '', tags='other') 

self.m3 = Metric('pkgB.m3', 'In pkgB', '', tags='testing') 

self.metric_set = MetricSet([self.m1, self.m2, self.m3]) 

 

def test_subset_A(self): 

subset = self.metric_set.subset('pkgA') 

self.assertEqual(len(subset), 2) 

self.assertIn(self.m1.name, subset) 

self.assertIn(self.m2.name, subset) 

self.assertNotIn(self.m3.name, subset) 

 

def test_subset_B(self): 

subset = self.metric_set.subset('pkgB') 

self.assertEqual(len(subset), 1) 

self.assertNotIn(self.m1.name, subset) 

self.assertNotIn(self.m2.name, subset) 

self.assertIn(self.m3.name, subset) 

 

def test_subset_testing_tag(self): 

subset = self.metric_set.subset(tags=['testing']) 

self.assertEqual(len(subset), 2) 

self.assertIn(self.m1.name, subset) 

self.assertNotIn(self.m2.name, subset) 

self.assertIn(self.m3.name, subset) 

 

def test_subset_A_testing_tag(self): 

subset = self.metric_set.subset(package='pkgA', tags=['testing']) 

self.assertEqual(len(subset), 1) 

self.assertIn(self.m1.name, subset) 

self.assertNotIn(self.m2.name, subset) 

self.assertNotIn(self.m3.name, subset) 

 

 

class MetricSetSerializationTestCase(unittest.TestCase): 

"""Test JSON serialization and deserialization for MetricSets.""" 

 

def setUp(self): 

self.m1 = Metric('pkgA.m1', 'In pkgA', '', tags=['testing']) 

self.m2 = Metric('pkgA.m2', 'In pkgA', '', tags=['other']) 

self.m3 = Metric('pkgB.m3', 'In pkgB', '', tags=['testing']) 

self.metric_set = MetricSet([self.m1, self.m2, self.m3]) 

 

def test_serialization(self): 

json_doc = self.metric_set.json 

new_metric_set = MetricSet.deserialize(json_doc) 

self.assertEqual(self.metric_set, new_metric_set)