Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscription tests and automatically coerce subscription id to string #36

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion nostr/subscription.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from .filter import Filters

class Subscription:
def __init__(self, id: str, filters: Filters=None) -> None:
def __init__(self, id: str, filters: Filters) -> None:
if not isinstance(id, str):
raise TypeError("Argument 'id' must be of type str")
self.id = id
self.filters = filters

Expand Down
27 changes: 27 additions & 0 deletions test/test_subscription.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest
import json
from nostr.filter import Filter, Filters
from nostr.message_type import ClientMessageType
from nostr.subscription import Subscription


def test_subscription_id():
"""
check that subscription contents dump to JSON and load
back to Python with expected types
"""
filters = Filters([Filter()])
id = 123

with pytest.raises(TypeError) as e:
subscription = Subscription(id=id, filters=filters)

armstrys marked this conversation as resolved.
Show resolved Hide resolved
subscription = Subscription(id=str(id), filters=filters)
request = [ClientMessageType.REQUEST, subscription.id]
request.extend(subscription.filters.to_json_array())
message = json.dumps(request)
request_received = json.loads(message)
message_type, subscription_id, req_filters = request_received
assert isinstance(subscription_id, str)
jeffthibault marked this conversation as resolved.
Show resolved Hide resolved
assert message_type == ClientMessageType.REQUEST
assert isinstance(req_filters, dict)