Skip to content

Commit 2af0e6b

Browse files
Create pytest compatible tests for existing tests. (#186)
* add pytest files * add pytest files * add syrupt,snapshot-folder and updates tests * regenerate some snapshot tests * fix build issues * consolidate pytes * fix formattting * fix typo in filename --------- Co-authored-by: Daniel Rosenwasser <DanielRosenwasser@users.noreply.github.com>
1 parent 497f3c9 commit 2af0e6b

11 files changed

Lines changed: 38 additions & 55 deletions

python/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ dependencies = [
4444
"pytest",
4545
"spotipy", # for examples
4646
"typing_extensions",
47+
"syrupy",
4748
]
4849

4950
[tool.hatch.envs.default.scripts]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# serializer version: 1
2+
# name: test_coffeeshop_schema
3+
TypeScriptSchemaConversionResult(typescript_schema_str='interface Cart {\n type: "Cart";\n items: Array<LineItem | UnknownText>;\n}\n\n// Represents any text that could not be understood.\ninterface UnknownText {\n type: "UnknownText";\n // The text that wasn\'t understood\n text: string;\n}\n\ninterface LineItem {\n type: "LineItem";\n product: BakeryProduct | LatteDrink | CoffeeDrink | EspressoDrink | UnknownText;\n quantity: number;\n}\n\ninterface EspressoDrink {\n type: "EspressoDrink";\n name: "espresso" | "lungo" | "ristretto" | "macchiato";\n temperature?: "hot" | "extra hot" | "warm" | "iced";\n // The default is \'doppio\'\n size?: "solo" | "doppio" | "triple" | "quad";\n options?: Array<Creamer | Sweetener | Syrup | Topping | Caffeine | LattePreparation>;\n}\n\ninterface LattePreparation {\n type: "LattePreparation";\n name: "for here cup" | "lid" | "with room" | "to go" | "dry" | "wet";\n}\n\ninterface Caffeine {\n type: "Caffeine";\n name: "regular" | "two thirds caf" | "half caf" | "one third caf" | "decaf";\n}\n\ninterface Topping {\n type: "Topping";\n name: "cinnamon" | "foam" | "ice" | "nutmeg" | "whipped cream" | "water";\n optionQuantity?: "no" | "light" | "regular" | "extra";\n}\n\ninterface Syrup {\n type: "Syrup";\n name: "almond syrup" | "buttered rum syrup" | "caramel syrup" | "cinnamon syrup" | "hazelnut syrup" | "orange syrup" | "peppermint syrup" | "raspberry syrup" | "toffee syrup" | "vanilla syrup";\n optionQuantity?: "no" | "light" | "regular" | "extra";\n}\n\ninterface Sweetener {\n type: "Sweetener";\n name: "equal" | "honey" | "splenda" | "sugar" | "sugar in the raw" | "sweet n low" | "espresso shot";\n optionQuantity?: "no" | "light" | "regular" | "extra";\n}\n\ninterface Creamer {\n type: "Creamer";\n name: "whole milk creamer" | "two percent milk creamer" | "one percent milk creamer" | "nonfat milk creamer" | "coconut milk creamer" | "soy milk creamer" | "almond milk creamer" | "oat milk creamer" | "half and half" | "heavy cream";\n}\n\ninterface CoffeeDrink {\n type: "CoffeeDrink";\n name: "americano" | "coffee";\n temperature?: "hot" | "extra hot" | "warm" | "iced";\n // The default is \'grande\'\n size?: "short" | "tall" | "grande" | "venti";\n options?: Array<Creamer | Sweetener | Syrup | Topping | Caffeine | LattePreparation>;\n}\n\ninterface LatteDrink {\n type: "LatteDrink";\n name: "cappuccino" | "flat white" | "latte" | "latte macchiato" | "mocha" | "chai latte";\n temperature?: "hot" | "extra hot" | "warm" | "iced";\n // The default is \'grande\'\n size?: "short" | "tall" | "grande" | "venti";\n options?: Array<Creamer | Sweetener | Syrup | Topping | Caffeine | LattePreparation>;\n}\n\ninterface BakeryProduct {\n type: "BakeryProduct";\n name: "apple bran muffin" | "blueberry muffin" | "lemon poppyseed muffin" | "bagel";\n options?: Array<BakeryOption | BakeryPreparation>;\n}\n\ninterface BakeryPreparation {\n type: "BakeryPreparation";\n name: "warmed" | "cut in half";\n}\n\ninterface BakeryOption {\n type: "BakeryOption";\n name: "butter" | "strawberry jam" | "cream cheese";\n optionQuantity?: "no" | "light" | "regular" | "extra";\n}\n', typescript_type_reference='Cart', errors=[])
4+
# ---
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# serializer version: 1
2+
# name: test_data_classes
3+
TypeScriptSchemaConversionResult(typescript_schema_str='interface Response {\n "attr_1": string;\n // Hello!\n "attr_2": number;\n "attr_3": string | null;\n "attr_4"?: string;\n "attr_5"?: string | null;\n "attr_6"?: string[];\n "attr_7"?: Options;\n "_underscore_attr_1"?: number;\n}\n\n// TODO: someone add something here.\ninterface Options {\n}\n', typescript_type_reference='Response', errors=[])
4+
# ---
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# serializer version: 1
2+
# name: test_generic_alias1
3+
TypeScriptSchemaConversionResult(typescript_schema_str='interface Nested {\n item: FirstOrSecond<string>;\n}\n\ntype FirstOrSecond<T> = First<T> | Second<T>\n\ninterface Second {\n kind: "second";\n "second_attr": T;\n}\n\ninterface First {\n kind: "first";\n "first_attr": T;\n}\n', typescript_type_reference='Nested', errors=[])
4+
# ---
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# serializer version: 1
2+
# name: test_generic_alias1
3+
TypeScriptSchemaConversionResult(typescript_schema_str='type FirstOrSecond<T> = First<T> | Second<T>\n\ninterface Second {\n kind: "second";\n "second_attr": T;\n}\n\ninterface First {\n kind: "first";\n "first_attr": T;\n}\n', typescript_type_reference='FirstOrSecond', errors=[])
4+
# ---
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# serializer version: 1
2+
# name: test_generic_alias1
3+
TypeScriptSchemaConversionResult(typescript_schema_str='type D_or_E = D | E\n\n// This is the definition of the class E.\ninterface E extends C<string> {\n tag: "E";\n next: this | null;\n}\n\n// This is a generic class named C.\ninterface C {\n x?: T;\n c: C<number | null>;\n}\n\n// This is the definition of the class D.\ninterface D extends C<string> {\n tag?: "D";\n // This comes from string metadata\n // within an Annotated hint.\n y: boolean | null;\n z?: number[] | null;\n other?: IndirectC;\n "non_class"?: NonClass;\n // This comes from later metadata.\n "multiple_metadata"?: string;\n}\n\ninterface NonClass {\n a: number;\n "my-dict": Record<string, number>;\n}\n\ntype IndirectC = C<number>\n', typescript_type_reference='D_or_E', errors=[])
4+
# ---
Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
from typing_extensions import Literal, NotRequired, TypedDict, Annotated, Doc
2-
1+
from typing_extensions import Literal, NotRequired, TypedDict, Annotated, Doc, Any
32
from typechat import python_type_to_typescript_schema
43

5-
64
class UnknownText(TypedDict):
75
"""
86
Represents any text that could not be understood.
@@ -141,12 +139,6 @@ class Cart(TypedDict):
141139
type: Literal["Cart"]
142140
items: list[LineItem | UnknownText]
143141

144-
result = python_type_to_typescript_schema(Cart)
145142

146-
print(f"// Entry point is: '{result.typescript_type_reference}'")
147-
print("// TypeScript Schema:\n")
148-
print(result.typescript_schema_str)
149-
if result.errors:
150-
print("// Errors:")
151-
for err in result.errors:
152-
print(f"// - {err}\n")
143+
def test_coffeeshop_schema(snapshot: Any):
144+
assert(python_type_to_typescript_schema(Cart) == snapshot)
Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
1+
from typing_extensions import Any
22
from typing import Annotated
3-
from typechat import python_type_to_typescript_schema
4-
53
from dataclasses import dataclass, field
4+
from typechat import python_type_to_typescript_schema
65

76
@dataclass
87
class Options:
@@ -26,12 +25,5 @@ def do_something(self):
2625
print(f"{self.attr_1=}")
2726

2827

29-
result = python_type_to_typescript_schema(Response)
30-
31-
print(f"// Entry point is: '{result.typescript_type_reference}'")
32-
print("// TypeScript Schema:\n")
33-
print(result.typescript_schema_str)
34-
if result.errors:
35-
print("// Errors:")
36-
for err in result.errors:
37-
print(f"// - {err}\n")
28+
def test_data_classes(snapshot: Any):
29+
assert(python_type_to_typescript_schema(Response) == snapshot)
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1+
from typing_extensions import TypeAliasType, Any
12
from typing import Literal, TypedDict, TypeVar, Generic
2-
from typing_extensions import TypeAliasType
3-
43
from typechat import python_type_to_typescript_schema
54

65
T = TypeVar("T", covariant=True)
@@ -18,12 +17,6 @@ class Second(Generic[T], TypedDict):
1817

1918
FirstOrSecond = TypeAliasType("FirstOrSecond", First[T] | Second[T], type_params=(T,))
2019

21-
result = python_type_to_typescript_schema(FirstOrSecond)
2220

23-
print(f"// Entry point is: '{result.typescript_type_reference}'")
24-
print("// TypeScript Schema:\n")
25-
print(result.typescript_schema_str)
26-
if result.errors:
27-
print("// Errors:")
28-
for err in result.errors:
29-
print(f"// - {err}\n")
21+
def test_generic_alias1(snapshot: Any):
22+
assert(python_type_to_typescript_schema(FirstOrSecond) == snapshot)
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1+
from typing_extensions import TypeAliasType, Any
12
from typing import Literal, TypedDict, Generic, TypeVar
2-
33
from typechat import python_type_to_typescript_schema
4-
from typing_extensions import TypeAliasType
54

65
T = TypeVar("T", covariant=True)
76

8-
97
class First(Generic[T], TypedDict):
108
kind: Literal["first"]
119
first_attr: T
@@ -23,12 +21,7 @@ class Nested(TypedDict):
2321
item: FirstOrSecond[str]
2422

2523

26-
result = python_type_to_typescript_schema(Nested)
2724

28-
print(f"// Entry point is: '{result.typescript_type_reference}'")
29-
print("// TypeScript Schema:\n")
30-
print(result.typescript_schema_str)
31-
if result.errors:
32-
print("// Errors:")
33-
for err in result.errors:
34-
print(f"// - {err}\n")
25+
def test_generic_alias1(snapshot: Any):
26+
assert(python_type_to_typescript_schema(Nested) == snapshot)
27+

0 commit comments

Comments
 (0)