@@ -196,7 +196,6 @@ class Foo(EIP712Struct):
196196 foo_copy = Foo (s = 'hello world' )
197197 foo_2 = Foo (s = 'blah' )
198198
199-
200199 assert foo != None
201200 assert foo != 'unrelated type'
202201 assert foo == foo
@@ -231,3 +230,59 @@ class Bar(EIP712Struct):
231230 s = String ()
232231 bar = Bar (s = 'hello world' )
233232 assert bar != foo
233+
234+
235+ def test_value_access ():
236+ class Foo (EIP712Struct ):
237+ s = String ()
238+ b = Bytes (32 )
239+
240+ test_str = 'hello world'
241+ test_bytes = os .urandom (32 )
242+ foo = Foo (s = test_str , b = test_bytes )
243+
244+ assert foo ['s' ] == test_str
245+ assert foo ['b' ] == test_bytes
246+
247+ test_bytes_2 = os .urandom (32 )
248+ foo ['b' ] = test_bytes_2
249+
250+ assert foo ['b' ] == test_bytes_2
251+
252+ with pytest .raises (KeyError ):
253+ foo ['x' ] = 'unacceptable'
254+
255+ # Check behavior when accessing a member that wasn't defined for the struct.
256+ with pytest .raises (KeyError ):
257+ foo ['x' ]
258+ # Lets cheat a lil bit for robustness- add an invalid 'x' member to the value dict, and check the error still raises
259+ foo .values ['x' ] = 'test'
260+ with pytest .raises (KeyError ):
261+ foo ['x' ]
262+ foo .values .pop ('x' )
263+
264+ with pytest .raises (ValueError ):
265+ foo ['s' ] = b'unacceptable'
266+ with pytest .raises (ValueError ):
267+ # Bytes do accept strings, but it has to be hex formatted.
268+ foo ['b' ] = 'unacceptable'
269+
270+ # Test behavior when attempting to set nested structs as values
271+ class Bar (EIP712Struct ):
272+ s = String ()
273+ f = Foo
274+
275+ class Baz (EIP712Struct ):
276+ s = String ()
277+ baz = Baz (s = test_str )
278+
279+ bar = Bar (s = test_str )
280+ bar ['f' ] = foo
281+ assert bar ['f' ] == foo
282+
283+ with pytest .raises (ValueError ):
284+ # Expects a Foo type, so should throw an error
285+ bar ['f' ] = baz
286+
287+ with pytest .raises (TypeError ):
288+ del foo ['s' ]
0 commit comments