def test_listtype_validates_the_basics(): list_of_strings = types.ListType(types.StringType()) list_of_strings.validate(['list', 'of', 'strings']) list_of_strings.validate([]) list_of_strings.validate(None) list_of_strings.validate(types.Unset)
def test_stringtype_check_required(): st = types.StringType(required=True) st.validate("This is a string") st.validate("") st.validate(None) with pytest.raises(exceptions.ValidationException): st.validate(types.Unset)
def test_stringtype_validates_the_basics(): st = types.StringType() # Accepted st.validate(types.Unset) st.validate(None) st.validate("This is a string") st.validate("") st.validate(1) st.validate(3.14)
def test_listtype_item_type_is_required(): list_of_strings = types.ListType(types.StringType(required=True)) list_of_strings.validate(['list', 'of', 'strings']) list_of_strings.validate([]) list_of_strings.validate(None) list_of_strings.validate(types.Unset) with pytest.raises(exceptions.ValidationException): list_of_strings.validate([types.Unset])
def test_listtype_validates_max_size(): list_of_strings = types.ListType(types.StringType(), max_length=3) list_of_strings.validate(['list', 'of', 'strings']) list_of_strings.validate([]) list_of_strings.validate(None) list_of_strings.validate(types.Unset) with pytest.raises(exceptions.ValidationException): list_of_strings.validate(['li', 'st', 'of', 'strings'])
def test_stringtype_max_length(): st = types.StringType(max_length=3) st.validate("hi") st.validate("") st.validate(None) st.validate(types.Unset) with pytest.raises(exceptions.ValidationException): st.validate("hello")
def test_stringtype_check_strict(): st = types.StringType(strict=True) # Accepted st.validate(types.Unset) st.validate(None) st.validate("This is a string") st.validate("") with pytest.raises(exceptions.ValidationException): st.validate(1) with pytest.raises(exceptions.ValidationException): st.validate(3.14)
def test_sumtype_check_required(): st = types.SumType(types.StringType(strict=True), required=True) st.validate("") st.validate(None) with pytest.raises(exceptions.ValidationException): st.validate(types.Unset) with pytest.raises(exceptions.ValidationException): st.validate(1) with pytest.raises(exceptions.ValidationException): st.validate(3.14)
def test_sumtype_validates_the_basics(): big_strings = types.StringType(min_length=7) small_strings = types.StringType(max_length=3) big_or_small_strings = types.SumType(big_strings, small_strings) with pytest.raises(exceptions.ValidationException): big_strings.validate('li') with pytest.raises(exceptions.ValidationException): big_strings.validate('st') big_strings.validate('of strings') small_strings.validate('li') small_strings.validate('st') with pytest.raises(exceptions.ValidationException): small_strings.validate('of strings') big_or_small_strings.validate('li') big_or_small_strings.validate('st') big_or_small_strings.validate('of strings') big_or_small_strings.validate(None) big_or_small_strings.validate(types.Unset)
def test_stringtype_min_length_and_required(): st = types.StringType(min_length=3, required=True) st.validate("hello") with pytest.raises(exceptions.ValidationException): st.validate(None) with pytest.raises(exceptions.ValidationException): st.validate(types.Unset) with pytest.raises(exceptions.ValidationException): st.validate("") with pytest.raises(exceptions.ValidationException): st.validate("hi")
def test_type_list_conversion(): lt = types.ListType(types.StringType(min_length=5)) lt_data = ['a', 'b', 'meow'] assert lt.to_primitive(lt_data) == lt_data assert lt.to_native(lt_data) == lt_data lt_data = ['data', 4] assert lt.to_primitive(lt_data) != lt_data assert lt.to_primitive(lt_data)[1] == '4' assert lt.to_native(lt_data) != lt_data assert lt.to_native(lt_data)[1] == '4'
class ArtistType(types.Record): name = types.StringType() epoch = types.StringType() albums = types.ListType(AlbumType())
class TestType(types.Type): s = types.StringType() b = types.BooleanType()
class TestRecord(types.Record): s1 = types.StringType(required=True) s2 = types.StringType()
class TestRecord(types.Record): s1 = types.StringType()
class SongRecord(types.Record): name = types.StringType(required=True) author = types.StringType() created_at = types.DateTimeType()
class SongRecord(types.Record): name = types.StringType(required=True) author = types.StringType(required=True) instruments = types.ListType(types.StringType())
class TestRecord(types.Record): s1 = types.StringType(default='foo')
class AlbumType(types.Record): name = types.StringType() songs = types.ListType(SongType())
class Artist(types.Record): name = types.StringType(required=True) created_at = types.DateTimeType() website = types.URLType() albums = types.ListType(Album())
class Album(types.Record): name = types.StringType(required=True) created_at = types.DateTimeType() songs = types.ListType(Song())
class Song(types.Record): name = types.StringType(required=True) created_at = types.DateTimeType() file = types.UnixPathType() lyrics = types.StringType(max_length=255)
class MusicianRecord(types.Record): name = types.StringType(required=True)
class SongRecord(types.Record): name = types.StringType(required=True) author = types.StringType(required=True)
class SongType(types.Record): name = types.StringType(required=True)
class TestType(types.Type): st = types.SumType(types.StringType(min_length=10), types.BooleanType()) b = types.BooleanType()