|
| 1 | +from random import randrange |
| 2 | +import pytest |
| 3 | + |
| 4 | +from tion_btle.light_family import TionLiteFamily |
| 5 | + |
| 6 | + |
| 7 | +def generator(_len: int) -> bytearray: |
| 8 | + """Generate random bytes bytearray wit len size.""" |
| 9 | + result = [] |
| 10 | + for i in range(_len): |
| 11 | + result.append(randrange(0xFF)) |
| 12 | + return bytearray(result) |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.parametrize( |
| 16 | + "target_length", |
| 17 | + [0, 1, 10, 30, 90] |
| 18 | +) |
| 19 | +def test_generator(target_length): |
| 20 | + assert len(generator(target_length)) == target_length |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.parametrize( |
| 24 | + "command", |
| 25 | + [ |
| 26 | + pytest.param(generator(10), id="len=10"), |
| 27 | + pytest.param(generator(20), id="len=20"), |
| 28 | + pytest.param(generator(30), id="len=30"), |
| 29 | + pytest.param(generator(40), id="len=40"), |
| 30 | + pytest.param(generator(50), id="len=50"), |
| 31 | + ] |
| 32 | +) |
| 33 | +def test_split_command(command: bytearray): |
| 34 | + tion = TionLiteFamily(mac="") |
| 35 | + splitted = tion.split_command(request=command) |
| 36 | + |
| 37 | + for i in range(len(splitted)): |
| 38 | + assert len(splitted[i]) <= 20 |
| 39 | + if i == 0: |
| 40 | + if len(command) <= 20: |
| 41 | + assert splitted[i][0] == TionLiteFamily.SINGLE_PACKET_ID |
| 42 | + else: |
| 43 | + assert splitted[i][0] == TionLiteFamily.FIRST_PACKET_ID |
| 44 | + elif i == len(splitted)-1: |
| 45 | + assert splitted[i][0] == TionLiteFamily.END_PACKET_ID |
| 46 | + else: |
| 47 | + assert splitted[i][0] == TionLiteFamily.MIDDLE_PACKET_ID |
| 48 | + |
| 49 | + |
| 50 | + |
0 commit comments