def test_formfield_callback(self): def callback(*args, **kwargs): return djangofields.FileField() fields = forms.fields_for_model(Vehicle, db, fields=("name", ), formfield_callback=callback) self.assertEqual(len(fields), 1) self.assertIsInstance(fields["name"], djangofields.FileField) with self.assertRaises(TypeError): fields = forms.fields_for_model(Vehicle, db, fields=("name", ), formfield_callback=[])
def test_labels(self): fields = forms.fields_for_model(Vehicle, db, fields=("name", ), labels={"name": "dummy"}) self.assertEqual(len(fields), 1) self.assertIsInstance(fields["name"], djangofields.CharField) self.assertEqual(fields["name"].label, "dummy")
def test_widgets(self): fields = forms.fields_for_model(Vehicle, db, fields=("name", ), widgets={"name": djangoforms.Textarea}) self.assertEqual(len(fields), 1) self.assertIsInstance(fields["name"], djangofields.CharField) self.assertIsInstance(fields["name"].widget, djangoforms.Textarea)
def test_localized_fields(self): fields = forms.fields_for_model(Vehicle, db, fields=("name", ), localized_fields=("name", )) self.assertEqual(len(fields), 1) self.assertIsInstance(fields["name"], djangofields.CharField) self.assertTrue(fields["name"].localize) fields = forms.fields_for_model( Vehicle, db, fields=("name", ), localized_fields=djangoforms.ALL_FIELDS) self.assertEqual(len(fields), 1) self.assertIsInstance(fields["name"], djangofields.CharField) self.assertTrue(fields["name"].localize)
def test_field_classes(self): fields = forms.fields_for_model( Vehicle, db, fields=("name", ), help_texts={"name": None}, field_classes={"name": djangofields.FileField}) self.assertEqual(len(fields), 1) self.assertIsInstance(fields["name"], djangofields.FileField)
def test_error_messages(self): fields = forms.fields_for_model( Vehicle, db, fields=("name", ), error_messages={"name": { "required": "dummy" }}) self.assertEqual(len(fields), 1) self.assertIsInstance(fields["name"], djangofields.CharField) self.assertEqual(fields["name"].error_messages, {"required": "dummy"})
def test_exclude(self): fields = forms.fields_for_model(Vehicle, db, exclude=("name", )) checks = [ ("type", sorceryfields.EnumField), ("created_at", djangofields.DateTimeField), ("paint", djangofields.TypedChoiceField), ("is_used", djangofields.BooleanField), ("msrp", djangofields.DecimalField), ("owner", sorceryfields.ModelChoiceField), ("parts", sorceryfields.ModelMultipleChoiceField), ("options", sorceryfields.ModelMultipleChoiceField), ] self.assertEqual(len(fields), len(checks)) for name, fieldtype in checks: self.assertIsInstance(fields[name], fieldtype)
def test_fields(self): fields = forms.fields_for_model(Vehicle, db, fields=("name", )) checks = [("name", djangofields.CharField)] self.assertEqual(len(fields), len(checks)) for name, fieldtype in checks: self.assertIsInstance(fields[name], fieldtype)