This repository has been archived by the owner on Dec 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_postgresql_migrator.py
94 lines (76 loc) · 2.96 KB
/
test_postgresql_migrator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""Cheetah ORM - SQLite Migrator Unit Tests"""
import unittest
from cheetah_orm.fields import FloatField, IntField, StringField
from cheetah_orm.indexes import UniqueIndex
from cheetah_orm.mappers import PostgreSQLMapper
from cheetah_orm.migrators import PostgreSQLMigrator
from cheetah_orm.model import DataModel
# Classes
# =======
class PlayerStats(DataModel):
table = "player_stats"
name = StringField(length=32, not_null=True)
highscore = IntField(not_null=True)
name_idx = UniqueIndex("name")
class TestPostgreSQLMigrator(unittest.TestCase):
"""PostgreSQL migrator tests."""
@classmethod
def setUpClass(cls):
"""Setup test suite."""
# Establish a database connection
cls.mapper = PostgreSQLMapper()
cls.mapper.connect(host="localhost", user="tester", password="test", dbname="testing")
# Cleanup last test
cls.mapper._cur.execute('DROP TABLE IF EXISTS "player_stats";')
cls.mapper._cur.execute('DROP TABLE IF EXISTS "migration_metadata";')
# Initialize data models
cls.mapper.init_model(PlayerStats)
# Initialize migrator
cls.migrator = PostgreSQLMigrator(cls.mapper)
@classmethod
def tearDownClass(cls):
"""Cleanup test suite."""
# Disconnect from the database
cls.mapper.disconnect()
def test_1_init_metadata(self):
"""Test migration metadata initialization."""
# Init data model migration metadata
self.migrator.migrate(PlayerStats)
# Add some player stats
dylan = PlayerStats(
name="Dylan",
highscore=100
)
emmi = PlayerStats(
name="Emmi",
highscore=100
)
self.mapper.save_model(dylan)
self.mapper.save_model(emmi)
self.mapper.commit()
def test_2_add_field(self):
"""Test field adding."""
# Redefine the player stats data model
class PlayerStats(DataModel):
table = "player_stats"
name = StringField(length=32, not_null=True)
highscore = IntField(not_null=True)
best_time = IntField(default=0, not_null=True)
name_idx = UniqueIndex("name")
# Apply migrations
self.migrator.migrate(PlayerStats)
def test_3_change_field_type(self):
"""Test field type changing."""
# Redefine the player stats data model
class PlayerStats(DataModel):
table = "player_stats"
name = StringField(length=32, not_null=True)
highscore = IntField(not_null=True)
best_time = FloatField(default=0, not_null=True)
name_idx = UniqueIndex("name")
# Apply migrations
self.migrator.migrate(PlayerStats)
def test_4_remove_field(self):
"""Test field removal."""
# Apply migrations (this will revert back to the original data model we defined globally)
self.migrator.migrate(PlayerStats)