Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Roberto Prevato
    @RobertoPrevato
    I think it's a fair assumption when testing any web application, that the application needs to be started and that tests include the initialization logic happening at program start. I agree that the TestClient looks better not being an async context manager, let's start with the second option - we can always improve it in the future.

    but it will requires one more async function from the developers side in order to start the app, isn't?

    this is needed anyway, but it's not an issue. It's really easy to prepare async tests using pytest-asyncio or the test framework in the standard library.

    For example, the code you wrote in Neoteroi/BlackSheep#141, could be like:
    import pytest
    from blacksheep.server.application import Application
    from blacksheep.testing.client import TestClient
    from blacksheep.server.responses import json
    
    app = Application()
    
    
    @app.route("/foo")
    async def foo(request):
        print(request.query)
        return json({"foo": "bar"})
    
    
    @app.route("/bar")
    async def bar(request):
        print(request.headers)
        return {"bar": "foo"}
    
    
    @pytest.mark.asyncio
    async def test_foo_and_bar():
        client = TestClient(app)
        response_one = await client.get("/foo", headers={"content-type": "application/json"})
        response_two = await client.get("/bar", query={"name": "foo and bar"})
    await can only be used inside an async block, so you couldn't simply instantiate a test client and call:
    client = TestClient(app)
    response_one = await client.get("/foo", headers={"content-type": "application/json"})
    response_two = await client.get("/bar", query={"name": "foo and bar"})
    Roberto Prevato
    @RobertoPrevato
    Note: when I wrote "we can always improve it in the future" above, I meant: "we can always change it in the future, if we realize that some things should be changed".
    Mykhailo Yusko
    @myusko

    when I wrote "we can always improve it in the future" above, I meant: "we can always change it in the future, if we realize that some things should be changed".

    Yeah, for sure! so, for now, let's agree that TestClient expecting a started application, yes?

    this is needed anyway, but it's not an issue. It's really easy to prepare async tests using pytest-asyncio or the test framework in the standard library.

    Sorry, that's my bad, I didn't explain it correctly, I meant something like this

    async def test_a():
        await app.start()
        client.get(...)
    
    async def test_b():
         await app.start()
         client.post(...)

    But it shouldn't be a problem since pytest also working with async fixtures, ok I think we come up with a solution:)

    FYI: Tomorrow I joining my new team, and won't be so active for open-source contributions during business days for 1 or 2 months:)
    Roberto Prevato
    @RobertoPrevato
    no problem, and thank you for contributing to blacksheep, I appreciate it a lot :) I wish you all the best in the new team! Are you changing position within the same company, or changing job?
    Mykhailo Yusko
    @myusko
    Yeah, I've joined a new company/project;)
    Thank you a lot:)
    Mykhailo Yusko
    @myusko
    @derekwin Hi, and nice to see you here;)
    Roberto Prevato
    @RobertoPrevato
    Welcome! :)
    Mykhailo Yusko
    @myusko
    @RobertoPrevato I going to push small changes for test client in 1-2 days, based on your comments
    Roberto Prevato
    @RobertoPrevato
    @myusko thank you - and no pressure :) I'm going to fix an error / improve the generation of OpenAPI Documentation
    although since that is not so important, I will probably finish the demo (I kept working on it, I am considering replacing the raw use of asyncpg with SQLAlchemy and its ORM pattern. Because I have the feeling that for a demo, using an ORM is closer to what developers normally expect than not using an ORM. What do you think about this?
    Roberto Prevato
    @RobertoPrevato

    I'm implementing support for typing.Generic in the automatic generation of OpenAPI Documentation
    image.png
    image.png
    (examples in pictures)
    The example above has the following class used to describe a paginated set of items:
    T = TypeVar("T")
    
    
    @dataclass
    class PaginatedSet(Generic[T]):
        items: List[T]
        total: int
    DerekLiu
    @derekwin
    Hi , nice to see you all, I am a student from China , recently discovered this framework and found it very interesting. I started learning from the documentation. By the way, I plan to translate the document into Chinese. My English speaking ability is a little lacking. Therefore, sometimes there are misunderstandings in English communication. I'm very optimistic about this framework. Some of its features are not seen in other frameworks I used before, and I feel very good.
    Roberto Prevato
    @RobertoPrevato

    Hi , nice to see you all, I am a student from China , recently discovered this framework and found it very interesting. I started learning from the documentation. By the way, I plan to translate the document into Chinese. My English speaking ability is a little lacking. Therefore, sometimes there are misunderstandings in English communication. I'm very optimistic about this framework. Some of its features are not seen in other frameworks I used before, and I feel very good.

    Nice to meet you! Thank you for your kind words. One of the reasons why I named the framework "Black sheep" is exactly because it has some features not common in Python community, and inspired by ASP.NET. I would love help in translating the documentation into Chinese! By the way, soon I will migrate to GitHub the repository with the documentation (it is currently in Azure DevOps), and improve its structure and CI/CD pipelines to support receiving PRs with documentation in more languages. I will let you know when this will happen.

    Mykhailo Yusko
    @myusko
    Regarding generic types, sounds really cool!
    DerekLiu
    @derekwin
    ok,thx for your work!
    Roberto Prevato
    @RobertoPrevato
    @myusko, thanks for the TestClient, I just merged it to the main branch. I plan to publish soon to PyPi the version 1.0.7, together with the fixes to support generic types in the generated OpenAPI Documentation - so it will soon be available in the installable library. :smile: it's so useful to have a class like this one, I'm looking forward to write some documentation about it
    Mykhailo Yusko
    @myusko
    @RobertoPrevato Thanks for your help! yeah I wanted to mention the same, that we also need to include documentation=)
    Mykhailo Yusko
    @myusko
    Also, I noticed that we don't have the ability to test Controllers, I've gone through the code, and I'm not sure that we can implement test client for an application that has been using controllers.
    Roberto Prevato
    @RobertoPrevato
    Why? Request handlers defined using controllers are bound like other request handlers, the only difference is that an instance of controller is created at each web request.
    Roberto Prevato
    @RobertoPrevato
    I'm sure controllers can be tested (they are tested and I wouldn't add a feature that is not testable). I will add one or more tests for controllers methods before publishing 1.0.7
    Roberto Prevato
    @RobertoPrevato
    (I mean to the new tests for the TestClient class)
    Mykhailo Yusko
    @myusko

    @RobertoPrevato Sorry, that's my fault, I've checked Controllers docs, and didn't realize the correct approach, how I can test it.

    If you are ok, I can add tests for controllers.

    Mykhailo Yusko
    @myusko
    @RobertoPrevato Neoteroi/BlackSheep#165 I added tests
    Roberto Prevato
    @RobertoPrevato
    @myusko thank you! I could have added them, but I appreciate this very much :sunflower:
    Mykhailo Yusko
    @myusko
    :)
    Roberto Prevato
    @RobertoPrevato
    Mykhailo Yusko
    @myusko
    Oh nice!
    Roberto Prevato
    @RobertoPrevato
    Hi @sjquant, welcome!
    Mykhailo Yusko
    @myusko

    Hi @sjquant !

    Nice to see you here!

    Seonu Jang
    @sjquant
    @RobertoPrevato @myusko Nice to meet you. I'm not currently using BlackSheep, but I'm really interested in using it.
    Mykhailo Yusko
    @myusko
    @sjquant Happy coding, and don't hesitate to ask questions:)
    Seonu Jang
    @sjquant
    @myusko Thank you :)
    Mykhailo Yusko
    @myusko
    :thumbsup:
    Roberto Prevato
    @RobertoPrevato

    I am working on the bug reported here Neoteroi/BlackSheep#173

    I already fixed this exact bug two days ago, but I noticed other things that weren't ok about the support for examples in OpenAPI Documentation.

    I'm adding automatic handling of common built-in types for examples: UUID, time, date, datetime, bytes in YAML documentation (they were already supported for JSON format).

    generating OpenAPI Documentation is hard, I dedicated lots of time to this feature. But now I think it starts to look very good in blacksheep.

    Roberto Prevato
    @RobertoPrevato

    I published the first version of documentation about Testing, documenting the wonderful contribution from @myusko (TestClient class) https://www.neoteroi.dev/blacksheep/testing/

    I also plan to record a video showing how to debug tests using Visual Studio Code (I could do the same for PyCharm later), and of course to write more documentation.
    The TestClient class is also now listed in the README, with a link to the documentation.
    Seonu Jang
    @sjquant
    Looks awesome!
    Mykhailo Yusko
    @myusko
    @RobertoPrevato Nice documentation! I love it! and thank you for the BlackSheep:)
    Roberto Prevato
    @RobertoPrevato

    Looks awesome!

    Thank You! :) By the way, regarding the thing you mentioned that you are not currently using blacksheep, it is almost the case also for me: so far I just started creating one app with it (I described it here).

    @RobertoPrevato Nice documentation! I love it! and thank you for the BlackSheep:)

    Thanks, and You're welcome :smile: I will soon add more docs :rocket: