~nch/glue

ref: 50333171e5b5973cdef9c6e8d045750a3a0936e6 glue/table_test.py -rw-r--r-- 2.2 KiB
50333171 — nc WIP this seems(?) to be working 1 year, 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from table import *
import unittest

class TestTable(unittest.TestCase):
    def test_initialize(self):
        t = Table([[1,2,'a'], [4,5,'b']], columns=['x', 'y', 'z'])
        self.assertEqual(list(t['x']), [1,4])
        self.assertEqual(list(t['z']), ['a','b'])

        t = Table({'x': [1,2,3], 'y': ['a', 'b', 'c'], 'z': [1, 1, 1]}, columns=['x', 'y', 'z'])
        self.assertEqual(list(t['x']), list(t[0]))
        self.assertEqual(list(t['x']), [1,2,3])

        t = Table([['a', 1, 1], ['b',2,1]])
        self.assertEqual(list(t[0]), ['a', 'b'])

    def test_coli(self):
        t = Table({'a': [1, 2, 3]})
        self.assertEqual(t.coli('a'), 0)
        with self.assertRaises(AttributeError) as ctx:
            t.coli('b')

    def test_subscript(self):
        t = Table({'a': np.array([1,2,3]),
                   'b': np.array(['a', 'b', 'c'])},
                   columns=['a', 'b'])

        self.assertEqual(list(t[0]), list(t['a']))

        self.assertEqual(list(t[0]), [1,2,3])
        self.assertEqual(list(t['a']), [1,2,3])
        self.assertEqual(list(t['a', :2]), [1,2])
        self.assertEqual(list(t[0, :2]), [1,2])
        self.assertEqual(list(t[['a', 'b'], :2]['a']), [1,2])
        t2 = t[:, t['a'] == 2]
        self.assertEqual(set(t2.columns), set(t.columns))

        self.assertEqual(t[:, 1], (2, 'b'))

    def test_vstack(self):
        t1 = Table({'a': [1,2,3], 'b': [2,3,4], 'c': [1,2,3]})
        t2 = Table({'a': [2], 'b': [2], 'c': [2]})

        self.assertEqual(t1.vstack(t2).columns, ['a', 'b', 'c'])
        self.assertEqual(list(t1.vstack(t2)['a']), [1,2,3,2])
        self.assertEqual(list(t1.vstack(t2)['b']), [2,3,4,2])
        self.assertEqual(list(t1.vstack(t2)['c']), [1,2,3,2])

    def test_tuples(self):
        t1 = Table({'a': [1,2,3], 'b': ['x', 'y', 'z']})
        self.assertEqual(list(t1.tuples()), [(1, 'x'), (2, 'y'), (3, 'z')])

    def test_append(self):
        t1 = Table({'a': [1,2], 'b': ['x', 'y']})
        t2 = t1.append({'a': 4, 'b': 'q'})
        self.assertEqual(list(t2.tuples()), [(1, 'x'), (2, 'y'), (4, 'q')])
        t2 = t1.append((4, 'q'))
        self.assertEqual(list(t2.tuples()), [(1, 'x'), (2, 'y'), (4, 'q')])

if __name__ == '__main__':
    unittest.main()