Best Python code snippet using autotest_python
test_namedtuple.py
Source:test_namedtuple.py
...7except ImportError:8 class Value(tuple):9 def __new__(cls, *args):10 return tuple.__new__(cls, args)11 def _asdict(self):12 return {'value': self[0]}13 class Point(tuple):14 def __new__(cls, *args):15 return tuple.__new__(cls, args)16 def _asdict(self):17 return {'x': self[0], 'y': self[1]}18else:19 Value = namedtuple('Value', ['value'])20 Point = namedtuple('Point', ['x', 'y'])21class DuckValue(object):22 def __init__(self, *args):23 self.value = Value(*args)24 def _asdict(self):25 return self.value._asdict()26class DuckPoint(object):27 def __init__(self, *args):28 self.point = Point(*args)29 def _asdict(self):30 return self.point._asdict()31class DeadDuck(object):32 _asdict = None33class DeadDict(dict):34 _asdict = None35CONSTRUCTORS = [36 lambda v: v,37 lambda v: [v],38 lambda v: [{'key': v}],39]40class TestNamedTuple(unittest.TestCase):41 def test_namedtuple_dumps(self):42 for v in [Value(1), Point(1, 2), DuckValue(1), DuckPoint(1, 2)]:43 d = v._asdict()44 self.assertEqual(d, json.loads(json.dumps(v)))45 self.assertEqual(46 d,47 json.loads(json.dumps(v, namedtuple_as_object=True)))48 self.assertEqual(d, json.loads(json.dumps(v, tuple_as_array=False)))49 self.assertEqual(50 d,51 json.loads(json.dumps(v, namedtuple_as_object=True,52 tuple_as_array=False)))53 def test_namedtuple_dumps_false(self):54 for v in [Value(1), Point(1, 2)]:55 l = list(v)56 self.assertEqual(57 l,58 json.loads(json.dumps(v, namedtuple_as_object=False)))59 self.assertRaises(TypeError, json.dumps, v,60 tuple_as_array=False, namedtuple_as_object=False)61 def test_namedtuple_dump(self):62 for v in [Value(1), Point(1, 2), DuckValue(1), DuckPoint(1, 2)]:63 d = v._asdict()64 sio = StringIO()65 json.dump(v, sio)66 self.assertEqual(d, json.loads(sio.getvalue()))67 sio = StringIO()68 json.dump(v, sio, namedtuple_as_object=True)69 self.assertEqual(70 d,71 json.loads(sio.getvalue()))72 sio = StringIO()73 json.dump(v, sio, tuple_as_array=False)74 self.assertEqual(d, json.loads(sio.getvalue()))75 sio = StringIO()76 json.dump(v, sio, namedtuple_as_object=True,77 tuple_as_array=False)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!