|
8 | 8 | from postgres.cursors import TooFew, TooMany, SimpleDictCursor
|
9 | 9 | from postgres.orm import ReadOnly, Model
|
10 | 10 | from psycopg2 import InterfaceError, ProgrammingError
|
11 |
| -from pytest import raises |
| 11 | +from pytest import mark, raises |
12 | 12 |
|
13 | 13 |
|
14 | 14 | DATABASE_URL = os.environ['DATABASE_URL']
|
@@ -334,6 +334,32 @@ def test_unregister_unregisters_multiple(self):
|
334 | 334 | self.db.unregister_model(self.MyModel)
|
335 | 335 | assert self.db.model_registry == {}
|
336 | 336 |
|
| 337 | + def test_add_column_doesnt_break_anything(self): |
| 338 | + self.db.run("ALTER TABLE foo ADD COLUMN boo text") |
| 339 | + one = self.db.one("SELECT foo.*::foo FROM foo WHERE bar='baz'") |
| 340 | + assert one.boo is None |
| 341 | + |
| 342 | + def test_replace_column_different_type(self): |
| 343 | + self.db.run("CREATE TABLE grok (bar int)") |
| 344 | + self.db.run("INSERT INTO grok VALUES (0)") |
| 345 | + class EmptyModel(Model): pass |
| 346 | + self.db.register_model(EmptyModel, 'grok') |
| 347 | + # Add a new column then drop the original one |
| 348 | + self.db.run("ALTER TABLE grok ADD COLUMN biz text NOT NULL DEFAULT 'x'") |
| 349 | + self.db.run("ALTER TABLE grok DROP COLUMN bar") |
| 350 | + # The number of columns hasn't changed but the names and types have |
| 351 | + one = self.db.one("SELECT grok.*::grok FROM grok LIMIT 1") |
| 352 | + assert one.biz == 'x' |
| 353 | + assert not hasattr(one, 'bar') |
| 354 | + |
| 355 | + @mark.xfail(raises=AttributeError) |
| 356 | + def test_replace_column_same_type_different_name(self): |
| 357 | + self.db.run("ALTER TABLE foo ADD COLUMN biz text NOT NULL DEFAULT 0") |
| 358 | + self.db.run("ALTER TABLE foo DROP COLUMN bar") |
| 359 | + one = self.db.one("SELECT foo.*::foo FROM foo LIMIT 1") |
| 360 | + assert one.biz == 0 |
| 361 | + assert not hasattr(one, 'bar') |
| 362 | + |
337 | 363 |
|
338 | 364 | # cursor_factory
|
339 | 365 | # ==============
|
|
0 commit comments