This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
generated from tophat/new-project-kit
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
from .fields import PostgresUUID # noqa: F401 | ||
from .fields import ( # noqa: F401 | ||
PostgresJSONB, | ||
PostgresUUID, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from .jsonb import PostgresJSONB # noqa: F401 | ||
from .uuid import PostgresUUID # noqa: F401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from typing import Any | ||
|
||
import ormar | ||
from sqlalchemy.dialects import postgresql | ||
from sqlalchemy.types import TypeDecorator | ||
|
||
|
||
class PostgresJSONBTypeDecorator(TypeDecorator): | ||
impl = postgresql.JSONB | ||
|
||
|
||
class PostgresJSONB(ormar.JSON): | ||
""" | ||
Custom JSON field uses a native PG JSONB type | ||
""" | ||
|
||
@classmethod | ||
def get_column_type(cls, **kwargs: Any) -> PostgresJSONBTypeDecorator: | ||
return PostgresJSONBTypeDecorator() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import json | ||
from typing import Optional | ||
|
||
import ormar | ||
import pytest | ||
|
||
from ormar_postgres_extensions.fields import PostgresJSONB | ||
from tests.database import ( | ||
database, | ||
metadata, | ||
) | ||
|
||
|
||
class JSONBTestModel(ormar.Model): | ||
class Meta: | ||
database = database | ||
metadata = metadata | ||
|
||
id: int = ormar.Integer(primary_key=True) | ||
data: dict = PostgresJSONB() | ||
|
||
|
||
class NullableJSONBTestModel(ormar.Model): | ||
class Meta: | ||
database = database | ||
metadata = metadata | ||
|
||
id: int = ormar.Integer(primary_key=True) | ||
data: Optional[dict] = PostgresJSONB(nullable=True) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_create_model_with_jsonb(db): | ||
created = await JSONBTestModel(data=json.dumps(dict(foo="bar"))).save() | ||
assert created.data == {"foo": "bar"} | ||
|
||
# Confirm the model got saved to the DB by querying it back | ||
found = await JSONBTestModel.objects.get() | ||
assert found.data == {"foo": "bar"} | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_create_model_with_nullable_jsonb(db): | ||
created = await NullableJSONBTestModel().save() | ||
assert created.data is None | ||
|
||
found = await NullableJSONBTestModel.objects.get() | ||
assert found.data is None |