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')
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')
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')
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')
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')
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')
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')
class NoteRestSchema(Schema): schema = { 'text': UnicodeField(), 'created_at': UnicodeField(), 'updated_at': UnicodeField(), 'links': { 'self': UnicodeField() } }
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'}))
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}}))
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' }))
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')
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')
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')
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())
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')
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__()
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')
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')
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')
def test_DictField_raises_ValidationError_on_incorrect_type(self): field = DictField({u'a': DictField({u'b': UnicodeField()})}) self.assertRaises(ValidationError, field.validate, None)
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])
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")))
def test_Schema_validates_value_matching_pattern(self): schema = Schema({u'a': {u'b': UnicodeField()}}) schema.validate({u'a': {u'b': u'hello world'}})
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())
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"))
def test_Schema_constructor_initializes_schema_member(self): s = Schema({u'a': UnicodeField()}) self.assertEquals(dict, type(s.get_schema()))
def test_FieldField_validates_Field_type(self): field = FieldField() field.validate(UnicodeField())
def test_UnicodeField_validates_when_length_is_unequal_length(self): field = UnicodeField(length=11) self.assertRaises(ValidationError, field.validate, u'bye')
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)
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'))
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')
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')
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')))
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'}))
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())
def test_Schema_with_type_key_raises_ValidationError_on_non_matching_key( self): schema = Schema({unicode: UnicodeField()}) self.assertRaises(ValidationError, schema.validate, {42: 'foobar'})