def test_list_dict_data(self): data = [{'a': i, 'b': 10 * i, 'c': str(i)} for i in range(10)] model = RowTableDataModel(data=data, row_header_data=KeyDataAccessor( key='a', value_type=IntValue(), ), column_data=[ KeyDataAccessor( key='b', value_type=IntValue(), ), KeyDataAccessor( key='c', value_type=TextValue(), ) ]) for row, column in model.iter_items(): with self.subTest(row=row, column=column): result = model.get_value(row, column) if len(row) == 0 and len(column) == 0: self.assertEqual(result, 'A') elif len(row) == 0: key = model.column_data[column[0]].key self.assertEqual(result, str(key).title()) elif len(column) == 0: self.assertEqual(result, data[row[0]]['a']) else: key = model.column_data[column[0]].key self.assertEqual(result, data[row[0]][key])
def setUp(self): super().setUp() self.data = [ DataItem(a=i, b=10*i, c=str(i)) for i in range(10) ] self.model = RowTableDataModel( data=self.data, row_header_data=AttributeDataAccessor( attr='a', value_type=IntValue(), ), column_data=[ AttributeDataAccessor( attr='b', value_type=IntValue(), ), AttributeDataAccessor( attr='c', value_type=TextValue(), ) ] ) self.values_changed_event = None self.structure_changed_event = None self.model.observe(self.model_values_changed, 'values_changed') self.model.observe(self.model_structure_changed, 'structure_changed')
def test_list_tuple_data(self): data = [(i, 10 * i, str(i)) for i in range(10)] model = RowTableDataModel(data=data, row_header_data=IndexDataAccessor( index=0, value_type=IntValue(), ), column_data=[ IndexDataAccessor( index=1, value_type=IntValue(), ), IndexDataAccessor( index=2, value_type=TextValue(), ) ]) for row, column in model.iter_items(): with self.subTest(row=row, column=column): result = model.get_value(row, column) if len(row) == 0 and len(column) == 0: self.assertEqual(result, '0') elif len(row) == 0: index = model.column_data[column[0]].index self.assertEqual(result, str(index)) elif len(column) == 0: self.assertEqual(result, row[0]) else: index = model.column_data[column[0]].index self.assertEqual(result, data[row[0]][index])
def test_value_type_changed(self): accessor = self.create_accessor() accessor.observe(self.accessor_observer, 'updated') with self.assertTraitChanges(accessor, 'updated', count=1): accessor.value_type = TextValue() self.assertEqual(self.accessor_event.new, (accessor, 'value'))
def test_column_data_updated(self): with self.assertTraitChanges(self.model, "structure_changed"): self.model.column_data = [ AttributeDataAccessor( attr='c', value_type=TextValue(), ), AttributeDataAccessor( attr='b', value_type=IntValue(), ), ] self.assertTrue(self.structure_changed_event.new)
def test_no_data_column_data_update(self): model = RowTableDataModel(row_header_data=AttributeDataAccessor( attr='a', value_type=IntValue(), ), column_data=[ AttributeDataAccessor( attr='b', value_type=IntValue(), ), AttributeDataAccessor( attr='c', value_type=TextValue(), ) ]) with self.assertTraitDoesNotChange(model, 'values_changed'): model.column_data[0].attr = 'a'
def test_no_data_row_header_data_update(self): model = RowTableDataModel(row_header_data=AttributeDataAccessor( attr='a', value_type=IntValue(), ), column_data=[ AttributeDataAccessor( attr='b', value_type=IntValue(), ), AttributeDataAccessor( attr='c', value_type=TextValue(), ) ]) # check that updating accessors is safe with empty data with self.assertTraitDoesNotChange(model, 'values_changed'): model.row_header_data.attr = 'b'
def get_value_type(self, row, column): return TextValue(is_editable=False)
def create_accessor(self): return ConstantDataAccessor( title='Test', value='test', value_type=TextValue(), )
def create_accessor(self): return KeyDataAccessor( key='one', value_type=TextValue(), )
def create_accessor(self): return IndexDataAccessor( index=1, value_type=TextValue(), )
def create_accessor(self): return AttributeDataAccessor( attr='attr_value', value_type=TextValue(), )
def _value_type_default(self): return TextValue(is_editable=False)
name = Str() age = Int() favorite_color = PyfaceColor() contacted = Bool() address = Instance(Address, ()) row_header_data = AttributeDataAccessor( title='People', attr='name', value_type=TextValue(), ) column_data = [ AttributeDataAccessor( attr="age", value_type=IntValue(minimum=0), ), AttributeDataAccessor( attr="favorite_color", value_type=ColorValue(), ), AttributeDataAccessor( attr="contacted", value_type=BoolValue(), ),