def test_encode_unmapped_content_raises(immutable_list_schema, encode_mapping): """If a content list item contains a field without a mapping, raise.""" composite = Composite(immutable_list_schema) content: List[Dict] = [{"unknown_field": "foo"}] with pytest.raises(ValueError): # Wrap in list to force lazy execution list(composite.encode_list(encode_mapping, content))
def test_encode_list(immutable_list_schema, encode_mapping): """Both mappings and content present.""" composite = Composite(immutable_list_schema) content: List[Dict] = [ { "name": "Al", "age": 25, "ice cream": "Rocky road" }, { "name": "Joe", "age": 34, "ice cream": "Clam chowder" # https://now.tufts.edu/articles/dish-we-all-scream-for-ice-cream } ] expected: List = [{ "inner_folder": { "nombre": "Al", "edad": 25 }, "helado": "Rocky road" }, { "inner_folder": { "nombre": "Joe", "edad": 34 }, "helado": "Clam chowder" }] actual: List = list(composite.encode_list(encode_mapping, content)) assert actual == expected
def test_encode_list_multiple_empty(immutable_list_schema, encode_mapping): """Mappings present, no content.""" composite = Composite(immutable_list_schema) content: List[Dict] = [{}, {}, {}] expected: List = content.copy() actual: List = list(composite.encode_list(encode_mapping, content)) assert actual == expected
def test_encode_null_content(immutable_list_schema, encode_mapping): """Content that is explicitly null is encoded.""" composite = Composite(immutable_list_schema) content: List[Dict] = [{"name": None}] expected: List = [{"inner_folder": {"nombre": None}}] actual: List = list(composite.encode_list(encode_mapping, content)) assert actual == expected
def test_encode_missing_content_skips(immutable_list_schema, encode_mapping): """If something isn't present in a content list item that is defined in the mappings, skip it.""" composite = Composite(immutable_list_schema) content: List[Dict] = [{ "ice cream": "Rocky road" }, { "name": "Joe", }] expected: List = [{ "helado": "Rocky road" }, { "inner_folder": { "nombre": "Joe" } }] actual: List = list(composite.encode_list(encode_mapping, content)) assert actual == expected
def test_encode_list_zero_length(immutable_list_schema, encode_mapping): """Mappings present, no content.""" composite = Composite(immutable_list_schema) actual: List = list(composite.encode_list(encode_mapping, [])) assert actual == []
def test_encode_list_trivial(trivial_schema): """No mappings, no content""" composite = Composite(trivial_schema) actual: List = list(composite.encode_list({}, [])) assert actual == []