1- import enum
2- import uuid
1+ import importlib
32
3+ import pytest
44from sqlalchemy import create_mock_engine
55from sqlalchemy .sql .type_api import TypeEngine
6- from sqlmodel import Field , SQLModel
6+ from sqlmodel import SQLModel
77
8+ from . import test_enums_models
89from .conftest import needs_pydanticv1 , needs_pydanticv2
910
1011"""
1617"""
1718
1819
19- class MyEnum1 (str , enum .Enum ):
20- A = "A"
21- B = "B"
22-
23-
24- class MyEnum2 (str , enum .Enum ):
25- C = "C"
26- D = "D"
27-
28-
29- class BaseModel (SQLModel ):
30- id : uuid .UUID = Field (primary_key = True )
31- enum_field : MyEnum2
32-
33-
34- class FlatModel (SQLModel , table = True ):
35- id : uuid .UUID = Field (primary_key = True )
36- enum_field : MyEnum1
37-
38-
39- class InheritModel (BaseModel , table = True ):
40- pass
41-
42-
4320def pg_dump (sql : TypeEngine , * args , ** kwargs ):
4421 dialect = sql .compile (dialect = postgres_engine .dialect )
4522 sql_str = str (dialect ).rstrip ()
@@ -58,25 +35,29 @@ def sqlite_dump(sql: TypeEngine, *args, **kwargs):
5835sqlite_engine = create_mock_engine ("sqlite://" , sqlite_dump )
5936
6037
61- def test_postgres_ddl_sql (capsys ):
38+ def test_postgres_ddl_sql (clear_sqlmodel , capsys : pytest .CaptureFixture [str ]):
39+ assert test_enums_models , "Ensure the models are imported and registered"
40+ importlib .reload (test_enums_models )
6241 SQLModel .metadata .create_all (bind = postgres_engine , checkfirst = False )
6342
6443 captured = capsys .readouterr ()
6544 assert "CREATE TYPE myenum1 AS ENUM ('A', 'B');" in captured .out
6645 assert "CREATE TYPE myenum2 AS ENUM ('C', 'D');" in captured .out
6746
6847
69- def test_sqlite_ddl_sql (capsys ):
48+ def test_sqlite_ddl_sql (clear_sqlmodel , capsys : pytest .CaptureFixture [str ]):
49+ assert test_enums_models , "Ensure the models are imported and registered"
50+ importlib .reload (test_enums_models )
7051 SQLModel .metadata .create_all (bind = sqlite_engine , checkfirst = False )
7152
7253 captured = capsys .readouterr ()
73- assert "enum_field VARCHAR(1) NOT NULL" in captured .out
54+ assert "enum_field VARCHAR(1) NOT NULL" in captured .out , captured
7455 assert "CREATE TYPE" not in captured .out
7556
7657
7758@needs_pydanticv1
7859def test_json_schema_flat_model_pydantic_v1 ():
79- assert FlatModel .schema () == {
60+ assert test_enums_models . FlatModel .schema () == {
8061 "title" : "FlatModel" ,
8162 "type" : "object" ,
8263 "properties" : {
@@ -97,7 +78,7 @@ def test_json_schema_flat_model_pydantic_v1():
9778
9879@needs_pydanticv1
9980def test_json_schema_inherit_model_pydantic_v1 ():
100- assert InheritModel .schema () == {
81+ assert test_enums_models . InheritModel .schema () == {
10182 "title" : "InheritModel" ,
10283 "type" : "object" ,
10384 "properties" : {
@@ -118,7 +99,7 @@ def test_json_schema_inherit_model_pydantic_v1():
11899
119100@needs_pydanticv2
120101def test_json_schema_flat_model_pydantic_v2 ():
121- assert FlatModel .model_json_schema () == {
102+ assert test_enums_models . FlatModel .model_json_schema () == {
122103 "title" : "FlatModel" ,
123104 "type" : "object" ,
124105 "properties" : {
@@ -134,7 +115,7 @@ def test_json_schema_flat_model_pydantic_v2():
134115
135116@needs_pydanticv2
136117def test_json_schema_inherit_model_pydantic_v2 ():
137- assert InheritModel .model_json_schema () == {
118+ assert test_enums_models . InheritModel .model_json_schema () == {
138119 "title" : "InheritModel" ,
139120 "type" : "object" ,
140121 "properties" : {
0 commit comments