diff --git a/docs/tutorial/automatic-id-none-refresh.md b/docs/tutorial/automatic-id-none-refresh.md index ed767a2..ac6a2a4 100644 --- a/docs/tutorial/automatic-id-none-refresh.md +++ b/docs/tutorial/automatic-id-none-refresh.md @@ -1,6 +1,6 @@ # Automatic IDs, None Defaults, and Refreshing Data -In the previous chapter we saw how to add rows to the database using **SQLModel**. +In the previous chapter, we saw how to add rows to the database using **SQLModel**. Now let's talk a bit about why the `id` field **can't be `NULL`** on the database because it's a **primary key**, and we declare it using `Field(primary_key=True)`. @@ -11,7 +11,7 @@ But the same `id` field actually **can be `None`** in the Python code, so we dec {!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py[ln:6-10]!} -# Code below ommitted 👇 +# Code below omitted 👇 ```
@@ -68,7 +68,7 @@ If we ran this code before saving the hero to the database and the `hero_1.id` w TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' ``` -But by declaring it with `Optional[int]` the editor will help us to avoid writing broken code by showing us a warning telling us that the code could be invalid if `hero_1.id` is `None`. 🔍 +But by declaring it with `Optional[int]`, the editor will help us to avoid writing broken code by showing us a warning telling us that the code could be invalid if `hero_1.id` is `None`. 🔍 ## Print the Default `id` Values @@ -79,7 +79,7 @@ We can confirm that by printing our heroes before adding them to the database: {!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py[ln:23-31]!} -# Code below ommitted 👇 +# Code below omitted 👇 ```
@@ -98,7 +98,7 @@ That will output: ```console $ python app.py -// Output above ommitted 👆 +// Output above omitted 👆 Before interacting with the database Hero 1: id=None name='Deadpond' secret_name='Dive Wilson' age=None @@ -118,7 +118,7 @@ What happens when we `add` these objects to the **session**? After we add the `Hero` instance objects to the **session**, the IDs are *still* `None`. -We can verify by creating a session using a `with` block, and adding the objects. And then printing them again: +We can verify by creating a session using a `with` block and adding the objects. And then printing them again: ```Python hl_lines="19-21" # Code above omitted 👆 @@ -144,7 +144,7 @@ This will, again, output the `id`s of the objects as `None`: ```console $ python app.py -// Output above ommitted 👆 +// Output above omitted 👆 After adding to the session Hero 1: id=None name='Deadpond' secret_name='Dive Wilson' age=None @@ -165,7 +165,7 @@ Then we can `commit` the changes in the session, and print again: {!./docs_src/tutorial/automatic_id_none_refresh/tutorial001.py[ln:33-48]!} -# Code below ommitted 👇 +# Code below omitted 👇 ```
@@ -184,7 +184,7 @@ And now, something unexpected happens, look at the output, it seems as if the `H ```console $ python app.py -// Output above ommitted 👆 +// Output above omitted 👆 // Here the engine talks to the database, the SQL part INFO Engine BEGIN (implicit) diff --git a/docs/tutorial/fastapi/delete.md b/docs/tutorial/fastapi/delete.md index 2ce3fe5..a481223 100644 --- a/docs/tutorial/fastapi/delete.md +++ b/docs/tutorial/fastapi/delete.md @@ -39,6 +39,6 @@ After deleting it successfully, we just return a response of: ## Recap -That's it, feel free to try it out in the interactve docs UI to delete some heroes. 💥 +That's it, feel free to try it out in the interactive docs UI to delete some heroes. 💥 Using **FastAPI** to read data and combining it with **SQLModel** makes it quite straightforward to delete data from the database. diff --git a/docs/tutorial/fastapi/limit-and-offset.md b/docs/tutorial/fastapi/limit-and-offset.md index 57043ce..92bbfc7 100644 --- a/docs/tutorial/fastapi/limit-and-offset.md +++ b/docs/tutorial/fastapi/limit-and-offset.md @@ -2,14 +2,14 @@ When a client sends a request to get all the heroes, we have been returning them all. -But if we had **thousands** of heroes that could consume a lot of **computational resources**, network bandwith, etc. +But if we had **thousands** of heroes that could consume a lot of **computational resources**, network bandwidth, etc. -So we probably want to limit it. +So, we probably want to limit it. Let's use the same **offset** and **limit** we learned about in the previous tutorial chapters for the API. !!! info - In many cases this is also called **pagination**. + In many cases, this is also called **pagination**. ## Add a Limit and Offset to the Query Parameters @@ -38,13 +38,13 @@ And by default, we will return a maximum of `100` heroes, so `limit` will have a
-We want to allow clients to set a different `offset` and `limit` values. +We want to allow clients to set different `offset` and `limit` values. But we don't want them to be able to set a `limit` of something like `9999`, that's over `9000`! 😱 So, to prevent it, we add additional validation to the `limit` query parameter, declaring that it has to be **l**ess **t**han or **e**qual to `100` with `lte=100`. -This way, a client can decide to take less heroes if they want, but not more. +This way, a client can decide to take fewer heroes if they want, but not more. !!! info If you need to refresh how query parameters and their validation work, check out the docs in FastAPI: diff --git a/docs/tutorial/fastapi/multiple-models.md b/docs/tutorial/fastapi/multiple-models.md index 3643ec8..c37fad3 100644 --- a/docs/tutorial/fastapi/multiple-models.md +++ b/docs/tutorial/fastapi/multiple-models.md @@ -2,7 +2,7 @@ We have been using the same `Hero` model to declare the schema of the data we receive in the API, the table model in the database, and the schema of the data we send back in responses. -But in most of the cases there are slight differences, let's use multiple models to solve it. +But in most of the cases, there are slight differences. Let's use multiple models to solve it. Here you will see the main and biggest feature of **SQLModel**. 😎 @@ -10,7 +10,7 @@ Here you will see the main and biggest feature of **SQLModel**. 😎 Let's start by reviewing the automatically generated schemas from the docs UI. -For input we have: +For input, we have: Interactive API docs UI @@ -20,7 +20,7 @@ This means that the client could try to use the same ID that already exists in t That's not what we want. -We want the client to only send the data that is needed to create a new hero: +We want the client only to send the data that is needed to create a new hero: * `name` * `secret_name` @@ -63,7 +63,7 @@ The ultimate goal of an API is for some **clients to use it**. The clients could be a frontend application, a command line program, a graphical user interface, a mobile application, another backend application, etc. -And the code those clients write depend on what our API tells them they **need to send**, and what they can **expect to receive**. +And the code those clients write depends on what our API tells them they **need to send**, and what they can **expect to receive**. Making both sides very clear will make it much easier to interact with the API. @@ -164,7 +164,7 @@ Let's first check how is the process to create a hero now: Let's check that in detail. -Now we use the type annotation `HeroCreate` for the request JSON data, in the `hero` parameter of the **path operation function**. +Now we use the type annotation `HeroCreate` for the request JSON data in the `hero` parameter of the **path operation function**. ```Python hl_lines="3" # Code above omitted 👆 @@ -180,9 +180,9 @@ The method `.from_orm()` reads data from another object with attributes and crea The alternative is `Hero.parse_obj()` that reads data from a dictionary. -But as in this case we have a `HeroCreate` instance in the `hero` variable, this is an object with attributes, so we use `.from_orm()` to read those attributes. +But as in this case, we have a `HeroCreate` instance in the `hero` variable. This is an object with attributes, so we use `.from_orm()` to read those attributes. -With this we create a new `Hero` instance (the one for the database) and put it in the variable `db_hero` from the data in the `hero` variable that is the `HeroCreate` instance we received from the request. +With this, we create a new `Hero` instance (the one for the database) and put it in the variable `db_hero` from the data in the `hero` variable that is the `HeroCreate` instance we received from the request. ```Python hl_lines="3" # Code above omitted 👆 @@ -192,7 +192,7 @@ With this we create a new `Hero` instance (the one for the database) and put it # Code below omitted 👇 ``` -Then we just `add` it to the **session**, `commit`, and `refresh` it, and finally we return the same `db_hero` variable that has the just refreshed `Hero` instance. +Then we just `add` it to the **session**, `commit`, and `refresh` it, and finally, we return the same `db_hero` variable that has the just refreshed `Hero` instance. Because it is just refreshed, it has the `id` field set with a new ID taken from the database. @@ -206,30 +206,30 @@ And now that we return it, FastAPI will validate the data with the `response_mod # Code below omitted 👇 ``` -This will validate that all the data that we promised is there, and will remove any data we didn't declare. +This will validate that all the data that we promised is there and will remove any data we didn't declare. !!! tip - This filtering could be very important, and could be a very good security feature, for example to make sure you filter private data, hashed passwords, etc. + This filtering could be very important and could be a very good security feature, for example, to make sure you filter private data, hashed passwords, etc. You can read more about it in the FastAPI docs about Response Model. -In particular, it will make sure that the `id` is there, and that it is indeed an integer (and not `None`). +In particular, it will make sure that the `id` is there and that it is indeed an integer (and not `None`). ## Shared Fields But looking closely, we could see that these models have a lot of **duplicated information**. -All **the 3 models** declare that thay share some **common fields** that look exactly the same: +All **the 3 models** declare that they share some **common fields** that look exactly the same: * `name`, required * `secret_name`, required * `age`, optional -And then they declare other fields with some differences (in this case only about the `id`). +And then they declare other fields with some differences (in this case, only about the `id`). We want to **avoid duplicated information** if possible. -This is important if, for example, in the future we decide to **refactor the code** and rename one field (column). For example, from `secret_name` to `secret_identity`. +This is important if, for example, in the future, we decide to **refactor the code** and rename one field (column). For example, from `secret_name` to `secret_identity`. If we have that duplicated in multiple models, we could easily forget to update one of them. But if we **avoid duplication**, there's only one place that would need updating. ✨ @@ -363,7 +363,7 @@ This means that there's nothing else special in this class apart from the fact t As an alternative, we could use `HeroBase` directly in the API code instead of `HeroCreate`, but it would show up in the automatic docs UI with that name "`HeroBase`" which could be **confusing** for clients. Instead, "`HeroCreate`" is a bit more explicit about what it is for. -On top of that, we could easily decide in the future that we want to receive **more data** when creating a new hero apart from the data in `HeroBase` (for example a password), and now we already have the class to put those extra fields. +On top of that, we could easily decide in the future that we want to receive **more data** when creating a new hero apart from the data in `HeroBase` (for example, a password), and now we already have the class to put those extra fields. ### The `HeroRead` **Data Model** @@ -390,7 +390,7 @@ This one just declares that the `id` field is required when reading a hero from ## Review the Updated Docs UI -The FastAPI code is still the same as above, we still use `Hero`, `HeroCreate`, and `HeroRead`. But now we define them in a smarter way with inheritance. +The FastAPI code is still the same as above, we still use `Hero`, `HeroCreate`, and `HeroRead`. But now, we define them in a smarter way with inheritance. So, we can jump to the docs UI right away and see how they look with the updated data. @@ -400,7 +400,7 @@ Let's see the new UI for creating a hero: Interactive API docs UI -Nice! It now shows that to create a hero, we just pass the `name`, `secret_name`, and optinally `age`. +Nice! It now shows that to create a hero, we just pass the `name`, `secret_name`, and optionally `age`. We no longer pass an `id`. @@ -416,7 +416,7 @@ And if we check the schema for the **Read Heroes** *path operation* it will also ## Inheritance and Table Models -We just saw how powerful inheritance of these models can be. +We just saw how powerful the inheritance of these models could be. This is a very simple example, and it might look a bit... meh. 😅 diff --git a/docs/tutorial/fastapi/read-one.md b/docs/tutorial/fastapi/read-one.md index b503546..8eea648 100644 --- a/docs/tutorial/fastapi/read-one.md +++ b/docs/tutorial/fastapi/read-one.md @@ -42,7 +42,7 @@ But if the integer is not the ID of any hero in the database, it will not find a So, we check it in an `if` block, if it's `None`, we raise an `HTTPException` with a `404` status code. -And to use it we first import `HTTPException` from `fastapi`. +And to use it, we first import `HTTPException` from `fastapi`. This will let the client know that they probably made a mistake on their side and requested a hero that doesn't exist in the database. diff --git a/docs/tutorial/fastapi/relationships.md b/docs/tutorial/fastapi/relationships.md index 3aa8863..78ef330 100644 --- a/docs/tutorial/fastapi/relationships.md +++ b/docs/tutorial/fastapi/relationships.md @@ -102,7 +102,7 @@ In this case, we used `response_model=TeamRead` and `response_model=HeroRead`, s Now let's stop for a second and think about it. -We cannot simply include *all* the data including all the internal relationships, because each **hero** has an attribute `team` with their team, and then that **team** also has an attribute `heroes` with all the **heroes** in the team, including this one. +We cannot simply include *all* the data, including all the internal relationships, because each **hero** has an attribute `team` with their team, and then that **team** also has an attribute `heroes` with all the **heroes** in the team, including this one. If we tried to include everything, we could make the server application **crash** trying to extract **infinite data**, going through the same hero and team over and over again internally, something like this: @@ -152,7 +152,7 @@ If we tried to include everything, we could make the server application **crash* } ``` -As you can see, in this example we would get the hero **Rusty-Man**, and from this hero we would get the team **Preventers**, and then from this team we would get its heroes, of course, including **Rusty-Man**... 😱 +As you can see, in this example, we would get the hero **Rusty-Man**, and from this hero we would get the team **Preventers**, and then from this team we would get its heroes, of course, including **Rusty-Man**... 😱 So we start again, and in the end, the server would just crash trying to get all the data with a `"Maximum recursion error"`, we would not even get a response like the one above. @@ -164,7 +164,7 @@ This is a decision that will depend on **each application**. In our case, let's say that if we get a **list of heroes**, we don't want to also include each of their teams in each one. -And if we get a **list of teams**, we don't want to get a a list of the heroes for each one. +And if we get a **list of teams**, we don't want to get a list of the heroes for each one. But if we get a **single hero**, we want to include the team data (without the team's heroes). @@ -195,7 +195,7 @@ We'll add them **after** the other models so that we can easily reference the pr
-These two models are very **simple in code**, but there's a lot happening here, let's check it out. +These two models are very **simple in code**, but there's a lot happening here. Let's check it out. ### Inheritance and Type Annotations @@ -203,7 +203,7 @@ The `HeroReadWithTeam` **inherits** from `HeroRead`, which means that it will ha And then it adds the **new field** `team`, which could be `None`, and is declared with the type `TeamRead` with the base fields for reading a team. -Then we do the same for the `TeamReadWithHeroes`, it **inherits** from `TeamRead`, and declare the **new field** `heroes` which is a list of `HeroRead`. +Then we do the same for the `TeamReadWithHeroes`, it **inherits** from `TeamRead`, and declares the **new field** `heroes`, which is a list of `HeroRead`. ### Data Models Without Relationship Attributes @@ -213,7 +213,7 @@ Instead, here these are only **data models** that will tell FastAPI **which attr ### Reference to Other Models -Also notice that the field `team` is not declared with this new `TeamReadWithHeroes`, because that would again create that infinite recursion of data. Instead, we declare it with the normal `TeamRead` model. +Also, notice that the field `team` is not declared with this new `TeamReadWithHeroes`, because that would again create that infinite recursion of data. Instead, we declare it with the normal `TeamRead` model. And the same for `TeamReadWithHeroes`, the model used for the new field `heroes` uses `HeroRead` to get only each hero's data. @@ -326,7 +326,7 @@ Now we get the list of **heroes** included: ## Recap -Using the same techniques to declare additonal **data models** we can tell FastAPI what data to return in the responses, even when we return **table models**. +Using the same techniques to declare additional **data models**, we can tell FastAPI what data to return in the responses, even when we return **table models**. Here we almost **didn't have to change the FastAPI app** code, but of course, there will be cases where you need to get the data and process it in different ways in the *path operation function* before returning it. @@ -334,4 +334,4 @@ But even in those cases, you will be able to define the **data models** to use i By this point, you already have a very robust API to handle data in a SQL database combining **SQLModel** with **FastAPI**, and implementing **best practices**, like data validation, conversion, filtering, and documentation. ✨ -In the next chapter I'll tell you how to implement automated **testing** for your application using FastAPI and SQLModel. ✅ +In the next chapter, I'll tell you how to implement automated **testing** for your application using FastAPI and SQLModel. ✅ diff --git a/docs/tutorial/fastapi/response-model.md b/docs/tutorial/fastapi/response-model.md index b4e0b67..c019f45 100644 --- a/docs/tutorial/fastapi/response-model.md +++ b/docs/tutorial/fastapi/response-model.md @@ -22,7 +22,7 @@ You can see that there's a possible "Successful Response" with a code `200`, but API docs UI without response data schemas -Right now we only tell FastAPI the data we want to receive, but we don't tell it yet the data we want to send back. +Right now, we only tell FastAPI the data we want to receive, but we don't tell it yet the data we want to send back. Let's do that now. 🤓 diff --git a/docs/tutorial/fastapi/session-with-dependency.md b/docs/tutorial/fastapi/session-with-dependency.md index 7f049f5..52a800b 100644 --- a/docs/tutorial/fastapi/session-with-dependency.md +++ b/docs/tutorial/fastapi/session-with-dependency.md @@ -90,7 +90,7 @@ We import `Depends()` from `fastapi`. Then we use it in the *path operation func You can read more about it in the FastAPI documentation Path Parameters and Numeric Validations - Order the parameters as you need, tricks -The value of a dependency will **only be used for one request**, FastAPI will call it right before calling your code, and will give you the value from that dependency. +The value of a dependency will **only be used for one request**, FastAPI will call it right before calling your code and will give you the value from that dependency. If it had `yield`, then it will continue the rest of the execution once you are done sending the response. In the case of the **session**, it will finish the cleanup code from the `with` block, closing the session, etc. diff --git a/docs/tutorial/fastapi/simple-hero-api.md b/docs/tutorial/fastapi/simple-hero-api.md index 5730186..53a5fa7 100644 --- a/docs/tutorial/fastapi/simple-hero-api.md +++ b/docs/tutorial/fastapi/simple-hero-api.md @@ -158,7 +158,7 @@ Here we use the **same** class model to define the **request body** that will be Because **FastAPI** is based on Pydantic, it will use the same model (the Pydantic part) to do automatic data validation and conversion from the JSON request to an object that is an actual instance of the `Hero` class. -And then because this same **SQLModel** object is not only a **Pydantic** model instance but also a **SQLAlchemy** model instance, we can use it directly in a **session** to create the row in the database. +And then, because this same **SQLModel** object is not only a **Pydantic** model instance but also a **SQLAlchemy** model instance, we can use it directly in a **session** to create the row in the database. So we can use intuitive standard Python **type annotations**, and we don't have to duplicate a lot of the code for the database models and the API data models. 🎉 @@ -190,13 +190,13 @@ When a client sends a request to the **path** `/heroes/` with a `GET` HTTP **ope ## One Session per Request -Remember that we shoud use a SQLModel **session** per each group of operations and if we need other unrelated operations we should use a different session? +Remember that we should use a SQLModel **session** per each group of operations and if we need other unrelated operations we should use a different session? Here it is much more obvious. We should normally have **one session per request** in most of the cases. -In some isolated cases we would want to have new sessions inside, so, **more than one session** per request. +In some isolated cases, we would want to have new sessions inside, so, **more than one session** per request. But we would **never want to *share* the same session** among different requests. @@ -277,7 +277,7 @@ And then you can get them back with the **Read Heroes** *path operation*: Now you can terminate that Uvicorn server by going back to the terminal and pressing Ctrl+C. -And then you can open **DB Browser for SQLite** and check the database, to explore the data and confirm that it indeed saved the heroes. 🎉 +And then, you can open **DB Browser for SQLite** and check the database, to explore the data and confirm that it indeed saved the heroes. 🎉 DB Browser for SQLite showing the heroes @@ -287,4 +287,4 @@ Good job! This is already a FastAPI **web API** application to interact with the There are several things we can improve and extend. For example, we want the database to decide the ID of each new hero, we don't want to allow a user to send it. -We will do all those improvements in the next chapters. 🚀 +We will make all those improvements in the next chapters. 🚀 diff --git a/docs/tutorial/fastapi/teams.md b/docs/tutorial/fastapi/teams.md index 9bc4af7..7a307b8 100644 --- a/docs/tutorial/fastapi/teams.md +++ b/docs/tutorial/fastapi/teams.md @@ -12,7 +12,7 @@ Let's add the models for the teams. It's the same process we did for heroes, with a base model, a **table model**, and some other **data models**. -We have a `TeamBase` **data model**, and from it we inherit with a `Team` **table model**. +We have a `TeamBase` **data model**, and from it, we inherit with a `Team` **table model**. Then we also inherit from the `TeamBase` for the `TeamCreate` and `TeamRead` **data models**. @@ -108,9 +108,9 @@ These are equivalent and very similar to the **path operations** for the **heroe ## Using Relationships Attributes -Up to this point we are actually not using the **relationship attributes**, but we could access them in our code. +Up to this point, we are actually not using the **relationship attributes**, but we could access them in our code. -In the next chapter we will play more with them. +In the next chapter, we will play more with them. ## Check the Docs UI diff --git a/docs/tutorial/fastapi/tests.md b/docs/tutorial/fastapi/tests.md index db71121..f817a88 100644 --- a/docs/tutorial/fastapi/tests.md +++ b/docs/tutorial/fastapi/tests.md @@ -76,7 +76,7 @@ Let's start with a simple test, with just the basic test code we need the check That's the **core** of the code we need for all the tests later. -But now we need to deal with a bit of logistics and details we are not paying attention to just yet. 🤓 +But now, we need to deal with a bit of logistics and details we are not paying attention to just yet. 🤓 ## Testing Database @@ -155,7 +155,7 @@ That way, when we call `.create_all()` all the **table models** are correctly re ## Memory Database -Now we are not using the production database, instead we use a **new testing database** with the `testing.db` file, which is great. +Now we are not using the production database. Instead, we use a **new testing database** with the `testing.db` file, which is great. But SQLite also supports having an **in memory** database. This means that all the database is only in memory, and it is never saved in a file on disk. @@ -171,7 +171,7 @@ Other alternatives and ideas 👀 Before arriving at the idea of using an **in-memory database** we could have explored other alternatives and ideas. -The first, is that we are not deleting the file after we finish the test, so, the next test could have **leftover data**. So, the right thing would be to delete the file right after finishing the test. 🔥 +The first is that we are not deleting the file after we finish the test, so the next test could have **leftover data**. So, the right thing would be to delete the file right after finishing the test. 🔥 But if each test has to create a new file and then delete it afterwards, running all the tests could be **a bit slow**. @@ -179,7 +179,7 @@ Right now, we have a file `testing.db` that is used by all the tests (we only ha So, if we tried to run the tests at the same time **in parallel** to try to speed things up a bit, they would clash trying to use the *same* `testing.db` file. -Of couse, we could also fix that, using some **random name** for each testing database file... but in the case of SQLite, we have an even better alternative with just using an **in-memory database**. ✨ +Of course, we could also fix that, using some **random name** for each testing database file... but in the case of SQLite, we have an even better alternative by just using an **in-memory database**. ✨
@@ -208,7 +208,7 @@ And all the other tests can do the same. Great, that works, and you could replicate all that process in each of the test functions. -But we had to add a lot of **boilerplate code** to handle the custom database, creating it in memory, the custom session, the dependency override. +But we had to add a lot of **boilerplate code** to handle the custom database, creating it in memory, the custom session, and the dependency override. Do we really have to duplicate all that for **each test**? No, we can do better! 😎 @@ -242,12 +242,12 @@ Let's see the first code example with a fixture: **pytest** fixtures work in a very similar way to FastAPI dependencies, but have some minor differences: -* In pytest fixtures we need to add a decorator of `@pytest.fixture()` on top. +* In pytest fixtures, we need to add a decorator of `@pytest.fixture()` on top. * To use a pytest fixture in a function, we have to declare the parameter with the **exact same name**. In FastAPI we have to **explicitly use `Depends()`** with the actual function inside it. But apart from the way we declare them and how we tell the framework that we want to have them in the function, they **work in a very similar way**. -Now we create lot's of tests, and re-use that same fixture in all of them, saving us that **boilerplate code**. +Now we create lot's of tests and re-use that same fixture in all of them, saving us that **boilerplate code**. **pytest** will make sure to run them right before (and finish them right after) each test function. So, each test function will actually have its own database, engine, and session. @@ -255,7 +255,7 @@ Now we create lot's of tests, and re-use that same fixture in all of them, savin Awesome, that fixture helps us prevent a lot of duplicated code. -But currently we still have to write some code in the test function that will be repetitive for other tests, right now we: +But currently, we still have to write some code in the test function that will be repetitive for other tests, right now we: * create the **dependency override** * put it in the `app.dependency_overrides` @@ -277,7 +277,7 @@ So, we can create a **client fixture** that will be used in all the tests, and i !!! tip Check out the number bubbles to see what is done by each line of code. -Now we have a **client fixture** that in turns uses the **session fixture**. +Now we have a **client fixture** that, in turn, uses the **session fixture**. And in the actual test function, we just have to declare that we require this **client fixture**. @@ -285,7 +285,7 @@ And in the actual test function, we just have to declare that we require this ** At this point, it all might seem like we just did a lot of changes for nothing, to get **the same result**. 🤔 -But normally we will create **lots of other test functions**. And now all the boilerplate and complexity is **writen only once**, in those two fixtures. +But normally we will create **lots of other test functions**. And now all the boilerplate and complexity is **written only once**, in those two fixtures. Let's add some more tests: @@ -315,9 +315,9 @@ Now, any additional test functions can be as **simple** as the first one, they j ## Why Two Fixtures -Now, seeing the code we could think, why do we put **two fixtures** instead of **just one** with all the code? And that makes total sense! +Now, seeing the code, we could think, why do we put **two fixtures** instead of **just one** with all the code? And that makes total sense! -For these examples, **that would have been simpler**, there's no need to separate that code in two fixtures for them... +For these examples, **that would have been simpler**, there's no need to separate that code into two fixtures for them... But for the next test function, we will require **both fixtures**, the **client** and the **session**. @@ -340,7 +340,7 @@ But for the next test function, we will require **both fixtures**, the **client* -In this test function we want to check that the *path operation* to **read a list of heroes** actually sends us heroes. +In this test function, we want to check that the *path operation* to **read a list of heroes** actually sends us heroes. But if the **database is empty**, we would get an **empty list**, and we wouldn't know if the hero data is being sent correctly or not. @@ -362,7 +362,7 @@ The function for the **client fixture** and the actual testing function will **b ## Add the Rest of the Tests -Using the same ideas, requiring the fixtures, creating data that we need for the tests, etc. we can now add the rest of the tests, they look quite similar to what we have done up to now. +Using the same ideas, requiring the fixtures, creating data that we need for the tests, etc., we can now add the rest of the tests. They look quite similar to what we have done up to now. ```Python hl_lines="3 18 33" # Code above omitted 👆 @@ -406,9 +406,9 @@ project/test_main.py ....... [100%] Did you read all that? Wow, I'm impressed! 😎 -Adding tests to your application will give you a lot of **certainty** that everything is **working correctly**, as you indended. +Adding tests to your application will give you a lot of **certainty** that everything is **working correctly**, as you intended. -And tests will be notoriously useful when **refactoring** your code, **changing things**, **adding features**. Because tests they can help catch a lot of errors that can be easily introduced by refactoring. +And tests will be notoriously useful when **refactoring** your code, **changing things**, **adding features**. Because tests can help catch a lot of errors that can be easily introduced by refactoring. And they will give you the confidence to work faster and **more efficiently**, because you know that you are checking if you are **not breaking anything**. 😅 diff --git a/docs/tutorial/fastapi/update.md b/docs/tutorial/fastapi/update.md index b845d5a..0b5292b 100644 --- a/docs/tutorial/fastapi/update.md +++ b/docs/tutorial/fastapi/update.md @@ -61,7 +61,7 @@ We will use a `PATCH` HTTP operation. This is used to **partially update data**, -We also read the `hero_id` from the *path parameter* an the request body, a `HeroUpdate`. +We also read the `hero_id` from the *path parameter* and the request body, a `HeroUpdate`. ### Read the Existing Hero @@ -100,7 +100,7 @@ But that also means that if we just call `hero.dict()` we will get a dictionary } ``` -And then if we update the hero in the database with this data, we would be removing any existing values, and that's probably **not what the client intended**. +And then, if we update the hero in the database with this data, we would be removing any existing values, and that's probably **not what the client intended**. But fortunately Pydantic models (and so SQLModel models) have a parameter we can pass to the `.dict()` method for that: `exclude_unset=True`. @@ -200,7 +200,7 @@ We are **not simply omitting** the data that has the **default values**. And we are **not simply omitting** anything that is `None`. -This means that, if a model in the database **has a value different than the default**, the client could **reset it to the same value as the default**, or even `None`, and we would **still notice it** and **update it accordingly**. 🤯🚀 +This means that if a model in the database **has a value different than the default**, the client could **reset it to the same value as the default**, or even `None`, and we would **still notice it** and **update it accordingly**. 🤯🚀 So, if the client wanted to intentionally remove the `age` of a hero, they could just send a JSON with: @@ -222,7 +222,7 @@ So, we would use that value and update the `age` to `None` in the database, **ju Notice that `age` here is `None`, and **we still detected it**. -Also that `name` was not even sent, and we don't *accidentally* set it to `None` or something, we just didn't touch it, because the client didn't sent it, so we are **perfectly fine**, even in these corner cases. ✨ +Also, that `name` was not even sent, and we don't *accidentally* set it to `None` or something. We just didn't touch it because the client didn't send it, so we are **perfectly fine**, even in these corner cases. ✨ These are some of the advantages of Pydantic, that we can use with SQLModel. 🎉