def test_pop(self): r0 = Record(x=1, y=2, z=3) r1 = Record(x=2, y=2, z=3) t = Table([r0, r1]) assert t.pop('x') == [1, 2] assert r0 == Record(y=2, z=3) assert r1 == Record(y=2, z=3)
def test_setitem_index(self): t = Table() t.append(Record(a=1, b=4, c=7)) t.append(Record(a=2, b=5, c=8)) t.append(Record(a=3, b=6, c=9)) t[0] = Record(a=0, b=2, c=4) assert t[0] == Record(a=0, b=2, c=4)
def test_add(self): t = Table() t.append(Record(a=1, b=4, c=7)) assert t + t == Table([ Record(a=1, b=4, c=7), Record(a=1, b=4, c=7)]) assert type(t + t) is Table
def test_filter(self): t = Table([ Record(x=1, y=2, z=3), Record(x=2, y=2, z=3), Record(x=1, y=2, z=4), Record(x=2, y=1, z=3), Record(x=1, y=2, z=3), Record(x=3, y=3, z=4), ]) assert t.filter(lambda r: r.z > 3) == Table([ Record(x=1, y=2, z=4), Record(x=3, y=3, z=4), ])
def test_key_filter(self): t = Table([ Record(x=1, y=2, z=3), Record(x=2, y=2, z=3), Record(x=1, y=2, z=4), Record(x=2, y=1, z=3), Record(x=1, y=2, z=3), Record(x=3, y=3, z=4), ]) assert t.key_filter({'x': 1, 'z': 3}) == Table([ Record(x=1, y=2, z=3), Record(x=1, y=2, z=3), ])
def test_index(self): t = Table() t.append(Record(a=1, b=4, c=7)) t.append(Record(a=2, b=5, c=8)) t.append(Record(a=3, b=6, c=9)) t.index(Record(a=1, b=4, c=7)) == 0
def test_sort(self): t = Table([ Record(x=1, y=2, z=3), Record(x=1, y=2, z=4), Record(x=2, y=1, z=3), Record(x=3, y=3, z=4), Record(x=2, y=2, z=3), Record(x=1, y=2, z=3), ]) t.key_sort(['x']) assert t == Table([ Record(x=1, y=2, z=3), Record(x=1, y=2, z=4), Record(x=1, y=2, z=3), Record(x=2, y=1, z=3), Record(x=2, y=2, z=3), Record(x=3, y=3, z=4), ]) assert type(t) is Table
def test_getitem(self): t = Table() t.append(Record(a=1, b=4, c=7)) t.append(Record(a=2, b=5, c=8)) t.append(Record(a=3, b=6, c=9)) assert t[0] == Record(a=1, b=4, c=7) assert t[1] == Record(a=2, b=5, c=8) assert t[2] == Record(a=3, b=6, c=9) # assert t[0:1] == {'a': [1], 'b': [4], 'c': [7]} # assert t[0:2] == {'a': [1, 2], 'b': [4, 5], 'c': [7, 8]} assert t['a'] == [1, 2, 3] assert type(t['a']) is list assert t[['a', 'c']] == Table([ Record(a=1, c=7), Record(a=2, c=8), Record(a=3, c=9)]) assert type(t[['a', 'c']]) is Table
def test_copy(self): t = Table() t.append(Record(x=1, y=2)) s = Table(t) s.append(Record(x=2, y=3)) assert len(t) == 1 assert t[0].x == 1
def test_key_sorted(self): t = Table([ Record(x=1, y=2, z=3), Record(x=2, y=2, z=3), Record(x=1, y=2, z=4), Record(x=3, y=3, z=4), Record(x=1, y=2, z=3), Record(x=2, y=1, z=3), ]) assert type(t.key_sorted(['x'])) is Table assert t.key_sorted(['y']) == Table([ Record(x=2, y=1, z=3), Record(x=1, y=2, z=3), Record(x=2, y=2, z=3), Record(x=1, y=2, z=4), Record(x=1, y=2, z=3), Record(x=3, y=3, z=4), ]) assert t.key_sorted(['z', 'y']) == Table([ Record(x=2, y=1, z=3), Record(x=1, y=2, z=3), Record(x=2, y=2, z=3), Record(x=1, y=2, z=3), Record(x=1, y=2, z=4), Record(x=3, y=3, z=4), ]) assert t.key_sorted(['x', 'y']) == Table([ Record(x=1, y=2, z=3), Record(x=1, y=2, z=4), Record(x=1, y=2, z=3), Record(x=2, y=1, z=3), Record(x=2, y=2, z=3), Record(x=3, y=3, z=4), ]) assert t.key_sorted(['z', 'x', 'y']) == Table([ Record(x=1, y=2, z=3), Record(x=1, y=2, z=3), Record(x=2, y=1, z=3), Record(x=2, y=2, z=3), Record(x=1, y=2, z=4), Record(x=3, y=3, z=4), ])
def test_creation(self): t = Table() assert len(t) == 0 t.append(Record(a=1, b=4, c=7)) assert len(t) == 1
def test_keys(self): t = Table() t.append(Record(a=1, b=2, c=3)) assert_unordered_equal(t.keys(), ['a', 'b', 'c'])
def test_setitem_key_slice(self): t = Table() t.append(Record(a=1, b=4, c=7)) t.append(Record(a=2, b=5, c=8)) t.append(Record(a=3, b=6, c=9)) s = Table() s.append(Record(a=10, c=13)) s.append(Record(a=11, c=14)) s.append(Record(a=12, c=15)) #t[['a', 'c']] = s r = Table() r.append(Record(a=10, b=4, c=13)) r.append(Record(a=11, b=5, c=14)) r.append(Record(a=12, b=6, c=15))
def test_setitem_index_slice(self): t = Table() t.append(Record(a=1, b=4, c=7)) t.append(Record(a=2, b=5, c=8)) t.append(Record(a=3, b=6, c=9)) s = Table() s.append(Record(a=10, b=11, c=12)) s.append(Record(a=13, b=14, c=15)) r = Table() r.append(Record(a=10, b=11, c=12)) r.append(Record(a=13, b=14, c=15)) r.append(Record(a=3, b=6, c=9)) t[0:2] = s assert t == r
def test_len(self): t = Table() assert len(t) == 0 t.append(Record(a=1, b=2)) assert len(t) == 1
def test_magic_with_missing(self): t = Table() t.append(Record(x=1, y=2, z=3)) t.append(Record(x=1, y=2, z=4)) t.append(Record(x=2, y=1)) t.append(Record(x=3, y=3)) t.append(Record(x=2, y=2, z=3)) t.append(Record(x=1, y=2, z=3)) gt = Table() gt.append(Record(x=1, _=Table([t[0], t[1], t[5]]))) gt.append(Record(x=2, _=Table([t[2], t[4]]))) gt.append(Record(x=3, _=Table([t[3]]))) assert t.magic(['x'], '_') == gt gt = Table() gt.append(Record( _=Table([t[2], t[3]]))) gt.append(Record(z=3, _=Table([t[0], t[4], t[5]]))) gt.append(Record(z=4, _=Table([t[1]]))) assert t.magic(['z'], '_') == gt
def test_magic(self): t = Table() t.append(Record(x=1, y=2, z=3)) t.append(Record(x=1, y=2, z=4)) t.append(Record(x=2, y=1, z=3)) t.append(Record(x=3, y=3, z=4)) t.append(Record(x=2, y=2, z=3)) t.append(Record(x=1, y=2, z=3)) gt = Table() gt.append(Record(x=1, _=Table([t[0], t[1], t[5]]))) gt.append(Record(x=2, _=Table([t[2], t[4]]))) gt.append(Record(x=3, _=Table([t[3]]))) assert t.magic(['x'], '_') == gt gt = Table() gt.append(Record(x=1, y=2, _=Table([t[0], t[1], t[5]]))) gt.append(Record(x=2, y=1, _=Table([t[2]]))) gt.append(Record(x=2, y=2, _=Table([t[4]]))) gt.append(Record(x=3, y=3, _=Table([t[3]]))) assert t.magic(['x', 'y'], '_') == gt