def test_basic_relationship():
    """
    Tests both sides of a simple many-to-many relationship (without relationship
    properties).
    """
    class RelatedPaper(models.NodeModel):
        authors = models.Relationship(Person,
                rel_type = neo4django.Outgoing.OWNED_BY,
                related_name = 'papers'
            )
    
    sandra = Person(name="Sandra")
    sandra.save()
    lifesWork = RelatedPaper()
    lifesWork.save()
    lifesWork.authors.add(sandra)
    
    lifesWork.save()
    work = list(sandra.papers.all())
    assert lifesWork in work, "Paper not found in %s" % repr(work)
    authors = list(lifesWork.authors.all())
    assert sandra in authors, "Author not found in %s" % repr(work)
    #find all shared neo4j relationships
    sandras = sandra.node.relationships.all(['OWNED_BY'])[:]
    eq_(len(sandras), 1)
    #test proper direction
    eq_(sandras[0].end, sandra.node)
    eq_(sandras[0].start, lifesWork.node)
Example #2
0
def test_basic_relationship():
    """
    Tests both sides of a simple many-to-many relationship (without relationship
    properties).
    """
    class RelatedPaper(models.NodeModel):
        authors = models.Relationship(Person,
                                      rel_type=neo4django.Outgoing.OWNED_BY,
                                      related_name='papers')

    sandra = Person(name="Sandra")
    sandra.save()
    lifesWork = RelatedPaper()
    lifesWork.save()
    lifesWork.authors.add(sandra)

    lifesWork.save()
    work = list(sandra.papers.all())
    assert lifesWork in work, "Paper not found in %s" % repr(work)
    authors = list(lifesWork.authors.all())
    assert sandra in authors, "Author not found in %s" % repr(work)
    #find all shared neo4j relationships
    sandras = sandra.node.relationships.all(['OWNED_BY'])[:]
    eq_(len(sandras), 1)
    #test proper direction
    eq_(sandras[0].end, sandra.node)
    eq_(sandras[0].start, lifesWork.node)
Example #3
0
def test_model_pickling():
    """
    Covers issue #46, pickling `NodeModel`s.
    """

    def pickle_and_restore(m):
        import pickle
        return pickle.loads(pickle.dumps(m))

    def pickle_eq(m1, m2):
        eq_(m1.name, m2.name)
        eq_(m2.using, m2.using)
        eq_(m2.id, m2.id)

    # try a simple model
    pete = Person(name="Pete")
    restored_pete = pickle_and_restore(pete)

    pickle_eq(pete, restored_pete)

    # try a saved model

    pete.save()
    restored_saved_pete = pickle_and_restore(pete)

    pickle_eq(pete, restored_saved_pete)

    # try related models

    from .models import IndexedMouse, RelatedCat, LazyCat
    jerry = IndexedMouse.objects.create(name='Jerry')
    tom = RelatedCat(name='Jerry')

    tom.chases = jerry
    tom.save()
    
    restored_tom = pickle_and_restore(tom)

    pickle_eq(tom, restored_tom)
    pickle_eq(jerry, list(restored_tom.chases.all())[0])

    # try a model with a lazy relation
    
    garfield = LazyCat(name='Garfield')
    garfield.chases.add(jerry)

    restored_garfield = pickle_and_restore(garfield)

    pickle_eq(garfield, restored_garfield)
    pickle_eq(jerry, list(restored_garfield.chases.all())[0])

    # and finally a saved model with a lazy relation

    garfield.save()

    restored_saved_garfield = pickle_and_restore(garfield)

    pickle_eq(garfield, restored_saved_garfield)
    pickle_eq(jerry, list(restored_saved_garfield.chases.all())[0])
Example #4
0
def test_model_pickling():
    """
    Covers issue #46, pickling `NodeModel`s.
    """
    def pickle_and_restore(m):
        import pickle
        return pickle.loads(pickle.dumps(m))

    def pickle_eq(m1, m2):
        eq_(m1.name, m2.name)
        eq_(m2.using, m2.using)
        eq_(m2.id, m2.id)

    # try a simple model
    pete = Person(name="Pete")
    restored_pete = pickle_and_restore(pete)

    pickle_eq(pete, restored_pete)

    # try a saved model

    pete.save()
    restored_saved_pete = pickle_and_restore(pete)

    pickle_eq(pete, restored_saved_pete)

    # try related models

    from .models import IndexedMouse, RelatedCat, LazyCat
    jerry = IndexedMouse.objects.create(name='Jerry')
    tom = RelatedCat(name='Jerry')

    tom.chases = jerry
    tom.save()

    restored_tom = pickle_and_restore(tom)

    pickle_eq(tom, restored_tom)
    pickle_eq(jerry, list(restored_tom.chases.all())[0])

    # try a model with a lazy relation

    garfield = LazyCat(name='Garfield')
    garfield.chases.add(jerry)

    restored_garfield = pickle_and_restore(garfield)

    pickle_eq(garfield, restored_garfield)
    pickle_eq(jerry, list(restored_garfield.chases.all())[0])

    # and finally a saved model with a lazy relation

    garfield.save()

    restored_saved_garfield = pickle_and_restore(garfield)

    pickle_eq(garfield, restored_saved_garfield)
    pickle_eq(jerry, list(restored_saved_garfield.chases.all())[0])
Example #5
0
def test_prop():
    pete = Person(name='Pete')
    assert pete.name == 'Pete'
    pete.save()
    assert pete.name == 'Pete'
    pete.name = 'Peter'
    assert pete.name == 'Peter'
    pete.save()
    assert pete.name == 'Peter'
