~christopher-hunt08/maus/beam_selection_development

« back to all changes in this revision

Viewing changes to src/reduce/ReducePyCkovPlot/test_ReducePyCkovPlot.py

  • Committer: Chris Rogers
  • Date: 2012-10-13 18:43:00 UTC
  • mfrom: (663.6.137 merge)
  • mto: (663.6.204 merge)
  • mto: This revision was merged to the branch mainline in revision 680.
  • Revision ID: chris.rogers@stfc.ac.uk-20121013184300-ry9q81m45dmtgejr
Bring control room branch into line with trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Tests for ReducePyCkovPlot
 
3
"""
 
4
#  This file is part of MAUS: http://micewww.pp.rl.ac.uk:8080/projects/maus
 
5
 
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.
 
10
 
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.
 
15
 
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/>.
 
18
 
 
19
# pylint: disable=C0103
 
20
 
 
21
import base64
 
22
import json
 
23
import unittest
 
24
 
 
25
from ReducePyCkovPlot import ReducePyCkovPlot
 
26
 
 
27
class ReducePyCkovPlotTestCase(unittest.TestCase): # pylint: disable=R0904, C0301
 
28
    """
 
29
    Test class for ReducePyCkovPlot
 
30
    """
 
31
 
 
32
    @classmethod
 
33
    def setUpClass(self): # pylint: disable=C0202
 
34
        """ 
 
35
        Prepare for test by setting up worker.
 
36
        @param self Object reference.
 
37
        """
 
38
        self.__reducer = ReducePyCkovPlot()
 
39
 
 
40
    def setUp(self):
 
41
        """ 
 
42
        Invoke "birth" and check for success.
 
43
        @param self Object reference.
 
44
        """
 
45
        success = self.__reducer.birth("{}")
 
46
        if not success:
 
47
            raise Exception('Test setUp failed', 'reducer.birth() failed')
 
48
 
 
49
    def test_birth_default(self):
 
50
        """
 
51
        Check default configuration after "birth" is called.
 
52
        @param self Object reference.
 
53
        """
 
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")
 
60
    
 
61
    def test_birth_file_type(self):
 
62
        """
 
63
        Test configuration when "birth" is called with a supported
 
64
        file type. 
 
65
        @param self Object reference.
 
66
        """
 
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")
 
72
 
 
73
    def test_invalid_json(self):
 
74
        """
 
75
        Test "process" with a bad JSON document as an argument string.
 
76
        @param self Object reference.
 
77
        """
 
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")
 
86
                
 
87
    def test_no_digits(self):
 
88
        """
 
89
        Test "process" with a JSON document with no "digits" entry.
 
90
        @param self Object reference.#
 
91
        """
 
92
        result = self.__process({})
 
93
        print result
 
94
        self.assertTrue("errors" in result, "No errors field")
 
95
        errors = result["errors"]
 
96
        
 
97
        self.assertTrue("ReducePyCkovPlot" in errors,
 
98
                        "space points not in spill")        
 
99
 
 
100
    def __process(self, json_doc):
 
101
        """
 
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".
 
106
        """
 
107
        json_str = json.dumps(json_doc)
 
108
        result_str = self.__reducer.process(json_str)
 
109
        return json.loads(result_str)
 
110
 
 
111
    def __check_result(self, spill_id, result):
 
112
        """
 
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
 
116
        "eps". 
 
117
        @param self Object reference.
 
118
        @param spill_id ID of spill just processed.
 
119
        @param result JSON document from "process".
 
120
        """
 
121
        json_doc = ('%s/src/reduce/ReducePyCkovPlot/processTest.txt' %
 
122
                    os.environ.get("MAUS_ROOT_DIR"))#pylint: disable=E0602
 
123
 
 
124
        result = self.__process(json_doc)
 
125
        self.assertEquals(spill_id + 1, self.__reducer.spill_count,
 
126
                          "Unexpected reducer.spill_count")
 
127
 
 
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)
 
134
        else:
 
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")
 
142
    def tearDown(self):
 
143
        """
 
144
        Invoke "death".
 
145
        @param self Object reference.
 
146
        """
 
147
        success = self.__reducer.death()
 
148
        if not success:
 
149
            raise Exception('Test setUp failed', 'reducer.death() failed')
 
150
        
 
151
if __name__ == '__main__':
 
152
    unittest.main()