How to use neomodel - 10 common examples

To help you get started, we’ve selected a few neomodel examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github neo4j-contrib / neomodel / test / test_transactions.py View on Github external
def test_rollback_and_commit_transaction():
    for p in APerson.nodes:
        p.delete()

    APerson(name='Roger').save()

    db.begin()
    APerson(name='Terry S').save()
    db.rollback()

    assert len(APerson.nodes) == 1

    db.begin()
    APerson(name='Terry S').save()
    db.commit()

    assert len(APerson.nodes) == 2
github neo4j-contrib / neomodel / test / test_batch.py View on Github external
from pytest import raises

from neomodel import (StructuredNode, StringProperty, IntegerProperty, UniqueIdProperty,
                      RelationshipTo, RelationshipFrom)
from neomodel.exceptions import UniqueProperty, DeflateError


class UniqueUser(StructuredNode):
    uid = UniqueIdProperty()
    name = StringProperty()
    age = IntegerProperty()


def test_unique_id_property_batch():
    users = UniqueUser.create(
        {'name': 'bob', 'age': 2},
        {'name': 'ben', 'age': 3}
    )

    assert users[0].uid != users[1].uid

    users = UniqueUser.get_or_create(
        {'uid': users[0].uid},
        {'name': 'bill', 'age': 4}
    )
github neo4j-contrib / neomodel / test / test_issue_87.py View on Github external
from datetime import datetime, timedelta


twelve_days = timedelta(days=12)
eleven_days = timedelta(days=11)
ten_days = timedelta(days=10)
nine_days = timedelta(days=9)
now = datetime.now()


class FriendRelationship(StructuredRel):
    since = DateTimeProperty(default=datetime.now)


class Person(StructuredNode):
    name = StringProperty()
    age = IntegerProperty()
    friends = Relationship('Person', 'friend_of', model=FriendRelationship)


def setup_friends(person0, person1, since=None):
    rel = person0.friends.connect(person1)
    if (since):
        rel.since = since
        rel.save()
    return rel.since


def test_traversal_single_param():
    connection().clear()
    jean = Person(name="Jean", age=25).save()
    johan = Person(name="Johan", age=19).save()
github neo4j-contrib / neomodel / test / test_indexing.py View on Github external
from pytest import raises

from neomodel import StructuredNode, StringProperty, IntegerProperty, UniqueProperty


class Human(StructuredNode):
    name = StringProperty(unique_index=True)
    age = IntegerProperty(index=True)


def test_unique_error():
    Human(name="j1m", age=13).save()
    try:
        Human(name="j1m", age=14).save()
    except UniqueProperty as e:
        assert True
        assert str(e).find('j1m')
        assert str(e).find('name')
        assert str(e).find('FooBarr')
    else:
        assert False, "UniqueProperty not raised."
github neo4j-contrib / neomodel / test / test_relationship_models.py View on Github external
def pre_save(self):
        HOOKS_CALLED['pre_save'] += 1

    def post_save(self):
        HOOKS_CALLED['post_save'] += 1


class Badger(StructuredNode):
    name = StringProperty(unique_index=True)
    friend = Relationship('Badger', 'FRIEND', model=FriendRel)
    hates = RelationshipTo('Stoat', 'HATES', model=HatesRel)


class Stoat(StructuredNode):
    name = StringProperty(unique_index=True)
    hates = RelationshipTo('Badger', 'HATES', model=HatesRel)


def test_either_connect_with_rel_model():
    paul = Badger(name="Paul").save()
    tom = Badger(name="Tom").save()

    # creating rels
    new_rel = tom.friend.disconnect(paul)
    new_rel = tom.friend.connect(paul)
    assert isinstance(new_rel, FriendRel)
    assert isinstance(new_rel.since, datetime)

    # updating properties
    new_rel.since = datetime.now(pytz.utc)
    assert isinstance(new_rel.save(), FriendRel)
github neo4j-contrib / neomodel / test / test_multilingual.py View on Github external
from neomodel import StructuredNode, StringProperty
from neomodel.contrib import Multilingual, Language


class Student(Multilingual, StructuredNode):
    name = StringProperty(unique_index=True)


