Skip to content

Commit 7763055

Browse files
committed
feat: update test
1 parent ec3e4d6 commit 7763055

File tree

5 files changed

+53
-23
lines changed

5 files changed

+53
-23
lines changed

dglabv3/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
from .dglab import dglabv3 # noqa: F401
2-
from .dtype import Channel, StrengthType, Button # noqa: F401
2+
from .dtype import Channel, StrengthType, Button, Strength # noqa: F401
33
from .waves import PULSES, ALL_PULSES, Pulse # noqa: F401
4-
from .wsmessage import Strength # noqa: F401

dglabv3/dglab.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
StrengthMode,
1414
MessageType,
1515
ChannelStrength,
16+
Strength
1617
)
17-
from dglabv3.wsmessage import Strength, WSMessage, WStype
18+
from dglabv3.wsmessage import WSMessage, WStype
1819
from dglabv3.event import EventEmitter
1920

2021
logging.basicConfig(level=logging.DEBUG)

dglabv3/dtype.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from enum import Enum, IntEnum, StrEnum
22
from typing import Final
33
from dataclasses import dataclass, field
4-
from dglabv3.wsmessage import Strength
54

65
__all__ = ["ChannelStrength", "StrengthType", "StrengthMode", "MessageType", "Channel"]
76

@@ -16,6 +15,14 @@ class Channel(IntEnum):
1615
MIN_STRENGTH: Final[int] = 0
1716

1817

18+
@dataclass
19+
class Strength:
20+
A: int
21+
B: int
22+
MAXA: int
23+
MAXB: int
24+
25+
1926
@dataclass
2027
class ChannelStrength:
2128
_A: int = field(default=0, init=False)
@@ -40,7 +47,7 @@ def MAX_A(self):
4047
@A.setter
4148
def A(self, value):
4249
if value > self.MAX_A:
43-
raise ValueError(f"stronge is greater than {self.MAX_A}")
50+
raise ValueError(f"stronger A cannot be greater than {self.MAX_A}")
4451
if value < MIN_STRENGTH:
4552
raise ValueError("stronger A cannot be less than 0")
4653
self._A = value
@@ -62,7 +69,7 @@ def MAX_B(self):
6269
@B.setter
6370
def B(self, value):
6471
if value > self.MAX_B:
65-
raise ValueError(f"stronger is greater than {self.MAX_B}")
72+
raise ValueError(f"stronger B cannot be greater than {self.MAX_B}")
6673
if value < MIN_STRENGTH:
6774
raise ValueError("stronger B cannot be less than 0")
6875
self._B = value

dglabv3/wsmessage.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
from dataclasses import dataclass
21
from enum import Enum
32
from typing import Optional
4-
from dglabv3.dtype import Button
3+
from dglabv3.dtype import Button, Strength
54

65

76
class WStype(Enum):
@@ -12,12 +11,7 @@ class WStype(Enum):
1211
ERROR = "error"
1312

1413

15-
@dataclass
16-
class Strength:
17-
A: int
18-
B: int
19-
MAXA: int
20-
MAXB: int
14+
2115

2216

2317
class WSMessage:

tests/test_dtype.py

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import pytest
2-
3-
from dglabv3.dtype import (
4-
ChannelStrength,
5-
Channel,
6-
StrengthType,
7-
StrengthMode,
8-
MessageType,
9-
)
2+
import random
3+
from dglabv3.dtype import ChannelStrength, Channel, StrengthType, StrengthMode, MessageType, Button
104

115

126
def test_channel_strength_initialization():
137
cs = ChannelStrength()
148
assert cs.A == 0
159
assert cs.B == 0
10+
assert cs.MAX_A == 200
11+
assert cs.MAX_B == 200
1612

1713

1814
def test_channel_strength_set_valid_values():
1915
cs = ChannelStrength()
2016
cs.A = 100
2117
cs.B = 150
18+
cs.MAX_A = 160
19+
cs.MAX_B = 160
2220
assert cs.A == 100
2321
assert cs.B == 150
22+
assert cs.MAX_A == 160
23+
assert cs.MAX_B == 160
2424

2525

2626
def test_channel_strength_set_invalid_values():
@@ -35,6 +35,26 @@ def test_channel_strength_set_invalid_values():
3535
cs.B = -1
3636

3737

38+
def test_channel_strength_set_invalid_max_values():
39+
cs = ChannelStrength()
40+
with pytest.raises(ValueError, match="MAX_A cannot be less than 0"):
41+
cs.MAX_A = -1
42+
with pytest.raises(ValueError, match="MAX_B cannot be less than 0"):
43+
cs.MAX_B = -1
44+
45+
46+
def test_channel_strength_set_random_values():
47+
cs = ChannelStrength()
48+
maxa = random.randint(50, 200)
49+
maxb = random.randint(50, 200)
50+
cs.MAX_A = maxa
51+
cs.MAX_B = maxb
52+
with pytest.raises(ValueError, match=f"stronger A cannot be greater than {maxa}"):
53+
cs.A = maxa + 1
54+
with pytest.raises(ValueError, match=f"stronger B cannot be greater than {maxb}"):
55+
cs.B = maxb + 1
56+
57+
3858
def test_channel_enum():
3959
assert Channel.A == 1
4060
assert Channel.B == 2
@@ -55,8 +75,17 @@ def test_strength_mode_enum():
5575

5676

5777
def test_message_type_enum():
58-
assert MessageType.SET_CHANNEL == "set channel"
5978
assert MessageType.HEARTBEAT == "heartbeat"
79+
assert MessageType.SET_CHANNEL == "set channel"
6080
assert MessageType.BIND == "bind"
6181
assert MessageType.CLIENT_MSG == "clientMsg"
6282
assert MessageType.MSG == "msg"
83+
84+
85+
def test_button():
86+
assert Button.button_1 == "1"
87+
assert Button.button_2 == "2"
88+
assert Button.button_3 == "3"
89+
assert Button.button_4 == "4"
90+
assert Button.button_5 == "5"
91+
assert Button.button_6 == "6"

0 commit comments

Comments
 (0)