Browse Source

more tests

git-svn-id: https://bucket.mit.edu/svn/nilm/nilmdb@10657 ddd99763-3ecb-0310-9145-efcb8ce7c51f
tags/bxinterval-last
Jim Paris 12 years ago
parent
commit
1046d0c47b
2 changed files with 32 additions and 27 deletions
  1. +2
    -2
      nilmdb/interval.py
  2. +30
    -25
      tests/test_interval.py

+ 2
- 2
nilmdb/interval.py View File

@@ -32,7 +32,7 @@ class Interval(object):
s = repr(self.start) + ", " + repr(self.end)
if self.tolerance:
s += ", " + repr(self.tolerance)
return self.__name__ + "(" + s + ")"
return self.__class__.__name__ + "(" + s + ")"

def __str__(self):
return "[" + str(self.start) + " -> " + str(self.end) + "]"
@@ -94,7 +94,7 @@ class IntervalSet(object):
return self.data.__iter__()

def __repr__(self):
return self.__name__ + "(" + repr(self.data) + ")"
return self.__class__.__name__ + "(" + repr(self.data) + ")"

def __eq__(self, other):
"""Test equality of two IntervalSets.


+ 30
- 25
tests/test_interval.py View File

@@ -6,32 +6,29 @@ from nose.tools import *
from nose.tools import assert_raises
import itertools

from nilmdb.interval import Interval, IntervalError

def test_interval():
"""Test the Interval class"""
"""Test Interval class"""
(d1, d2, d3) = [ datetime_tz.datetime_tz.smartparse(x).totimestamp()
for x in [ "03/24/2012", "03/25/2012", "03/26/2012" ] ]

# basic construction
i = nilmdb.interval.Interval(d1, d1)
i = nilmdb.interval.Interval(d1, d3)
i = Interval(d1, d1)
i = Interval(d1, d3)
assert(i.start == d1)
assert(i.end == d3)

# assignment should work
# assignment is allowed, but not verified
i.start = d2
try:
i.end = d1
raise Exception("should have died there")
except IntervalError:
pass
#with assert_raises(IntervalError):
# i.end = d1
i.start = d1
i.end = d2

# end before start
assert_raises(IntervalError, Interval, d3, d1)

# wrong type
assert_raises(IntervalError, Interval, 1, 2)
with assert_raises(IntervalError):
i = Interval(d3, d1)

# compare
assert(Interval(d1, d2) == Interval(d1, d2))
@@ -41,31 +38,38 @@ def test_interval():
assert(Interval(d1, d3) < Interval(d2, d3))
assert(Interval(d2, d2) > Interval(d1, d3))
assert(Interval(d3, d3) == Interval(d3, d3))
assert_raises(TypeError, cmp, i, 123)
with assert_raises(AttributeError):
x = (i == 123)

# subset
assert(Interval(d1, d3).subset(d1, d2) == Interval(d1, d2))
assert_raises(IntervalError, Interval(d2, d3).subset, d1, d2)
with assert_raises(IntervalError):
x = Interval(d2, d3).subset(d1, d2)

# append
assert(Interval(d1, d2).is_adjacent(Interval(d2,d3)))
assert(Interval(d2, d3).is_adjacent(Interval(d1,d2)))
assert(not Interval(d2, d3).is_adjacent(Interval(d1,d3)))
assert_raises(TypeError, Interval(d1, d2).is_adjacent, 1)
assert(Interval(d1, d2).is_adjacent(Interval(d2, d3)))
assert(Interval(d2, d3).is_adjacent(Interval(d1, d2)))
assert(not Interval(d2, d3).is_adjacent(Interval(d1, d3)))
with assert_raises(AttributeError):
x = Interval(d1, d2).is_adjacent(1)

# misc
assert(repr(i) == repr(eval(repr(i).replace("datetime.",""))))
assert(str(i) == "[1980-12-05 00:00:00 -> 1990-02-16 00:00:00]")
i = Interval(d1, d2)
j = Interval(d1, d2, 1.23)
eq_(repr(i), repr(eval(repr(i))))
eq_(repr(j), repr(eval(repr(j))))
eq_(str(i), "[1332561600.0 -> 1332648000.0]")

def test_interval_intersect():
"""Test Interval intersections"""
dates = [ datetime.strptime(year, "%y") for year in [ "00", "01", "02", "03" ] ]
dates = [ 100, 200, 300, 400 ]
perm = list(itertools.permutations(dates, 2))
prod = list(itertools.product(perm, perm))
should_intersect = {
False: [4, 5, 8, 20, 48, 56, 60, 96, 97, 100],
True: [0, 1, 2, 12, 13, 14, 16, 17, 24, 25, 26, 28, 29,
32, 49, 50, 52, 53, 61, 62, 64, 65, 68, 98, 101, 104]}
32, 49, 50, 52, 53, 61, 62, 64, 65, 68, 98, 101, 104]
}
for i,((a,b),(c,d)) in enumerate(prod):
try:
i1 = Interval(a, b)
@@ -75,10 +79,11 @@ def test_interval_intersect():
except IntervalError:
assert(i not in should_intersect[True] and
i not in should_intersect[False])
assert_raises(TypeError, i1.intersects, 1234)
with assert_raises(AttributeError):
x = i1.intersects(1234)

def test_intervalset_construct():
"""Test interval set construction"""
"""Test IntervalSet construction"""
dates = [ datetime.strptime(year, "%y") for year in [ "00", "01", "02", "03" ]]

a = Interval(dates[0], dates[1])


Loading…
Cancel
Save