Example #6
0
def test_prop():
    pete = Person(name="Pete")
    assert pete.name == "Pete"
    pete.save()
    assert pete.name == "Pete"
    pete.name = "Peter"
    assert pete.name == "Peter"
    pete.save()
    assert pete.name == "Peter"
Example #7
0
def test_prop():
    pete = Person(name='Pete')
    assert pete.name == 'Pete'
    pete.save()
    assert pete.name == 'Pete'
    pete.name = 'Peter'
    assert pete.name == 'Peter'
    pete.save()
    assert pete.name == 'Peter'
Example #8
0
def test_delete():
    """Confirm 'delete()' works for NodeQuerySet."""
    jack = Person(name='jack')
    jack.save()
    jacks_pk = jack.pk
    Person.objects.filter(name='jack').delete()
    try:
        gdb.nodes.get(jacks_pk)
    except:
        pass
    else:
        raise AssertionError("Jack's pk is still in the graph- he wasn't "
                             "deleted.")
def test_delete():
    """Confirm 'delete()' works for NodeQuerySet."""
    jack = Person(name='jack')
    jack.save()
    jacks_pk = jack.pk
    Person.objects.filter(name='jack').delete()
    try:
        gdb.nodes.get(jacks_pk)
    except:
        pass
    else:
        raise AssertionError("Jack's pk is still in the graph- he wasn't "
                             "deleted.")
Example #10
0
def test_save_delete():
    """Basic sanity check for NodeModel save and delete.  """
    from neo4jrestclient.client import NotFoundError

    pete = Person(name='Pete')
    pete.save()
    node_id = pete.id
    pete.delete()
    try:
        gdb.nodes.get(node_id)
    except NotFoundError:
        pass
    else:
        assert False, 'Pete was not properly deleted.'
Example #11
0
def test_save_delete():
    """Basic sanity check for NodeModel save and delete.  """
    from neo4jrestclient.client import NotFoundError

    pete = Person(name='Pete')
    pete.save()
    node_id = pete.id
    pete.delete()
    try:
        gdb.nodes.get(node_id)
    except NotFoundError:
        pass
    else:
        assert False, 'Pete was not properly deleted.'
Example #12
0
def test_none_prop():
    """Confirm that `None` and null verification work properly."""
    pete = Person()
    pete.save()
    assert pete.name is None
    
    class NotNullPerson(models.NodeModel):
        class Meta:
            app_label = 'test'
        name = models.StringProperty(null=False)
    try:
        andy = NotNullPerson(name = None)
        andy.save()
    except:
        pass
    else:
        raise AssertionError('Non-nullable field accepted `None` as a value.')
Example #13
0
def test_basic_relationship():
    """
    Tests both sides of a simple many-to-many relationship (without relationship
    properties).
    """
    class RelatedPaper(neo4django.NodeModel):
        authors = neo4django.Relationship(Person,
                rel_type = neo4django.Outgoing.OWNED_BY,
                related_name = 'papers'
            )
    
    sandra = Person(name="Sandra")
    sandra.save()
    lifesWork = RelatedPaper()
    lifesWork.save()
    lifesWork.authors.add(sandra)
    lifesWork.save()
    work = list(sandra.papers.all())
    assert lifesWork in work, "Paper not found in %s" % repr(work)
    authors = list(lifesWork.authors.all())
    assert sandra in authors, "Author not found in %s" % repr(work)
Example #14
0
def test_none_prop():
    """Confirm that `None` and null verification work properly."""
    #first show that unsert properties are None
    pete = Person()
    pete.save()
    assert pete.name is None

    #then that `null=False` works properly
    class NotNullPerson(models.NodeModel):
        class Meta:
            app_label = 'test'

        name = models.StringProperty(null=False)

    try:
        andy = NotNullPerson(name=None)
        andy.save()
    except:
        pass
    else:
        raise AssertionError('Non-nullable field accepted `None` as a value.')

    #and finally, that setting a property to None deletes it in the db
    pete.name = 'Pete'
    pete.save()
    pete.name = None
    pete.save()

    assert_gremlin('results=!g.v(node_id).any{it.hasProperty("name")}',
                   {'node_id': pete.id})
Example #15
0
def test_none_prop():
    """Confirm that `None` and null verification work properly."""
    #first show that unsert properties are None
    pete = Person()
    pete.save()
    assert pete.name is None
    
    #then that `null=False` works properly
    class NotNullPerson(models.NodeModel):
        class Meta:
            app_label = 'test'
        name = models.StringProperty(null=False)
    try:
        andy = NotNullPerson(name = None)
        andy.save()
    except:
        pass
    else:
        raise AssertionError('Non-nullable field accepted `None` as a value.')

    #and finally, that setting a property to None deletes it in the db
    pete.name = 'Pete'
    pete.save()
    pete.name = None
    pete.save()

    assert_gremlin('results=!g.v(node_id).any{it.hasProperty("name")}',
                   {'node_id':pete.id})
Example #16
0
def test_json_serialize():
    from django.core import serializers
    dave = Person(name='dave')
    dave.save()
    json_serializer = serializers.get_serializer('json')()
    assert json_serializer.serialize(Person.objects.all())
    dave = Person(name='dave', age=12)
    dave.save()
    assert json_serializer.serialize(Person.objects.all())
    dave = Person()
    dave.save()
    assert json_serializer.serialize(Person.objects.all())
Example #17
0
 def try_int(integer):
     node = Person(name="SandraInt", age=integer)
     node.save()
     assert node.age == integer
     node.delete()
Example #18
0
 def try_int(integer):
     node = Person(name="SandraInt", age=integer)
     node.save()
     assert node.age == integer
     node.delete()