Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create pytest compatible tests for existing tests. #186

Merged
merged 11 commits into from
Feb 27, 2024
Merged
1 change: 1 addition & 0 deletions python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ dependencies = [
"pytest",
"spotipy", # for examples
"typing_extensions",
"syrupy",
]

[tool.hatch.envs.default.scripts]
Expand Down
4 changes: 4 additions & 0 deletions python/tests/__snapshots__/test_coffeeshop.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# serializer version: 1
# name: test_coffeeshop_schema
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 changes: 4 additions & 0 deletions python/tests/__snapshots__/test_dataclasses.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# serializer version: 1
# name: test_data_classes
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 changes: 4 additions & 0 deletions python/tests/__snapshots__/test_generic_alias_2.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# serializer version: 1
# name: test_generic_alias1
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 changes: 4 additions & 0 deletions python/tests/__snapshots__/test_generric_alias_1.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# serializer version: 1
# name: test_generic_alias1
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 changes: 4 additions & 0 deletions python/tests/__snapshots__/test_hello_world.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# serializer version: 1
# name: test_generic_alias1
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=[])
# ---
13 changes: 0 additions & 13 deletions python/tests/coffeeshop.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from typing_extensions import Literal, NotRequired, TypedDict, Annotated, Doc

from typechat import python_type_to_typescript_schema


class UnknownText(TypedDict):
"""
Represents any text that could not be understood.
Expand Down Expand Up @@ -140,13 +137,3 @@ class LineItem(TypedDict):
class Cart(TypedDict):
type: Literal["Cart"]
items: list[LineItem | UnknownText]

result = python_type_to_typescript_schema(Cart)

print(f"// Entry point is: '{result.typescript_type_reference}'")
print("// TypeScript Schema:\n")
print(result.typescript_schema_str)
if result.errors:
print("// Errors:")
for err in result.errors:
print(f"// - {err}\n")
12 changes: 0 additions & 12 deletions python/tests/generic_alias_1.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from typing import Literal, TypedDict, TypeVar, Generic
from typing_extensions import TypeAliasType

from typechat import python_type_to_typescript_schema

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


Expand All @@ -17,13 +15,3 @@ class Second(Generic[T], TypedDict):


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

result = python_type_to_typescript_schema(FirstOrSecond)

print(f"// Entry point is: '{result.typescript_type_reference}'")
print("// TypeScript Schema:\n")
print(result.typescript_schema_str)
if result.errors:
print("// Errors:")
for err in result.errors:
print(f"// - {err}\n")
12 changes: 0 additions & 12 deletions python/tests/generic_alias_2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from typing import Literal, TypedDict, Generic, TypeVar

from typechat import python_type_to_typescript_schema
from typing_extensions import TypeAliasType

T = TypeVar("T", covariant=True)
Expand All @@ -22,13 +20,3 @@ class Second(Generic[T], TypedDict):
class Nested(TypedDict):
item: FirstOrSecond[str]


result = python_type_to_typescript_schema(Nested)

print(f"// Entry point is: '{result.typescript_type_reference}'")
print("// TypeScript Schema:\n")
print(result.typescript_schema_str)
if result.errors:
print("// Errors:")
for err in result.errors:
print(f"// - {err}\n")
13 changes: 0 additions & 13 deletions python/tests/hello_dataclasses.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@

from typing import Annotated
from typechat import python_type_to_typescript_schema

from dataclasses import dataclass, field

@dataclass
Expand All @@ -24,14 +22,3 @@ class Response:

def do_something(self):
print(f"{self.attr_1=}")


result = python_type_to_typescript_schema(Response)

print(f"// Entry point is: '{result.typescript_type_reference}'")
print("// TypeScript Schema:\n")
print(result.typescript_schema_str)
if result.errors:
print("// Errors:")
for err in result.errors:
print(f"// - {err}\n")
12 changes: 0 additions & 12 deletions python/tests/hello_world.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Annotated, Literal, NotRequired, Optional, Required, Self, TypedDict, TypeVar, Generic
from typing_extensions import TypeAliasType
from typechat import python_type_to_typescript_schema

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

Expand Down Expand Up @@ -35,14 +34,3 @@ class E(C[str]):


D_or_E = TypeAliasType("D_or_E", D | E)


result = python_type_to_typescript_schema(D_or_E)

print(f"// Entry point is: '{result.typescript_type_reference}'")
print("// TypeScript Schema:\n")
print(result.typescript_schema_str)
if result.errors:
print("// Errors:")
for err in result.errors:
print(f"// - {err}\n")
6 changes: 6 additions & 0 deletions python/tests/test_coffeeshop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from typing_extensions import Any
from typechat import python_type_to_typescript_schema
from tests.coffeeshop import Cart

def test_coffeeshop_schema(snapshot: Any):
assert(python_type_to_typescript_schema(Cart) == snapshot)
6 changes: 6 additions & 0 deletions python/tests/test_dataclasses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from typing_extensions import Any
from typechat import python_type_to_typescript_schema
from tests.hello_dataclasses import Response

def test_data_classes(snapshot: Any):
assert(python_type_to_typescript_schema(Response) == snapshot)
7 changes: 7 additions & 0 deletions python/tests/test_generic_alias_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from typing_extensions import Any
from typechat import python_type_to_typescript_schema
from tests.generic_alias_2 import Nested

def test_generic_alias1(snapshot : Any):
pcdeadeasy marked this conversation as resolved.
Show resolved Hide resolved
assert(python_type_to_typescript_schema(Nested) == snapshot)

6 changes: 6 additions & 0 deletions python/tests/test_generric_alias_1.py
pcdeadeasy marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from typing_extensions import Any
from typechat import python_type_to_typescript_schema
from tests.generic_alias_1 import FirstOrSecond

def test_generic_alias1(snapshot : Any):
pcdeadeasy marked this conversation as resolved.
Show resolved Hide resolved
assert(python_type_to_typescript_schema(FirstOrSecond) == snapshot)
6 changes: 6 additions & 0 deletions python/tests/test_hello_world.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from typing_extensions import Any
from typechat import python_type_to_typescript_schema
from tests.hello_world import D_or_E

def test_generic_alias1(snapshot: Any):
assert(python_type_to_typescript_schema(D_or_E) == snapshot)
Loading