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
79 additions
and
0 deletions.
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,5 +1,6 @@ | ||
from .fields import ( # noqa: F401 | ||
ARRAY, | ||
JSONB, | ||
MACADDR, | ||
UUID, | ||
) |
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,3 +1,4 @@ | ||
from .array import ARRAY # noqa: F401 | ||
from .jsonb import JSONB # noqa: F401 | ||
from .macaddr import MACADDR # noqa: F401 | ||
from .uuid import UUID # 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,13 @@ | ||
from typing import Any | ||
|
||
import ormar | ||
from sqlalchemy.dialects import postgresql | ||
|
||
|
||
class MACADDR(ormar.fields.model_fields.ModelFieldFactory, str): | ||
_type = str | ||
|
||
@classmethod | ||
def get_column_type(cls, **kwargs: Any) -> postgresql.MACADDR: | ||
# Tell Ormar that this column should be a postgres macaddr type | ||
return postgresql.MACADDR() |
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,64 @@ | ||
from typing import Optional | ||
|
||
import ormar | ||
import pytest | ||
|
||
import ormar_postgres_extensions as ormar_pg_ext | ||
from tests.database import ( | ||
database, | ||
metadata, | ||
) | ||
|
||
|
||
class MacAddrTestModel(ormar.Model): | ||
class Meta: | ||
database = database | ||
metadata = metadata | ||
|
||
id: int = ormar.Integer(primary_key=True) | ||
addr: str = ormar_pg_ext.MACADDR() | ||
|
||
|
||
class NullableMacAddrTestModel(ormar.Model): | ||
class Meta: | ||
database = database | ||
metadata = metadata | ||
|
||
id: int = ormar.Integer(primary_key=True) | ||
addr: Optional[str] = ormar_pg_ext.MACADDR(nullable=True) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_create_model_with_macaddr_specified(db): | ||
created = await MacAddrTestModel(addr="08:00:2b:01:02:03").save() | ||
assert str(created.addr) == "08:00:2b:01:02:03" | ||
assert isinstance(created.addr, str) | ||
|
||
# Confirm the model got saved to the DB by querying it back | ||
found = await MacAddrTestModel.objects.get() | ||
assert found.addr == created.addr | ||
assert isinstance(found.addr, str) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_get_model_by_macaddr(db): | ||
created = await MacAddrTestModel(addr="08:00:2b:01:02:03").save() | ||
|
||
found = await MacAddrTestModel.objects.filter(addr="08:00:2b:01:02:03").all() | ||
assert len(found) == 1 | ||
assert found[0] == created | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_create_model_with_nullable_macaddr(db): | ||
created = await NullableMacAddrTestModel().save() | ||
assert created.addr is None | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_get_model_with_nullable_macaddr(db): | ||
created = await NullableMacAddrTestModel().save() | ||
|
||
# Ensure querying a model with a null UUID works | ||
found = await NullableMacAddrTestModel.objects.get() | ||
assert found == created |