def test_constraints_maxlength_valid_value_equals(self): '''maxLength with valid value equal to each other''' value = ['a', 'b', 'c'] field = self._make_default_field(type='array', constraints={'maxLength': 3}) _type = types.ArrayType(field) self.assertEqual(_type.cast(value), value)
def test_constraints_minlength_valid_value(self): '''minLength with valid value''' value = ['a', 'b', 'c'] field = self._make_default_field(type='array', constraints={'minLength': 2}) _type = types.ArrayType(field) self.assertEqual(_type.cast(value), value)
def test_constraints_pattern_valid_value(self): '''value is valid for pattern''' value = '["a", "b", "c"]' field = self._make_default_field( type='array', constraints={"pattern": '\[("[a-c]",?\s?)*\]'}) _type = types.ArrayType(field) self.assertEqual(_type.cast(value), json.loads(value))
def test_constraints_maxlength_invalid_value(self): '''maxLength with invalid value''' value = ['a', 'b', 'c'] field = self._make_default_field(type='array', constraints={'maxLength': 2}) _type = types.ArrayType(field) with pytest.raises(exceptions.ConstraintError) as e: _type.cast(value) self.assertEqual(e.value.msg, "The field 'Name' must have a maximum length of 2")
def test_constraints_pattern_invalid_value(self): '''value is invalid for pattern''' value = '["a", "b", "c", "d"]' field = self._make_default_field( type='array', constraints={"pattern": '\[("[a-c]",?\s?)*\]'}) _type = types.ArrayType(field) with pytest.raises(exceptions.ConstraintError) as e: _type.cast(value) self.assertEqual( e.value.msg, "The value for field 'Name' " "must match the pattern")
def test_constraints_enum_valid_value(self): '''value is in enum array''' value = ['first', 'second', 'third'] field = self._make_default_field(type='array', constraints={ 'enum': [ ["first", "second", "third"], ["fred", "alice", "bob"], ] }) _type = types.ArrayType(field) self.assertEqual(_type.cast(value), value)
def test_constraints_enum_invalid_value(self): '''value is not in enum array''' value = ['first', 'second', 'third'] field = self._make_default_field( type='array', constraints={'enum': [ ["fred", "alice", "bob"], ]}) _type = types.ArrayType(field) with pytest.raises(exceptions.ConstraintError) as e: _type.cast(value) self.assertEqual( e.value.msg, "The value for field 'Name' " "must be in the enum array")
def test_constraints_enum_invalid_value_different_order(self): '''value is not in enum array. Same members in each array, but different order.''' value = ['first', 'second', 'third'] field = self._make_default_field( type='array', constraints={'enum': [ ["first", "third", "second"], ]}) _type = types.ArrayType(field) with pytest.raises(exceptions.ConstraintError) as e: _type.cast(value) self.assertEqual( e.value.msg, "The value for field 'Name' " "must be in the enum array")
def test_array_type_simple_false(self): value = 'string, string' _type = types.ArrayType(self.field) self.assertRaises(exceptions.InvalidArrayType, _type.cast, value)
def test_array_type_simple_json_string(self): value = '["1", "2"]' _type = types.ArrayType(self.field) self.assertEquals(_type.cast(value), [u'1', u'2'])
def test_array_type_simple_true(self): value = ['1', '2'] _type = types.ArrayType(self.field) self.assertEquals(_type.cast(value), value)