Esempio n. 1
0
    def test_can_execute_example_code(self):

        class Person(object):

            def __init__(self, email=None, name=None, age=None):
                self.email = email
                self.name = name
                self.age = age

            def __str__(self):
                return self.name

        graph = Graph()
        store = Store(graph)

        alice = Person("*****@*****.**", "Alice", 34)
        store.save_unique("People", "email", alice.email, alice)

        bob = Person("*****@*****.**", "Bob", 66)
        carol = Person("*****@*****.**", "Carol", 42)
        store.relate(alice, "LIKES", bob)
        store.relate(alice, "LIKES", carol)
        store.save(alice)

        friends = store.load_related(alice, "LIKES", Person)
        print("Alice likes {0}".format(" and ".join(str(f) for f in friends)))
Esempio n. 2
0
class TestRelate(object):
    @pytest.fixture(autouse=True)
    def setup(self, graph):
        self.graph = graph
        self.store = Store(self.graph)

    def test_can_relate_to_other_object(self):
        alice = Person("*****@*****.**", "Alice", 34)
        bob = Person("*****@*****.**", "Bob", 66)
        self.store.relate(alice, "LIKES", bob)
        assert hasattr(alice, "__rel__")
        assert isinstance(alice.__rel__, dict)
        assert "LIKES" in alice.__rel__
        assert alice.__rel__["LIKES"] == [({}, bob)]

    def test_can_relate_to_other_object_with_properties(self):
        alice = Person("*****@*****.**", "Alice", 34)
        bob = Person("*****@*****.**", "Bob", 66)
        self.store.relate(alice, "LIKES", bob, {"since": 1999})
        assert hasattr(alice, "__rel__")
        assert isinstance(alice.__rel__, dict)
        assert "LIKES" in alice.__rel__
        assert alice.__rel__["LIKES"] == [({"since": 1999}, bob)]
Esempio n. 3
0
    def test_can_execute_example_code(self):
        class Person(object):
            def __init__(self, email=None, name=None, age=None):
                self.email = email
                self.name = name
                self.age = age

            def __str__(self):
                return self.name

        graph = Graph()
        store = Store(graph)

        alice = Person("*****@*****.**", "Alice", 34)
        store.save_unique("People", "email", alice.email, alice)

        bob = Person("*****@*****.**", "Bob", 66)
        carol = Person("*****@*****.**", "Carol", 42)
        store.relate(alice, "LIKES", bob)
        store.relate(alice, "LIKES", carol)
        store.save(alice)

        friends = store.load_related(alice, "LIKES", Person)
        print("Alice likes {0}".format(" and ".join(str(f) for f in friends)))
Esempio n. 4
0
class TestRelate(object):

    @pytest.fixture(autouse=True)
    def setup(self, graph):
        self.graph = graph
        self.store = Store(self.graph)

    def test_can_relate_to_other_object(self):
        alice = Person("*****@*****.**", "Alice", 34)
        bob = Person("*****@*****.**", "Bob", 66)
        self.store.relate(alice, "LIKES", bob)
        assert hasattr(alice, "__rel__")
        assert isinstance(alice.__rel__, dict)
        assert "LIKES" in alice.__rel__
        assert alice.__rel__["LIKES"] == [({}, bob)]

    def test_can_relate_to_other_object_with_properties(self):
        alice = Person("*****@*****.**", "Alice", 34)
        bob = Person("*****@*****.**", "Bob", 66)
        self.store.relate(alice, "LIKES", bob, {"since": 1999})
        assert hasattr(alice, "__rel__")
        assert isinstance(alice.__rel__, dict)
        assert "LIKES" in alice.__rel__
        assert alice.__rel__["LIKES"] == [({"since": 1999}, bob)]
