Exemple #1
0
def test_field_param_value():
    """
    Test setting the value of the field when creating the instance to serve as a default.
    """

    field = String(value="myteststr")
    assert field.value == "myteststr"
    field.validate()
    assert field.value == "myteststr"
Exemple #2
0
def test_field_param_null():
    """Test the `null` instance attribute using `Field` subclasses."""
    string_null = String(null=True)
    string_null.validate()

    string_not_null = String()
    string_not_null.value = ""

    with pytest.raises(ValidationError):
        string_not_null.validate()
Exemple #3
0
class User(Schema):
    """
    A user type schema that contains the field definitions for the model object.
    """

    pk = Integer()
    username = String()
    profile = Profile()
Exemple #4
0
def test_field_param_name():
    """Test the `name` instance attribute."""
    field = String(null=True)
    assert field.field_name is None
    field.validate()
    assert field.field_name is None

    field = String(field_name="name", null=True)
    assert field.field_name == "name"
    field.validate()
    assert field.field_name == "name"
Exemple #5
0
class Profile(Schema):
    """
    A user type schema that contains the field definitions for the model object.
    """

    metadata = String()
Exemple #6
0
 class UserSchema(Schema):
     user_id = Integer()
     name = String()
Exemple #7
0
class MockSchema(Schema):
    mystr = String()
    mybytes = String()
    myint = Integer()
    myfloat = Float()
    mynumber = Number()
Exemple #8
0
 class User(Schema):
     pk = Integer()
     username = String()
     profile = Profile()
Exemple #9
0
 class Profile(Schema):
     metadata = String()
Exemple #10
0
def test_field_param_min_max():
    """Test the `min` and `max` instance attributes using `Field` subclasses."""
    string_min_max = String(min=1, max=10)
    string_min_max.value = "mystr"
    string_min_max.validate()

    string_min_max.value = b"mybytes"
    string_min_max.validate()

    integer_min_max = Integer(min=1, max=10)
    integer_min_max.value = 1
    integer_min_max.validate()

    float_min_max = Float(min=0.01, max=1.1)
    float_min_max.value = 1.00
    float_min_max.validate()

    number_min_max = Number(min=0.01, max=1.1)
    number_min_max.value = 1
    number_min_max.validate()

    string_min_max.value = "mystringistoolong"
    with pytest.raises(ValidationError):
        string_min_max.validate()

    integer_min_max.value = 10000
    with pytest.raises(ValidationError):
        integer_min_max.validate()

    float_min_max.value = 0.0001
    with pytest.raises(ValidationError):
        float_min_max.validate()

    number_min_max.value = 100000
    with pytest.raises(ValidationError):
        number_min_max.validate()
Exemple #11
0
def test_string_field():
    """Ensure only `bytes` or `str` types pass `String` validation."""
    myfield = String()

    myfield.value = "mystr"
    myfield.validate()

    myfield2 = String()
    myfield2.value = b"mybytes"
    myfield2.validate()

    myfield3 = String()
    myfield3.value = 1
    with pytest.raises(ValidationError):
        myfield3.validate()
Exemple #12
0
def test_field_repr():
    field = String(value="mystr", null=True, min=1, max=10)
    assert (repr(field) ==
            "<String(field_name=None, value=mystr, max=10, min=1, null=True)>")