-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from mr-raccoon-97/main
Simplified publisher
- Loading branch information
Showing
4 changed files
with
110 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "pybondi" | ||
version = "0.2.7" | ||
version = "0.3.0" | ||
description = "A lightweight library for creating event driven systems using domain driven design." | ||
authors = ["Eric Cardozo <[email protected]>"] | ||
license = "MIT" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,59 @@ | ||
from pytest import fixture | ||
from pybondi.publisher import Publisher, Subscriber | ||
|
||
def handle_event(event): | ||
assert event == 'data' | ||
from pytest import fixture, fail | ||
from unittest.mock import Mock | ||
from pybondi.publisher import Publisher | ||
|
||
@fixture | ||
def subscriber(): | ||
subscriber = Subscriber() | ||
subscriber.subscribe('event', handle_event) | ||
return subscriber | ||
def publisher(): | ||
return Publisher() | ||
|
||
@fixture | ||
def publisher(subscriber): | ||
publisher = Publisher() | ||
publisher.subscribe(subscriber) | ||
return publisher | ||
def test_subscribe_and_publish_message(publisher: Publisher): | ||
subscriber = Mock() | ||
publisher.subscribe("topic1", subscriber) | ||
|
||
publisher.handle("topic1", "test message") | ||
publisher.commit() | ||
subscriber.assert_called_once_with("test message") | ||
|
||
def test_multiple_subscribers_on_same_topic(publisher: Publisher): | ||
subscriber1 = Mock() | ||
subscriber2 = Mock() | ||
publisher.subscribe("topic1", subscriber1) | ||
publisher.subscribe("topic1", subscriber2) | ||
publisher.handle("topic1", "test message") | ||
publisher.commit() | ||
|
||
subscriber1.assert_called_once_with("test message") | ||
subscriber2.assert_called_once_with("test message") | ||
|
||
def test_no_subscribers_no_errors(publisher: Publisher): | ||
# Handle a message with no subscribers | ||
publisher.handle("topic1", "test message") | ||
try: | ||
publisher.commit() | ||
except Exception as e: | ||
fail(f"Commit failed with error: {e}") | ||
|
||
def test_rollback_message(publisher: Publisher): | ||
subscriber = Mock() | ||
publisher.subscribe("topic1", subscriber) | ||
|
||
publisher.handle("topic1", "test message") | ||
publisher.rollback() | ||
|
||
publisher.commit() | ||
subscriber.assert_not_called() | ||
|
||
def test_begin_transaction(publisher: Publisher): | ||
subscriber = Mock() | ||
publisher.subscribe("begin", subscriber) | ||
|
||
publisher.begin() | ||
subscriber.assert_called_once_with(None) | ||
|
||
def test_publisher(publisher: Publisher): | ||
publisher.publish('event', 'data') | ||
def test_close_transaction(publisher: Publisher): | ||
subscriber = Mock() | ||
publisher.subscribe("close", subscriber) | ||
|
||
publisher.close() | ||
|
||
subscriber.assert_called_once_with(None) |