class User(Entity, schema="execution", registry=REGISTRY): id: Serial uuid: UUID name: String = Field(size=100) bio: String fixed_char: String = Field(size=[5, 5]) secret: Bytes address_id: Auto = ForeignKey(Address.id) address: One[Address] salary: Numeric = Field(size=[15, 2]) distance_mm: Float distance_km: Float = Field(size=8) point: Point is_active: Bool = True birth_date: Date naive_date: DateTime = datetime(2019, 1, 1, 12, 34, 55) created_time: DateTimeTz = func.now() updated_time: DateTimeTz time: Time time_tz: TimeTz @virtual def virtual_prop(self): return f"VIRTUAL:{self.id}"
def test_field_size(): f = Field() assert f.min_size == -1 assert f.max_size == -1 f = Field(size=1) assert f.min_size == 0 assert f.max_size == 1 f = Field(size=[10, 12]) assert f.min_size == 10 assert f.max_size == 12 with pytest.raises(ValueError): Field(size=[13, 12])
class Node(Entity, schema="circular_deps", registry=REGISTRY, polymorph="type"): id: Serial type: String = Field(nullable=False) parent_id: Auto = ForeignKey("Node.id") author_id: Auto = ForeignKey("circular_deps.User.id") author: One["circular_deps.User"]
class User(Entity, schema="execution", registry=new_reg): id: Serial uuid: UUID name_x: String = Field(size=100) bio: String = Field(size=200) fixed_char: Bytes secret: Bytes address_id: Auto = ForeignKey(Address.id) address: One[Address] salary: Numeric = Field(size=[15, 3]) distance_mm: Float distance_km: Float = Field(size=4) point: Point is_active: Bool = True birth_date: String naive_date: DateTimeTz = func.now() created_time: DateTimeTz = func.CURRENT_TIMESTAMP updated_time: DateTimeTz time: Time time_tz: TimeTz
def test_field_default(): assert Field(default="DEFAULT_VALUE").default == "DEFAULT_VALUE" assert Field().default is None
class Pk_large(BaseEntity): id: Int = Field(size=8) // PrimaryKey(auto_increment=True) id2: Serial = Field(size=8)
class Pk_medium(BaseEntity): id: Int = Field(size=4) // PrimaryKey(auto_increment=True) id2: Serial = Field(size=4)
class Pk_small(BaseEntity): id: Int = Field(size=2) // PrimaryKey(auto_increment=True) id2: Serial = Field(size=2)
class Ints(BaseEntity): int_small: Int = Field(size=2) int_medium: Int = Field(size=4) int_large: Int = Field(size=8)
class A(BaseEntity): id: String = Field(size=[10, 10]) // PrimaryKey() name: String = Field(size=50) email: String = Field(size=50) description: String
class Tag(Entity): id: Serial value: String = Field(size=50)
class Document(Entity, schema="circular_deps", registry=REGISTRY): id: Serial group_id: Auto = Field(nullable=False) // ForeignKey("DocumentGroup.id") group: One["DocumentGroup"] author_id: Auto = ForeignKey("circular_deps.User.id") author: One["circular_deps.User"]
class CallableDefault(Entity, registry=reg, schema="execution"): id: Serial creator_id: Int = Field(size=4, nullable=False, default=lambda: 1)
class Defaults(Entity, registry=reg, schema="execution"): int_w_def: Int = 0 int2_w_def: Int = Field(size=2, default=1) string_w_def: String = "Hello" bool_w_def: Bool = True interval: Int = Field(nullable=False)
class CompositePk(Entity, registry=R2, schema="execution"): id1: Int = Field(nullable=False) // PrimaryKey() later_id: Int = Field(nullable=False) // PrimaryKey() id2: Int = Field(nullable=False)
class OnUpdate(Entity, registry=R, schema="execution"): id: Serial value: String updater_id: Int = Field(on_update=lambda entity: 1) updater_id2: Int = Field(on_update=get_user_id)