示例#1
0
class NoteRestSchema(Schema):

    schema = {
        'text': UnicodeField(),
        'created_at': UnicodeField(),
        'updated_at': UnicodeField(),
        'links': {
            'self': UnicodeField()
        }
    }
示例#2
0
    def test_create_with_defaults(self):
        schema = Schema({u'a': UnicodeField(default=u'hello world')})
        self.assertEquals({u'a': u'hello world'}, schema.create())

        # test creating instance with given default value
        schema = Schema({
            u'a': UnicodeField(default=u'hello world'),
            u'b': UnicodeField(default=u'hello good bye')
        })
        self.assertEquals({
            u'a': u'hello venus',
            u'b': u'hello good bye'
        }, schema.create({u'a': u'hello venus'}))
示例#3
0
    def test_DictField_validates_value_matching_pattern(self):
        field = DictField({u'a': DictField({u'b': UnicodeField()})})

        try:
            field.validate({u'a': {u'b': u'hello world'}})
        except ValidationError:
            self.fail('DictField.validate raised ValidationError unexpectedly')
示例#4
0
 def test_json_converter(self):
     schema = Schema({u'a': UnicodeField(), u'b': DatetimeField()})
     json_converter = JsonConverter(schema)
     result_doc = json_converter.from_schema({
         u'a':
         u'x',
         u'b':
         datetime.datetime(2011, 9, 11, 17, 29, 0)
     })
     self.assertEquals({'a': 'x', 'b': '2011-09-11T17:29:00Z'}, result_doc)
     self.assertEquals({
         'a': 'x',
         'b': '2011-09-11T17:29:00Z'
     },
                       json_converter.from_schema({
                           u'a':
                           u'x',
                           u'b':
                           datetime.datetime(2011, 9, 11, 17, 29, 0)
                       }))
     self.assertTrue(isinstance(result_doc.keys()[0], str))
     self.assertTrue(isinstance(result_doc.keys()[1], str))
     self.assertTrue(isinstance(result_doc['a'], str))
     self.assertEquals(
         {
             u'a': u'x',
             u'b': datetime.datetime(2011, 9, 11, 17, 29, 0)
         },
         json_converter.to_schema({
             'a': u'x',
             'b': '2011-09-11T17:29:00Z'
         }))
示例#5
0
    def test_Schema_is_partially_valid_returns_whether_value_is_partially_valid(
            self):
        schema = Schema({u'a': {u'b': UnicodeField()}})

        self.assertFalse(schema.is_partially_valid({u'a': {u'c': 42}}))
        self.assertTrue(schema.is_partially_valid({u'a': {}}))
        self.assertFalse(schema.is_partially_valid({u'a': {u'b': 42}}))
示例#6
0
    def test_Schema_with_type_key_validates_value_matching_pattern(self):
        schema = Schema({unicode: UnicodeField()})

        try:
            schema.validate({u'a': u'hello world'})
        except ValidationError:
            self.fail('Schema.validate() raised ValidationError unexpectedly')
示例#7
0
    def test_ListField_validates_list_values_with_given_type(self):
        field = ListField(UnicodeField())

        try:
            field.validate([])
            field.validate([u'hello moon', u'hello world'])
        except ValidationError:
            self.fail('ListField raised ValidationError unexpectedly')
示例#8
0
    def test_UnicodeField_validates_when_value_matches_pattern(self):
        field = UnicodeField(match=re.compile(ur'hello .+'))

        try:
            field.validate(u'hello world')
        except ValidationError:
            self.fail(
                'UnicodeField.validate raised ValidationError unexpectedly')
示例#9
0
    def test_UnicodeField_validates_when_length_is_equal_length(self):
        field = UnicodeField(length=11)

        try:
            field.validate(u'hello world')
        except ValidationError:
            self.fail(
                'UnicodeField.validate raised ValidationError unexpectedly')
示例#10
0
    def test_create_empty_doc(self):
        # Empty schema
        schema = Schema({})
        self.assertEquals({}, schema.create())

        # Schema with only optional fields
        schema = Schema({u'a': UnicodeField(optional=True)})
        self.assertEquals({}, schema.create())
示例#11
0
    def test_UnicodeField_validates_None_if_value_can_be_None(self):
        field = UnicodeField(can_be_none=True)

        try:
            field.validate(u'hello world')
            field.validate(None)
        except ValidationError:
            self.fail(
                'UnicodeField.validate raised ValidationError unexpectedly')
示例#12
0
    def test_DictField_validates_None_value_if_value_can_be_None(self):
        field = DictField(
            {u'a': DictField({u'b': UnicodeField()}, can_be_none=True)})

        try:
            field.validate({u'a': None})
        except ValidationError:
            self.fail(
                'DictField.validate() raised ValidationError unexpectedly')
