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_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_additional_fields_in_nested_dictionary(self): # test whether a key in a nested dictionary not defined in the schema # causes a validation error. For testing whether recursion in schema # field access works properly. schema = Schema({'d': DictField({u'a': LongField()})}) schema.validate({u'd': {'a': 50L}}) self.assertRaises(ValidationError, schema.validate, {u'd': {'a': 50L, 'b': 20}})
def test_list_fields(self): schema = Schema({'l': ListField(LongField())}) # Empty lists schema.validate({u'l': []}) # list with one correctly typed element schema.validate({u'l': [700L]}) # list with one incorrectly typed element self.assertRaises(ValidationError, schema.validate, {u'l': [u'hello']})
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_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_create_with_callable_builds_dict_by_calling_function(self): global counter counter = 0 def inc(): global counter counter += 1 return counter schema = Schema({u'counter': IntField(default=inc)}) self.assertEquals({u'counter': 1}, schema.create()) self.assertEquals({u'counter': 2}, schema.create())
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_nested_dictionaries(self): # Check if nested dictionaries are handled correctly schema = Schema({'d': {u'a': LongField()}}) schema.validate({u'd': {'a': 50L}}) # double-nested ... schema = Schema({'d': {u'a': {u'b': LongField()}}}) schema.validate({u'd': {'a': {u'b': 50L}}})
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_required_fields(self): schema = Schema({u'text': UnicodeField()}) schema.validate({u'text': u'hello world'}) self.assertRaises(ValidationError, schema.validate, {u'tag': u'greeting'})
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'})
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_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_Schema_constructor_initializes_schema_member(self): s = Schema({u'a': UnicodeField()}) self.assertEquals(dict, type(s.get_schema()))
def test_optional_type_as_key(self): schema = Schema({u'text': UnicodeField(), unicode: LongField(optional=True)}) schema.validate({u'text': u'hello world'})
def test_type_as_key(self): schema = Schema({u'text': UnicodeField(), unicode: LongField()}) schema.validate({u'text': u'hello world', u'a': 200L}) self.assertRaises(ValidationError, schema.validate, {u'text': u'hello world'})
def test_nested_fields(self): schema = Schema({u'text': UnicodeField(), u'd': DictField({u'a': UnicodeField()})}) schema.validate({u'text': u'hello world', u'd': {u'a': u'bye bye world'}}) self.assertRaises(ValidationError,schema.validate, {u'text': u'hello world', u'd': {u'a': u'bye bye world', u'b': u'oops'}})
def test_empty_doc(self): schema = Schema({u'tag': UnicodeField(optional=True)}) schema.validate({})
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_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_optional_field(self): schema = Schema({u'text': UnicodeField(), u'tag': UnicodeField(optional=True)}) schema.validate({u'text': u'hello world'}) schema.validate({u'text': u'hello world', u'tag': u'greeting'})