def test_cell_data_func(self): field = AccessorField('Test', None, 'unit_index', Decimal) bool_field = AccessorField('Test', None, 'allow_fraction', bool) unit = self.create_sellable_unit() unit.unit_index = 10 search = self._create_search([field, bool_field], [unit], spec=SellableUnit) col = search.columns[0] # Note that below we are testing the properties of the renderer, and not # really the value returned renderer = Gtk.CellRendererText() search._on_cell_data_func(col, renderer, unit, '10') self.assertFalse(renderer.get_property('weight-set')) field.set_new_value(unit, 15) search._on_cell_data_func(col, renderer, unit, '10') self.assertTrue(renderer.get_property('weight-set')) self.assertEqual(renderer.get_property('weight'), Pango.Weight.BOLD) # Boolean column bool_col = search.columns[1] renderer = Gtk.CellRendererToggle() search._on_cell_data_func(bool_col, renderer, unit, None) self.assertEqual(renderer.get_property('active'), True)
def test_set_field(self): price_field = AccessorField('Test', None, 'base_price', Decimal) cost_field = AccessorField('Test', None, 'cost', Decimal) operation = MultiplyOperation(self.store, price_field, []) self.assertEqual(operation._field, price_field) operation.set_field(cost_field) self.assertEqual(operation._field, cost_field)
def test_change_operation(self): price_field = AccessorField('Test', None, 'base_price', Decimal) cost_field = AccessorField('Test', None, 'cost', Decimal) editor = DecimalEditor(self.store, price_field, [cost_field, price_field]) self.assertEqual(type(editor._oper), SetValueOperation) editor.operations_combo.select(AddOperation) self.assertEqual(type(editor._oper), AddOperation)
def test_set_field(self): price_field = AccessorField('Test', None, 'base_price', Decimal) cost_field = AccessorField('Test', None, 'cost', Decimal) editor = DecimalEditor(self.store, price_field, [cost_field, price_field]) self.assertEqual(editor._oper._field, price_field) editor.set_field(cost_field) self.assertEqual(editor._oper._field, cost_field)
def test_change_editor(self): price_field = AccessorField('Test', None, 'base_price', Decimal) cost_field = AccessorField('Test', None, 'cost', Decimal) sellable = self.create_sellable(price=10) search = self._create_search([price_field, cost_field], [sellable]) search.mass_editor.field_combo.select(price_field) field_editor = search.mass_editor._editor # Changing the field that has the same datatype should not change the # editor search.mass_editor.field_combo.select(cost_field) self.assertEqual(search.mass_editor._editor, field_editor)
def test_replace(self): sellable = self.create_sellable() sellable.description = u'foo bar foo Foo' field = AccessorField('Test', None, 'description', Decimal) operation = ReplaceOperation(self.store, field, []) operation.one_entry.set_text('foo') operation.other_entry.set_text('XXX') self.assertEqual(field.get_new_value(sellable), u'foo bar foo Foo') operation.apply_operation(sellable) self.assertEqual(field.get_new_value(sellable), u'XXX bar XXX Foo')
def test_set_date(self): sellable = self.create_sellable() old_date = datetime.date(2016, 1, 1) new_date = datetime.date(2015, 2, 2) sellable.on_sale_start_date = old_date field = AccessorField('Test', None, 'on_sale_start_date', datetime.date) operation = SetDateValueOperation(self.store, field, []) operation.entry.update(new_date) self.assertEqual(field.get_new_value(sellable).date(), old_date) operation.apply_operation(sellable) self.assertEqual(field.get_new_value(sellable), new_date)
def test_confirm_not_changed(self, warning, yesno): price_field = AccessorField('Test', None, 'base_price', Decimal) sellable = self.create_sellable(price=10) search = self._create_search([price_field], [sellable]) self.click(search.ok_button) self.assertEqual(yesno.call_count, 0) self.assertEqual(warning.call_count, 0)
def test_set(self): sellable = self.create_sellable() sellable.cost = 100 sellable.base_price = 1 price_field = AccessorField('Test', None, 'base_price', Decimal) operation = SetValueOperation(self.store, price_field, []) operation.entry.set_text('4') self.assertEqual(price_field.get_new_value(sellable), 1) operation.apply_operation(sellable) self.assertEqual(price_field.get_new_value(sellable), 4) # An invalid value should not change the object operation.entry.set_text('foo') operation.apply_operation(sellable) self.assertEqual(price_field.get_new_value(sellable), 4)
def test_accessor(self): sellable = self.create_sellable(price=10) sellable.product.ncm = u'123' field = AccessorField('Test', 'product', 'ncm', str) self.assertEqual(field.get_value(sellable), '123') field.set_new_value(sellable, u'456') field.save_value(sellable) self.assertEqual(sellable.product.ncm, '456')
def test_confirm_invalid(self, warning, yesno): price_field = AccessorField('Test', None, 'base_price', Decimal) desc_field = AccessorField('Test', None, 'description', str) sellable = self.create_sellable(price=10) search = self._create_search([price_field, desc_field], [sellable]) sellable.non_existing_attr = -20 column = search.results.get_columns()[0] search.results.emit('cell-edited', sellable, column) # Force cell editing of unicode column that needs special treatment column = search.results.get_columns()[1] search.results.emit('cell-edited', sellable, column) # Dialog should still be open yesno.return_value = False self.click(search.ok_button) yesno.return_value = True self.click(search.ok_button) args, kwargs = warning.call_args self.assertEqual(args[0], 'There was an error saving one of the values')
def test_add(self): sellable = self.create_sellable() sellable.cost = 100 sellable.base_price = 1 price_field = AccessorField('Test', None, 'base_price', Decimal) cost_field = AccessorField('Test', None, 'cost', Decimal) operation = AddOperation(self.store, price_field, [cost_field]) operation.combo.select(cost_field) operation.entry.set_text('3') self.assertEqual(price_field.get_new_value(sellable), 1) operation.apply_operation(sellable) self.assertEqual(price_field.get_new_value(sellable), 103) # An invalid value should not change the object operation.entry.set_text('foo') operation.apply_operation(sellable) self.assertEqual(price_field.get_new_value(sellable), 103)
def test_format_func(self): sellable = self.create_sellable(price=10) sellable.code = u'123' sellable.category = self.create_sellable_category(u'Categoria') field = AccessorField('Test', None, 'code', str) self.assertEqual(field.format_func(sellable), '123') sellable.code = None self.assertEqual(field.format_func(sellable), '') field = ReferenceField('Test', None, 'category', SellableCategory, 'description') self.assertEqual(field.format_func(sellable), 'Categoria') sellable.category = None self.assertEqual(field.format_func(sellable), '') # Accessor field with custom format func def format_func(data): return 'foobar' field = AccessorField('Test', None, 'code', str, format_func=format_func) self.assertEqual(field.format_func(sellable), 'foobar') sellable.code = None self.assertEqual(field.format_func(sellable), 'foobar')
def test_get_fields(self): field = AccessorField('Test', None, 'base_price', Decimal) sellable = self.create_sellable(price=10) search = self._create_search([field], [sellable]) self.assertEqual(search.get_fields(self.store), [field])
def test_methods(self): sellable = self.create_sellable(price=10) field = AccessorField('Test', None, 'base_price', Decimal) # The value is still 10 self.assertEqual(field.get_value(sellable), 10) self.assertEqual(field.get_new_value(sellable), 10) self.assertFalse(field.is_changed(sellable)) # Setting it to 10 again should not change anything field.set_new_value(sellable, 10) self.assertFalse(field.is_changed(sellable)) # Lets update it to 15 field.set_new_value(sellable, 15) # The current value of the object is still 10, but the new value is 15 self.assertEqual(sellable.price, 10) self.assertEqual(field.get_value(sellable), 10) self.assertEqual(field.get_new_value(sellable), 15) self.assertTrue(field.is_changed(sellable)) # Now lest save the value. field.save_value(sellable) self.assertEqual(sellable.price, 15)
def get_fields(self, store): # TODO: Add: status, max_discount default_fields = [ # Sellable fields AccessorField(_('Code'), 'sellable', 'code', str, unique=True), AccessorField(_('Barcode'), 'sellable', 'barcode', str, unique=True), ReferenceField(_('Category'), 'sellable', 'category', SellableCategory, 'description'), AccessorField(_('Description'), 'sellable', 'description', str), ReferenceField(_('Unit'), 'sellable', 'unit', SellableUnit, 'description', visible=False), ReferenceField(_('C.F.O.P.'), 'sellable', 'default_sale_cfop', CfopData, 'description', visible=False), # Sellable values AccessorField(_('Markup'), None, 'markup', decimal.Decimal, read_only=True, visible=True, format_func=get_formatted_percentage), AccessorField(_('Cost'), 'sellable', 'cost', currency, validator=validate_price), AccessorField(_('Default Price'), 'sellable', 'base_price', currency, validator=validate_price), AccessorField(_('On Sale Price'), 'sellable', 'on_sale_price', currency, validator=validate_price), AccessorField(_('On Sale Start Date'), 'sellable', 'on_sale_start_date', datetime.date), AccessorField(_('On Sale End Date'), 'sellable', 'on_sale_end_date', datetime.date), # Cost and price update time AccessorField(_('Need Price Update'), None, 'need_price_update', bool, read_only=True, visible=False), AccessorField(_('Cost Last Updated'), 'sellable', 'cost_last_updated', datetime.date, read_only=True, visible=False), AccessorField(_('Price Last Updated'), 'sellable', 'price_last_updated', datetime.date, read_only=True, visible=False), # Product Fields AccessorField(_('NCM'), 'product', 'ncm', str, visible=False), AccessorField(_('Location'), 'product', 'location', str, visible=False), AccessorField(_('Brand'), 'product', 'brand', str, visible=False), AccessorField(_('Family'), 'product', 'family', str, visible=False), AccessorField(_('Model'), 'product', 'model', str, visible=False), AccessorField(_('Width'), 'product', 'width', decimal.Decimal, visible=False), AccessorField(_('Height'), 'product', 'height', decimal.Decimal, visible=False), AccessorField(_('Depth'), 'product', 'depth', decimal.Decimal, visible=False), AccessorField(_('Weight'), 'product', 'weight', decimal.Decimal, visible=False), ReferenceField(_('Manufacturer'), 'product', 'manufacturer', ProductManufacturer, 'name', visible=False), # Service Fields AccessorField(_('Service List Item Code'), 'service', 'service_list_item_code', str, visible=False), AccessorField(_('City Taxation Code'), 'service', 'city_taxation_code', str, visible=False), AccessorField(_('ISS Aliquot'), 'service', 'p_iss', decimal.Decimal, visible=False), ] category_fields = [] self.categories = store.find(ClientCategory) for cat in self.categories: category_fields.append( CategoryPriceField(cat, validator=validate_price)) return default_fields + category_fields