def test_extend(self): field = opengui.Field(name="a", fields=[]) field.extend([ {"name": "b"}, {"name": "c"} ]) self.assertEqual(len(field.fields), 2) self.assertEqual(field.fields[0].name, "b") self.assertEqual(field.fields[1].name, "c")
def test_append(self): field = opengui.Field(name="a", value={"b": 1}, original={"b": 2}, fields=[]) field.append({"name": "b", "label": "B"}) self.assertEqual(field.fields[0].name, "b") self.assertEqual(field.fields[0].value, 1) self.assertEqual(field.fields[0].original, 2) self.assertEqual(field.fields[0].content, {"label": "B"})
def test_to_dict(self): field = opengui.Field("unit") self.assertEqual(field.to_dict(), { "name": "unit" }) field = opengui.Field( "unit", value="test", original="recipe", default="factory", options="family", optional="pants", multi="functional", trigger="ed", readonly="yes", content={"name": "nope", "label": "yep"}, errors="whoops", fields=[{"name": "a"}] ) self.assertEqual(field.to_dict(), { "name": "unit", "value": "test", "original": "recipe", "default": "factory", "options": "family", "optional": "pants", "multi": "functional", "trigger": "ed", "readonly": "yes", "label": "yep", "errors": "whoops", "fields": [{"name": "a"}] })
def test____getitem__(self): field = opengui.Field(name="a", fields=[{ "name": "b" }, { "name": "c", "fields": [{ "name": "d" }] }]) self.assertEqual(field[0].name, "b") self.assertEqual(field[1][0].name, "d") self.assertEqual(field["c"].name, "c") self.assertEqual(field["c"]["d"].name, "d")
def test____len__(self): field = opengui.Field(name="a", fields=[{"name": "a"}, {"name": "b"}]) self.assertEqual(len(field), 2)
def test___iter__(self): field = opengui.Field(name="a", fields=[{"name": "b"}, {"name": "c"}]) self.assertEqual([subfield.name for subfield in field], ["b", "c"])
def test___getattr__(self): field = opengui.Field("a", value="b", original="c") self.assertEqual(field.values, "b") self.assertEqual(field.originals, "c")
def test_validate(self): field = opengui.Field(name="a") self.assertFalse(field.validate()) self.assertEqual(field.errors, ["missing value"]) field = opengui.Field(name="a", multi=True) self.assertFalse(field.validate()) self.assertEqual(field.errors, ["missing value"]) field = opengui.Field(name="a", multi=True, value=0) self.assertFalse(field.validate()) self.assertEqual(field.errors, ["multi requires list"]) field = opengui.Field(name="b", default=2) self.assertTrue(field.validate()) self.assertEqual(field.value, 2) field = opengui.Field(name="b", readonly=True, original=2) self.assertTrue(field.validate()) self.assertEqual(field.value, 2) field = opengui.Field(name="b", readonly=True) self.assertTrue(field.validate()) field = opengui.Field(name="c", options=[1,2]) field.value = 0 self.assertFalse(field.validate()) self.assertEqual(field.errors, ["invalid value '0'"]) field = opengui.Field(name="c", options=[1,2], multi=True) field.value = [0] self.assertFalse(field.validate()) self.assertEqual(field.errors, ["invalid values [0]"]) field = opengui.Field(name="c", options=[1,2], optional=True) self.assertTrue(field.validate()) field = opengui.Field(name="d", options=[1,2]) field.value = 1 self.assertTrue(field.validate()) field = opengui.Field(name="e", fields=[ {"name": "f"}, {"name": "g"} ]) self.assertFalse(field.validate()) self.assertEqual(field["f"].errors, ["missing value"]) self.assertEqual(field["g"].errors, ["missing value"]) field = opengui.Field(name="h", validation="yep") field.value = "yepyep" self.assertTrue(field.validate()) field.value = "nope" self.assertFalse(field.validate()) self.assertEqual(field.errors, ["must match 'yep'"]) def sure(field): if field.value != "sure": field.errors.append("not sure") field = opengui.Field(name="i", validation=sure) field.value = "sure" self.assertTrue(field.validate()) field.value = "nope" self.assertFalse(field.validate()) self.assertEqual(field.errors, ["not sure"])