示例#1
0
async def test_deep_load_one(conn):
    q = Query(Something2).where(Something2.id == 1).load(
        Something2,
        Something2.something,
        Something2.something.article,
        Something2.something.article.creator,
    )
    dialect = PostgreDialect()
    sql, params = dialect.create_query_compiler().compile_select(q)
    assert sql == """SELECT "t0"."id", "t0"."something_id", "t1"."id", "t1"."article_id", "t2"."id", "t2"."creator_id", "t2"."updater_id", "t3"."id", ("t3"."name")."title", ("t3"."name")."family", ("t3"."name")."given", "t3"."address_id" FROM "ent_load"."Something2" "t0" LEFT JOIN "ent_load"."Something" "t1" ON "t0"."something_id" = "t1"."id" LEFT JOIN "ent_load"."Article" "t2" ON "t2"."id" = "t1"."article_id" LEFT JOIN "ent_load"."User" "t3" ON "t3"."id" = "t2"."creator_id" WHERE "t0"."id" = $1"""
    assert params == (1, )

    s = await conn.select(q).first()
    assert s.id == 1
    assert s.something.id == 2
    assert s.something.article.id == 42
    assert s.something.article.creator.name.family == "Article"
示例#2
0
async def test_load(conn):
    addr = Address(addr="Address1")
    await conn.save(addr)
    await conn.save(Address(addr="Address2"))

    tag1 = Tag(value="tag1")
    await conn.save(tag1)

    tag2 = Tag(value="tag2")
    await conn.save(tag2)

    await conn.save(Tag(value="tag3"))

    user = User(name="User", address=addr, tags=[tag1, tag2])
    user.children.append(UserChild(name="Child1"))
    user.children.append(UserChild(name="Child2"))
    await conn.save(user)

    q = Query(User).where(User.id == 1).load(User, User.address, User.children,
                                             User.tags)
    dialect = PostgreDialect()
    sql, params = dialect.create_query_compiler().compile_select(q)
    assert sql == """SELECT "t4"."id", ("t4"."name")."title", ("t4"."name")."family", ("t4"."name")."given", "t4"."address_id", "t5"."id", "t5"."addr", (SELECT array_agg("t1") FROM (SELECT "t6"."id", "t6"."parent_id", "t6"."name" FROM "ent_load"."UserChild" "t6" WHERE "t6"."parent_id" = "t4"."id") as "t1") as "t0", (SELECT array_agg("t3") FROM (SELECT "t8"."id", "t8"."value" FROM "ent_load"."UserTags" "t7" INNER JOIN "ent_load"."Tag" "t8" ON "t7"."tag_id" = "t8"."id" WHERE "t7"."user_id" = "t4"."id") as "t3") as "t2" FROM "ent_load"."User" "t4" LEFT JOIN "ent_load"."Address" "t5" ON "t4"."address_id" = "t5"."id" WHERE "t4"."id" = $1"""
    assert params == (1, )

    user = await conn.select(q).first()
    assert user.id == 1
    assert user.address.id == user.address_id
    assert user.address.addr == "Address1"
    assert len(user.children) == 2
    assert user.children[0].name in ("Child1", "Child2")
    assert user.children[1].name in ("Child1", "Child2")
    assert user.children[0].name != user.children[1].name
    assert len(user.tags) == 2
    assert user.tags[0].value in ("tag1", "tag2")
    assert user.tags[1].value in ("tag1", "tag2")
    assert user.tags[0].value != user.tags[1].value
示例#3
0
async def test_load_many_across(conn):
    user = User(
        name={"family": "User"},
        tags=[
            Tag(value="ctag1"),
            Tag(value="ctag2"),
            Tag(value="ctag3"),
        ],
    )
    await conn.save(user)

    q = Query(User).load(User, User.tags).where(User.id == user.id)
    dialect = PostgreDialect()
    sql, params = dialect.create_query_compiler().compile_select(q)
    assert sql == """SELECT "t2"."id", ("t2"."name")."title", ("t2"."name")."family", ("t2"."name")."given", "t2"."address_id", (SELECT array_agg("t1") FROM (SELECT "t4"."id", "t4"."value" FROM "ent_load"."UserTags" "t3" INNER JOIN "ent_load"."Tag" "t4" ON "t3"."tag_id" = "t4"."id" WHERE "t3"."user_id" = "t2"."id") as "t1") as "t0" FROM "ent_load"."User" "t2" WHERE "t2"."id" = $1"""
    assert params == (user.id, )

    user = await conn.select(q).first()
    assert user.name.family == "User"
    assert len(user.tags) == 3

    assert user.tags[0].value == "ctag1"
    assert user.tags[1].value == "ctag2"
    assert user.tags[2].value == "ctag3"
示例#4
0
from yapic.entity.sql import PostgreDialect
from yapic.entity import Entity, Registry, Int, String, DateTime, PrimaryKey, Serial
from yapic.entity._entity import EntityState

dialect = PostgreDialect()


def test_entity():
    registry = Registry()

    class User(Entity, registry=registry):
        pass


def test_entity_state():
    registry = Registry()

    class User(Entity, registry=registry):
        id: Serial
        name: String

    user = User(id=1, name="Almafa")
    user.__state__.changes()
    user.__state__.changes_with_previous()
    user.__state__.changed_realtions()

    if user.__state__.is_dirty:
        pass

    if user.__state__.is_empty:
        pass
示例#5
0
from enum import Enum, Flag
import pytest

from yapic.entity import (Int, Serial, String, Choice, Field, PrimaryKey, ForeignKey, Date, DateTime, DateTimeTz, Bool,
                          func, const, Registry, Json, Composite, Auto, One, Index, StringArray, IntArray)
from yapic.entity.sql import PostgreDialect, Entity

dialect = PostgreDialect()
ddl = dialect.create_ddl_compiler()


class BaseEntity(Entity, registry=Registry(), _root=True):
    pass


def test_basic():
    class User(BaseEntity):
        pass

    class User2(BaseEntity, name="__user__"):
        pass

    class User3(BaseEntity, schema="auth"):
        pass

    class User4(BaseEntity, schema="auth", name="userX"):
        pass

    result = ddl.compile_entity(User)
    assert result[0] == """CREATE TABLE "User" (
);"""