2
Tests for ReducePyCkovPlot
4
# This file is part of MAUS: http://micewww.pp.rl.ac.uk:8080/projects/maus
6
# MAUS is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
11
# MAUS is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with MAUS. If not, see <http://www.gnu.org/licenses/>.
19
# pylint: disable=C0103
25
from ReducePyCkovPlot import ReducePyCkovPlot
27
class ReducePyCkovPlotTestCase(unittest.TestCase): # pylint: disable=R0904, C0301
29
Test class for ReducePyCkovPlot
33
def setUpClass(self): # pylint: disable=C0202
35
Prepare for test by setting up worker.
36
@param self Object reference.
38
self.__reducer = ReducePyCkovPlot()
42
Invoke "birth" and check for success.
43
@param self Object reference.
45
success = self.__reducer.birth("{}")
47
raise Exception('Test setUp failed', 'reducer.birth() failed')
49
def test_birth_default(self):
51
Check default configuration after "birth" is called.
52
@param self Object reference.
54
self.assertEquals(0, self.__reducer.spill_count,
55
"Unexpected reducer.spill_count")
56
self.assertEquals("eps", self.__reducer.image_type,
57
"Unexpected reducer.image_type")
58
self.assertTrue(not self.__reducer.auto_number,
59
"Unexpected reducer.auto_number")
61
def test_birth_file_type(self):
63
Test configuration when "birth" is called with a supported
65
@param self Object reference.
67
self.__reducer = ReducePyCkovPlot()
68
success = self.__reducer.birth("""{"histogram_image_type":"png"}""")
69
self.assertTrue(success, "reducer.birth() failed")
70
self.assertEquals("png", self.__reducer.image_type,
71
"Unexpected reducer.image_type")
73
def test_invalid_json(self):
75
Test "process" with a bad JSON document as an argument string.
76
@param self Object reference.
78
result_str = self.__reducer.process("{")
79
result = json.loads(result_str)
80
self.assertTrue("errors" in result, "No errors field")
81
errors = result["errors"]
82
self.assertTrue("ReducePyCkovPlot" in errors,
83
"No ReducePyCkovPlot field")
84
errors = errors["ReducePyCkovPlot"]
85
self.assertTrue(len(errors) >= 1, "Missing error trace")
87
def test_no_digits(self):
89
Test "process" with a JSON document with no "digits" entry.
90
@param self Object reference.#
92
result = self.__process({})
94
self.assertTrue("errors" in result, "No errors field")
95
errors = result["errors"]
97
self.assertTrue("ReducePyCkovPlot" in errors,
98
"space points not in spill")
100
def __process(self, json_doc):
102
Convert given JSON document to a string and pass to "process".
103
@param self Object reference.
104
@param json_doc JSON document.
105
@returns JSON document string from "process".
107
json_str = json.dumps(json_doc)
108
result_str = self.__reducer.process(json_str)
109
return json.loads(result_str)
111
def __check_result(self, spill_id, result):
113
Validate results from "process". Check the current spill count
114
in the worker and the image name is as expected. Then check
115
the "image" contents. This method assumes the image type is
117
@param self Object reference.
118
@param spill_id ID of spill just processed.
119
@param result JSON document from "process".
121
json_doc = ('%s/src/reduce/ReducePyCkovPlot/processTest.txt' %
122
os.environ.get("MAUS_ROOT_DIR"))#pylint: disable=E0602
124
result = self.__process(json_doc)
125
self.assertEquals(spill_id + 1, self.__reducer.spill_count,
126
"Unexpected reducer.spill_count")
128
self.assertTrue("image" in result, "No image field")
129
image = result["image"]
130
self.assertEquals(self.__reducer.image_type, image["image_type"],
131
"Unexpected image_type")
132
if (self.__reducer.auto_number):
133
tag = "CkovChargePMT1-8%06d" % (spill_id + 1)
135
tag = "CkovChargePMT1-8"
136
self.assertEquals(tag, image["tag"], "Unexpected tag")
137
self.assertTrue("content" in image, "No content field")
138
self.assertTrue("data" in image, "No data field")
139
decoded_data = base64.b64decode(image["data"])
140
self.assertTrue(decoded_data.find("EPS") != -1,
141
"Unexpected image data")
145
@param self Object reference.
147
success = self.__reducer.death()
149
raise Exception('Test setUp failed', 'reducer.death() failed')
151
if __name__ == '__main__':