-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
bd544d4
commit ed88460
Showing
11 changed files
with
188 additions
and
8 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
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,8 @@ | ||
from .auto_schema import ( | ||
ModelAutoSchema, | ||
ModelAutoSchemaError, | ||
ModelAutoSchemaT, | ||
UnableProcessTypeError, | ||
UnableToExtractEnumClassError, | ||
) | ||
from .fields import PostgresRange, PostgresRangeTypeT |
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
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,50 @@ | ||
import datetime | ||
import typing | ||
|
||
import pydantic | ||
import sqlalchemy.dialects.postgresql | ||
|
||
PostgresRangeTypeT = typing.TypeVar("PostgresRangeTypeT", bound=typing.Any) | ||
|
||
|
||
class PostgresRange(pydantic.BaseModel, typing.Generic[PostgresRangeTypeT]): | ||
"""Representation of sqlalchemy.dialects.postgresql.Range.""" | ||
|
||
model_config = pydantic.ConfigDict(from_attributes=True) | ||
|
||
lower: PostgresRangeTypeT | None | ||
upper: PostgresRangeTypeT | None | ||
bounds: sqlalchemy.dialects.postgresql.ranges._BoundsType = "[]" | ||
|
||
def to_postgres( | ||
self, | ||
) -> sqlalchemy.dialects.postgresql.Range[PostgresRangeTypeT]: | ||
"""Convert to postgres range.""" | ||
return sqlalchemy.dialects.postgresql.Range( | ||
lower=self.lower, | ||
upper=self.upper, | ||
bounds=self.bounds, | ||
) | ||
|
||
def model_post_init(self, __context: dict[str, typing.Any]) -> None: | ||
"""Adjust limit depending on bounds. | ||
Postgres always keeps and returns ranges in `[)` no matter how you | ||
save it. Thats why we need to correct it for frontend. | ||
""" | ||
match self.bounds: | ||
case "[]": | ||
return # pragma: no cover | ||
case "(]": | ||
if self.lower: # pragma: no cover | ||
self.lower = self.lower + datetime.timedelta(days=1) | ||
case "[)": | ||
if self.upper: | ||
self.upper = self.upper - datetime.timedelta(days=1) | ||
case "()": # pragma: no cover | ||
if self.lower: # pragma: no cover | ||
self.lower = self.lower + datetime.timedelta(days=1) | ||
if self.upper: # pragma: no cover | ||
self.upper = self.upper - datetime.timedelta(days=1) | ||
self.bounds = "[]" |
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 .factories import AsyncSQLAlchemyModelFactory, AsyncSQLAlchemyOptions | ||
from .fields import DateRangeFactory |
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,45 @@ | ||
import datetime | ||
|
||
import factory | ||
import faker | ||
import sqlalchemy.dialects.postgresql | ||
|
||
|
||
class DateRangeFactory(factory.LazyAttribute): | ||
"""Generate date range.""" | ||
|
||
def __init__( | ||
self, | ||
start_date: str = "-3d", | ||
end_date: str = "+3d", | ||
) -> None: | ||
self.start_date = start_date | ||
self.end_date = end_date | ||
super().__init__( | ||
function=self._generate_date_range, | ||
) | ||
|
||
def _generate_date_range( | ||
self, | ||
*args, # noqa: ANN002 | ||
**kwargs, | ||
) -> sqlalchemy.dialects.postgresql.Range[datetime.date]: | ||
"""Generate range.""" | ||
fake = faker.Faker() | ||
lower = fake.date_between( | ||
start_date=self.start_date, | ||
end_date=self.end_date, | ||
) | ||
upper = fake.date_between( | ||
start_date=lower, | ||
end_date=self.end_date, | ||
) | ||
# Need to make sure that the dates are not the same | ||
if upper == lower: | ||
upper += datetime.timedelta(days=1) | ||
|
||
return sqlalchemy.dialects.postgresql.Range( | ||
lower=lower, | ||
upper=upper, | ||
bounds="[)", | ||
) |
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
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
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
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
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