openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/test_extract_files.py
64lines · modeblame
08b8179aDavid Schnurr2 years ago | 1 | from __future__ import annotations |
| 2 | | |
| 3 | from typing import Sequence | |
| 4 | | |
| 5 | import pytest | |
| 6 | | |
| 7 | from openai._types import FileTypes | |
| 8 | from openai._utils import extract_files | |
| 9 | | |
| 10 | | |
| 11 | def test_removes_files_from_input() -> None: | |
| 12 | query = {"foo": "bar"} | |
| 13 | assert extract_files(query, paths=[]) == [] | |
| 14 | assert query == {"foo": "bar"} | |
| 15 | | |
| 16 | query2 = {"foo": b"Bar", "hello": "world"} | |
| 17 | assert extract_files(query2, paths=[["foo"]]) == [("foo", b"Bar")] | |
| 18 | assert query2 == {"hello": "world"} | |
| 19 | | |
| 20 | query3 = {"foo": {"foo": {"bar": b"Bar"}}, "hello": "world"} | |
| 21 | assert extract_files(query3, paths=[["foo", "foo", "bar"]]) == [("foo[foo][bar]", b"Bar")] | |
| 22 | assert query3 == {"foo": {"foo": {}}, "hello": "world"} | |
| 23 | | |
| 24 | query4 = {"foo": {"bar": b"Bar", "baz": "foo"}, "hello": "world"} | |
| 25 | assert extract_files(query4, paths=[["foo", "bar"]]) == [("foo[bar]", b"Bar")] | |
| 26 | assert query4 == {"hello": "world", "foo": {"baz": "foo"}} | |
| 27 | | |
| 28 | | |
| 29 | def test_multiple_files() -> None: | |
| 30 | query = {"documents": [{"file": b"My first file"}, {"file": b"My second file"}]} | |
| 31 | assert extract_files(query, paths=[["documents", "<array>", "file"]]) == [ | |
| 32 | ("documents[][file]", b"My first file"), | |
| 33 | ("documents[][file]", b"My second file"), | |
| 34 | ] | |
| 35 | assert query == {"documents": [{}, {}]} | |
| 36 | | |
| 37 | | |
| 38 | @pytest.mark.parametrize( | |
| 39 | "query,paths,expected", | |
| 40 | [ | |
| 41 | [ | |
| 42 | {"foo": {"bar": "baz"}}, | |
| 43 | [["foo", "<array>", "bar"]], | |
| 44 | [], | |
| 45 | ], | |
| 46 | [ | |
| 47 | {"foo": ["bar", "baz"]}, | |
| 48 | [["foo", "bar"]], | |
| 49 | [], | |
| 50 | ], | |
| 51 | [ | |
| 52 | {"foo": {"bar": "baz"}}, | |
| 53 | [["foo", "foo"]], | |
| 54 | [], | |
| 55 | ], | |
| 56 | ], | |
86b263f1Stainless Bot2 years ago | 57 | ids=["dict expecting array", "array expecting dict", "unknown keys"], |
08b8179aDavid Schnurr2 years ago | 58 | ) |
| 59 | def test_ignores_incorrect_paths( | |
| 60 | query: dict[str, object], | |
| 61 | paths: Sequence[Sequence[str]], | |
| 62 | expected: list[tuple[str, FileTypes]], | |
| 63 | ) -> None: | |
| 64 | assert extract_files(query, paths=paths) == expected |