2019-07-15 09:32:14 +00:00
|
|
|
|
import pytest
|
|
|
|
|
|
2019-07-09 14:03:41 +00:00
|
|
|
|
from funkwhale_api.tags import serializers
|
|
|
|
|
|
|
|
|
|
|
2019-07-29 08:44:01 +00:00
|
|
|
|
def test_tag_serializer(factories, to_api_date):
|
2019-07-09 14:03:41 +00:00
|
|
|
|
tag = factories["tags.Tag"]()
|
|
|
|
|
|
|
|
|
|
serializer = serializers.TagSerializer(tag)
|
|
|
|
|
|
2019-07-29 08:44:01 +00:00
|
|
|
|
expected = {"name": tag.name, "creation_date": to_api_date(tag.creation_date)}
|
2019-07-09 14:03:41 +00:00
|
|
|
|
|
|
|
|
|
assert serializer.data == expected
|
2019-07-15 09:32:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"name",
|
|
|
|
|
[
|
|
|
|
|
"",
|
|
|
|
|
"invalid because spaces",
|
|
|
|
|
"invalid-because-dashes",
|
|
|
|
|
"invalid because non breaking spaces",
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
def test_tag_name_field_validation(name):
|
|
|
|
|
field = serializers.TagNameField()
|
|
|
|
|
with pytest.raises(serializers.serializers.ValidationError):
|
|
|
|
|
field.to_internal_value(name)
|
2019-07-15 09:53:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"max, tags, expected",
|
|
|
|
|
[
|
|
|
|
|
(5, ["hello", "world"], ["hello", "world"]),
|
|
|
|
|
# we truncate extra tags
|
|
|
|
|
(1, ["hello", "world"], ["hello"]),
|
|
|
|
|
(2, ["hello", "world", "foo"], ["hello", "world"]),
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
def test_tags_list_field_honor_TAGS_MAX_BY_OBJ(max, tags, expected, settings):
|
|
|
|
|
settings.TAGS_MAX_BY_OBJ = max
|
|
|
|
|
field = serializers.TagsListField()
|
|
|
|
|
assert field.to_internal_value(tags) == expected
|