/ CLUE_Sensor_Plotter / test_PlotSource.py
test_PlotSource.py
1 # SPDX-FileCopyrightText: 2020 Kevin J Walters for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 # The MIT License (MIT) 6 # 7 # Copyright (c) 2020 Kevin J. Walters 8 # 9 # Permission is hereby granted, free of charge, to any person obtaining a copy 10 # of this software and associated documentation files (the "Software"), to deal 11 # in the Software without restriction, including without limitation the rights 12 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 # copies of the Software, and to permit persons to whom the Software is 14 # furnished to do so, subject to the following conditions: 15 # 16 # The above copyright notice and this permission notice shall be included in 17 # all copies or substantial portions of the Software. 18 # 19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 # THE SOFTWARE. 26 27 import sys 28 import os 29 30 import unittest 31 from unittest.mock import Mock, MagicMock, PropertyMock 32 33 verbose = int(os.getenv('TESTVERBOSE', '2')) 34 35 # Mocking libraries which are about to be import'd by Plotter 36 sys.modules['analogio'] = MagicMock() 37 38 # Borrowing the dhalbert/tannewt technique from adafruit/Adafruit_CircuitPython_Motor 39 sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) 40 41 # import what we are testing or will test in future 42 # pylint: disable=unused-import,wrong-import-position 43 from plot_source import PlotSource, TemperaturePlotSource, PressurePlotSource, \ 44 HumidityPlotSource, ColorPlotSource, ProximityPlotSource, \ 45 IlluminatedColorPlotSource, VolumePlotSource, \ 46 AccelerometerPlotSource, GyroPlotSource, \ 47 MagnetometerPlotSource, PinPlotSource 48 49 50 # pylint: disable=protected-access 51 class Test_TemperaturePlotSource(unittest.TestCase): 52 53 SENSOR_DATA = (20, 21.3, 22.0, 0.0, -40, 85) 54 55 def test_celsius(self): 56 """Create the source in Celsius mode and test with some values.""" 57 58 # Emulate the clue's temperature sensor by 59 # returning a temperature from a small tuple 60 # of test data 61 mocked_clue = Mock() 62 expected_data = self.SENSOR_DATA 63 type(mocked_clue).temperature = PropertyMock(side_effect=self.SENSOR_DATA) 64 65 source = TemperaturePlotSource(mocked_clue, 66 mode="Celsius") 67 68 for expected_value in expected_data: 69 self.assertAlmostEqual(source.data(), 70 expected_value, 71 msg="Checking converted temperature is correct") 72 73 def test_fahrenheit(self): 74 """Create the source in Fahrenheit mode and test with some values.""" 75 # Emulate the clue's temperature sensor by 76 # returning a temperature from a small tuple 77 # of test data 78 mocked_clue = Mock() 79 expected_data = (68, 70.34, 71.6, 80 32.0, -40, 185) 81 type(mocked_clue).temperature = PropertyMock(side_effect=self.SENSOR_DATA) 82 83 source = TemperaturePlotSource(mocked_clue, 84 mode="Fahrenheit") 85 86 for expected_value in expected_data: 87 self.assertAlmostEqual(source.data(), 88 expected_value, 89 msg="Checking converted temperature is correct") 90 91 def test_kelvin(self): 92 """Create the source in Kelvin mode and test with some values.""" 93 # Emulate the clue's temperature sensor by 94 # returning a temperature from a small tuple 95 # of test data 96 mocked_clue = Mock() 97 expected_data = (293.15, 294.45, 295.15, 98 273.15, 233.15, 358.15) 99 type(mocked_clue).temperature = PropertyMock(side_effect=self.SENSOR_DATA) 100 101 source = TemperaturePlotSource(mocked_clue, 102 mode="Kelvin") 103 104 for expected_value in expected_data: 105 data = source.data() 106 # self.assertEqual(data, 107 # expected_value, 108 # msg="An inappropriate check for floating-point") 109 self.assertAlmostEqual(data, 110 expected_value, 111 msg="Checking converted temperature is correct") 112 113 114 if __name__ == '__main__': 115 unittest.main(verbosity=verbose)