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)
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())
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()), [])