Spaces:
Running
Running
Download tests/test_uploads.py from mertkayacs/postkitty-api: direct link, hf CLI and curl.
- Browser
- Download file 4.72 kB
-
https://huggingface.co/spaces/mertkayacs/postkitty-api/resolve/main/tests/test_uploads.py
- Command line
-
hf download hf://spaces/mertkayacs/postkitty-api/tests/test_uploads.py
-
curl -L -o test_uploads.py https://huggingface.co/spaces/mertkayacs/postkitty-api/resolve/main/tests/test_uploads.py
4.72 kB
| """Deciding what an upload is from its bytes.""" | |
| from __future__ import annotations | |
| import io | |
| import pytest | |
| from app import uploads | |
| from app.config import Limits | |
| from app.errors import ApiError | |
| def test_accepted_signatures(head, expected): | |
| assert uploads.sniff(head) == expected | |
| def test_refused_signatures(head): | |
| assert uploads.sniff(head) is None | |
| def test_file_name_becomes_a_harmless_label(given, shown): | |
| assert uploads.label(given) == shown | |
| def test_extension_comes_from_the_bytes_and_the_name_is_random(media, tmp_path): | |
| first = uploads.store(io.BytesIO(media["team.png"].read_bytes()), "holiday.jpg", tmp_path, Limits()) | |
| second = uploads.store(io.BytesIO(media["team.png"].read_bytes()), "holiday.jpg", tmp_path, Limits()) | |
| assert first.path.suffix == ".png" and first.path.parent == tmp_path and first.path != second.path | |
| assert (first.name, first.kind, first.width, first.height) == ("holiday.jpg", "image", 1200, 800) | |
| def test_video_reports_its_size_and_length(media, tmp_path): | |
| item = uploads.store(io.BytesIO(media["clip.mp4"].read_bytes()), "clip.mp4", tmp_path, Limits()) | |
| assert (item.kind, item.width, item.height, item.path.suffix) == ("video", 640, 360, ".mp4") | |
| assert 2.5 < item.duration < 3.5 | |
| def test_refused_file_is_not_left_on_disk(media, tmp_path): | |
| with pytest.raises(ApiError) as caught: | |
| uploads.store(io.BytesIO(media["moving.gif"].read_bytes()), "moving.gif", tmp_path, Limits()) | |
| assert caught.value.code == "animated_image" and list(tmp_path.iterdir()) == [] | |
| def test_audio_only_file_in_a_video_container_is_refused(tmp_path): | |
| import subprocess | |
| sound = tmp_path / "sound.m4a" | |
| subprocess.run(["ffmpeg", "-hide_banner", "-loglevel", "error", "-nostdin", "-y", "-f", "lavfi", "-i", | |
| "sine=frequency=440:duration=1", "-c:a", "aac", str(sound)], check=True) | |
| with pytest.raises(ApiError) as caught: | |
| uploads.store(io.BytesIO(sound.read_bytes()), "sound.mp4", tmp_path / "in", Limits()) | |
| assert caught.value.code == "unreadable_file" | |
| def test_webm_matroska_and_quicktime_videos_are_read(tmp_path, extension, codec): | |
| import subprocess | |
| clip = tmp_path / f"clip.{extension}" | |
| subprocess.run(["ffmpeg", "-hide_banner", "-loglevel", "error", "-nostdin", "-y", "-f", "lavfi", "-i", | |
| "testsrc2=size=160x90:rate=10:duration=1", *codec, "-pix_fmt", "yuv420p", str(clip)], check=True) | |
| # The name claims another type on purpose: the stored extension follows the bytes. | |
| item = uploads.store(io.BytesIO(clip.read_bytes()), "holiday.mp4", tmp_path / "in", Limits()) | |
| assert (item.kind, item.width, item.height, item.path.suffix) == ("video", 160, 90, f".{extension}") | |
| assert 0.9 <= item.duration <= 1.2 | |