Skip to content

Commit

Permalink
updated serialization docs (#95)
Browse files Browse the repository at this point in the history
* updated serialization docs

* fixing session auth tests
  • Loading branch information
sinisaos committed Oct 21, 2021
1 parent 830a454 commit 1f3ed05
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 41 deletions.
37 changes: 7 additions & 30 deletions docs/source/crud/serializers.rst
Original file line number Diff line number Diff line change
@@ -1,36 +1,13 @@
Serializers
===========

``PiccoloCRUD`` uses `Pydantic <https://github.com/samuelcolvin/pydantic>`_
internally to serialize and deserialize data.
``PiccoloCRUD`` uses ``create_pydantic_model`` to serialize and deserialize data.

create_pydantic_model
---------------------

By using ``create_pydantic_model`` we can very easily create a `Pydantic model <https://pydantic-docs.helpmanual.io/usage/models/>`_
from a Piccolo ``Table``.

When using ``PiccoloCRUD``, you don't have to worry about this - it's all
handled internally. However, ``create_pydantic_model`` is very useful when
When using ``PiccoloCRUD``, you don't have to worry about serialisation because ``PiccoloCRUD``
internally uses ``create_pydantic_model`` to create a `Pydantic model <https://pydantic-docs.helpmanual.io/usage/models/>`_
from a Piccolo ``Table``. However, ``create_pydantic_model`` is very useful when
integrating Piccolo with a framework such as `FastAPI <https://github.com/tiangolo/fastapi>`_,
which also uses Pydantic for serialisation.

Source
~~~~~~

.. automodule:: piccolo_api.crud.serializers
:members:

FastAPI template
~~~~~~~~~~~~~~~~

To create a new FastAPI app using Piccolo, simply use:

.. code-block:: bash
piccolo asgi new
This uses ``create_pydantic_model`` to create serializers.
which also uses `Pydantic <https://github.com/samuelcolvin/pydantic>`_ for serialisation.

See the `Piccolo ASGI docs <https://piccolo-orm.readthedocs.io/en/latest/piccolo/asgi/index.html>`_
for details.
See the `create_pydantic_model docs <https://piccolo-orm.readthedocs.io/en/latest/piccolo/serialization/index.html>`_
for more details.
36 changes: 25 additions & 11 deletions tests/session_auth/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,8 @@ def test_default_login_template(self):
"""
Make sure the default login template works.
"""
app = session_login()
client = TestClient(app)
response = client.get("/")
client = TestClient(APP)
response = client.get("/login/")
self.assertTrue(b"<h1>Login</h1>" in response.content)

def test_simple_custom_login_template(self):
Expand All @@ -229,9 +228,17 @@ def test_simple_custom_login_template(self):
"simple_login_template",
"login.html",
)
app = session_login(template_path=template_path)
router = Router(
routes=[
Route(
"/login/",
session_login(template_path=template_path),
),
]
)
app = ExceptionMiddleware(router)
client = TestClient(app)
response = client.get("/")
response = client.get("/login/")
self.assertEqual(response.content, b"<p>Hello world</p>")

def test_complex_custom_login_template(self):
Expand All @@ -245,9 +252,17 @@ def test_complex_custom_login_template(self):
"complex_login_template",
"login.html",
)
app = session_login(template_path=template_path)
router = Router(
routes=[
Route(
"/login/",
session_login(template_path=template_path),
),
]
)
app = ExceptionMiddleware(router)
client = TestClient(app)
response = client.get("/")
response = client.get("/login/")
self.assertEqual(response.content, b"<p>Hello world</p>")

def test_logout_success(self):
Expand All @@ -264,8 +279,7 @@ def test_logout_success(self):
self.assertTrue(response.status_code == 303)
self.assertTrue("id" in response.cookies.keys())

app = session_logout()
client = TestClient(app)
client = TestClient(APP)

response = client.post(
"/logout/",
Expand All @@ -279,8 +293,8 @@ def test_logout_get_template(self):
"""
Make sure a GET request to `session_logout` returns a logout form.
"""
client = TestClient(session_logout())
response = client.get("/")
client = TestClient(APP)
response = client.get("/logout/")
self.assertTrue(response.status_code == 200)
self.assertTrue(
response.headers["content-type"] == "text/html; charset=utf-8"
Expand Down

0 comments on commit 1f3ed05

Please sign in to comment.