コード例 #1
0
def test_model_graph():
    t1 = Table("t1", [
        Column("c1", int, None, True, False, None),
        Column("c2", int, None, False, False, None),
    ])
    t2 = Table("t2", [
        Column("c1", int, None, True, False, None),
        Column("c2", int, None, False, False, None),
    ])

    m1 = define_model(t1, [CRUDMixin, GraphEntityMixin])
    m2 = define_model(t2, [CRUDMixin, GraphEntityMixin])

    t = graph_template(
        m1 = m1,
        m2 = m2,
    )
    t.m2 >> t.m1

    graph = Graph(t)

    graph.append(
        m1 = m1(c1 = 1, c2 = "a"),
        m2 = m2(c1 = 1, c2 = 1),
    )
    graph.append(
        m1 = m1(c1 = 2, c2 = "b"),
        m2 = m2(c1 = 2, c2 = 2),
    )
    graph.append(
        m1 = m1(c1 = 2, c2 = "dummy"),
        m2 = m2(c1 = 3, c2 = 2),
    )
    graph.append(
        m1 = m1(c1 = None, c2 = None),
        m2 = m2(c1 = 3, c2 = 2),
    )
    graph.append(
        m1 = m1(c1 = 3, c2 = "c"),
        m2 = m2(c1 = 1, c2 = 3),
    )
    graph.append(
        m1 = m1(c1 = 4, c2 = "d"),
        m2 = m2(c1 = None, c2 = None),
    )

    assert graph_dict(
        graph.view,
        m1 = (),
        m2 = (),
    ) == dict(
        m1 = [
            dict(c1 = 1, c2 = "a", m2 = [dict(c1 = 1, c2 = 1)]),
            dict(c1 = 2, c2 = "b", m2 = [dict(c1 = 2, c2 = 2), dict(c1 = 3, c2 = 2)]),
            dict(c1 = 3, c2 = "c", m2 = [dict(c1 = 1, c2 = 3)]),
            dict(c1 = 4, c2 = "d", m2 = []),
        ]
    )
コード例 #2
0
def test_no_pk():
    t1 = Table("t1", [
        Column("c1", int, None, False, False, None),
        Column("c2", int, None, False, False, None),
    ])

    m1 = define_model(t1, [CRUDMixin, GraphEntityMixin])

    t = graph_template(m1 = m1)
    graph = Graph(t)

    graph.append(m1 = m1(c1 = 1, c2 = "a"))
    graph.append(m1 = m1(c1 = 1, c2 = "a"))

    assert len(graph.view.m1) == 2
コード例 #3
0
def test_add_serializer():
    t1 = Table("t1", [
        Column("c1", int, None, True, False, None),
        Column("c2", int, None, False, False, None),
    ])

    m1 = define_model(t1, [CRUDMixin, GraphEntityMixin])

    t = graph_template(m1 = m1)
    graph = Graph(t)

    graph.append(m1 = m1(c1 = 1, c2 = "a"))

    add_serializer(m1, lambda s, x: dict(c1 = x.c1 * 2, c2 = f"__{x.c2}__"))

    assert graph_dict(graph.view, m1 = ()) == dict(m1 = [dict(c1 = 2, c2 = "__a__")])
コード例 #4
0
def test_add_identifier():
    t1 = Table("t1", [
        Column("c1", int, None, True, False, None),
        Column("c2", int, None, False, False, None),
    ])

    m1 = define_model(t1, [CRUDMixin, GraphEntityMixin])

    add_identifier(m1, lambda x: x.c2)

    t = graph_template(m1 = m1)
    graph = Graph(t)

    graph.append(m1 = m1(c1 = 1, c2 = "a"))
    graph.append(m1 = m1(c1 = 2, c2 = "a"))

    assert len(graph.view.m1) == 1
    v = graph.view.m1[0]()
    assert v.c1 == 1
    assert v.c2 == "a"
コード例 #5
0
def test_add_entity_filter():
    t1 = Table("t1", [
        Column("c1", int, None, True, False, None),
        Column("c2", int, None, False, False, None),
    ])

    m1 = define_model(t1, [CRUDMixin, GraphEntityMixin])

    add_entity_filter(m1, lambda x: x.c1 >= 0)

    t = graph_template(m1 = m1, v = int)
    t.m1 << t.v
    graph = Graph(t)

    graph.append(m1 = m1(c1 = -1, c2 = "a"), v = 1)
    graph.append(m1 = m1(c1 = 0, c2 = "b"), v = 2)
    graph.append(m1 = m1(c1 = 1, c2 = "c"), v = 3)

    assert [(n().c1, n().c2) for n in graph.view.m1] == [(0, "b"), (1, "c")]
    assert [n() for n in graph.view.v] == [2, 3]
    assert [n() for n in graph.view.m1[0].v] == [2]
    assert [n() for n in graph.view.m1[1].v] == [3]
コード例 #6
0
ファイル: graph_example.py プロジェクト: sozu/py-pyracmon
for i in range(0, 20):
    blog_category.inserts(
        db,
        [dict(
            blog_id=i + 1,
            name=f"category_{i}_{j}",
        ) for j in range(0, 3)])

# Declare template representing graph structure.
t = graph_template(
    blogs=blog,
    recent_posts=post,
    total_posts=int,
    categories=blog_category,
    images=image,
    recent_comments=post_comment,
    most_liked_comment=post_comment,
    total_comments=int,
    total=int,
)
t.blogs << [t.categories, t.total_posts, t.recent_posts]
t.recent_posts << [
    t.images, t.recent_comments, t.most_liked_comment, t.total_comments
]


def fetch_blogs():
    # Create graph instance.
    graph = new_graph(t)