Esempio n. 5
0
class TestSeparate(object):

    @pytest.fixture(autouse=True)
    def setup(self, graph):
        self.graph = graph
        self.store = Store(self.graph)

    def test_can_separate_from_other_objects(self):
        alice = Person("*****@*****.**", "Alice", 34)
        bob = Person("*****@*****.**", "Bob", 66)
        carol = Person("*****@*****.**", "Carol", 42)
        self.store.relate(alice, "LIKES", bob)
        self.store.relate(alice, "LIKES", carol)
        self.store.separate(alice, "LIKES", carol)
        assert alice.__rel__["LIKES"] == [({}, bob)]
        self.store.separate(alice, "LIKES", bob)
        assert alice.__rel__["LIKES"] == []

    def test_can_separate_without_previous_relate(self):
        alice = Person("*****@*****.**", "Alice", 34)
        bob = Person("*****@*****.**", "Bob", 66)
        assert not hasattr(alice, "__rel__")
        self.store.separate(alice, "LIKES", bob)
        assert not hasattr(alice, "__rel__")

    def test_nothing_happens_if_unknown_rel_type_supplied(self):
        alice = Person("*****@*****.**", "Alice", 34)
        bob = Person("*****@*****.**", "Bob", 66)
        self.store.relate(alice, "LIKES", bob)
        self.store.separate(alice, "DISLIKES", bob)
        assert alice.__rel__["LIKES"] == [({}, bob)]

    def test_nothing_happens_if_unknown_endpoint_supplied(self):
        alice = Person("*****@*****.**", "Alice", 34)
        bob = Person("*****@*****.**", "Bob", 66)
        carol = Person("*****@*****.**", "Carol", 42)
        self.store.relate(alice, "LIKES", bob)
        self.store.separate(alice, "LIKES", carol)
        assert alice.__rel__["LIKES"] == [({}, bob)]
Esempio n. 6
0
class TestSeparate(object):
    @pytest.fixture(autouse=True)
    def setup(self, graph):
        self.graph = graph
        self.store = Store(self.graph)

    def test_can_separate_from_other_objects(self):
        alice = Person("*****@*****.**", "Alice", 34)
        bob = Person("*****@*****.**", "Bob", 66)
        carol = Person("*****@*****.**", "Carol", 42)
        self.store.relate(alice, "LIKES", bob)
        self.store.relate(alice, "LIKES", carol)
        self.store.separate(alice, "LIKES", carol)
        assert alice.__rel__["LIKES"] == [({}, bob)]
        self.store.separate(alice, "LIKES", bob)
        assert alice.__rel__["LIKES"] == []

    def test_can_separate_without_previous_relate(self):
        alice = Person("*****@*****.**", "Alice", 34)
        bob = Person("*****@*****.**", "Bob", 66)
        assert not hasattr(alice, "__rel__")
        self.store.separate(alice, "LIKES", bob)
        assert not hasattr(alice, "__rel__")

    def test_nothing_happens_if_unknown_rel_type_supplied(self):
        alice = Person("*****@*****.**", "Alice", 34)
        bob = Person("*****@*****.**", "Bob", 66)
        self.store.relate(alice, "LIKES", bob)
        self.store.separate(alice, "DISLIKES", bob)
        assert alice.__rel__["LIKES"] == [({}, bob)]

    def test_nothing_happens_if_unknown_endpoint_supplied(self):
        alice = Person("*****@*****.**", "Alice", 34)
        bob = Person("*****@*****.**", "Bob", 66)
        carol = Person("*****@*****.**", "Carol", 42)
        self.store.relate(alice, "LIKES", bob)
        self.store.separate(alice, "LIKES", carol)
        assert alice.__rel__["LIKES"] == [({}, bob)]
        self.email = email
        self.name = name
        self.age = age

    def __str__(self):
        return self.name

graph = Graph('http://127.0.0.1:7474/db/data/')
store = Store(graph)

alice = Person("*****@*****.**", "Tobin", 34)
store.save_unique("People", "email", alice.email, alice)

bob = Person("*****@*****.**", "Bob", 66)
carol = Person("*****@*****.**", "Carol", 42)
jenny = Person("*****@*****.**", "Jenny", 43)
edgar = Person("*****@*****.**", "Edgar", 44)
mike = Person("*****@*****.**", "Mike", 45)
store.relate(alice, "LIKES", bob)     # these relationships are not saved
store.relate(alice, "LIKES", carol)   # until `alice` is saved
store.relate(alice, "LIKES", jenny)   # until `alice` is saved
store.relate(alice, "LIKES", edgar)   # until `alice` is saved
store.relate(alice, "LIKES", mike)   # until `alice` is saved
store.save(alice)

friends = store.load_related(alice, "LIKES", Person)
print "Alice likes {0}".format(" and ".join(str(f) for f in friends))

'''MATCH (you )-[:LIKES]->(yourFriends)
RETURN you, yourFriends'''
store = Store(graph)

series = ["Downton Abbey", "Daniel Tiger", ]


downton = Series(name="Downton Abbey")

episode1 = Episode(name="Ep1")

