Skip to content

Commit

Permalink
entity default values
Browse files Browse the repository at this point in the history
  • Loading branch information
nekufa committed Nov 13, 2023
1 parent 60be2ee commit b240e5c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
12 changes: 10 additions & 2 deletions registry/entity.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dataclasses import dataclass
from dataclasses import _MISSING_TYPE, dataclass

from registry.schema import BucketStatus, StorageClass, StorageDriver

Expand All @@ -7,6 +7,14 @@
class Entity:
id: int

@classmethod
def get_default_values(cls) -> dict:
return {
key: field.default
for key, field in cls.__dataclass_fields__.items()
if not isinstance(field.default, _MISSING_TYPE)
}


@dataclass
class Storage(Entity):
Expand All @@ -22,8 +30,8 @@ def __hash__(self) -> int:
class Bucket(Entity):
key: str
repository: str
storage_id: int
status: BucketStatus
storage_id: int = 0

def __hash__(self) -> int:
return hash((self.repository, self.key))
9 changes: 6 additions & 3 deletions registry/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ async def find_or_create(
if query is None:
query = data
context = await self.context(entity, key)
for key, value in entity.get_default_values().items():
if key not in data:
data[key] = value
return context.repository.make(
entity=entity,
row=await context.driver.find_or_create(
Expand Down Expand Up @@ -159,17 +162,17 @@ async def get_bucket(self, repository: Repository, key: Any):
if isinstance(bucket, Bucket):
return bucket

key = await repository.transform_key(key)
return await self.find_or_create(
entity=Bucket,
query={
'key': await repository.get_key(key),
'key': key,
'repository': repository,
},
data={
'key': await repository.get_key(key),
'key': key,
'repository': repository,
'status': BucketStatus.NEW,
'storage_id': 0,
},
)

Expand Down
4 changes: 2 additions & 2 deletions registry/repository.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclasses import dataclass
from functools import cache
from typing import Optional
from typing import Any, Optional

from registry.drivers import Driver
from registry.entity import Bucket, Entity, Storage
Expand Down Expand Up @@ -34,7 +34,7 @@ def __init__(self) -> None:
async def cast_storage(self, storages: list[Storage]) -> Storage:
return storages[0]

async def get_key(self, context: dict) -> str:
async def transform_key(self, key: Any) -> str:
return ''

async def init_data(self, bucket: Bucket, driver: Driver) -> None:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from pytest import mark

from registry.drivers import get_driver
from registry.entity import Entity
from registry.registry import Registry
from registry.repository import Index, Repository
Expand All @@ -10,6 +11,7 @@
@dataclass
class Action(Entity):
type: str
owner_id: int = 0


@dataclass
Expand All @@ -35,8 +37,14 @@ async def test_hello():
action2 = await registry.find_or_create(Action, {'type': 'tester2'})
assert action1.id == 1
assert action1.type == 'tester'
assert action1.owner_id == 0
assert action2.id == 2
assert action2.type == 'tester2'
assert action2.owner_id == 0
assert len(await registry.find(Action)) == 2
assert (await registry.get_instance(Action, 2)).type == 'tester2'
assert (await registry.get_instance(Action, 3)) is None

[storage] = registry.storages
driver = await get_driver(storage.driver, storage.dsn)
assert driver.data[Action][0]['owner_id'] == 0

0 comments on commit b240e5c

Please sign in to comment.