Skip to content

Commit

Permalink
change the function name to require_valid_group_name and remove asser…
Browse files Browse the repository at this point in the history
…ts whenever necessary
  • Loading branch information
IronJam11 committed Jan 21, 2025
1 parent 48533bd commit a13f9eb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
12 changes: 5 additions & 7 deletions channels/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def valid_channel_name(self, name, receive=False):
return True
raise TypeError(self.invalid_name_error.format("Channel", name))

def valid_group_name(self, name):
def require_valid_group_name(self, name):
if len(name) >= self.MAX_NAME_LENGTH:
raise TypeError(
f"Group name must be less than {self.MAX_NAME_LENGTH} characters."
Expand Down Expand Up @@ -345,16 +345,16 @@ async def group_add(self, group, channel):
Adds the channel name to a group.
"""
# Check the inputs
assert self.valid_group_name(group)
assert self.valid_channel_name(channel), "Channel name not valid"
self.require_valid_group_name(group)
self.valid_channel_name(channel), "Channel name not valid"
# Add to group dict
self.groups.setdefault(group, {})
self.groups[group][channel] = time.time()

async def group_discard(self, group, channel):
# Both should be text and valid
assert self.valid_channel_name(channel), "Invalid channel name"
assert self.valid_group_name(group)
self.require_valid_group_name(group)
# Remove from group set
group_channels = self.groups.get(group, None)
if group_channels:
Expand All @@ -367,9 +367,7 @@ async def group_discard(self, group, channel):
async def group_send(self, group, message):
# Check types
assert isinstance(message, dict), "Message is not a dict"
assert self.valid_group_name(group), (
f"Group name must be" f"less than {self.MAX_NAME_LENGTH} characters."
)
self.require_valid_group_name(group)
# Run clean
self._clean_expired()

Expand Down
7 changes: 5 additions & 2 deletions tests/test_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ async def test_send_receive():

@pytest.mark.parametrize(
"method",
[BaseChannelLayer().valid_channel_name, BaseChannelLayer().valid_group_name],
[
BaseChannelLayer().valid_channel_name,
BaseChannelLayer().require_valid_group_name,
],
)
@pytest.mark.parametrize(
"channel_name,expected_valid",
Expand Down Expand Up @@ -104,4 +107,4 @@ def test_group_name_length_error_message(name, expected_error_message):
layer = BaseChannelLayer()

with pytest.raises(TypeError, match=expected_error_message):
layer.valid_group_name(name)
layer.require_valid_group_name(name)

0 comments on commit a13f9eb

Please sign in to comment.