Skip to content

Commit

Permalink
Add schema validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ananto30 committed Jun 28, 2024
1 parent 67a73cc commit 8906ecf
Show file tree
Hide file tree
Showing 22 changed files with 216 additions and 186 deletions.
36 changes: 36 additions & 0 deletions examples/basic/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from zero import AsyncZeroClient
from zero.error import ZeroException

from .schema import User

zero_client = AsyncZeroClient("localhost", 5559)


Expand Down Expand Up @@ -37,10 +39,44 @@ async def two_rets():
print(resp)


async def hello_user():
resp = await zero_client.call(
"hello_user",
User(
name="John",
age=25,
emails=["[email protected]"],
),
)
print(resp)


async def hello_users():
resp = await zero_client.call(
"hello_users",
[
User(
name="John",
age=25,
emails=["[email protected]"],
),
User(
name="Jane",
age=30,
emails=["[email protected]"],
),
],
)
print(resp)


if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(echo())
loop.run_until_complete(enc_dec_jwt())
loop.run_until_complete(sum_list())
loop.run_until_complete(necho())
loop.run_until_complete(two_rets())
loop.run_until_complete(hello_user())
loop.run_until_complete(hello_users())
loop.close()
9 changes: 9 additions & 0 deletions examples/basic/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from typing import List

import msgspec


class User(msgspec.Struct):
name: str
age: int
emails: List[str]
17 changes: 15 additions & 2 deletions examples/basic/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

from zero import ZeroServer

from .schema import User

app = ZeroServer(port=5559)


async def echo(msg: str) -> str:
return msg
Expand All @@ -24,12 +28,21 @@ async def sum_list(msg: typing.List[int]) -> int:
return sum(msg)


async def two_rets(msg: typing.List) -> typing.Tuple[int, int]:
async def two_rets(msg: str) -> typing.Tuple[int, int]:
return 1, 2


@app.register_rpc
def hello_user(user: User) -> str:
return f"Hello {user.name}! You are {user.age} years old. Your email is {user.emails[0]}!"


@app.register_rpc
def hello_users(users: typing.List[User]) -> str:
return f"Hello {', '.join([user.name for user in users])}! Your emails are {', '.join([email for user in users for email in user.emails])}!"


if __name__ == "__main__":
app = ZeroServer(port=5559)
app.register_rpc(echo)
app.register_rpc(hello_world)
app.register_rpc(decode_jwt)
Expand Down
3 changes: 2 additions & 1 deletion tests/concurrency/rps_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
async def task(semaphore, items):
async with semaphore:
try:
res = await async_client.call("sum_async", items)
await async_client.call("sum_async", items)
# res = await async_client.call("sum_async", items)
# print(res)
except Exception as e:
print(e)
Expand Down
3 changes: 2 additions & 1 deletion tests/concurrency/rps_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@


def get_and_sum(msg):
resp = sum_func(msg)
sum_func(msg)
# resp = sum_func(msg)
# print(resp)


Expand Down
14 changes: 12 additions & 2 deletions tests/functional/single_server/client_server_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import zero.error
from zero import AsyncZeroClient, ZeroClient
from zero.error import ValidationException

from . import server
from .server import Message
Expand Down Expand Up @@ -38,8 +39,17 @@ def test_sum_list():

def test_echo_dict():
zero_client = ZeroClient(server.HOST, server.PORT)
msg = zero_client.call("echo_dict", {"a": "b"})
assert msg == {"a": "b"}
msg = zero_client.call("echo_dict", {1: "b"})
assert msg == {1: "b"}


def test_echo_dict_validation_error():
zero_client = ZeroClient(server.HOST, server.PORT)
with pytest.raises(ValidationException):
msg = zero_client.call("echo_dict", {"a": "b"})
assert msg == {
"__zerror__validation_error": "Expected `int`, got `str` - at `key` in `$`"
}


def test_echo_tuple():
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from zero import ZeroServer
from zero.encoder.protocols import Encoder
from zero.zero_mq.protocols import ZeroMQBroker
from zero.zeromq_patterns.protocols import ZeroMQBroker

DEFAULT_PORT = 5559
DEFAULT_HOST = "0.0.0.0"
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_zero_mq_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest
import zmq

from zero.zero_mq.queue_device.worker import ZeroMQWorker
from zero.zeromq_patterns.queue_device.worker import ZeroMQWorker


class TestWorker(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion zero/encoder/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


@runtime_checkable
class Encoder(Protocol):
class Encoder(Protocol): # pragma: no cover
def encode(self, data: Any) -> bytes:
...

Expand Down
6 changes: 5 additions & 1 deletion zero/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ class ConnectionException(ZeroException):
pass


class RemoteException(Exception):
class RemoteException(ZeroException):
pass


class ValidationException(ZeroException):
pass
Loading

0 comments on commit 8906ecf

Please sign in to comment.