1+ from __future__ import annotations
12import os
23import shutil
34import tempfile
45import time
56import typing as t
67from unittest import TestCase
8+ import uuid
79
10+ from piccolo .columns .defaults .uuid import UUID4
811from piccolo .conf .apps import AppConfig
9- from piccolo .columns .column_types import Integer , Varchar
12+ from piccolo .columns .column_types import Integer , Text , UUID , Varchar
1013from piccolo .apps .migrations .commands .new import (
1114 _create_new_migration ,
1215 _create_migrations_folder ,
1720from piccolo .utils .sync import run_sync
1821from tests .base import postgres_only
1922
23+ if t .TYPE_CHECKING :
24+ from piccolo .columns .base import Column
25+
26+
27+ def string_default ():
28+ return "hello world"
29+
30+
31+ def integer_default ():
32+ return 1
33+
2034
2135@postgres_only
2236class TestMigrations (TestCase ):
@@ -39,8 +53,7 @@ def _test_migrations(self, table_classes: t.List[t.Type[Table]]):
3953
4054 if os .path .exists (migrations_folder_path ):
4155 shutil .rmtree (migrations_folder_path )
42-
43- _create_migrations_folder (migrations_folder_path )
56+ _create_migrations_folder (migrations_folder_path )
4457
4558 app_config = AppConfig (
4659 app_name = "test_app" ,
@@ -64,44 +77,79 @@ def _test_migrations(self, table_classes: t.List[t.Type[Table]]):
6477 # TODO - check the migrations ran correctly
6578
6679 ###########################################################################
67- # Writing all of these by hand is going to be really tedious ...
6880
69- def test_add_varchar_column (self ):
81+ def table (self , column : Column ):
82+ return create_table_class (
83+ class_name = "MyTable" , class_members = {"my_column" : column }
84+ )
85+
86+ def test_varchar_column (self ):
7087 self ._test_migrations (
7188 table_classes = [
72- create_table_class ("MyTable" ),
73- create_table_class (
74- "MyTable" , class_members = {"name" : Varchar ()}
75- ),
89+ self .table (column )
90+ for column in [
91+ Varchar (),
92+ Varchar (length = 100 ),
93+ Varchar (default = "hello world" ),
94+ Varchar (default = string_default ),
95+ Varchar (null = True ),
96+ Varchar (null = False ),
97+ Varchar (index = True ),
98+ Varchar (index = False ),
99+ ]
76100 ]
77101 )
78102
79- def test_remove_varchar_column (self ):
103+ def test_text_column (self ):
80104 self ._test_migrations (
81105 table_classes = [
82- create_table_class (
83- "MyTable" , class_members = {"name" : Varchar ()}
84- ),
85- create_table_class ("MyTable" ),
106+ self .table (column )
107+ for column in [
108+ Text (),
109+ Text (default = "hello world" ),
110+ Text (default = string_default ),
111+ Text (null = True ),
112+ Text (null = False ),
113+ Text (index = True ),
114+ Text (index = False ),
115+ ]
86116 ]
87117 )
88118
89- def test_add_integer_column (self ):
119+ def test_integer_column (self ):
90120 self ._test_migrations (
91121 table_classes = [
92- create_table_class ("MyTable" ),
93- create_table_class (
94- "MyTable" , class_members = {"name" : Integer ()}
95- ),
122+ self .table (column )
123+ for column in [
124+ Integer (),
125+ Integer (default = 1 ),
126+ Integer (default = integer_default ),
127+ Integer (null = True ),
128+ Integer (null = False ),
129+ Integer (index = True ),
130+ Integer (index = False ),
131+ ]
96132 ]
97133 )
98134
99- def test_remove_integer_column (self ):
135+ def test_uuid_column (self ):
100136 self ._test_migrations (
101137 table_classes = [
102- create_table_class (
103- "MyTable" , class_members = {"name" : Integer ()}
104- ),
105- create_table_class ("MyTable" ),
138+ self .table (column )
139+ for column in [
140+ UUID (),
141+ # UUID(default="ecf338cd-6da7-464c-b31e-4b2e3e12f0f0"),
142+ # UUID(
143+ # default=uuid.UUID(
144+ # "2dfc9c47-adab-4692-b804-f692f3b0fc07"
145+ # )
146+ # ),
147+ UUID (default = uuid .uuid4 ),
148+ UUID (default = UUID4 ),
149+ UUID (null = True ),
150+ UUID (null = False ),
151+ UUID (index = True ),
152+ UUID (index = False ),
153+ ]
106154 ]
107155 )
0 commit comments