Example #1
0
    def test_record_keys(self):
        r = Record(R_AB, (42, 43))
        vv = r.keys()
        self.assertEqual(tuple(vv), ('a', 'b'))

        # test invalid record
        with self.assertRaisesRegex(TypeError, 'not iterable'):
            Record(None, (42, 43)).keys()
Example #2
0
    def test_record_keys(self):
        r = Record(R_AB, (42, 43))
        vv = r.keys()
        self.assertEqual(tuple(vv), ('a', 'b'))

        # test invalid record
        with self.assertRaisesRegex(TypeError, 'not iterable'):
            Record(None, (42, 43)).keys()
Example #3
0
    def test_record_items(self):
        r = Record(R_AB, (42, 43))

        self.assertEqual(dict(r), {'a': 42, 'b': 43})
        self.assertEqual(
            list(collections.OrderedDict(r).items()),
            [('a', 42), ('b', 43)])

        with self.checkref(r):
            rk = r.items()
            self.assertEqual(rk.__length_hint__(), 2)
            self.assertEqual(next(rk), ('a', 42))
            self.assertEqual(rk.__length_hint__(), 1)
            self.assertEqual(next(rk), ('b', 43))
            self.assertEqual(rk.__length_hint__(), 0)

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

            self.assertEqual(rk.__length_hint__(), 0)

        self.assertEqual(list(r.items()), [('a', 42), ('b', 43)])

        # Check invalid records just in case
        r = Record(R_A, (42, 43))
        self.assertEqual(list(r.items()), [('a', 42)])
        r = Record(R_AB, (42,))
        self.assertEqual(list(r.items()), [('a', 42)])
        r = Record(None, (42, 43))
        with self.assertRaises(TypeError):
            list(r.items())
Example #4
0
    def test_record_items(self):
        r = Record(R_AB, (42, 43))

        self.assertEqual(dict(r), {'a': 42, 'b': 43})
        self.assertEqual(list(collections.OrderedDict(r).items()), [('a', 42),
                                                                    ('b', 43)])

        with self.checkref(r):
            rk = r.items()
            self.assertEqual(rk.__length_hint__(), 2)
            self.assertEqual(next(rk), ('a', 42))
            self.assertEqual(rk.__length_hint__(), 1)
            self.assertEqual(next(rk), ('b', 43))
            self.assertEqual(rk.__length_hint__(), 0)

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

            self.assertEqual(rk.__length_hint__(), 0)

        self.assertEqual(list(r.items()), [('a', 42), ('b', 43)])

        # Check invalid records just in case
        r = Record(R_A, (42, 43))
        self.assertEqual(list(r.items()), [('a', 42)])
        r = Record(R_AB, (42, ))
        self.assertEqual(list(r.items()), [('a', 42)])

        # Try to iterate over exhausted items() iterator
        r = Record(R_A, (42, 43))
        it = r.items()
        list(it)
        list(it)
Example #5
0
 def test_record_empty(self):
     r = Record(None, ())
     self.assertEqual(r, ())
     self.assertLess(r, (1,))
     self.assertEqual(len(r), 0)
     self.assertFalse(r)
     self.assertNotIn('a', r)
     self.assertEqual(repr(r), '<Record>')
     self.assertEqual(str(r), '<Record>')
     with self.assertRaisesRegex(KeyError, 'aaa'):
         r['aaa']
     self.assertEqual(dict(r.items()), {})
     self.assertEqual(list(r.keys()), [])
     self.assertEqual(list(r.values()), [])
Example #6
0
 def test_record_empty(self):
     r = Record(None, ())
     self.assertEqual(r, ())
     self.assertLess(r, (1,))
     self.assertEqual(len(r), 0)
     self.assertFalse(r)
     self.assertNotIn('a', r)
     self.assertEqual(repr(r), '<Record>')
     self.assertEqual(str(r), '<Record>')
     with self.assertRaisesRegex(KeyError, 'aaa'):
         r['aaa']
     self.assertEqual(dict(r.items()), {})
     self.assertEqual(list(r.keys()), [])
     self.assertEqual(list(r.values()), [])
Example #7
0
 def test_record_keys(self):
     r = Record(R_AB, (42, 43))
     vv = r.keys()
     self.assertEqual(tuple(vv), ('a', 'b'))
     self.assertEqual(list(Record(None, (42, 43)).keys()), [])
Example #8
0
 def test_record_values(self):
     r = Record(R_AB, (42, 43))
     vv = r.values()
     self.assertEqual(tuple(vv), (42, 43))
     self.assertTrue(repr(vv).startswith('<RecordIterator '))
Example #9
0
 def test_record_values(self):
     r = Record(R_AB, (42, 43))
     vv = r.values()
     self.assertEqual(tuple(vv), (42, 43))
     self.assertTrue(repr(vv).startswith('<RecordIterator '))
Example #10
0
 def test_record_get(self):
     r = Record(R_AB, (42, 43))
     with self.checkref(r):
         self.assertEqual(r.get('a'), 42)
         self.assertEqual(r.get('nonexistent'), None)
         self.assertEqual(r.get('nonexistent', 'default'), 'default')
Example #11
0
 def test_record_get(self):
     r = Record(R_AB, (42, 43))
     with self.checkref(r):
         self.assertEqual(r.get('a'), 42)
         self.assertEqual(r.get('nonexistent'), None)
         self.assertEqual(r.get('nonexistent', 'default'), 'default')
Example #12
0
 def test_record_keys(self):
     r = Record(R_AB, (42, 43))
     vv = r.keys()
     self.assertEqual(tuple(vv), ('a', 'b'))
     self.assertEqual(list(Record(None, (42, 43)).keys()), [])