-
-
Notifications
You must be signed in to change notification settings - Fork 80
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
Request for guides / tutorials / documentation #175
Comments
Hi, Can we documentation on how to set up tests using the client? I'm using pytest and I have no clue if there an easy way to tear down the created objects after the tests. Moreover, is there a way to configure the client to use a mock instance of the database for testing? Thank you. |
Hey @HOZHENWAI! Thanks for the suggestion, this definitely needs good documentation. In the meantime you can take a look at the This shows how you can automatically connect to the client and cleanup the database. Let me know if anything else isn't clear / if you have any questions!
I'd highly recommend not mocking the Prisma Client. By mocking you are leaving an important layer in your application untested. In my opinion your tests should use as little mocking as possible, only doing so when it would be difficult / expensive to not mock. If you mock the client you could easily introduce bugs that are not caught by your tests. For example, consider the case where you're running a |
Hello @RobertCraigie. I have some issues understanding some part of the shared_conftest, so if you don't mind, I'll try to write what I understood and if you could correct me I'd be grateful:
|
I'm not sure I understand what you mean by "does the result both get stored into the batch?". The query batching is done so that there is only one internal HTTP call (how we communicate with the Prisma Query Engine) for a slight performance improvement. This documentation might be helpful for you if you haven't already read it: https://prisma-client-py.readthedocs.io/en/stable/reference/batching/
For our internal tests we don't really care what the result of the database is, just that before each test the database is clean. If you wanted to perform cleanup after a test is ran, that would be perfectly fine but you'd also want to cleanup before as well, otherwise the first test in your test suite would not have a clean database.
Yes that is right, apart from that it is cleaning up the database before each test.
The persist data marker works by skipping the database cleanup we do, the way it does this here:
The way I use it is by defining a fixture that will be automatically ran and then decorating every test in the file with I'd also like to point out that this is purely used for convenience / test speedup, you can completely omit using this in your tests.
No you are completely right. The only wwreason it is there is to change the default behaviour of
Always happy to help! |
Could the custom generator be expanded in terms of documentation, I think it's possible but can you use custom generator like "extension" in prisma nodejs? I'd like to add methods to all my models and partials. |
Yes you could use a custom generator to generate models that inherit from the default. But it should be noted that you'd have to update your code to use that everywhere which may be a bit annoying. Unfortunately an equivalent of client extensions isn't possible due to the limitations of the Python type system. Also unfortunately I don't have the capacity to provide an example right now but I'm happy to help if you run into any issues :) |
I'd love a tutorial for working with FastAPI Dependency Injection with a class that contains Prisma. class Commons:
def __init__(self) -> None:
self.settings = Settings()
self.prisma = Prisma()
self.other_things = ...
async def _init(self):
await self.prisma.connect()
self.user_storage = self.prisma.user
async def get_commons() -> Commons:
commons = Commons()
await commons._init()
return commons and then router = APIRouter(
prefix="/test",
dependencies=[
...
],
) as well as @router.post("/create_user")
async def create_user(
request: Request,
background_tasks: BackgroundTasks,
commons=Depends(get_commons),
):
await commons.prisma.user.create(data={...}) I'm almost certain there's a better way to do this without re-initing every request but I'm not familiar enough with python prisma to do it. |
More documentation on updating arrays. I’m not quite sure how to append to an array with a reference to another object. |
Docs on how to use npm-based generators alongside the Python ones. A few examples of what I'm talking about below: |
I am looking into improving the documentation for this project and a part of that is creating guides for using Prisma Client Python with existing frameworks.
This issue serves as the place to request that I write a tutorial / guide for any framework, please leave a comment if there is any specific framework you would like me to write a tutorial for!
Guides:
The text was updated successfully, but these errors were encountered: