openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.13.2

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/test_extract_files.py

64lines · modeblame

08b8179aDavid Schnurr2 years ago1from __future__ import annotations
2
3from typing import Sequence
4
5import pytest
6
7from openai._types import FileTypes
8from openai._utils import extract_files
9
10
11def test_removes_files_from_input() -> None:
12query = {"foo": "bar"}
13assert extract_files(query, paths=[]) == []
14assert query == {"foo": "bar"}
15
16query2 = {"foo": b"Bar", "hello": "world"}
17assert extract_files(query2, paths=[["foo"]]) == [("foo", b"Bar")]
18assert query2 == {"hello": "world"}
19
20query3 = {"foo": {"foo": {"bar": b"Bar"}}, "hello": "world"}
21assert extract_files(query3, paths=[["foo", "foo", "bar"]]) == [("foo[foo][bar]", b"Bar")]
22assert query3 == {"foo": {"foo": {}}, "hello": "world"}
23
24query4 = {"foo": {"bar": b"Bar", "baz": "foo"}, "hello": "world"}
25assert extract_files(query4, paths=[["foo", "bar"]]) == [("foo[bar]", b"Bar")]
26assert query4 == {"hello": "world", "foo": {"baz": "foo"}}
27
28
29def test_multiple_files() -> None:
30query = {"documents": [{"file": b"My first file"}, {"file": b"My second file"}]}
31assert extract_files(query, paths=[["documents", "<array>", "file"]]) == [
32("documents[][file]", b"My first file"),
33("documents[][file]", b"My second file"),
34]
35assert 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 ago57ids=["dict expecting array", "array expecting dict", "unknown keys"],
08b8179aDavid Schnurr2 years ago58)
59def test_ignores_incorrect_paths(
60query: dict[str, object],
61paths: Sequence[Sequence[str]],
62expected: list[tuple[str, FileTypes]],
63) -> None:
64assert extract_files(query, paths=paths) == expected