Coverage for tests/test_annotations.py: 30%
89 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-03-22 13:32 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-03-22 13:32 +0000
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/>.
22import pickle
23import tempfile
24import unittest
26import lsst.utils.tests
27from lsst.summit.extras.annotations import Annotations, _idTrans
29tagId1 = {"dayObs": "1970-01-01", "seqNum": 1} # A
30tagId2 = {"dayObs": "1970-01-02", "seqNum": 2} # AB
31tagId3 = {"dayObs": "1970-01-03", "seqNum": 3} # ABC
32tagAndNotesId = {"dayObs": "1970-01-04", "seqNum": 4} # D tag with a note
33notesOnlyId = {"dayObs": "1970-01-05", "seqNum": 5} # this is a note alone
34checkedOnlyId = {"dayObs": "1970-01-06", "seqNum": 6} # ""
35nonexistentId = {"dayObs": "3030-13-32", "seqNum": 666} # 6
36testTuple = (tagId1["dayObs"], tagId1["seqNum"])
37testTupleNonExistent = (nonexistentId["dayObs"], nonexistentId["seqNum"])
39allGoodIdsInternalFormat = set(
40 [_idTrans(x) for x in [tagId1, tagId2, tagId3, notesOnlyId, tagAndNotesId, checkedOnlyId]]
41)
44class AnnotationsTestCase(lsst.utils.tests.TestCase):
45 """A test case for annotations."""
47 def setUp(self):
48 inputFile = tempfile.mktemp() + ".pickle"
49 tags = {
50 ("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 }
57 with open(inputFile, "wb") as f:
58 pickle.dump(tags, f)
60 annotations = Annotations(inputFile)
61 self.assertTrue(annotations is not None)
62 self.annotations = annotations
64 def test_isExamined(self):
65 self.assertTrue(self.annotations.isExamined(tagId1))
66 self.assertTrue(self.annotations.isExamined(tagId2))
67 self.assertTrue(self.annotations.isExamined(tagId3))
68 self.assertTrue(self.annotations.isExamined(tagAndNotesId))
69 self.assertTrue(self.annotations.isExamined(notesOnlyId))
70 self.assertTrue(self.annotations.isExamined(checkedOnlyId))
72 self.assertTrue(self.annotations.isExamined(testTuple))
74 self.assertFalse(self.annotations.isExamined(nonexistentId))
75 self.assertFalse(self.annotations.isExamined(testTupleNonExistent))
77 def test_getTags(self):
78 self.assertTrue(self.annotations.getTags(tagId1) == "A")
79 self.assertTrue(self.annotations.getTags(tagId2) == "AB")
80 self.assertTrue(self.annotations.getTags(tagId3) == "ABC")
81 self.assertTrue(self.annotations.getTags(tagAndNotesId) == "D")
82 self.assertTrue(self.annotations.getTags(notesOnlyId) == "") # examined but no tags so not None
83 self.assertTrue(self.annotations.getTags(nonexistentId) is None) # not examined so is None
85 def test_getNotes(self):
86 self.assertTrue(self.annotations.getNotes(tagId1) is None)
87 self.assertTrue(self.annotations.getNotes(tagId2) is None)
88 self.assertTrue(self.annotations.getNotes(tagId3) is None)
89 self.assertTrue(self.annotations.getNotes(tagAndNotesId) == "tag with a note")
90 self.assertTrue(self.annotations.getNotes(notesOnlyId) == "this is a note alone")
92 def test_hasTags(self):
93 self.assertTrue(self.annotations.hasTags(tagId1, "a"))
94 self.assertTrue(self.annotations.hasTags(tagId1, "A")) # case insensitive on tags
95 self.assertTrue(self.annotations.hasTags(tagId2, "AB")) # multicharacter
96 self.assertTrue(self.annotations.hasTags(tagId2, "Ab")) # mixed case
97 self.assertFalse(self.annotations.hasTags(tagId1, "B+")) # false multichar
99 self.assertTrue(self.annotations.hasTags(tagAndNotesId, "d"))
100 self.assertFalse(self.annotations.hasTags(notesOnlyId, "a"))
101 self.assertTrue(self.annotations.hasTags(notesOnlyId, ""))
102 # XXX fix or remove after other tests
104 def test_getListOfCheckedData(self):
105 correctIds = set(
106 [_idTrans(x) for x in [tagId1, tagId2, tagId3, notesOnlyId, tagAndNotesId, checkedOnlyId]]
107 )
108 ids = self.annotations.getListOfCheckedData()
109 self.assertTrue(correctIds == set(ids))
111 def test_getListOfDataWithNotes(self):
112 correctIds = set([_idTrans(x) for x in [notesOnlyId, tagAndNotesId]])
113 ids = self.annotations.getListOfDataWithNotes()
114 self.assertTrue(correctIds == set(ids))
116 def test_getIdsWithGivenTags(self):
117 allIds = allGoodIdsInternalFormat
118 _t = _idTrans
120 ids = self.annotations.getIdsWithGivenTags("", exactMatches=False)
121 self.assertTrue(allIds == set(ids))
122 ids = self.annotations.getIdsWithGivenTags("", exactMatches=True)
123 self.assertTrue(allIds != set(ids))
125 ids = self.annotations.getIdsWithGivenTags("b", exactMatches=False)
126 correct = set([_t(dId) for dId in [tagId2, tagId3]])
127 self.assertTrue(set(ids) == correct)
129 ids = self.annotations.getIdsWithGivenTags("b", exactMatches=True) # nothing only 'b' alone
130 self.assertTrue(ids == [])
132 ids = self.annotations.getIdsWithGivenTags("Ba", exactMatches=False) # reversed so not a substring
133 correct = set([_t(dId) for dId in [tagId2, tagId3]])
134 self.assertTrue(set(ids) == correct)
136 # check exact matches for multiple tags
137 ids = self.annotations.getIdsWithGivenTags("ab", exactMatches=True)
138 correct = set([_t(dId) for dId in [tagId2]])
139 self.assertTrue(set(ids) == correct)
142class TestMemory(lsst.utils.tests.MemoryTestCase):
143 pass
146def setup_module(module):
147 lsst.utils.tests.init()
150if __name__ == "__main__": 150 ↛ 151line 150 didn't jump to line 151, because the condition on line 150 was never true
151 lsst.utils.tests.init()
152 unittest.main()