def test_validate_fields_nested(self): self.SomeTestClass.__fields__ = { "hello": {"a": Field.required(str), "b": Field.required(int, 2), "c": Field.optional(int)} } d = self.SomeTestClass() d.hello = DotDict({"a": "s"}) d.validate_fields() self.assertEquals(d.hello.b, 2)
def test_validate_fields_nested(self): self.SomeTestClass.__fields__ = { 'hello': { 'a': Field.required(str), 'b': Field.required(int, 2), 'c': Field.optional(int) } } d = self.SomeTestClass() d.hello = DotDict({'a': 's'}) d.validate_fields() self.assertEquals(d.hello.b, 2)
def test_missing_required_field(self): self.SomeTestClass.__fields__ = { 'hello': Field.required(str, None) } d = self.SomeTestClass() self.assertRaises(KeyError, d.save)
def test_validate_fields_default(self): self.SomeTestClass.__fields__ = {"hello": Field.required(str, "world")} d = self.SomeTestClass() d.save() self.assertEqual(d.hello, "world")
def test_validate_fields_yo_dawg(self): self.SomeTestClass.__fields__ = {"hello": {"a": {"b": {"c": Field.required(int, 42)}}}} d = self.SomeTestClass() d.validate_fields() self.assertEquals(d.hello.a.b.c, 42)
def test_validate_fields_list_empty(self): self.SomeTestClass.__fields__ = {"hello": [{"a": Field.required(int, 42)}]} d = self.SomeTestClass() d.hello = [] d.validate_fields() self.assertEquals(len(d.hello), 0)
def test_validate_fields_list(self): self.SomeTestClass.__fields__ = {"hello": [{"a": Field.required(int, 42)}]} d = self.SomeTestClass() d.hello = [DotDict({"x": 42})] d.validate_fields() self.assertEquals(d.hello[0].a, 42)
def test_descriptive_errors_dict_keyerror(self): self.SomeTestClass.__fields__ = {"hello": {"a": {"b": {"c": Field.required(int)}}}} d = self.SomeTestClass() try: d.validate_fields() self.fail() except KeyError as e: try: msg = e.message except AttributeError: msg = str(*e.args) self.assertEquals(msg, ".hello.a.b.c")
def test_descriptive_errors_list_typeerror(self): self.SomeTestClass.__fields__ = {"hello": [{"a": Field.required(int)}]} d = self.SomeTestClass() d.hello = [{"a": 1}, {"a": []}] try: d.validate_fields() self.fail() except TypeError as e: try: msg = e.message except AttributeError: msg = str(*e.args) self.assertEquals(msg, ".hello[1].a")
def test_validate_fields_yo_dawg(self): self.SomeTestClass.__fields__ = { 'hello': { 'a': { 'b': { 'c': Field.required(int, 42) } } } } d = self.SomeTestClass() d.validate_fields() self.assertEquals(d.hello.a.b.c, 42)