def to_python(value): """Reconstruct a multistring from the database string representation.""" if not value: return multistring("", encoding="UTF-8") elif isinstance(value, multistring): return value elif isinstance(value, str): return parse_multistring(value) elif isinstance(value, dict): return multistring([val for __, val in sorted(value.items())], encoding="UTF-8") else: return multistring(value, encoding="UTF-8")
def to_python(value): """Reconstruct a multistring from the database string representation.""" if not value: return multistring("", encoding="UTF-8") elif isinstance(value, multistring): return value elif isinstance(value, basestring): return parse_multistring(value) elif isinstance(value, dict): return multistring([val for __, val in sorted(value.items())], encoding="UTF-8") else: return multistring(value, encoding="UTF-8")
def test_parse_multistring(db_string, expected_ms, is_plural): parsed_ms = parse_multistring(db_string) assert parsed_ms == expected_ms assert parsed_ms.plural == is_plural
def test_parse_multistring_invalid(invalid_value): """Tests parsing doesn't support non-string values""" with pytest.raises(ValueError): parse_multistring(invalid_value)