def test_extend(self): r = TupleSet([ Tuple(y=3,z=30), Tuple(y=1,z=10), Tuple(y=2,z=20)]) self.failUnless(y.extend(z=lambda tup: tup.y*10) == r)
def test_join(self): A = Relation(Header(a=int, cx=int), Tuple(a=2, cx=0), Tuple(a=2, cx=1), Tuple(a=3, cx=2), Tuple(a=7, cx=3), Tuple(a=9, cx=4)) B = Relation(Header(a=int, cy=int), Tuple(a=2, cy=5), Tuple(a=8, cy=6), Tuple(a=3, cy=7), Tuple(a=9, cy=8), Tuple(a=9, cy=9)) self.assertEqual(A.join(A), A) self.assertEqual(A.join(B), B.join(A))
def test_count(self): self.failUnless(Relation(Header()).count == 0) self.failUnless(Relation(Header(a=str)).count == 0) self.failUnless(Relation(Header(a=str), Tuple(a='aaa')).count == 1) self.failUnless( Relation(Header(a=str, b=int), Tuple(a='aaa', b=123), Tuple(a='ccc', b=456)).count == 2)
def test_join(self): r = TupleSet([ Tuple(c='a',d='b'), Tuple(c='b',d='c'), Tuple(c='c',d='d'), Tuple(c='d',d='e'), Tuple(c='e',d='f')]) self.failUnless(q.join(w).allbut('i') == r)
def test_attribute(self): s1 = Tuple(sno='S1', sname='Smith', status=20) self.assertEqual(s1.sno, 'S1') self.assertEqual(s1.sname, 'Smith') self.assertEqual(s1.status, 20) C = Tuple(x=2, y=5) self.failUnless(Tuple(x=C.x, y=C.y) == C) with self.assertRaises(AttributeError): Tuple(x=11).a
def test_extend(self): T = Tuple(a=11, b=22, c=33) self.assertEqual(T.extend(x=lambda t: t.a * 10), Tuple(a=11, b=22, c=33, x=110)) self.assertEqual( T.extend(x=lambda t: t.a * 10, y=lambda t: t.a * t.b + t.c, z=lambda _: True), Tuple(a=11, b=22, c=33, x=110, y=275, z=True))
def test_set_ops(self): e = TupleSet([]) ab = TupleSet([Tuple(a='b')]) ac = TupleSet([Tuple(a='c')]) abc = TupleSet([Tuple(a='b'),Tuple(a='c')]) self.failUnless(e|e == e) self.failUnless(e|x == x == x|e) # correct? self.failUnless(x|x == x) self.failUnless(ab|ac == ac|ab) self.failUnless(ab|abc == abc)
def test_names(self): # names is a sorted tuple with no dups. self.failUnless(Relation(Header()).names == ()) self.failUnless(Relation(Header(), Tuple()).names == ()) self.failUnless(Relation(Header(a=int)).names == ('a', )) self.assertEqual( Relation(Header(c=int, b=str, a=float)).names, ('a', 'b', 'c')) self.assertEqual( Relation(Header(c=int, b=str, a=float), Tuple(c=123, b="abc", a=1.0)).names, ('a', 'b', 'c'))
def test_create(self): self.failUnless(R == Rc) I = Relation(Header(a=int, b=int, c=int), Tuple(a=2, b=2, c=0)) Ic = Relation.create(('a', 'b', 'c'), (int, int, int), [(2, 2, 0)]) DEE = Relation(Header(), Tuple()) DEEc = Relation.create((), (), [()]) self.failUnless(DEE == DEEc) DUM = Relation(Header()) DUMc = Relation.create((), (), []) self.failUnless(DUM == DUMc) self.failUnless(I == Ic) self.failUnless(Relation.create(R.names, R.types, R.rows) == R) # crazy: header = Header(**dict(zip(R.names, R.types))) rows = list(Tuple(**dict(zip(R.names, t))) for t in R.rows) self.failUnless(Relation(header, *rows) == R)
def test_sum(self): self.failUnless(R.sum(lambda r: r.c) == 10) self.assertEqual(R.restrict(lambda _: False).sum(lambda r: r.c), 0) self.failUnless(R.ave(lambda r: r.c) == 2) self.failUnless(R.min(lambda r: r.c) == 0) self.failUnless(R.max(lambda r: r.c) == 4) # E1 = Relation(Header(),Tuple()) E = Relation(Header(b=bool)) T = Relation(Header(b=bool), Tuple(b=True)) F = Relation(Header(b=bool), Tuple(b=False)) self.failUnless(E.all(lambda r: r.b) == True) self.failUnless(E.any(lambda r: r.b) == False) self.failUnless(T.all(lambda r: r.b) == True) self.failUnless(T.any(lambda r: r.b) == True) self.failUnless(F.all(lambda r: r.b) == False) self.failUnless(F.any(lambda r: r.b) == False)
def test_project(self): self.assertEqual(Relation(Header(a=int)).project().names, ()) self.assertEqual(Relation(Header(a=int)).project('a').names, ('a', )) self.assertEqual( Relation(Header(a=int, b=int)).project('a').names, ('a', )) self.assertEqual( Relation(Header(b=int, a=int)).project('b', 'a').names, ('a', 'b')) R = Relation(Header(a=int, b=int), Tuple(a=2, b=2), Tuple(a=2, b=3), Tuple(a=3, b=2), Tuple(a=9, b=2)) self.assertEqual(R.project('a').names, ('a', )) self.assertEqual(R.project('b').names, ('b', )) self.assertEqual(R.project('a', 'b').names, ('a', 'b')) self.assertEqual(R.project('b', 'a').names, ('a', 'b')) self.assertEqual(R.project('a').count, 3) self.assertEqual(R.project('b').count, 2) self.assertEqual(R.project('a', 'b'), R) self.assertEqual(R.project(), Relation(Header()))
def test_rename(self): r = TupleSet([Tuple(j=0,x=2,y=1), Tuple(j=3,x=8,y=1), Tuple(j=6,x=14,y=1), Tuple(j=5,x=12,y=3), Tuple(j=2,x=6,y=3), Tuple(j=4,x=10,y=2), Tuple(j=1,x=4,y=2)]) self.failUnless(t.rename(i='j') == r)
def test_project(self): # idempotent self.failUnless(Tuple(b=22).project('b') == Tuple(b=22)) self.failUnless(Tuple(a=11, b=22).project('b') == Tuple(b=22)) with self.assertRaises(AttributeError): Tuple(a=11, b=22).project('c') with self.assertRaises(AttributeError): Tuple(a=11, b=22).project('c', 'd') with self.assertRaises(AttributeError): Tuple(a=11, b=22).project('a', 'd') with self.assertRaises(AttributeError): Tuple(a=11, b=22).project('a', 'b', 'c') self.assertEqual( Tuple(a=11, b=22, c=33).project('a', 'b').project('a'), Tuple(a=11, b=22, c=33).project('a', 'c').project('a')) self.failUnless(Tuple().project() == Tuple()) self.failUnless(Tuple(b=22).project() == Tuple()) self.assertEqual( Tuple(a=11, b=22, c=33).project('a', 'b', 'c'), Tuple(a=11, b=22, c=33))
def test_degree(self): self.failUnless(Tuple().degree == 0) self.failUnless(Tuple(a=11).degree == 1) self.failUnless(Tuple(a=11, b=22).degree == 2)
def test_not_equal(self): self.failUnless(Tuple() != Tuple(a='')) self.failUnless(Tuple(s='01') != Tuple(t='01')) self.failUnless(Tuple(s='01') != Tuple(s='02')) self.failUnless(Tuple(s='01') != Tuple(s='01', t='01'))
def test_equal(self): # transitive,symmetric,associative Tuple(s='01', n='S', t=20) self.assertEqual(Tuple(), Tuple()) self.assertEqual(Tuple(s='01'), Tuple(s='01')) self.assertEqual(Tuple(s='01', n='S', t=20), Tuple(s='01', n='S', t=20)) self.assertEqual(Tuple(n='S', t=20, s='01'), Tuple(s='01', n='S', t=20)) self.failIf(Tuple() == Tuple(a='')) self.failIf(Tuple(s='01') == Tuple(t='01')) self.failIf(Tuple(s='01') == Tuple(s='02'))
def test_names(self): # names are sorted # no name dups s1 = Tuple(s='S1', n='Smith', t=20) self.assertEqual(s1.names, ('n', 's', 't'))
def test_repr(self): self.assertEqual(repr(Tuple(sno='S1', sname='Smith', status=20)), "Tuple(sname='Smith',sno='S1',status=20)")
def test_to_pytuple(self): self.failUnless(Tuple(a=11, b=22, c=33).to_pytuple() == (11, 22, 33)) self.failUnless(Tuple().to_pytuple() == ()) self.failUnless(Tuple(x='blue').to_pytuple() == ('blue', ))
def test_project(self): self.failUnless(t.project('i','x','y') == t) self.failUnless(t.project('x') == x) self.failUnless(t.project('x','y') == xy) self.failUnless(t.project() == TupleSet([Tuple()]))
def test_rename(self): X = Relation(Header(x=int, b=int, c=int), Tuple(x=2, b=2, c=0), Tuple(x=2, b=3, c=1), Tuple(x=3, b=2, c=2), Tuple(x=9, b=2, c=3), Tuple(x=9, b=4, c=4)) XY = Relation(Header(x=int, y=int, c=int), Tuple(x=2, y=2, c=0), Tuple(x=2, y=3, c=1), Tuple(x=3, y=2, c=2), Tuple(x=9, y=2, c=3), Tuple(x=9, y=4, c=4)) AB = Relation(Header(b=int, a=int, c=int), Tuple(b=2, a=2, c=0), Tuple(b=2, a=3, c=1), Tuple(b=3, a=2, c=2), Tuple(b=9, a=2, c=3), Tuple(b=9, a=4, c=4)) self.failUnless(R.rename(a='a') == R) self.failUnless(R.rename(a='x') == X) self.failUnless(R.rename(a='x', b='y') == XY) self.failUnless(R.rename(a='b', b='a') == AB)
#! /usr/bin/env python import unittest import functools import sys sys.path.insert(0, '../') from relbis.relD import (Relation, Header, DEE, DUM) from relbis.TupleSet import TupleSet from relbis.Tuple import Tuple R = Relation(Header(a=int, b=int, c=int), Tuple(a=2, b=2, c=0), Tuple(a=2, b=3, c=1), Tuple(a=3, b=2, c=2), Tuple(a=9, b=2, c=3), Tuple(a=9, b=4, c=4)) Rc = Relation.create(('a', 'b', 'c'), (int, int, int), [(2, 2, 0), (2, 3, 1), (3, 2, 2), (9, 2, 3), (9, 4, 4)]) M = Relation(Header(a=int, b=int, c=int), Tuple(a=2, b=2, c=0), Tuple(a=2, b=3, c=1), Tuple(a=3, b=2, c=2)) A = Relation(Header(a=int, b=int, c=int), Tuple(a=2, b=2, c=0), Tuple(a=2, b=3, c=1)) B = Relation(Header(a=int, b=int, c=int), Tuple(a=2, b=3, c=1), Tuple(a=3, b=2, c=2)) I = Relation(Header(a=int, b=int, c=int), Tuple(a=2, b=2, c=0)) J = Relation(Header(a=int, b=int, c=int), Tuple(a=2, b=3, c=1))
def test_or(self): self.failUnless(Tuple() | Tuple() == Tuple()) self.failUnless(Tuple(b=22) | Tuple(b=22) == Tuple(b=22)) self.assertEqual(Tuple(a=11) | Tuple(), Tuple(a=11)) self.assertEqual(Tuple(a=11) | Tuple(b=22), Tuple(a=11, b=22)) self.assertEqual( Tuple(a=11, b=22, c=33) | Tuple(b=22, d=44), Tuple(a=11, b=22, c=33, d=44)) with self.assertRaises(TypeError): Tuple(b=22) | Tuple(b=99) with self.assertRaises(TypeError): Tuple(b=22) | Tuple(b='22')
def test_extend(self): Hello = Relation(Header(a=int, b=int, c=int, greeting=str), Tuple(a=2, b=2, c=0, greeting="Hello, World!"), Tuple(a=2, b=3, c=1, greeting="Hello, World!"), Tuple(a=3, b=2, c=2, greeting="Hello, World!"), Tuple(a=9, b=2, c=3, greeting="Hello, World!"), Tuple(a=9, b=4, c=4, greeting="Hello, World!")) D = Relation(Header(a=int, b=int, c=int, d=int), Tuple(a=2, b=2, c=0, d=0), Tuple(a=2, b=3, c=1, d=10), Tuple(a=3, b=2, c=2, d=20), Tuple(a=9, b=2, c=3, d=30), Tuple(a=9, b=4, c=4, d=40)) X = Relation(Header(a=int, b=int, c=int, d=int, e=int), Tuple(a=2, b=2, c=0, d=0, e=220), Tuple(a=3, b=2, c=2, d=20, e=322), Tuple(a=2, b=3, c=1, d=10, e=231), Tuple(a=9, b=2, c=3, d=30, e=923), Tuple(a=9, b=4, c=4, d=40, e=944)) self.assertEqual(R.extend(greeting=(str, lambda _: "Hello, World!")), Hello) self.assertEqual(R.extend(d=(int, lambda r: r.c * 10)), D) self.assertEqual( R.extend(d=(int, lambda r: r.c * 10), e=(int, lambda r: r.a * 100 + r.b * 10 + r.c)), X) self.assertEqual(DEE.extend(x=(int, lambda _: 27)), Relation(Header(x=int), Tuple(x=27)))
def test_rename(self): self.assertEqual( Tuple(a=11, b=22, c=33).rename(a='a'), Tuple(a=11, b=22, c=33)) self.assertEqual( Tuple(a=11, b=22, c=33).rename(a='x'), Tuple(x=11, b=22, c=33)) self.assertEqual( Tuple(a=11, b=22, c=33).rename(a='x', b='y', c='z'), Tuple(x=11, y=22, z=33)) self.assertEqual( Tuple(a=11, b=22, c=33).rename(a='b', b='a'), Tuple(b=11, a=22, c=33)) self.assertEqual( Tuple(a=11, b=22, c=33).rename(a='b', b='c', c='a'), Tuple(b=11, c=22, a=33)) with self.assertRaises(AttributeError): Tuple(a=11, b=22, c=33).rename(x='w') with self.assertRaises(AttributeError): Tuple().rename(x='w') with self.assertRaises(TypeError): Tuple(a=11, b=22, c=33).rename(a='z', b='z') with self.assertRaises(TypeError): Tuple(a=11, b=22, c=33).rename(a='c')
#! /usr/bin/env python import unittest import sys sys.path.insert(0,'../') from relbis.TupleSet import TupleSet from relbis.Tuple import Tuple t = TupleSet([ Tuple(i=0,x=2,y=1), Tuple(i=1,x=4,y=2), Tuple(i=2,x=6,y=3), Tuple(i=3,x=8,y=1), Tuple(i=4,x=10,y=2), Tuple(i=5,x=12,y=3), Tuple(i=6,x=14,y=1)]) xy = TupleSet([ Tuple(x=2,y=1), Tuple(x=4,y=2), Tuple(x=6,y=3), Tuple(x=8,y=1), Tuple(x=10,y=2), Tuple(x=12,y=3), Tuple(x=14,y=1)]) x = TupleSet([ Tuple(x=2), Tuple(x=4), Tuple(x=6),