Skip to content

Commit

Permalink
Added tests for float properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
mhluongo committed Jan 11, 2014
1 parent b0e4860 commit 26c400d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
30 changes: 30 additions & 0 deletions neo4django/tests/nodequeryset_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,36 @@ def test_filter_range():
all_between_s_u = Person.objects.filter(name__range=('S','U'))
assert len(all_between_s_u) >= 1, "There's at least one 'T' name!"


@with_setup(None, teardown)
def test_filter_float_range():
import random

epsilon = 0.001
ages = []
while len(ages) < 5:
r = random.uniform(-5.0, 5.0)
if not(any(r > i - epsilon and r < i + epsilon for i in ages)):
ages.append(r)

class FloatAgedPerson(models.NodeModel):
age = models.FloatProperty()
indexed_age = models.FloatProperty(indexed=True)

for a in ages:
FloatAgedPerson.objects.create(age=a, indexed_age=a)

# unindexed range queries
for a in ages:
eq_(1, len(FloatAgedPerson.objects.filter(
age__range=[a - epsilon,a + epsilon])))

# indexed range queries
for a in ages:
eq_(1, len(list(FloatAgedPerson.objects.filter(
indexed_age__range=[a - epsilon, a + epsilon]))))


@with_setup(None, teardown)
def test_filter_date_range():
class Lifetime(models.NodeModel):
Expand Down
36 changes: 35 additions & 1 deletion neo4django/tests/property_tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from nose.tools import eq_, with_setup
from nose.tools import eq_, with_setup, assert_almost_equal
from django.core.exceptions import ValidationError

import datetime
Expand Down Expand Up @@ -75,7 +75,21 @@ def try_int(integer):

for i in [0,1,-1,28,neo4django.db.models.properties.MAX_INT,neo4django.db.models.properties.MIN_INT]:
try_int(i)

@with_setup(None, teardown)
def test_float():
class FloatPerson(models.NodeModel):
age = models.FloatProperty()
age_indexed = models.FloatProperty(indexed=True)

vals = [0.0, -1.0, 1.0, 12345.6789, 98766.123123, -1232375.4234]

# check the values
for f in vals:
node = FloatPerson.objects.create(age=f, age_indexed=f)
assert_almost_equal(f, FloatPerson.objects.get(id=node.id).age)


def test_date_constructor():
class DateNode(models.NodeModel):
date = models.DateProperty()
Expand Down Expand Up @@ -281,6 +295,26 @@ class IntArrayNode(models.NodeModel):
else:
raise AssertionError('tuples of strs should not validate')

@with_setup(None, teardown)
def test_float_array_property():
"""Tests that FloatArrayProperty validates, saves and returns properly."""
class FloatArrayNode(models.NodeModel):
vals = models.FloatArrayProperty()

vals = (1,2.1234,3.0,-1.0)
n1 = FloatArrayNode(vals = vals)
eq_(n1.vals, vals)
n1.save()
eq_(n1.vals, vals)

try:
n2 = IntArrayNode(vals = ('1.12','2.0','-3'))
n2.save()
except ValidationError:
pass
else:
raise AssertionError('tuples of strs should not validate')

def test_str_array_property_validator():
"""Tests that StringArrayProperty validates properly."""
class StrArrayNode(models.NodeModel):
Expand Down
10 changes: 8 additions & 2 deletions reg_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@
'test_create_with_id',
'test_relationship_get_by_id',
'test_update',
'test_relationship_filter_many_to_many']
'test_relationship_filter_many_to_many',
'test_float',
'test_filter_float_range',
'test_float_array_property']
should_fail=[
'test_dates',
'test_relationship_model',
Expand All @@ -196,4 +199,7 @@
'test_relationship_models',
'test_typenode_transactionality',
'test_autoproperty_transactionality',
'test_relationship_filter_many_to_many']
'test_relationship_filter_many_to_many',
'test_float',
'test_filter_float_range',
'test_float_array_property']

0 comments on commit 26c400d

Please sign in to comment.