-
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
Showing
19 changed files
with
474 additions
and
243 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,36 @@ | ||
import datetime | ||
import random | ||
|
||
from faker import Faker | ||
from pydantic import BaseModel, Field | ||
|
||
faker = Faker() | ||
|
||
|
||
def gen_event(): | ||
"""To address randomness, we need to make this special fixture-like function.""" | ||
|
||
class FakeEventData(BaseModel): | ||
""" | ||
The "address" and "schedule" fields are optional, | ||
meaning they can be set to `None`, and adding randomness | ||
may be beneficial for testing purposes. | ||
""" | ||
|
||
name: str = Field(default_factory=lambda: faker.text(max_nb_chars=20)) | ||
address: str | None = Field( | ||
default_factory=random.choice([faker.street_address, lambda: None]) | ||
) | ||
schedule: str | None = Field( | ||
default_factory=random.choice( | ||
[ | ||
lambda: faker.future_datetime(tzinfo=datetime.timezone.utc) | ||
.replace(microsecond=random.randint(0, 999999)) | ||
.isoformat(), | ||
lambda: None, | ||
] | ||
) | ||
) | ||
host_name: str = Field(default_factory=faker.name, max_length=50) | ||
|
||
return FakeEventData() |
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,37 @@ | ||
import json | ||
from http import HTTPStatus | ||
from typing import Annotated | ||
|
||
import svcs | ||
from edgedb.asyncio_client import AsyncIOClient | ||
from fastapi import APIRouter, Body | ||
|
||
from .factories import gen_event | ||
from .models import DevDataCreate | ||
from .queries import create_event_async_edgeql as create_event_qry | ||
from .queries import prepare_dev_data_async_edgeql as prepare_dev_data_qry | ||
|
||
router = APIRouter(include_in_schema=False) | ||
|
||
|
||
@router.post( | ||
"/fixtures", | ||
status_code=HTTPStatus.OK, | ||
response_model=list[create_event_qry.CreateEventResult], | ||
tags=["fixtures"], | ||
) | ||
async def prepare_dev_data( | ||
services: svcs.fastapi.DepContainer, | ||
dev_data: Annotated[DevDataCreate, Body()] = DevDataCreate(), | ||
): | ||
""" | ||
The endpoint is enabled for dev purpose only. | ||
`prepare_dev_data_qry.prepare_dev_data` will **DELETE ALL USERS AND EVENTS**!!! | ||
Well, after all, this is a setup for dev. | ||
""" | ||
client = await services.aget(AsyncIOClient) | ||
data = [ | ||
gen_event().model_dump(include={"name", "address", "schedule", "host_name"}) | ||
for _ in range(dev_data.n) | ||
] | ||
return await prepare_dev_data_qry.prepare_dev_data(client, data=json.dumps(data)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
select 1; |
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,16 @@ | ||
# AUTOGENERATED FROM 'app/queries/ping_db.edgeql' WITH: | ||
# $ edgedb-py | ||
|
||
|
||
from __future__ import annotations | ||
import edgedb | ||
|
||
|
||
async def ping_db( | ||
executor: edgedb.AsyncIOExecutor, | ||
) -> int: | ||
return await executor.query_single( | ||
"""\ | ||
select 1;\ | ||
""", | ||
) |
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,11 @@ | ||
delete Event; | ||
delete User; | ||
with data := <json>$data, | ||
for item in json_array_unpack(data) union ( | ||
select (insert Event { name := <str>item['name'], | ||
address := <optional str>item['address'] ?? <str>{}, | ||
schedule := <datetime>(<optional str>item['schedule'] ?? <str>{}), | ||
host := (insert User {name:= <str>item['host_name']}) | ||
} | ||
) {name, address, schedule, host_name:=.host.name} | ||
); |
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,57 @@ | ||
# AUTOGENERATED FROM 'app/queries/prepare_dev_data.edgeql' WITH: | ||
# $ edgedb-py | ||
|
||
|
||
from __future__ import annotations | ||
import dataclasses | ||
import datetime | ||
import edgedb | ||
import uuid | ||
|
||
|
||
class NoPydanticValidation: | ||
@classmethod | ||
def __get_pydantic_core_schema__(cls, _source_type, _handler): | ||
# Pydantic 2.x | ||
from pydantic_core.core_schema import any_schema | ||
return any_schema() | ||
|
||
@classmethod | ||
def __get_validators__(cls): | ||
# Pydantic 1.x | ||
from pydantic.dataclasses import dataclass as pydantic_dataclass | ||
pydantic_dataclass(cls) | ||
cls.__pydantic_model__.__get_validators__ = lambda: [] | ||
return [] | ||
|
||
|
||
@dataclasses.dataclass | ||
class PrepareDevDataResult(NoPydanticValidation): | ||
id: uuid.UUID | ||
name: str | ||
address: str | None | ||
schedule: datetime.datetime | None | ||
host_name: str | ||
|
||
|
||
async def prepare_dev_data( | ||
executor: edgedb.AsyncIOExecutor, | ||
*, | ||
data: str, | ||
) -> list[PrepareDevDataResult]: | ||
return await executor.query( | ||
"""\ | ||
delete Event; | ||
delete User; | ||
with data := <json>$data, | ||
for item in json_array_unpack(data) union ( | ||
select (insert Event { name := <str>item['name'], | ||
address := <optional str>item['address'] ?? <str>{}, | ||
schedule := <datetime>(<optional str>item['schedule'] ?? <str>{}), | ||
host := (insert User {name:= <str>item['host_name']}) | ||
} | ||
) {name, address, schedule, host_name:=.host.name} | ||
);\ | ||
""", | ||
data=data, | ||
) |
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
Oops, something went wrong.