def test_two_arrays_with_different_names_are_not_equal(): # given field = Array(String(), name="field_name") another_field = Array(String(), name="different_name") # when, then assert field != another_field
def test_two_arrays_are_equal(): # given field = Array(String()) another_field = Array(String()) # when, then assert field == another_field
def test_two_arrays_with_different_element_types_are_not_equal(): # given field = Array(String()) another_field = Array(Float()) # when, then assert field != another_field
def should_reject_non_sequence(): # given array_of_floats_field = Array(Float()) value = {} # when, then with pytest.raises( FieldValueValidationError, match=re.escape( "Value for an array must be a sequence, not 'dict'")): array_of_floats_field._validate_on_value(value)
def should_reject_invalid_element_value(): # given array_of_floats_field = Array(Float()) value = [3.5, "string"] # when, then with pytest.raises( FieldValueValidationError, match=re.escape( "Value 'string' has invalid type 'str'. Allowed types are: 'float'" ), ): array_of_floats_field._validate_on_value(value)
def should_reject_non_field_element(): # given bad_element = "this is a str, which is not a field" # when, then with pytest.raises(ValueError, match=re.escape("Array element must be a field. Found type: str")): Array(bad_element)
def should_not_allow_element_with_explicit_name(): # given element = String(name="explicit_name") # when, then with pytest.raises( ValueError, match="When using a field as the element field of an array, the field should not have a name." ): Array(element)
def should_replace_parent_should_replace_parent_of_element(): # given array_element = String() array = Array(array_element, name="array") class ParentStruct(Struct): pass new_parent = ParentStruct() # when returned_array = array._replace_parent(new_parent) # then assert array._parent_struct is None assert returned_array is not array assert returned_array._parent_struct is new_parent assert isinstance(returned_array, Array) assert returned_array.e._parent_struct is new_parent
class Conference(Struct): name = String(False) city = String(False) city_location = Location(False) country = String(False) twitter = String() start_date = Timestamp(False) end_date = Timestamp(False) url = String() keywords = Array(String(False), True)
class Article(Struct): id = String(nullable=False) tags = Array(Tag(nullable=True))
class OuterObject(Struct): sequence = Array(ComplexElementStruct())
class TestArrayField: @staticmethod def should_not_allow_element_with_explicit_name(): # given element = String(name="explicit_name") # when, then with pytest.raises( ValueError, match= "When using a field as the element field of an array, the field should not have a name." ): Array(element) @staticmethod def should_enable_path_via_explicit_element_field(): # given class ComplexElementStruct(Struct): string_field = String() float_field = Float() class OuterObject(Struct): sequence = Array(ComplexElementStruct()) # when path = OuterObject.sequence.e.string_field.PATH # then assert path == "sequence.string_field" @staticmethod def should_enable_path_via_passthrough(): # given class ComplexElementStruct(Struct): string_field = String() float_field = Float() class OuterObject(Struct): sequence = Array(ComplexElementStruct()) # when path = OuterObject.sequence.string_field.PATH # then assert path == "sequence.string_field" @staticmethod def should_replace_parent_should_replace_parent_of_element(): # given array_element = String() array = Array(array_element, name="array") class ParentStruct(Struct): pass new_parent = ParentStruct() # when returned_array = array._replace_parent(new_parent) # then assert array._parent_struct is None assert returned_array is not array assert returned_array._parent_struct is new_parent assert isinstance(returned_array, Array) assert returned_array.e._parent_struct is new_parent @staticmethod def should_reject_non_field_element(): # given bad_element = "this is a str, which is not a field" # when, then with pytest.raises( ValueError, match=re.escape( "Array element must be a field. Found type: str")): Array(bad_element) @staticmethod @pytest.mark.parametrize( "instance", [ pytest.param(Array(Float(), True, "name"), id="nullable instance"), pytest.param(Array(Float(), False, "name"), id="non-nullable instance"), pytest.param(Array(Float(), False), id="non-nullable nameless instance"), pytest.param(Array(Float()), id="instance with default constructor"), ], ) def should_be_hashable(instance: Array): _field_can_be_used_as_a_key = {instance: "value"}
class Article(Struct): title = String(nullable=False) author = User(nullable=False) comments = Array(Comment())
class TestArrayFieldValidateOnValue: @staticmethod def should_reject_non_sequence(): # given array_of_floats_field = Array(Float()) value = {} # when, then with pytest.raises( FieldValueValidationError, match=re.escape( "Value for an array must be a sequence, not 'dict'")): array_of_floats_field._validate_on_value(value) @staticmethod def should_reject_invalid_element_value(): # given array_of_floats_field = Array(Float()) value = [3.5, "string"] # when, then with pytest.raises( FieldValueValidationError, match=re.escape( "Value 'string' has invalid type 'str'. Allowed types are: 'float'" ), ): array_of_floats_field._validate_on_value(value) @staticmethod @pytest.mark.parametrize( "array_field, value, expected_error", [ pytest.param( Array(String(), name="name_of_array_field"), None, does_not_raise(), id="array-of-nullable-elemets-should-accept-none", ), pytest.param( Array(String(), name="name_of_array_field"), "this is a string value", pytest.raises( FieldValueValidationError, match=re.escape( "Value for an array must not be a string. Found value 'this is a string value'. Did you mean " "to use a list of strings?"), ), id="string-array-should-reject-non-sequence", ), pytest.param( Array(Float(), name="name_of_array_field"), 3.5, pytest.raises( FieldValueValidationError, match=re.escape( "Value for an array must be a sequence, not 'float'")), id="float-array-should-reject-non-sequence", ), pytest.param( Array(Float(nullable=False), name="name_of_array_field"), [None], pytest.raises( FieldValueValidationError, match=re.escape( "Encountered None value in array, but the element field of this array is specified as " "non-nullable (array field name = 'name_of_array_field')" ), ), id= "float-array-of-non-nullable-elements-should-reject-null-element", ), ], ) def test_arrays_should_be_handled_correctly(array_field: Array, value, expected_error): # given, when, then with expected_error: array_field._validate_on_value(value)
def test_arrays_should_be_handled_correctly(array_field: Array, value, expected_error): # given, when, then with expected_error: array_field._validate_on_value(value)
class Article(Struct): title = String(nullable=False) tags = Array(String(), nullable=False) comments = Array(String(nullable=False))
class AnObject(Struct): text_sequence = Array(String()) float_sequence = Array(Float())
class ComplexStruct(Struct): string_field = String() values = Array(AnElement())
class StructWithArray(Struct): array_field = Array(Element(), name="alt_array_field_name", nullable=True)
class Message(Struct): sender = User() recipients = Array(User())