コード例 #1
0
 def test_spec_serializer(self):
     spec = GraphSpec()
     spec.add_serializer(int, lambda s, x: x * 2)
     assert spec.to_dict(
         self._graph(),
         a=S.of(),
     ) == {
         "a": [2, 4, 6]
     }
コード例 #2
0
    def test_inherit(self):
        class A:
            pass

        class B(A):
            pass

        ident = lambda x: x

        spec = GraphSpec()
        spec.add_identifier(A, ident)

        assert spec.get_identifier(B) is ident
コード例 #3
0
    def test_identifier(self):
        ident1 = lambda x: x * 1
        ident2 = lambda x: x * 2

        spec = GraphSpec()
        spec.add_identifier(int, ident1)

        t = spec.new_template(
            a=int,
            b=(int, ),
            c=(int, None),
            d=(int, ident2),
        )

        assert (t.a.name, t.a.kind, t.a.identifier.identifier(4)) == ("a", int,
                                                                      4)
        assert (t.b.name, t.b.kind, t.b.identifier.identifier(4)) == ("b", int,
                                                                      4)
        assert (t.c.name, t.c.kind, t.c.identifier.identifier(4)) == ("c", int,
                                                                      None)
        assert (t.d.name, t.d.kind, t.d.identifier.identifier(4)) == ("d", int,
                                                                      8)
コード例 #4
0
    def test_find(self):
        identi = lambda x: x
        idents = lambda x: x

        spec = GraphSpec()
        spec.add_identifier(int, identi)
        spec.add_identifier(str, idents)

        assert spec.get_identifier(int) is identi
        assert spec.get_identifier(str) is idents
        assert spec.get_identifier(float) is None
コード例 #5
0
    def test_default(self):
        t = GraphSpec().new_template(a=(),
                                     b=(int, ),
                                     c=int,
                                     d=(str, len, lambda s: len(s) > 3))

        assert len(t._properties) == 4
        assert len(t._relations) == 0

        assert (t.a.name, t.a.kind,
                t.a.identifier.identifier("abc")) == ("a", None, None)
        assert (t.b.name, t.b.kind,
                t.b.identifier.identifier("abc")) == ("b", int, None)
        assert (t.c.name, t.c.kind,
                t.c.identifier.identifier("abc")) == ("c", int, None)
        assert (t.d.name, t.d.kind,
                t.d.identifier.identifier("abc")) == ("d", str, 3)
        assert (t.a.entity_filter, t.b.entity_filter,
                t.c.entity_filter) == (None, None, None)
        assert t.d.entity_filter("abc") == False
        assert t.d.entity_filter("abcd") == True
コード例 #6
0
import pytest
from pyracmon.graph.spec import GraphSpec
from pyracmon.graph.graph import *
from pyracmon.graph.template import P

spec = GraphSpec()


class TestView:
    def _template(self):
        t = spec.new_template(
            a=(),
            b=(),
            c=(),
            d=(),
            e=(),
        )
        t.a << [t.b, t.c]
        t.b << t.d
        return t

    def test_node_view(self):
        t = self._template()
        n = Node(t.a, 1, None, 0)
        n.add_child(Node(t.b, 2, None, 0))
        n.add_child(Node(t.c, 3, None, 0))
        n.add_child(Node(t.c, 4, None, 1))
        view = n.view

        assert view() == 1
        assert [n() for n in view.b] == [2]