File size: 1,341 Bytes
af4851b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from pathlib import Path

import pytest

from backend.input_files import resolve_uploaded_name, resolve_uploaded_path


def test_resolve_uploaded_path_accepts_string_path(tmp_path: Path) -> None:
    path = tmp_path / "book.epub"
    path.write_text("x", encoding="utf-8")

    resolved = resolve_uploaded_path(str(path))

    assert resolved == path


def test_resolve_uploaded_path_accepts_gradio_dict_payload(tmp_path: Path) -> None:
    path = tmp_path / "book.epub"
    path.write_text("x", encoding="utf-8")

    resolved = resolve_uploaded_path({"path": str(path), "meta": {"_type": "gradio.FileData"}})

    assert resolved == path


def test_resolve_uploaded_path_rejects_missing_path_key() -> None:
    with pytest.raises(TypeError, match="Unsupported uploaded file payload"):
        resolve_uploaded_path({"url": "/tmp/book.epub"})


def test_resolve_uploaded_name_prefers_original_filename() -> None:
    name = resolve_uploaded_name(
        {"path": "/tmp/gradio/abcd", "orig_name": "real-book.epub", "meta": {"_type": "gradio.FileData"}}
    )

    assert name == "real-book.epub"


def test_resolve_uploaded_name_falls_back_to_path_name(tmp_path: Path) -> None:
    path = tmp_path / "fallback.epub"
    path.write_text("x", encoding="utf-8")

    name = resolve_uploaded_name(str(path))

    assert name == "fallback.epub"