@@ -448,6 +448,64 @@ class NotADuck:
448
448
with pytest .raises (AttributeError , match = "'NotADuck' object has no attribute" ):
449
449
self .table_class ().append (NotADuck ())
450
450
451
+ def test_setitem (self ):
452
+ table = self .table_class ()
453
+ for row in self .make_transposed_input_data (10 ):
454
+ table .append (table .row_class (** row ))
455
+ table2 = self .table_class ()
456
+ for row in self .make_transposed_input_data (20 )[10 :]:
457
+ table2 .append (table .row_class (** row ))
458
+ assert table != table2
459
+
460
+ copy = table .copy ()
461
+ for j in range (10 ):
462
+ table [j ] = table [j ]
463
+ table .assert_equals (copy )
464
+
465
+ for j in range (10 ):
466
+ table [j ] = table2 [j ]
467
+ table .assert_equals (table2 )
468
+
469
+ def test_setitem_duck_type (self ):
470
+ class Duck :
471
+ pass
472
+
473
+ table = self .table_class ()
474
+ for row in self .make_transposed_input_data (10 ):
475
+ table .append (table .row_class (** row ))
476
+ table2 = self .table_class ()
477
+ for row in self .make_transposed_input_data (20 )[10 :]:
478
+ table2 .append (table .row_class (** row ))
479
+ assert table != table2
480
+
481
+ for j in range (10 ):
482
+ duck = Duck ()
483
+ for k , v in dataclasses .asdict (table2 [j ]).items ():
484
+ setattr (duck , k , v )
485
+ table [j ] = duck
486
+ table .assert_equals (table2 )
487
+
488
+ def test_setitem_error (self ):
489
+ class NotADuck :
490
+ pass
491
+
492
+ table = self .table_class ()
493
+ table .append (table .row_class (** self .make_transposed_input_data (1 )[0 ]))
494
+ with pytest .raises (AttributeError , match = "'NotADuck' object has no attribute" ):
495
+ table [0 ] = NotADuck ()
496
+
497
+ with pytest .raises (IndexError , match = "Index out of bounds" ):
498
+ self .table_class ()[0 ] = table [0 ]
499
+ with pytest .raises (IndexError , match = "Index out of bounds" ):
500
+ self .table_class ()[- 1 ] = table [0 ]
501
+
502
+ with pytest .raises (TypeError , match = "Index must be integer" ):
503
+ self .table_class ()[0.5 ] = table [0 ]
504
+ with pytest .raises (TypeError , match = "Index must be integer" ):
505
+ self .table_class ()[None ] = table [0 ]
506
+ with pytest .raises (TypeError , match = "Index must be integer" ):
507
+ self .table_class ()[[1 ]] = table [0 ]
508
+
451
509
def test_set_columns_data (self ):
452
510
for num_rows in [0 , 10 , 100 , 1000 ]:
453
511
input_data = {col .name : col .get_input (num_rows ) for col in self .columns }
@@ -1347,12 +1405,6 @@ def test_bad_indexes(self, table):
1347
1405
with pytest .raises (TypeError , match = "not supported between instances" ):
1348
1406
table ["foobar" ]
1349
1407
1350
- def test_not_writable (self , table ):
1351
- with pytest .raises (TypeError , match = "object does not support item assignment" ):
1352
- table [5 ] = 5
1353
- with pytest .raises (TypeError , match = "object does not support item assignment" ):
1354
- table [[5 ]] = 5
1355
-
1356
1408
1357
1409
common_tests = [
1358
1410
CommonTestsMixin ,
@@ -4650,3 +4702,14 @@ def test_with_mutation_times(self):
4650
4702
tables .compute_mutation_times ()
4651
4703
ts = tables .tree_sequence ()
4652
4704
self .verify_subset_union (ts )
4705
+
4706
+
4707
+ class TestTableSetitemMetadata :
4708
+ @pytest .mark .parametrize ("table_name" , tskit .TABLE_NAMES )
4709
+ def test_setitem_metadata (self , ts_fixture , table_name ):
4710
+ table = getattr (ts_fixture .tables , table_name )
4711
+ if hasattr (table , "metadata_schema" ):
4712
+ assert table .metadata_schema == tskit .MetadataSchema ({"codec" : "json" })
4713
+ assert table [0 ].metadata != table [1 ].metadata
4714
+ table [0 ] = table [1 ]
4715
+ assert table [0 ] == table [1 ]
0 commit comments