File: test_mapped_sequence.py

package info (click to toggle)
python-agate 1.13.0-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,008 kB
  • sloc: python: 8,578; makefile: 126
file content (121 lines) | stat: -rw-r--r-- 3,603 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
import unittest

from agate.mapped_sequence import MappedSequence


class TestMappedSequence(unittest.TestCase):
    def setUp(self):
        self.column_names = ('one', 'two', 'three')
        self.data = ('a', 'b', 'c')
        self.row = MappedSequence(self.data, self.column_names)

    def test_is_immutable(self):
        with self.assertRaises(TypeError):
            self.row[0] = 'foo'

        with self.assertRaises(TypeError):
            self.row['one'] = 100

    def test_stringify(self):
        self.assertEqual(str(self.row), "<agate.MappedSequence: ('a', 'b', 'c')>")

    def test_stringify_long(self):
        column_names = ('one', 'two', 'three', 'four', 'five', 'six')
        data = ('a', 'b', 'c', 'd', 'e', 'f')
        row = MappedSequence(data, column_names)

        self.assertEqual(str(row), "<agate.MappedSequence: ('a', 'b', 'c', 'd', 'e', ...)>")

    def test_length(self):
        self.assertEqual(len(self.row), 3)

    def test_eq(self):
        row2 = MappedSequence(self.data, self.column_names)

        self.assertTrue(self.row == ('a', 'b', 'c'))
        self.assertTrue(self.row == ['a', 'b', 'c'])
        self.assertTrue(self.row == row2)
        self.assertFalse(self.row == ('a', 'b', 'c', 'd'))
        self.assertFalse(self.row == 1)

    def test_ne(self):
        row2 = MappedSequence(self.data, self.column_names)

        self.assertFalse(self.row != ('a', 'b', 'c'))
        self.assertFalse(self.row != ['a', 'b', 'c'])
        self.assertFalse(self.row != row2)
        self.assertTrue(self.row != ('a', 'b', 'c', 'd'))
        self.assertTrue(self.row != 1)

    def test_contains(self):
        self.assertTrue('a' in self.row)
        self.assertFalse('d' in self.row)

    def test_set_item(self):
        with self.assertRaises(TypeError):
            self.row['one'] = 't'

        with self.assertRaises(TypeError):
            self.row['five'] = 'g'

    def test_get_item(self):
        self.assertEqual(self.row['one'], 'a')
        self.assertEqual(self.row['two'], 'b')
        self.assertEqual(self.row['three'], 'c')

    def test_get_by_key(self):
        self.assertEqual(self.row['one'], 'a')
        self.assertEqual(self.row[0], 'a')

    def test_get_by_slice(self):
        self.assertSequenceEqual(self.row[1:], ('b', 'c'))

    def test_get_invalid(self):
        with self.assertRaises(IndexError):
            self.row[3]

        with self.assertRaises(KeyError):
            self.row['foo']

    def test_keys(self):
        self.assertIs(self.row.keys(), self.column_names)

    def test_values(self):
        self.assertIs(self.row.values(), self.data)

    def test_items(self):
        self.assertSequenceEqual(self.row.items(), [
            ('one', 'a'),
            ('two', 'b'),
            ('three', 'c')
        ])

    def test_get(self):
        self.assertEqual(self.row.get('one'), 'a')

    def test_get_default(self):
        self.assertEqual(self.row.get('four'), None)
        self.assertEqual(self.row.get('four', 'foo'), 'foo')

    def test_dict(self):
        self.assertDictEqual(self.row.dict(), {
            'one': 'a',
            'two': 'b',
            'three': 'c'
        })

    def test_dict_no_keys(self):
        row = MappedSequence(self.data)

        with self.assertRaises(KeyError):
            row.dict()

    def test_iterate(self):
        it = iter(self.row)

        self.assertSequenceEqual(next(it), 'a')
        self.assertSequenceEqual(next(it), 'b')
        self.assertSequenceEqual(next(it), 'c')

        with self.assertRaises(StopIteration):
            next(it)