-
Notifications
You must be signed in to change notification settings - Fork 262
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
Synchronous connection interface #32
Comments
I'm having some kind of issue related to this, I don't know if it fits as a bug, but I'm doing this: def test_app():
query = users.insert()
values = {
"username": "woile",
}
database.execute(query, values)
client = TestClient(application.app)
response = client.get("/users")
assert ..something... But, because On the other hand if I do this: @pytest.mark.asyncio
async def test_app():
query = users.insert()
values = {
"username": "woile",
}
await database.execute(query, values)
client = TestClient(application.app)
response = client.get("/users")
assert ...something... fails because
What is the best approach to follow in this case? Note: My |
Update I found a way to execute import asyncio
def sync(coroutine):
event_loop = None
try:
event_loop = asyncio.get_event_loop()
except RuntimeError:
event_loop = asyncio.new_event_loop()
asyncio.set_event_loop(event_loop)
return event_loop.run_until_complete(coroutine)
def test_app():
query = users.insert()
values = {
"username": "woile",
}
sync(database.execute(query, values))
client = TestClient(application.app)
response = client.get("/users")
assert ..something... |
|
The other way around for us to approach this would be to provide an async test client. Then we can just use This https://github.com/encode/requests-async gets us a lot closer to doing that, since we've got a package that can either be used for making outgoing async HTTP requests, or for an async test client, or plugging into a stub service to mock out network requests. (eg. when you're running the test suite, you probably want to stub out any external network requests that your service makes.) |
Yes, async test client sounds just right! I think I can make an initial draft for this based on the |
we solved all of these issues with our async test client. We have a fixture that yields the client and the app. Also we register the database as a zca IDatabase utility. and this makes so easy to swap, mock or whatever a singleton throught all the app. |
@jordic Any progress on publishing it? I've run into the same thing. |
@gvbgduh I'm using your draft for AsyncTestClient and so far, so good. I'd love to see it get merged into Starlette. |
@taybin - See Will need to update Starlette to start using this at some point. |
For working from sync test cases, or for working from the console, it'd be useful to be able to acquire a synchronous interface onto a connection.
Eg.
Or...
The text was updated successfully, but these errors were encountered: