Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_record_freelist_ok(self):
for _ in range(10000):
Record(R_A, (42,))
Record(R_AB, (42, 42,))
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()), [])
'')
# test invalid records just in case
with self.assertRaisesRegex(RuntimeError, 'invalid .* mapping'):
repr(Record(R_A, (42, 43)))
self.assertEqual(repr(Record(R_AB, (42,))), '')
class Key:
def __str__(self):
1 / 0
def __repr__(self):
1 / 0
with self.assertRaises(ZeroDivisionError):
repr(Record({Key(): 0}, (42,)))
with self.assertRaises(ZeroDivisionError):
repr(Record(R_A, (Key(),)))
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)
def test_record_cmp(self):
AB = collections.namedtuple('AB', ('a', 'b'))
r1 = Record(R_AB, (42, 43))
r2 = Record(R_AB, (42, 43))
r3 = Record(R_AB.copy(), (42, 43))
r4 = Record(R_AB.copy(), (42, 45))
r5 = Record(R_ABC, (42, 46, 57))
r6 = Record(R_AC, (42, 43))
r7 = (42, 43)
r8 = [42, 43]
r9 = AB(42, 43)
r10 = AB(42, 44)
self.assertEqual(r1, r2)
self.assertEqual(r1, r3)
self.assertEqual(r1, r6)
self.assertEqual(r1, r7)
self.assertEqual(r1, r9)
self.assertNotEqual(r1, r4)
self.assertNotEqual(r1, r10)
def test_record_immutable(self):
r = Record(R_A, (42,))
with self.assertRaisesRegex(TypeError, 'does not support item'):
r[0] = 1
def test_record_contains(self):
r = Record(R_AB, (42, 43))
self.assertIn('a', r)
self.assertIn('b', r)
self.assertNotIn('z', r)
r = Record(None, (42, 43))
self.assertNotIn('a', r)
with self.assertRaises(TypeError):
type(r).__contains__(None, 'a')
def test_record_slice(self):
r = Record(R_ABC, (1, 2, 3))
self.assertEqual(r[:], (1, 2, 3))
self.assertEqual(r[:1], (1,))
self.assertEqual(r[::-1], (3, 2, 1))
self.assertEqual(r[::-2], (3, 1))
self.assertEqual(r[1:2], (2,))
self.assertEqual(r[2:2], ())
# test invalid records just in case
with self.assertRaisesRegex(RuntimeError, 'invalid .* mapping'):
repr(Record(R_A, (42, 43)))
self.assertEqual(repr(Record(R_AB, (42,))), '')
class Key:
def __str__(self):
1 / 0
def __repr__(self):
1 / 0
with self.assertRaises(ZeroDivisionError):
repr(Record({Key(): 0}, (42,)))
with self.assertRaises(ZeroDivisionError):
repr(Record(R_A, (Key(),)))
def test_record_hash(self):
AB = collections.namedtuple('AB', ('a', 'b'))
r1 = Record(R_AB, (42, 43))
r2 = Record(R_AB, (42, 43))
r3 = Record(R_AB, (42, 45))
r4 = (42, 43)
r5 = AB(42, 43)
self.assertEqual(hash(r1), hash(r2))
self.assertNotEqual(hash(r1), hash(r3))
self.assertEqual(hash(r1), hash(r4))
self.assertEqual(hash(r1), hash(r5))
d = {}
d[r1] = 123
self.assertEqual(d[r1], 123)
self.assertIn(r2, d)
self.assertEqual(d[r2], 123)
self.assertNotIn(r3, d)