def set_value_widget(self): """add custom widget for boolean values (after form has been initialized)""" # temp store attributes so they can be restored to reset widget value_fields = [ "value", "string_value", "date_value", "datetime_value" ] widget_attrs = {} for f in value_fields: self.fields[f].widget.attrs["class"] = "qa-input" widget_attrs[f] = self.fields[f].widget.attrs test_type = self.unit_test_info.test.type if test_type == models.BOOLEAN: self.fields["value"].widget = RadioSelect(choices=BOOL_CHOICES) elif test_type == models.CONSTANT: test = self.unit_test_info.test formatted = format_qc_value(test.constant_value, test.formatting) self.fields["value"].widget.attrs['title'] = _( 'Actual value = %(constant_value)s') % { 'constant_value': test.constant_value } self.fields["value"].widget.attrs['data-formatted'] = formatted elif test_type == models.MULTIPLE_CHOICE: self.fields["string_value"].widget = Select( choices=[("", "")] + self.unit_test_info.test.get_choices()) elif test_type == models.UPLOAD: self.fields["string_value"].widget = HiddenInput() self.fields["json_value"].widget = HiddenInput() elif test_type == models.COMPOSITE: self.fields["value"].widget = Input() if getattr(self, "instance", None): test = self.unit_test_info.test formatted = format_qc_value(self.instance.value, test.formatting) self.fields["value"].widget.attrs['data-formatted'] = formatted elif test_type in models.STRING_TYPES: self.fields['string_value'].widget = Input({'maxlength': 20000}) else: attrs = {"step": "any"} if test_type == models.WRAPAROUND: attrs['max'] = self.unit_test_info.test.wrap_high attrs['min'] = self.unit_test_info.test.wrap_low self.fields["value"].widget = NumberInput(attrs=attrs) if test_type in (models.BOOLEAN, models.MULTIPLE_CHOICE): if hasattr(self, "instance") and self.instance.value is not None: self.initial["value"] = int(self.instance.value) for f in value_fields: self.fields[f].widget.attrs.update(widget_attrs[f])
def test_default_format_old(self): assert qautils.format_qc_value(1, None) == qautils.format_qc_value( 1, "{:.3f}")
def test_invalid_default_fallback(self): assert qautils.format_qc_value(1, None) == qautils.to_precision( 1, settings.CONSTANT_PRECISION)
def test_non_numerical_val(self): assert qautils.format_qc_value(None, "%d") == "None"
def test_default_format_new(self): assert qautils.format_qc_value(1, "") == qautils.format_qc_value( 1, "{:.3f}")
def test_invalid_format(self): assert qautils.format_qc_value(1, "{:foo}") == qautils.to_precision( 1, settings.CONSTANT_PRECISION)
def test_new_style(self): assert qautils.format_qc_value(1, "{:.3f}") == "1.000"
def test_old_style(self): assert qautils.format_qc_value(1, "%.3f") == "1.000"
def test_empty_format(self): assert qautils.format_qc_value(1, "") == "1.0"
def test_null_format(self): assert qautils.format_qc_value(1, None) == "1.0"