|
| 1 | +# Copyright (c) 2019-2020 AnimatedLEDStrip |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the "Software"), to deal |
| 5 | +# in the Software without restriction, including without limitation the rights |
| 6 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +# copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in |
| 11 | +# all copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +# THE SOFTWARE. |
| 20 | + |
| 21 | +from unittest import mock |
| 22 | + |
| 23 | +from animatedledstrip import * |
| 24 | + |
| 25 | + |
| 26 | +def test_constructor(): |
| 27 | + sect = Section() |
| 28 | + |
| 29 | + assert sect.name == '' |
| 30 | + assert sect.start_pixel == -1 |
| 31 | + assert sect.end_pixel == -1 |
| 32 | + assert sect.physical_start == -1 |
| 33 | + assert sect.num_leds == 0 |
| 34 | + |
| 35 | + |
| 36 | +def test_name(caplog): |
| 37 | + sect = Section() |
| 38 | + |
| 39 | + sect.name = 'Test' |
| 40 | + assert sect.check_data_types() is True |
| 41 | + |
| 42 | + sect.name = 5 |
| 43 | + with mock.patch('animatedledstrip.global_vars.STRICT_TYPE_CHECKING', False): |
| 44 | + assert sect.check_data_types() is False |
| 45 | + log_messages = {(log.msg, log.levelname) for log in caplog.records} |
| 46 | + assert log_messages == {("Bad data type for name: <class 'int'> (should be <class 'str'>)", 'ERROR')} |
| 47 | + |
| 48 | + try: |
| 49 | + sect.check_data_types() |
| 50 | + raise AssertionError |
| 51 | + except TypeError: |
| 52 | + pass |
| 53 | + |
| 54 | + |
| 55 | +def test_start_pixel(caplog): |
| 56 | + sect = Section() |
| 57 | + |
| 58 | + sect.start_pixel = 3 |
| 59 | + assert sect.check_data_types() is True |
| 60 | + |
| 61 | + sect.start_pixel = '5' |
| 62 | + with mock.patch('animatedledstrip.global_vars.STRICT_TYPE_CHECKING', False): |
| 63 | + assert sect.check_data_types() is False |
| 64 | + log_messages = {(log.msg, log.levelname) for log in caplog.records} |
| 65 | + assert log_messages == {("Bad data type for start_pixel: <class 'str'> (should be <class 'int'>)", 'ERROR')} |
| 66 | + |
| 67 | + try: |
| 68 | + sect.check_data_types() |
| 69 | + raise AssertionError |
| 70 | + except TypeError: |
| 71 | + pass |
| 72 | + |
| 73 | + |
| 74 | +def test_end_pixel(caplog): |
| 75 | + sect = Section() |
| 76 | + |
| 77 | + sect.end_pixel = 3 |
| 78 | + assert sect.check_data_types() is True |
| 79 | + |
| 80 | + sect.end_pixel = '5' |
| 81 | + with mock.patch('animatedledstrip.global_vars.STRICT_TYPE_CHECKING', False): |
| 82 | + assert sect.check_data_types() is False |
| 83 | + log_messages = {(log.msg, log.levelname) for log in caplog.records} |
| 84 | + assert log_messages == {("Bad data type for end_pixel: <class 'str'> (should be <class 'int'>)", 'ERROR')} |
| 85 | + |
| 86 | + try: |
| 87 | + sect.check_data_types() |
| 88 | + raise AssertionError |
| 89 | + except TypeError: |
| 90 | + pass |
| 91 | + |
| 92 | + |
| 93 | +def test_physical_start(caplog): |
| 94 | + sect = Section() |
| 95 | + |
| 96 | + sect.physical_start = 3 |
| 97 | + assert sect.check_data_types() is True |
| 98 | + |
| 99 | + sect.physical_start = '5' |
| 100 | + with mock.patch('animatedledstrip.global_vars.STRICT_TYPE_CHECKING', False): |
| 101 | + assert sect.check_data_types() is False |
| 102 | + log_messages = {(log.msg, log.levelname) for log in caplog.records} |
| 103 | + assert log_messages == {("Bad data type for physical_start: <class 'str'> (should be <class 'int'>)", 'ERROR')} |
| 104 | + |
| 105 | + try: |
| 106 | + sect.check_data_types() |
| 107 | + raise AssertionError |
| 108 | + except TypeError: |
| 109 | + pass |
| 110 | + |
| 111 | + |
| 112 | +def test_num_leds(caplog): |
| 113 | + sect = Section() |
| 114 | + |
| 115 | + sect.num_leds = 3 |
| 116 | + assert sect.check_data_types() is True |
| 117 | + |
| 118 | + sect.num_leds = '5' |
| 119 | + with mock.patch('animatedledstrip.global_vars.STRICT_TYPE_CHECKING', False): |
| 120 | + assert sect.check_data_types() is False |
| 121 | + log_messages = {(log.msg, log.levelname) for log in caplog.records} |
| 122 | + assert log_messages == {("Bad data type for num_leds: <class 'str'> (should be <class 'int'>)", 'ERROR')} |
| 123 | + |
| 124 | + try: |
| 125 | + sect.check_data_types() |
| 126 | + raise AssertionError |
| 127 | + except TypeError: |
| 128 | + pass |
| 129 | + |
| 130 | + |
| 131 | +def test_json(): |
| 132 | + sect = Section() |
| 133 | + |
| 134 | + sect.name = 'TEST' |
| 135 | + sect.start_pixel = 40 |
| 136 | + sect.end_pixel = 50 |
| 137 | + |
| 138 | + assert sect.json() == 'SECT:{"name":"TEST","startPixel":40,"endPixel":50}' |
| 139 | + |
| 140 | + |
| 141 | +def test_json_bad_type(): |
| 142 | + sect = Section() |
| 143 | + sect.start_pixel = 'bad' |
| 144 | + |
| 145 | + with mock.patch('animatedledstrip.global_vars.STRICT_TYPE_CHECKING', False): |
| 146 | + assert sect.json() == '' |
| 147 | + |
| 148 | + try: |
| 149 | + sect.json() |
| 150 | + raise AssertionError |
| 151 | + except TypeError: |
| 152 | + pass |
| 153 | + |
| 154 | + |
| 155 | +def test_from_json(): |
| 156 | + json_data = 'SECT:{"physicalStart":0,"numLEDs":240,"name":"section","startPixel":0,"endPixel":239}' |
| 157 | + |
| 158 | + sect = Section.from_json(json_data) |
| 159 | + assert sect.name == 'section' |
| 160 | + assert sect.start_pixel == 0 |
| 161 | + assert sect.end_pixel == 239 |
| 162 | + assert sect.physical_start == 0 |
| 163 | + assert sect.num_leds == 240 |
| 164 | + |
| 165 | + assert sect.json() == 'SECT:{"name":"section","startPixel":0,"endPixel":239}' |
0 commit comments