示例#13
0
    def test_UnicodeField_validates_unicode_values_correctly(self):
        field = UnicodeField()

        try:
            field.validate(u'')
            field.validate(u'hello world')
        except ValidationError:
            self.fail(
                'UnicodeField.validate() raised ValidationError unexpectedly')
示例#14
0
    def test_UnicodeField_validates_when_value_length_is_between_min_and_max_length(
            self):
        field = UnicodeField(min_len=5, max_len=8)

        try:
            field.validate(u'hello')
            field.validate(u'12345678')
        except ValidationError:
            self.fail(
                'UnicodeField.validate() raised ValidationError unexpectedly')
示例#15
0
class NoteSchema(Schema):

    schema = {
        '_id': UuidField(),
        'created_at': DatetimeField(),
        'updated_at': DatetimeField(can_be_none=True),
        'text': UnicodeField(),
    }

    def __init__(self):
        super(NoteSchema, self).__init__()
示例#16
0
 def test_UnicodeField_raises_ValidationError_on_incorrect_type(self):
     field = UnicodeField()
     self.assertRaises(ValidationError, field.validate,
                       'hello world')  # n.b.: byte string
     self.assertRaises(ValidationError, field.validate, 42)
示例#17
0
    def test_UnicodeField_to_json_converts_unicode_string_to_byte_string(self):
        f = UnicodeField()

        self.assertEquals('we are motörhead', f.to_json(u'we are motörhead'))
        self.assertEquals(str, type(f.to_json(u'we are motörhead')))
示例#18
0
    def test_create_builds_nested_dict_with_default_values(self):
        schema = Schema({u'a': {u'b': UnicodeField(default=u'hello world')}})

        self.assertEquals({u'a': {u'b': u'hello world'}}, schema.create())
示例#19
0
    def test_create_with_type_key_builds_dict_with_default_values(self):
        schema = Schema({u'a': UnicodeField(default=u'hello world'), unicode: UnicodeField(default=u'aarrgghh')})

        self.assertEquals({u'a': u'hello world'}, schema.create())
        self.assertEquals({u'a': u'hello world', u'b': u'world'}, schema.create({u'b': u'world'}))
示例#20
0
    def test_Schema_constructor_initializes_schema_member(self):
        s = Schema({u'a': UnicodeField()})

        self.assertEquals(dict, type(s.get_schema()))
示例#21
0
 def test_FieldField_validates_Field_type(self):
     field = FieldField()
     field.validate(UnicodeField())
示例#22
0
    def test_Schema_with_type_key_raises_ValidationError_on_non_matching_key(
            self):
        schema = Schema({unicode: UnicodeField()})

        self.assertRaises(ValidationError, schema.validate, {42: 'foobar'})
示例#23
0
 def test_UnicodeField_validates_when_length_is_unequal_length(self):
     field = UnicodeField(length=11)
     self.assertRaises(ValidationError, field.validate, u'bye')
示例#24
0
 def test_Schema_validates_value_matching_pattern(self):
     schema = Schema({u'a': {u'b': UnicodeField()}})
     schema.validate({u'a': {u'b': u'hello world'}})
示例#25
0
    def test_UnicodeField_raises_ValidationError_when_length_is_outside_allowed_range(
            self):
        field = UnicodeField(min_len=5, max_len=8)

        self.assertRaises(ValidationError, field.validate, u'1234')
        self.assertRaises(ValidationError, field.validate, u'123456789')
示例#26
0
    def test_UnicodeField_from_json_converts_byte_string_to_unicode_string(
            self):
        f = UnicodeField()

        self.assertEquals(u'we are motörhead', f.from_json('we are motörhead'))
示例#27
0
    def test_ListField_raises_ValidationError_on_incorrect_list_element_type(
            self):
        field = ListField(UnicodeField())

        self.assertRaises(ValidationError, field.validate, u'just a string')
        self.assertRaises(ValidationError, field.validate, [u'hello', 42])
示例#28
0
    def test_UnicodeField_raises_ValidationError_when_value_does_not_match_pattern(
            self):
        field = UnicodeField(match=re.compile(ur'hello .+'))

        self.assertRaises(ValidationError, field.validate, u'bye world')
示例#29
0
 def test_create_nested(self):
     schema = Schema({u'a': {u'b': UnicodeField(default=u'hello world')}})
     self.assertEquals({u'a': {u'b': u'hello world'}}, schema.create())
示例#30
0
    def test_DictField_raises_ValidationError_on_incorrect_type(self):
        field = DictField({u'a': DictField({u'b': UnicodeField()})})

        self.assertRaises(ValidationError, field.validate, None)