Skip to content

Commit 82ea45c

Browse files
committed
adding tests for converting rows to different data types
1 parent a152f53 commit 82ea45c

File tree

2 files changed

+66
-9
lines changed

2 files changed

+66
-9
lines changed

piccolo/query/methods/alter.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,18 @@ def querystring(self) -> QueryString:
9292

9393

9494
@dataclass
95-
class SetColumnType(AlterColumnStatement):
96-
__slots__ = ("column_name", "column")
95+
class SetColumnType:
96+
__slots__ = ("old_column", "new_column")
9797

98-
column_name: str
99-
column: Column
98+
old_column: Column
99+
new_column: Column
100100

101101
@property
102102
def querystring(self) -> QueryString:
103+
self.new_column._meta._table = self.old_column._meta.table
104+
column_name = self.old_column._meta.name
103105
return QueryString(
104-
f"ALTER COLUMN {self.column_name} TYPE {self.column.column_type}"
106+
f"ALTER COLUMN {column_name} TYPE {self.new_column.column_type}"
105107
)
106108

107109

@@ -349,12 +351,12 @@ def rename_column(
349351
self._rename_columns.append(RenameColumn(column, new_name))
350352
return self
351353

352-
def set_column_type(self, column_name: str, column: Column) -> Alter:
354+
def set_column_type(self, old_column: Column, new_column: Column) -> Alter:
353355
"""
354356
Change the type of a column.
355357
"""
356358
self._set_column_type.append(
357-
SetColumnType(column_name=column_name, column=column)
359+
SetColumnType(old_column=old_column, new_column=new_column)
358360
)
359361
return self
360362

tests/table/test_alter.py

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
from piccolo.columns.column_types import Varchar
12
from unittest import TestCase
23

3-
from piccolo.columns import Integer, Numeric
4+
from piccolo.columns import BigInt, Integer, Numeric
45
from piccolo.table import Table
56

67
from ..base import DBTestCase, postgres_only
@@ -113,7 +114,6 @@ def test_unique(self):
113114
self.assertTrue(len(response), 2)
114115

115116

116-
# TODO - make it work for SQLite. Should work.
117117
@postgres_only
118118
class TestMultiple(DBTestCase):
119119
"""
@@ -137,6 +137,61 @@ def test_multiple(self):
137137
self.assertTrue("column_b" in column_names)
138138

139139

140+
@postgres_only
141+
class TestSetColumnType(DBTestCase):
142+
def test_integer_to_bigint(self):
143+
"""
144+
Test converting an Integer column to BigInt.
145+
"""
146+
self.insert_row()
147+
148+
alter_query = Band.alter().set_column_type(
149+
old_column=Band.popularity, new_column=BigInt()
150+
)
151+
alter_query.run_sync()
152+
153+
query = """
154+
SELECT data_type FROM information_schema.columns
155+
WHERE table_name = 'band'
156+
AND table_catalog = 'piccolo'
157+
AND column_name = 'popularity'
158+
"""
159+
160+
response = Band.raw(query).run_sync()
161+
self.assertEqual(response[0]["data_type"].upper(), "BIGINT")
162+
163+
popularity = (
164+
Band.select(Band.popularity).first().run_sync()["popularity"]
165+
)
166+
self.assertEqual(popularity, 1000)
167+
168+
def test_integer_to_varchar(self):
169+
"""
170+
Test converting an Integer column to Varchar.
171+
"""
172+
self.insert_row()
173+
174+
alter_query = Band.alter().set_column_type(
175+
old_column=Band.popularity, new_column=Varchar()
176+
)
177+
alter_query.run_sync()
178+
179+
query = """
180+
SELECT data_type FROM information_schema.columns
181+
WHERE table_name = 'band'
182+
AND table_catalog = 'piccolo'
183+
AND column_name = 'popularity'
184+
"""
185+
186+
response = Band.raw(query).run_sync()
187+
self.assertEqual(response[0]["data_type"].upper(), "CHARACTER VARYING")
188+
189+
popularity = (
190+
Band.select(Band.popularity).first().run_sync()["popularity"]
191+
)
192+
self.assertEqual(popularity, "1000")
193+
194+
140195
@postgres_only
141196
class TestSetNull(DBTestCase):
142197
def test_set_null(self):

0 commit comments

Comments
 (0)