File: test_table.py

package info (click to toggle)
hdmf 3.14.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,372 kB
  • sloc: python: 34,738; makefile: 303; sh: 35
file content (124 lines) | stat: -rw-r--r-- 4,040 bytes parent folder | download | duplicates (2)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import pandas as pd

from hdmf.container import Table, Row, RowGetter
from hdmf.testing import TestCase


class TestTable(TestCase):

    @classmethod
    def get_table_class(cls):
        class MyTable(Table):

            __defaultname__ = 'my_table'

            __columns__ = [
                {'name': 'col1', 'type': str, 'help': 'a string column'},
                {'name': 'col2', 'type': int, 'help': 'an integer column'},
            ]
        return MyTable

    def test_init(self):
        MyTable = TestTable.get_table_class()
        table = MyTable('test_table')
        self.assertTrue(hasattr(table, '__colidx__'))
        self.assertEqual(table.__colidx__, {'col1': 0, 'col2': 1})

    def test_add_row_getitem(self):
        MyTable = TestTable.get_table_class()
        table = MyTable('test_table')
        table.add_row(col1='foo', col2=100)
        table.add_row(col1='bar', col2=200)
        row1 = table[0]
        row2 = table[1]
        self.assertEqual(row1, ('foo', 100))
        self.assertEqual(row2, ('bar', 200))

    def test_to_dataframe(self):
        MyTable = TestTable.get_table_class()
        table = MyTable('test_table')
        table.add_row(col1='foo', col2=100)
        table.add_row(col1='bar', col2=200)

        df = table.to_dataframe()
        exp = pd.DataFrame(data=[{'col1': 'foo', 'col2': 100}, {'col1': 'bar', 'col2': 200}])
        pd.testing.assert_frame_equal(df, exp)

    def test_from_dataframe(self):
        MyTable = TestTable.get_table_class()
        exp = pd.DataFrame(data=[{'col1': 'foo', 'col2': 100}, {'col1': 'bar', 'col2': 200}])
        table = MyTable.from_dataframe(exp)
        row1 = table[0]
        row2 = table[1]
        self.assertEqual(row1, ('foo', 100))
        self.assertEqual(row2, ('bar', 200))


class TestRow(TestCase):

    def setUp(self):
        self.MyTable = TestTable.get_table_class()

        class MyRow(Row):
            __table__ = self.MyTable

        self.MyRow = MyRow

        self.table = self.MyTable('test_table')

    def test_row_no_table(self):
        with self.assertRaisesRegex(ValueError, '__table__ must be set if sub-classing Row'):
            class MyRow(Row):
                pass

    def test_table_init(self):
        MyTable = TestTable.get_table_class()
        table = MyTable('test_table')
        self.assertFalse(hasattr(table, 'row'))

        table_w_row = self.MyTable('test_table')
        self.assertTrue(hasattr(table_w_row, 'row'))
        self.assertIsInstance(table_w_row.row, RowGetter)
        self.assertIs(table_w_row.row.table, table_w_row)

    def test_init(self):
        row1 = self.MyRow(col1='foo', col2=100, table=self.table)

        # make sure Row object set up properly
        self.assertEqual(row1.idx, 0)
        self.assertEqual(row1.col1, 'foo')
        self.assertEqual(row1.col2, 100)

        # make sure Row object is stored in Table properly
        tmp_row1 = self.table.row[0]
        self.assertEqual(tmp_row1, row1)

    def test_add_row_getitem(self):
        self.table.add_row(col1='foo', col2=100)
        self.table.add_row(col1='bar', col2=200)

        row1 = self.table.row[0]
        self.assertIsInstance(row1, self.MyRow)
        self.assertEqual(row1.idx, 0)
        self.assertEqual(row1.col1, 'foo')
        self.assertEqual(row1.col2, 100)

        row2 = self.table.row[1]
        self.assertIsInstance(row2, self.MyRow)
        self.assertEqual(row2.idx, 1)
        self.assertEqual(row2.col1, 'bar')
        self.assertEqual(row2.col2, 200)

        # test memoization
        row3 = self.table.row[0]
        self.assertIs(row3, row1)

    def test_todict(self):
        row1 = self.MyRow(col1='foo', col2=100, table=self.table)
        self.assertEqual(row1.todict(), {'col1': 'foo', 'col2': 100})

    def test___str__(self):
        row1 = self.MyRow(col1='foo', col2=100, table=self.table)
        row1_str = str(row1)
        expected_str = "Row(0, test_table) = {'col1': 'foo', 'col2': 100}"
        self.assertEqual(row1_str, expected_str)