def setup():
    for l in ['fr', 'ar', 'pl', 'es']:
        Language(code=l).save()


def test_multilingual():
    bob = Student(name="Bob", age=77).save()
    bob.attach_language(Language.get("fr"))
    bob.attach_language("ar")
    bob.attach_language(Language.get("ar"))
    bob.attach_language(Language.get("pl"))
    assert bob.has_language("fr")
    assert not bob.has_language("es")
github neo4j-contrib / neomodel / test / test_properties.py View on Github external
def test_default_value_callable():
    def uid_generator():
        return 'xx'

    class DefaultTestValueTwo(StructuredNode):
        uid = StringProperty(default=uid_generator, index=True)

    a = DefaultTestValueTwo().save()
    assert a.uid == 'xx'
github neo4j-contrib / neomodel / test / test_batch.py View on Github external
users = Customer.create(
        {'email': 'jim6@aol.com', 'age': 3},
    )
    assert users
    with raises(UniqueProperty):
        Customer.create(
            {'email': 'jim6@aol.com', 'age': 3},
            {'email': 'jim7@aol.com', 'age': 5},
        )

    # not found
    assert not Customer.nodes.filter(email='jim7@aol.com')


class Dog(StructuredNode):
    name = StringProperty(required=True)
    owner = RelationshipTo('Person', 'owner')


class Person(StructuredNode):
    name = StringProperty(unique_index=True)
    pets = RelationshipFrom('Dog', 'owner')


def test_get_or_create_with_rel():
    bob = Person.get_or_create({"name": "Bob"})[0]
    bobs_gizmo = Dog.get_or_create({"name": "Gizmo"}, relationship=bob.pets)

    tim = Person.get_or_create({"name": "Tim"})[0]
    tims_gizmo = Dog.get_or_create({"name": "Gizmo"}, relationship=tim.pets)
github neo4j-contrib / neomodel / test / test_cardinality.py View on Github external
from pytest import raises

from neomodel import (StructuredNode, StringProperty, IntegerProperty, OneOrMore, ZeroOrMore,
                      RelationshipTo, AttemptedCardinalityViolation, CardinalityViolation,
                      ZeroOrOne, One)


class HairDryer(StructuredNode):
    version = IntegerProperty()


class ScrewDriver(StructuredNode):
    version = IntegerProperty()


class Car(StructuredNode):
    version = IntegerProperty()


class Monkey(StructuredNode):
    name = StringProperty()
    dryers = RelationshipTo('HairDryer', 'OWNS_DRYER', cardinality=ZeroOrMore)
    driver = RelationshipTo('ScrewDriver', 'HAS_SCREWDRIVER', cardinality=ZeroOrOne)
    car = RelationshipTo('Car', 'HAS_CAR', cardinality=OneOrMore)
    toothbrush = RelationshipTo('ToothBrush', 'HAS_TOOTHBRUSH', cardinality=One)


class ToothBrush(StructuredNode):
    name = StringProperty()


def test_cardinality_zero_or_more():
github neo4j-contrib / neomodel / test / test_cardinality.py View on Github external
version = IntegerProperty()


class Car(StructuredNode):
    version = IntegerProperty()


class Monkey(StructuredNode):
    name = StringProperty()
    dryers = RelationshipTo('HairDryer', 'OWNS_DRYER', cardinality=ZeroOrMore)
    driver = RelationshipTo('ScrewDriver', 'HAS_SCREWDRIVER', cardinality=ZeroOrOne)
    car = RelationshipTo('Car', 'HAS_CAR', cardinality=OneOrMore)
    toothbrush = RelationshipTo('ToothBrush', 'HAS_TOOTHBRUSH', cardinality=One)


class ToothBrush(StructuredNode):
    name = StringProperty()


def test_cardinality_zero_or_more():
    m = Monkey(name='tim').save()
    assert m.dryers.all() == []
    assert m.dryers.single() is None
    h = HairDryer(version=1).save()

    m.dryers.connect(h)
    assert len(m.dryers.all()) == 1
    assert m.dryers.single().version == 1

    m.dryers.disconnect(h)
    assert m.dryers.all() == []
    assert m.dryers.single() is None