Skip to content

Commit

Permalink
Adding an RPC codegen test
Browse files Browse the repository at this point in the history
  • Loading branch information
blast-hardcheese committed Nov 29, 2024
1 parent 9b57ee1 commit b78db53
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tests/codegen/rpc/generated/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Code generated by river.codegen. DO NOT EDIT.
from pydantic import BaseModel
from typing import Literal

import replit_river as river


from .test_service import Test_ServiceService


class RpcClient:
def __init__(self, client: river.Client[Literal[None]]):
self.test_service = Test_ServiceService(client)
36 changes: 36 additions & 0 deletions tests/codegen/rpc/generated/test_service/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Code generated by river.codegen. DO NOT EDIT.
from collections.abc import AsyncIterable, AsyncIterator
from typing import Any
import datetime

from pydantic import TypeAdapter

from replit_river.error_schema import RiverError
import replit_river as river


from .rpc_method import encode_Rpc_MethodInput, Rpc_MethodInput, Rpc_MethodOutput


class Test_ServiceService:
def __init__(self, client: river.Client[Any]):
self.client = client

async def rpc_method(
self,
input: Rpc_MethodInput,
timeout: datetime.timedelta,
) -> Rpc_MethodOutput:
return await self.client.send_rpc(
'test_service',
'rpc_method',
input,
encode_Rpc_MethodInput,
lambda x: TypeAdapter(Rpc_MethodOutput).validate_python(
x # type: ignore[arg-type]
),
lambda x: TypeAdapter(RiverError).validate_python(
x # type: ignore[arg-type]
),
timeout,
)
40 changes: 40 additions & 0 deletions tests/codegen/rpc/generated/test_service/rpc_method.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# ruff: noqa
# Code generated by river.codegen. DO NOT EDIT.
from collections.abc import AsyncIterable, AsyncIterator
import datetime
from typing import (
Any,
Callable,
Dict,
List,
Literal,
Optional,
Mapping,
Union,
Tuple,
TypedDict,
)

from pydantic import BaseModel, Field, TypeAdapter
from replit_river.error_schema import RiverError

import replit_river as river


encode_Rpc_MethodInput: Callable[['Rpc_MethodInput'], Any] = lambda x: {
k: v
for (k, v) in (
{
'data': x.get('data'),
}
).items()
if v is not None
}


class Rpc_MethodInput(TypedDict):
data: str


class Rpc_MethodOutput(BaseModel):
data: str
32 changes: 32 additions & 0 deletions tests/codegen/rpc/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"services": {
"test_service": {
"procedures": {
"rpc_method": {
"input": {
"type": "object",
"properties": {
"data": {
"type": "string"
}
},
"required": ["data"]
},
"output": {
"type": "object",
"properties": {
"data": {
"type": "string"
}
},
"required": ["data"]
},
"errors": {
"not": {}
},
"type": "rpc"
}
}
}
}
}
24 changes: 24 additions & 0 deletions tests/codegen/test_rpc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from datetime import timedelta

import pytest

from replit_river.client import Client
from replit_river.codegen.client import schema_to_river_client_codegen
from tests.common_handlers import basic_rpc_method


@pytest.mark.asyncio
@pytest.mark.parametrize("handlers", [{**basic_rpc_method}])
async def test_basic_rpc(client: Client) -> None:
schema_to_river_client_codegen(
"tests/codegen/rpc/schema.json", "tests/codegen/rpc/generated", "RpcClient", True
)
from tests.codegen.rpc.generated import RpcClient

res = await RpcClient(client).test_service.rpc_method(
{
"data": "feep",
},
timedelta(seconds=5),
)
assert res.data == "Hello, feep!", f"Expected 'Hello, feep!' received {res.data}"

0 comments on commit b78db53

Please sign in to comment.