Coverage for tests/test_annotations.py: 30%

89 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-02-08 04:29 -0800

1# This file is part of summit_extras. 

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 

22import unittest 

23import tempfile 

24import pickle 

25 

26import lsst.utils.tests 

27from lsst.summit.extras.annotations import Annotations, _idTrans 

28 

29 

30tagId1 = {'dayObs': '1970-01-01', 'seqNum': 1} # A 

31tagId2 = {'dayObs': '1970-01-02', 'seqNum': 2} # AB 

32tagId3 = {'dayObs': '1970-01-03', 'seqNum': 3} # ABC 

33tagAndNotesId = {'dayObs': '1970-01-04', 'seqNum': 4} # D tag with a note 

34notesOnlyId = {'dayObs': '1970-01-05', 'seqNum': 5} # this is a note alone 

35checkedOnlyId = {'dayObs': '1970-01-06', 'seqNum': 6} # "" 

36nonexistentId = {'dayObs': '3030-13-32', 'seqNum': 666} # 6 

37testTuple = (tagId1['dayObs'], tagId1['seqNum']) 

38testTupleNonExistent = (nonexistentId['dayObs'], nonexistentId['seqNum']) 

39 

40allGoodIdsInternalFormat = set([_idTrans(x) for x in [tagId1, tagId2, tagId3, notesOnlyId, 

41 tagAndNotesId, checkedOnlyId]]) 

42 

43 

44class AnnotationsTestCase(lsst.utils.tests.TestCase): 

45 """A test case for annotations.""" 

46 

47 def setUp(self): 

48 

49 inputFile = tempfile.mktemp() + '.pickle' 

50 tags = {('1970-01-01', 1): 'a', 

51 ('1970-01-02', 2): 'aB', 

52 ('1970-01-03', 3): 'Abc', 

53 ('1970-01-04', 4): 'd tag with a note', 

54 ('1970-01-05', 5): ' this is a note alone', 

55 ('1970-01-06', 6): ''} 

56 with open(inputFile, "wb") as f: 

57 pickle.dump(tags, f) 

58 

59 annotations = Annotations(inputFile) 

60 self.assertTrue(annotations is not None) 

61 self.annotations = annotations 

62 

63 def test_isExamined(self): 

64 self.assertTrue(self.annotations.isExamined(tagId1)) 

65 self.assertTrue(self.annotations.isExamined(tagId2)) 

66 self.assertTrue(self.annotations.isExamined(tagId3)) 

67 self.assertTrue(self.annotations.isExamined(tagAndNotesId)) 

68 self.assertTrue(self.annotations.isExamined(notesOnlyId)) 

69 self.assertTrue(self.annotations.isExamined(checkedOnlyId)) 

70 

71 self.assertTrue(self.annotations.isExamined(testTuple)) 

72 

73 self.assertFalse(self.annotations.isExamined(nonexistentId)) 

74 self.assertFalse(self.annotations.isExamined(testTupleNonExistent)) 

75 

76 def test_getTags(self): 

77 self.assertTrue(self.annotations.getTags(tagId1) == 'A') 

78 self.assertTrue(self.annotations.getTags(tagId2) == 'AB') 

79 self.assertTrue(self.annotations.getTags(tagId3) == 'ABC') 

80 self.assertTrue(self.annotations.getTags(tagAndNotesId) == 'D') 

81 self.assertTrue(self.annotations.getTags(notesOnlyId) == '') # examined but no tags so not None 

82 self.assertTrue(self.annotations.getTags(nonexistentId) is None) # not examined so is None 

83 

84 def test_getNotes(self): 

85 self.assertTrue(self.annotations.getNotes(tagId1) is None) 

86 self.assertTrue(self.annotations.getNotes(tagId2) is None) 

87 self.assertTrue(self.annotations.getNotes(tagId3) is None) 

88 self.assertTrue(self.annotations.getNotes(tagAndNotesId) == 'tag with a note') 

89 self.assertTrue(self.annotations.getNotes(notesOnlyId) == 'this is a note alone') 

90 

91 def test_hasTags(self): 

92 self.assertTrue(self.annotations.hasTags(tagId1, 'a')) 

93 self.assertTrue(self.annotations.hasTags(tagId1, 'A')) # case insensitive on tags 

94 self.assertTrue(self.annotations.hasTags(tagId2, 'AB')) # multicharacter 

95 self.assertTrue(self.annotations.hasTags(tagId2, 'Ab')) # mixed case 

96 self.assertFalse(self.annotations.hasTags(tagId1, 'B+')) # false multichar 

97 

98 self.assertTrue(self.annotations.hasTags(tagAndNotesId, 'd')) 

99 self.assertFalse(self.annotations.hasTags(notesOnlyId, 'a')) 

100 self.assertTrue(self.annotations.hasTags(notesOnlyId, '')) 

101 # XXX fix or remove after other tests 

102 

103 def test_getListOfCheckedData(self): 

104 correctIds = set([_idTrans(x) for x in [tagId1, tagId2, tagId3, notesOnlyId, 

105 tagAndNotesId, checkedOnlyId]]) 

106 ids = self.annotations.getListOfCheckedData() 

107 self.assertTrue(correctIds == set(ids)) 

108 

109 def test_getListOfDataWithNotes(self): 

110 correctIds = set([_idTrans(x) for x in [notesOnlyId, tagAndNotesId]]) 

111 ids = self.annotations.getListOfDataWithNotes() 

112 self.assertTrue(correctIds == set(ids)) 

113 

114 def test_getIdsWithGivenTags(self): 

115 allIds = allGoodIdsInternalFormat 

116 _t = _idTrans 

117 

118 ids = self.annotations.getIdsWithGivenTags('', exactMatches=False) 

119 self.assertTrue(allIds == set(ids)) 

120 ids = self.annotations.getIdsWithGivenTags('', exactMatches=True) 

121 self.assertTrue(allIds != set(ids)) 

122 

123 ids = self.annotations.getIdsWithGivenTags('b', exactMatches=False) 

124 correct = set([_t(dId) for dId in [tagId2, tagId3]]) 

125 self.assertTrue(set(ids) == correct) 

126 

127 ids = self.annotations.getIdsWithGivenTags('b', exactMatches=True) # nothing only 'b' alone 

128 self.assertTrue(ids == []) 

129 

130 ids = self.annotations.getIdsWithGivenTags('Ba', exactMatches=False) # reversed so not a substring 

131 correct = set([_t(dId) for dId in [tagId2, tagId3]]) 

132 self.assertTrue(set(ids) == correct) 

133 

134 # check exact matches for multiple tags 

135 ids = self.annotations.getIdsWithGivenTags('ab', exactMatches=True) 

136 correct = set([_t(dId) for dId in [tagId2]]) 

137 self.assertTrue(set(ids) == correct) 

138 

139 

140class TestMemory(lsst.utils.tests.MemoryTestCase): 

141 pass 

142 

143 

144def setup_module(module): 

145 lsst.utils.tests.init() 

146 

147 

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

149 lsst.utils.tests.init() 

150 unittest.main()