Skip to content

Commit 33b03a1

Browse files
committed
add some tests closes #1
1 parent 167f665 commit 33b03a1

File tree

4 files changed

+192
-0
lines changed

4 files changed

+192
-0
lines changed

pytest.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[pytest]
2+
python_files = test/**.py
3+
addopts = -ra -q --doctest-modules
4+
doctest_optionflags = NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL

test/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import os
2+
import sys
3+
4+
project_path = os.path.realpath(os.path.join(os.path.dirname(__file__), ".."))
5+
sys.path.append(project_path)

test/test_guess_type.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import platform
2+
import packaging.version
3+
import typing
4+
5+
import pytest
6+
from function_schema.core import guess_type
7+
8+
9+
def test_primitive():
10+
"""Test primitive types"""
11+
assert guess_type(int) == "integer"
12+
assert guess_type(str) == "string"
13+
assert guess_type(float) == "number"
14+
assert guess_type(bool) == "boolean"
15+
16+
17+
def test_optional():
18+
"""Test optional types"""
19+
assert guess_type(typing.Optional[int]) == "integer"
20+
assert guess_type(typing.Optional[str]) == "string"
21+
assert guess_type(typing.Optional[float]) == "number"
22+
assert guess_type(typing.Optional[bool]) == "boolean"
23+
24+
25+
def test_union_null():
26+
"""Test union types with null"""
27+
assert guess_type(typing.Union[int, None]) == "integer"
28+
assert guess_type(typing.Union[str, None]) == "string"
29+
assert guess_type(typing.Union[float, None]) == "number"
30+
assert guess_type(typing.Union[bool, None]) == "boolean"
31+
32+
33+
def test_union():
34+
"""Test union types"""
35+
assert guess_type(typing.Union[int, str]) == ["integer", "string"]
36+
assert guess_type(typing.Union[int, float]) == "number"
37+
assert guess_type(typing.Union[int, bool]) == ["integer", "boolean"]
38+
assert guess_type(typing.Union[bool, int]) == ["boolean", "integer"]
39+
assert guess_type(typing.Union[str, float]) == ["string", "number"]
40+
assert guess_type(typing.Union[str, bool]) == ["string", "boolean"]
41+
assert guess_type(typing.Union[float, bool]) == ["number", "boolean"]
42+
assert guess_type(typing.Union[str, float, bool]) == [
43+
"string",
44+
"number",
45+
"boolean",
46+
]
47+
assert guess_type(typing.Union[str, float, bool, None]) == [
48+
"string",
49+
"number",
50+
"boolean",
51+
]
52+
53+
54+
def test_union_type():
55+
"""Test union types in Python 3.10+"""
56+
current_version = packaging.version.parse(platform.python_version())
57+
py_310 = packaging.version.parse("3.10")
58+
if current_version < py_310:
59+
pytest.skip("Union type is only available in Python 3.10+")
60+
61+
assert guess_type(int | str) == ["integer", "string"]
62+
assert guess_type(int | float) == "number"
63+
assert guess_type(int | bool) == ["integer", "boolean"]
64+
assert guess_type(bool | int) == ["boolean", "integer"]
65+
assert guess_type(str | float) == ["string", "number"]
66+
assert guess_type(str | bool) == ["string", "boolean"]
67+
assert guess_type(float | bool) == ["number", "boolean"]
68+
assert guess_type(str | float | bool) == [
69+
"string",
70+
"number",
71+
"boolean",
72+
]
73+
assert guess_type(str | float | bool | None) == [
74+
"string",
75+
"number",
76+
"boolean",
77+
]

test/test_schema.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import enum
2+
from typing import Optional, Annotated
3+
from function_schema.core import get_function_schema
4+
5+
6+
def test_simple_function():
7+
"""Test a simple function"""
8+
9+
def func1():
10+
"""My function"""
11+
...
12+
13+
schema = get_function_schema(func1)
14+
15+
assert schema["name"] == "func1", "Function name should be func1"
16+
assert (
17+
schema["description"] == "My function"
18+
), "Function description should be My function"
19+
assert (
20+
schema["parameters"]["properties"] == {}
21+
), "Function parameters should be empty"
22+
assert schema["parameters"]["required"] == [], "required parameters should be empty"
23+
24+
25+
def test_function_with_args():
26+
"""Test a function with args"""
27+
28+
def func1(a: int, b: str, c: float = 1.0):
29+
"""My function"""
30+
...
31+
32+
schema = get_function_schema(func1)
33+
34+
assert schema["name"] == "func1", "Function name should be func1"
35+
assert (
36+
schema["description"] == "My function"
37+
), "Function description should be there"
38+
assert (
39+
schema["parameters"]["properties"]["a"]["type"] == "integer"
40+
), "parameter a should be an integer"
41+
assert (
42+
schema["parameters"]["properties"]["b"]["type"] == "string"
43+
), "parameter b should be a string"
44+
assert (
45+
schema["parameters"]["properties"]["c"]["type"] == "number"
46+
), "parameter c should be a number"
47+
assert (
48+
schema["parameters"]["properties"]["c"]["default"] == 1.0
49+
), "c should have a default value of 1.0"
50+
assert schema["parameters"]["required"] == [
51+
"a",
52+
"b",
53+
], "parameters with no default value should be required"
54+
55+
56+
def test_annotated_function():
57+
"""Test a function with annotations"""
58+
59+
def func1(
60+
a: Annotated[int, "An integer parameter"],
61+
b: Annotated[str, "A string parameter"],
62+
):
63+
"""My function"""
64+
...
65+
66+
schema = get_function_schema(func1)
67+
68+
assert (
69+
schema["parameters"]["properties"]["a"]["type"] == "integer"
70+
), "parameter a should be an integer"
71+
assert (
72+
schema["parameters"]["properties"]["a"]["description"] == "An integer parameter"
73+
), "parameter a should have a description"
74+
assert (
75+
schema["parameters"]["properties"]["b"]["type"] == "string"
76+
), "parameter b should be a string"
77+
assert (
78+
schema["parameters"]["properties"]["b"]["description"] == "A string parameter"
79+
), "parameter b should have a description"
80+
81+
assert schema["parameters"]["required"] == [
82+
"a",
83+
"b",
84+
], "parameters with no default value should be required"
85+
86+
87+
def test_annotated_function_with_enum():
88+
"""Test a function with annotations and enum"""
89+
def func1(
90+
animal: Annotated[
91+
str,
92+
"The animal you want to pet",
93+
enum.Enum("Animal", "Cat Dog"),
94+
],
95+
):
96+
"""My function"""
97+
...
98+
99+
schema = get_function_schema(func1)
100+
print(schema)
101+
assert (
102+
schema["parameters"]["properties"]["animal"]["type"] == "string"
103+
), "parameter animal should be a string"
104+
assert (
105+
schema["parameters"]["properties"]["animal"]["enum"] == ["Cat", "Dog"]
106+
), "parameter animal should have an enum"

0 commit comments

Comments
 (0)