episode2 = Episode(name="Ep2")


bbc = Organization(name="BBC")

hbo = Organization(name="HBO")


actor1 = Actor(name="Bob")
actor2 = Actor(name="Mary")
actor3 = Actor(name="Mark")

store.relate(downton, "HAS_EPISODE", episode1)
store.relate(downton, "HAS_EPISODE", episode2)
store.relate(downton, "PRODUCED_BY", bbc)
store.relate(downton, "DISTRIBUTED_BY", hbo)
store.relate(downton, "ACTED_IN", actor1)
store.relate(downton, "ACTED_IN", actor2)
store.relate(downton, "ACTED_IN", actor3)

store.save(downton)
Esempio n. 9
0
class TestLoadRelated(object):
    @pytest.fixture(autouse=True)
    def setup(self, graph):
        self.graph = graph
        self.store = Store(self.graph)

    def test_can_load_single_related_object(self):
        alice = Person("*****@*****.**", "Alice", 34)
        bob = Person("*****@*****.**", "Bob", 66)
        self.store.relate(alice, "LIKES", bob)
        self.store.save(alice)
        friends = self.store.load_related(alice, "LIKES", Person)
        assert friends == [bob]

    def test_can_load_multiple_related_objects(self):
        alice = Person("*****@*****.**", "Alice", 34)
        bob = Person("*****@*****.**", "Bob", 66)
        carol = Person("*****@*****.**", "Carol", 42)
        self.store.relate(alice, "LIKES", bob)
        self.store.relate(alice, "LIKES", carol)
        self.store.save(alice)
        friends = self.store.load_related(alice, "LIKES", Person)
        assert friends == [bob, carol]

    def test_can_load_related_objects_among_other_relationships(self):
        alice = Person("*****@*****.**", "Alice", 34)
        bob = Person("*****@*****.**", "Bob", 66)
        carol = Person("*****@*****.**", "Carol", 42)
        dave = Person("*****@*****.**", "Dave", 18)
        self.store.relate(alice, "LIKES", bob)
        self.store.relate(alice, "LIKES", carol)
        self.store.relate(alice, "DISLIKES", dave)
        self.store.save(alice)
        friends = self.store.load_related(alice, "LIKES", Person)
        assert friends == [bob, carol]
        enemies = self.store.load_related(alice, "DISLIKES", Person)
        assert enemies == [dave]

    def test_can_load_related_when_never_related(self):
        alice = Person("*****@*****.**", "Alice", 34)
        friends = self.store.load_related(alice, "LIKES", Person)
        assert friends == []
Esempio n. 10
0
class TestLoadRelated(object):

    @pytest.fixture(autouse=True)
    def setup(self, graph):
        self.graph = graph
        self.store = Store(self.graph)

    def test_can_load_single_related_object(self):
        alice = Person("*****@*****.**", "Alice", 34)
        bob = Person("*****@*****.**", "Bob", 66)
        self.store.relate(alice, "LIKES", bob)
        self.store.save(alice)
        friends = self.store.load_related(alice, "LIKES", Person)
        assert friends == [bob]

    def test_can_load_multiple_related_objects(self):
        alice = Person("*****@*****.**", "Alice", 34)
        bob = Person("*****@*****.**", "Bob", 66)
        carol = Person("*****@*****.**", "Carol", 42)
        self.store.relate(alice, "LIKES", bob)
        self.store.relate(alice, "LIKES", carol)
        self.store.save(alice)
        friends = self.store.load_related(alice, "LIKES", Person)
        assert friends == [bob, carol]

    def test_can_load_related_objects_among_other_relationships(self):
        alice = Person("*****@*****.**", "Alice", 34)
        bob = Person("*****@*****.**", "Bob", 66)
        carol = Person("*****@*****.**", "Carol", 42)
        dave = Person("*****@*****.**", "Dave", 18)
        self.store.relate(alice, "LIKES", bob)
        self.store.relate(alice, "LIKES", carol)
        self.store.relate(alice, "DISLIKES", dave)
        self.store.save(alice)
        friends = self.store.load_related(alice, "LIKES", Person)
        assert friends == [bob, carol]
        enemies = self.store.load_related(alice, "DISLIKES", Person)
        assert enemies == [dave]

    def test_can_load_related_when_never_related(self):
        alice = Person("*****@*****.**", "Alice", 34)
        friends = self.store.load_related(alice, "LIKES", Person)
        assert friends == []