def test_update_output_to_name_with_dict(): data = {'name': 'mike', 'test': 'true', 'nested': {'foo': 'bar'}} output = {} field = Field(name='name', required=True) session = Session(field, data, output) session.data = data['name'] update_output_to_name(session) assert output == {'name': 'mike'}
def test_get_data_from_source_pipe_dot_syntax(): data = {'user': {'name': 'mike'}} output = {} field = Field(source='foo.bar') session = Session(field, data, output) assert get_data_from_source(session) is None field = Field(source='user.name') session = Session(field, data, output) assert get_data_from_source(session) == 'mike'
def test_get_data_from_source_pipe(): data = {'name': 'mike'} output = {} field = Field(source='foo') session = Session(field, data, output) assert get_data_from_source(session) is None field = Field(source='name') session = Session(field, data, output) assert get_data_from_source(session) == 'mike'
def test_update_output_to_source_with_object_dot_notiation(): class MyObject(object): pass output = MyObject() output.nested = MyObject() field = Field(source='nested.source', required=True) session = Session(field, {'name': 'mike'}, output) session.data = 'mike' update_output_to_source(session) assert output.nested.source == 'mike'
def test_update_output_to_source_with_object(): data = {'source': 'mike', 'test': 'true', 'nested': {'foo': 'bar'}} class MyObject(object): pass output = MyObject() field = Field(source='source', required=True) session = Session(field, data, output) session.data = data['source'] update_output_to_source(session) assert output.source == 'mike'
def test_update_output_to_name_with_dict(): data = { 'name': 'mike', 'test': 'true', 'nested': {'foo': 'bar'} } output = {} field = Field(name='name', required=True) session = Session(field, data, output) session.data = data['name'] update_output_to_name(session) assert output == {'name': 'mike'}
def test_set_default(): data = { 'name': None, 'test': 'true', 'nested': {'foo': 'bar'} } output = {} field = Field(name='name', required=True, default='default') session = Session(field, data, output) session.data = data['name'] set_default(session) assert session.data == 'default'
def test_set_default_falsy(): # Default should not apply if value is falsy data = { 'name': '', 'test': 'true', 'nested': {'foo': 'bar'} } output = {} field = Field(name='name', required=True, default='default') session = Session(field, data, output) session.data = data['name'] set_default(session) assert session.data == ''
def test_is_valid_decimal_pipe(): """test piping data through is_valid_decimal. """ field = Decimal(name='test') session = Session(field, 'test', {}) with pytest.raises(FieldInvalid): is_valid_decimal(session) session.data = '2.5' assert is_valid_decimal(session) == decimal.Decimal('2.5') session.data = 2 assert is_valid_decimal(session) == decimal.Decimal(2) session.data = 2.3 assert is_valid_decimal(session) == decimal.Decimal(2.3)
def test_is_valid_integer_pipe(): """test piping data through is_valid_integer. """ field = Integer(name='test') session = Session(field, 'test', {}) with pytest.raises(FieldInvalid): is_valid_integer(session) session.data = '2' assert is_valid_integer(session) == 2 session.data = 2 assert is_valid_integer(session) == 2 session.data = 2.3 assert is_valid_integer(session) == 2
def test_is_valid_datetime_pipe(): """test piping data through is_valid_datetime. """ field = DateTime(name='test') session = Session(field, 'test', {}) with pytest.raises(FieldInvalid): is_valid_datetime(session) session.data = '2016-02-29T12:00:12Z' assert is_valid_datetime(session) == \ datetime(2016, 2, 29, 12, 0, 12, tzinfo=Utc()) session.data = '2015-06-29T08:00:12Z' assert is_valid_datetime(session) == \ datetime(2015, 6, 29, 8, 0, 12, tzinfo=Utc())
def test_is_valid_string_pipe(): """test piping data through is_valid_string. """ class InvalidString(object): def __str__(self): raise ValueError('invalid string') field = String(name='test') invalid_string = InvalidString() session = Session(field, invalid_string, {}) with pytest.raises(FieldInvalid): is_valid_string(session) session.data = 'yes' assert is_valid_string(session) == 'yes'
def test_get_data_from_source_pipe_self(): data = {'name': 'mike'} output = {} field = Field(source='__self__') session = Session(field, data, output) assert get_data_from_source(session) is data
def test_update_output_to_source_with_object(): data = { 'source': 'mike', 'test': 'true', 'nested': {'foo': 'bar'} } class MyObject(object): pass output = MyObject() field = Field(source='source', required=True) session = Session(field, data, output) session.data = data['source'] update_output_to_source(session) assert output.source == 'mike'
def test_update_output_to_source_invalid_output_type(): data = {'source': 'mike', 'test': 'true', 'nested': {'foo': 'bar'}} output = 1 field = Field(source='source', required=True) session = Session(field, data, output) with pytest.raises(FieldError): update_output_to_source(session)
def test_is_allowed_value_with_custom_values(): field = Boolean(name='test', true_boolean_values=['foo'], false_boolean_values=['bar']) data = True session = Session(field, data, {}) with pytest.raises(FieldInvalid): is_valid_choice(session) session.data = False with pytest.raises(FieldInvalid): is_valid_choice(session) session.data = 'foo' assert is_valid_choice(session) == 'foo' session.data = 'bar' assert is_valid_choice(session) == 'bar'
def test_get_data_from_name_pipe(): data = { 'name': 'mike', 'test': 'true', 'falsy': 0, 'nested': { 'foo': 'bar' } } default = 'bar' field = Field(name='foo', required=True) field2 = Field(name='foo', default=default, required=False) field3 = Field(name='foo', allow_none=False, required=False) field4 = Field(name='falsy', required=True) field5 = Field(name='falsy', allow_none=False) # Required but not present and no default output = {} session = Session(field, data, output) with pytest.raises(FieldInvalid): get_data_from_name(session) # Not present but default set session = Session(field2, data, output) assert get_data_from_name(session) == default # Not present and none not allowed session = Session(field3, data, output) with pytest.raises(FieldInvalid): get_data_from_name(session) # Required, value present and falsy - should still be allowed session = Session(field4, data, output) assert get_data_from_name(session) == 0 # None not allowed, value present and falsy - should still be allowed session = Session(field5, data, output) assert get_data_from_name(session) == 0
def test_get_static_value(): field = Static('foo', name='test') session = Session(field, 'bar', {}) assert get_static_value(session) == 'foo'
def test_is_allowed_value(): field = Boolean(name='test') session = Session(field, 'test', {}) with pytest.raises(FieldInvalid): is_valid_choice(session) session.data = True assert is_valid_choice(session) is True session.data = 'true' assert is_valid_choice(session) == 'true' session.data = '1' assert is_valid_choice(session) == '1' session.data = 'True' assert is_valid_choice(session) == 'True' session.data = False assert is_valid_choice(session) is False session.data = 'false' assert is_valid_choice(session) == 'false' session.data = '0' assert is_valid_choice(session) == '0' session.data = 0 assert is_valid_choice(session) == 0 session.data = 'False' assert is_valid_choice(session) == 'False'