From 0f10058fd38f1b97068320fe62eb2494803a263a Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 15 Jan 2012 11:35:26 -0600 Subject: A theoretical set of models to migrate about with, plus one migration ;) --- mediagoblin/tests/test_sql_migrations.py | 177 +++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 mediagoblin/tests/test_sql_migrations.py (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py new file mode 100644 index 00000000..32950d38 --- /dev/null +++ b/mediagoblin/tests/test_sql_migrations.py @@ -0,0 +1,177 @@ +# GNU MediaGoblin -- federated, autonomous media hosting +# Copyright (C) 2011 MediaGoblin contributors. See AUTHORS. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import copy + +from sqlalchemy import ( + Table, Column, MetaData, + Integer, Unicode, UnicodeText, DateTime, Boolean, ForeignKey, + UniqueConstraint, PickleType) +from sqlalchemy.ext.declarative import declarative_base +from migrate import changeset + +from mediagoblin.db.sql.base import GMGTableBase + + +# This one will get filled with local migrations +FULL_MIGRATIONS = {} + + +####################################################### +# Migration set 1: Define initial models, no migrations +####################################################### + +Base1 = declarative_base(cls=GMGTableBase) + +class Creature1(Base1): + __tablename__ = "creature" + + id = Column(Integer, primary_key=True) + name = Column(Unicode, unique=True, nullable=False, index=True) + num_legs = Column(Integer, nullable=False) + is_demon = Column(Boolean) + +class Level1(Base1): + __tablename__ = "level" + + id = Column(Integer, primary_key=True) + name = Column(Unicode, unique=True, nullable=False, index=True) + description = Column(UnicodeText) + exits = Column(PickleType) + +SET1_MODELS = [Creature1, Level1] + +SET1_MIGRATIONS = [] + +####################################################### +# Migration set 2: A few migrations and new model +####################################################### + +Base2 = declarative_base(cls=GMGTableBase) + +class Creature2(Base2): + __tablename__ = "creature" + + id = Column(Integer, primary_key=True) + name = Column(Unicode, unique=True, nullable=False, index=True) + num_legs = Column(Integer, nullable=False) + +class CreaturePower2(Base2): + __tablename__ = "creature_power" + + id = Column(Integer, primary_key=True) + creature = Column( + Integer, ForeignKey('creature.id'), nullable=False) + name = Column(Unicode) + description = Column(Unicode) + +class Level2(Base2): + __tablename__ = "level" + + id = Column(Integer, primary_key=True) + name = Column(Unicode) + description = Column(UnicodeText) + +class LevelExit2(Base2): + __tablename__ = "level_exit" + + id = Column(Integer, primary_key=True) + name = Column(Unicode) + from_level = Column( + Integer, ForeignKey('level.id'), nullable=False) + to_level = Column( + Integer, ForeignKey('level.id'), nullable=False) + +SET2_MODELS = [Creature2, CreaturePower2, Level2, LevelExit2] + + +@RegisterMigration(1, FULL_MIGRATIONS) +def creature_remove_is_demon(db_conn): + creature_table = Table( + 'creature', MetaData(), + autoload=True, autoload_with=db_conn.engine) + db_conn.execute( + creature_table.drop_column('is_demon')) + + +@RegisterMigration(2, FULL_MIGRATIONS) +def creature_powers_new_table(db_conn): + pass + +@RegisterMigration(3, FULL_MIGRATIONS) +def level_exits_new_table(db_conn): + pass + + +# A hack! At this point we freeze-fame and get just a partial list of +# migrations + +PARTIAL_MIGRATIONS = copy.copy(FULL_MIGRATIONS) + +####################################################### +# Migration set 3: Final migrations +####################################################### + +Base3 = declarative_base(cls=GMGTableBase) + +class Creature3(Base3): + __tablename__ = "creature" + + id = Column(Integer, primary_key=True) + name = Column(Unicode, unique=True, nullable=False, index=True) + num_limbs= Column(Integer, nullable=False) + +class CreaturePower3(Base3): + __tablename__ = "creature_power" + + id = Column(Integer, primary_key=True) + creature = Column( + Integer, ForeignKey('creature.id'), nullable=False, index=True) + name = Column(Unicode) + description = Column(Unicode) + +class Level3(Base3): + __tablename__ = "level" + + id = Column(Integer, primary_key=True) + name = Column(Unicode) + description = Column(UnicodeText) + +class LevelExit3(Base3): + __tablename__ = "level_exit" + + id = Column(Integer, primary_key=True) + name = Column(Unicode) + from_level = Column( + Integer, ForeignKey('level.id'), nullable=False, index=True) + to_level = Column( + Integer, ForeignKey('level.id'), nullable=False, index=True) + + +SET3_MODELS = [Creature3, CreaturePower3, Level3, LevelExit3] + + +@RegisterMigration(4, FULL_MIGRATIONS) +def creature_num_legs_to_num_limbs(db_conn): + pass + +@RegisterMigration(5, FULL_MIGRATIONS) +def level_exit_index_from_and_to_level(db_conn): + pass + +@RegisterMigration(6, FULL_MIGRATIONS) +def creature_power_index_creature(db_conn): + pass -- cgit v1.2.3 From 129c36be6f29977ca18ae719c1d3a5c2aad25d89 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 15 Jan 2012 11:36:00 -0600 Subject: Might as well call it "set2 migrations" --- mediagoblin/tests/test_sql_migrations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 32950d38..8682e69d 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -119,7 +119,7 @@ def level_exits_new_table(db_conn): # A hack! At this point we freeze-fame and get just a partial list of # migrations -PARTIAL_MIGRATIONS = copy.copy(FULL_MIGRATIONS) +SET2_MIGRATIONS = copy.copy(FULL_MIGRATIONS) ####################################################### # Migration set 3: Final migrations -- cgit v1.2.3 From 89694d6d69c855f1dbaaa725867ab070cc7b2490 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 15 Jan 2012 14:55:07 -0600 Subject: More test migration work. Closing to working migrations for set 2... Also, this file is written in 2012, correct that ;) --- mediagoblin/tests/test_sql_migrations.py | 67 +++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 5 deletions(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 8682e69d..0f1f02bd 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -1,5 +1,5 @@ # GNU MediaGoblin -- federated, autonomous media hosting -# Copyright (C) 2011 MediaGoblin contributors. See AUTHORS. +# Copyright (C) 2012, 2012 MediaGoblin contributors. See AUTHORS. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by @@ -18,9 +18,10 @@ import copy from sqlalchemy import ( Table, Column, MetaData, - Integer, Unicode, UnicodeText, DateTime, Boolean, ForeignKey, - UniqueConstraint, PickleType) + Integer, Float, Unicode, UnicodeText, DateTime, Boolean, + ForeignKey, UniqueConstraint, PickleType) from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.sql import select, insert from migrate import changeset from mediagoblin.db.sql.base import GMGTableBase @@ -77,6 +78,7 @@ class CreaturePower2(Base2): Integer, ForeignKey('creature.id'), nullable=False) name = Column(Unicode) description = Column(Unicode) + hitpower = Column(Integer, nullable=False) class Level2(Base2): __tablename__ = "level" @@ -109,11 +111,61 @@ def creature_remove_is_demon(db_conn): @RegisterMigration(2, FULL_MIGRATIONS) def creature_powers_new_table(db_conn): - pass + metadata = MetaData() + creature_powers = Table( + 'creature_power', metadata, + Column('id', Integer, primary_key=True), + Column('creature', + Integer, ForeignKey('creature.id'), nullable=False), + Column('name', Unicode), + Column('description', Unicode), + Column('hitpower', Integer, nullable=False)) + metadata.create_all(db_conn.engine) + @RegisterMigration(3, FULL_MIGRATIONS) def level_exits_new_table(db_conn): - pass + # First, create the table + # ----------------------- + metadata = MetaData() + level_exits = Table( + 'level_exit', metadata, + Column('id', Integer, primary_key=True), + Column('name', Unicode), + Column('from_level', + Integer, ForeignKey('level.id'), nullable=False), + Column('to_level', + Integer, ForeignKey('level.id'), nullable=False)) + metadata.create_all(db_conn.engine) + + # And now, convert all the old exit pickles to new level exits + # ------------------------------------------------------------ + + # Minimal representation of level table. + # Not auto-introspecting here because of pickle table. I'm not + # sure sqlalchemy can auto-introspect pickle columns. + levels = Table( + 'level', metadata, + Column('id', Integer, primary_key=True), + Column('exits', PickleType)) + + # query over and insert + result = db_conn.execute( + select([levels], levels.c.exits!=None)) + + for level in result: + this_exit = level['exits'] + + # Insert the level exit + db_conn.execute( + level_exits.insert().values( + name=this_exit['name'], + from_level=this_exit['from_level'], + to_level=this_exit['to_level'])) + + # Finally, drop the old level exits pickle table + # ---------------------------------------------- + # A hack! At this point we freeze-fame and get just a partial list of @@ -142,6 +194,7 @@ class CreaturePower3(Base3): Integer, ForeignKey('creature.id'), nullable=False, index=True) name = Column(Unicode) description = Column(Unicode) + hitpower = Column(Float, nullable=False) class Level3(Base3): __tablename__ = "level" @@ -175,3 +228,7 @@ def level_exit_index_from_and_to_level(db_conn): @RegisterMigration(6, FULL_MIGRATIONS) def creature_power_index_creature(db_conn): pass + +@RegisterMigration(7, FULL_MIGRATIONS) +def creature_power_hitpower_to_float(db_conn): + pass -- cgit v1.2.3 From 473e06053c45de534d1e9e5d94f48cf0188856af Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 15 Jan 2012 15:43:59 -0600 Subject: binding migration metadata to engine, and level_exits_new_table should now work --- mediagoblin/tests/test_sql_migrations.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 0f1f02bd..ba9e967a 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -102,16 +102,16 @@ SET2_MODELS = [Creature2, CreaturePower2, Level2, LevelExit2] @RegisterMigration(1, FULL_MIGRATIONS) def creature_remove_is_demon(db_conn): + metadata = MetaData(bind=db_conn.engine) creature_table = Table( - 'creature', MetaData(), + 'creature', metadata, autoload=True, autoload_with=db_conn.engine) - db_conn.execute( - creature_table.drop_column('is_demon')) + creature_table.drop_column('is_demon') @RegisterMigration(2, FULL_MIGRATIONS) def creature_powers_new_table(db_conn): - metadata = MetaData() + metadata = MetaData(bind=db_conn.engine) creature_powers = Table( 'creature_power', metadata, Column('id', Integer, primary_key=True), @@ -127,7 +127,7 @@ def creature_powers_new_table(db_conn): def level_exits_new_table(db_conn): # First, create the table # ----------------------- - metadata = MetaData() + metadata = MetaData(bind=db_conn.engine) level_exits = Table( 'level_exit', metadata, Column('id', Integer, primary_key=True), @@ -165,7 +165,7 @@ def level_exits_new_table(db_conn): # Finally, drop the old level exits pickle table # ---------------------------------------------- - + levels.drop_column('exits') # A hack! At this point we freeze-fame and get just a partial list of -- cgit v1.2.3 From 248b5061ec5671e5dd371c2ea1573e5b10219656 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 15 Jan 2012 16:43:14 -0600 Subject: All theoretical migrations written! --- mediagoblin/tests/test_sql_migrations.py | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index ba9e967a..d5b9b30e 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -17,7 +17,7 @@ import copy from sqlalchemy import ( - Table, Column, MetaData, + Table, Column, MetaData, Index Integer, Float, Unicode, UnicodeText, DateTime, Boolean, ForeignKey, UniqueConstraint, PickleType) from sqlalchemy.ext.declarative import declarative_base @@ -219,16 +219,36 @@ SET3_MODELS = [Creature3, CreaturePower3, Level3, LevelExit3] @RegisterMigration(4, FULL_MIGRATIONS) def creature_num_legs_to_num_limbs(db_conn): - pass + metadata = MetaData(bind=db_conn.engine) + creature_table = Table( + 'creature', metadata, + autoload=True, autoload_with=db_conn.engine) + creature_table.c.num_legs.alter(name="num_limbs") + @RegisterMigration(5, FULL_MIGRATIONS) def level_exit_index_from_and_to_level(db_conn): - pass + metadata = MetaData(bind=db_conn.engine) + level_exit = Table( + 'level_exit', metadata, + autoload=True, autoload_with=db_conn.engine) + Index('ix_from_level', level_exit.c.from_level).create(engine) + Index('ix_to_exit', level_exit.c.to_exit).create(engine) + @RegisterMigration(6, FULL_MIGRATIONS) def creature_power_index_creature(db_conn): - pass + metadata = MetaData(bind=db_conn.engine) + creature_power = Table( + 'creature_power', metadata, + autoload=True, autoload_with=db_conn.engine) + Index('ix_creature', creature_power.c.creature).create(engine) + @RegisterMigration(7, FULL_MIGRATIONS) def creature_power_hitpower_to_float(db_conn): - pass + metadata = MetaData(bind=db_conn.engine) + creature_power = Table( + 'creature_power', metadata, + autoload=True, autoload_with=db_conn.engine) + creature_power.c.hitpower.alter(type=Float) -- cgit v1.2.3 From 64d280647c1b6c1166b1e096914a590710eb0f4e Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Mon, 16 Jan 2012 14:39:24 -0600 Subject: Insert migration1 objects. Also, Level1 id from Integer->Unicode --- mediagoblin/tests/test_sql_migrations.py | 50 +++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 7 deletions(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index d5b9b30e..50bdbd9d 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -20,6 +20,7 @@ from sqlalchemy import ( Table, Column, MetaData, Index Integer, Float, Unicode, UnicodeText, DateTime, Boolean, ForeignKey, UniqueConstraint, PickleType) +from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.sql import select, insert from migrate import changeset @@ -48,7 +49,7 @@ class Creature1(Base1): class Level1(Base1): __tablename__ = "level" - id = Column(Integer, primary_key=True) + id = Column(Unicode, primary_key=True) name = Column(Unicode, unique=True, nullable=False, index=True) description = Column(UnicodeText) exits = Column(PickleType) @@ -83,7 +84,7 @@ class CreaturePower2(Base2): class Level2(Base2): __tablename__ = "level" - id = Column(Integer, primary_key=True) + id = Column(Unicode, primary_key=True) name = Column(Unicode) description = Column(UnicodeText) @@ -93,9 +94,9 @@ class LevelExit2(Base2): id = Column(Integer, primary_key=True) name = Column(Unicode) from_level = Column( - Integer, ForeignKey('level.id'), nullable=False) + Unicode, ForeignKey('level.id'), nullable=False) to_level = Column( - Integer, ForeignKey('level.id'), nullable=False) + Unicode, ForeignKey('level.id'), nullable=False) SET2_MODELS = [Creature2, CreaturePower2, Level2, LevelExit2] @@ -199,7 +200,7 @@ class CreaturePower3(Base3): class Level3(Base3): __tablename__ = "level" - id = Column(Integer, primary_key=True) + id = Column(Unicode, primary_key=True) name = Column(Unicode) description = Column(UnicodeText) @@ -209,9 +210,9 @@ class LevelExit3(Base3): id = Column(Integer, primary_key=True) name = Column(Unicode) from_level = Column( - Integer, ForeignKey('level.id'), nullable=False, index=True) + Unicode, ForeignKey('level.id'), nullable=False, index=True) to_level = Column( - Integer, ForeignKey('level.id'), nullable=False, index=True) + Unicode, ForeignKey('level.id'), nullable=False, index=True) SET3_MODELS = [Creature3, CreaturePower3, Level3, LevelExit3] @@ -252,3 +253,38 @@ def creature_power_hitpower_to_float(db_conn): 'creature_power', metadata, autoload=True, autoload_with=db_conn.engine) creature_power.c.hitpower.alter(type=Float) + + +def _insert_migration1_objects(session): + # Insert creatures + session.add_all( + [Creature1(name='centipede', + num_legs=100, + is_demon=False), + Creature1(name='wolf', + num_legs=4, + is_demon=False), + # don't ask me what a wizardsnake is. + Creature1(name='wizardsnake', + num_legs=0, + is_demon=True)]) + + session.add_all( + [Level1(id='necroplex', + name='The Necroplex', + description='A complex full of pure deathzone.', + exits={ + 'deathwell': 'evilstorm', + 'portal': 'central_park'}), + Level1(id='evilstorm', + name='Evil Storm', + description='A storm full of pure evil.', + exits={}), # you can't escape the evilstorm + Level1(id='central_park' + name='Central Park, NY, NY', + description="New York's friendly Central Park.", + exits={ + 'portal': 'necroplex'})]) + + session.commit() + -- cgit v1.2.3 From d74a9483ded26c575686b751e47c1e2a6f395fef Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Mon, 16 Jan 2012 16:22:25 -0600 Subject: Theoretical full set of migration2 objects to insert for testing --- mediagoblin/tests/test_sql_migrations.py | 77 +++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 50bdbd9d..4d58cfbf 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -20,7 +20,7 @@ from sqlalchemy import ( Table, Column, MetaData, Index Integer, Float, Unicode, UnicodeText, DateTime, Boolean, ForeignKey, UniqueConstraint, PickleType) -from sqlalchemy.orm import sessionmaker +from sqlalchemy.orm import sessionmaker, relationship from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.sql import select, insert from migrate import changeset @@ -70,6 +70,7 @@ class Creature2(Base2): id = Column(Integer, primary_key=True) name = Column(Unicode, unique=True, nullable=False, index=True) num_legs = Column(Integer, nullable=False) + magical_powers = relationship("CreaturePower2") class CreaturePower2(Base2): __tablename__ = "creature_power" @@ -196,6 +197,7 @@ class CreaturePower3(Base3): name = Column(Unicode) description = Column(Unicode) hitpower = Column(Float, nullable=False) + magical_powers = relationship("CreaturePower3") class Level3(Base3): __tablename__ = "level" @@ -269,6 +271,7 @@ def _insert_migration1_objects(session): num_legs=0, is_demon=True)]) + # Insert levels session.add_all( [Level1(id='necroplex', name='The Necroplex', @@ -288,3 +291,75 @@ def _insert_migration1_objects(session): session.commit() + +def _insert_migration2_objects(session): + # Insert creatures + session.add_all( + [Creature2( + name='centipede', + num_legs=100), + Creature2( + name='wolf', + num_legs=4, + magical_powers = [ + CreaturePower2( + name="ice breath", + description="A blast of icy breath!", + hitpower=20), + CreaturePower2( + name="death stare", + description="A frightening stare, for sure!", + hitpower=45)]), + Creature2( + name='wizardsnake', + num_legs=0, + magical_powers=[ + CreaturePower2( + name='death_rattle', + description='A rattle... of DEATH!', + hitpower=1000), + CreaturePower2( + name='sneaky_stare', + description="The sneakiest stare you've ever seen!" + hitpower=300), + CreaturePower2( + name='slithery_smoke', + description="A blast of slithery, slithery smoke.", + hitpower=10), + CreaturePower2( + name='treacherous_tremors', + description="The ground shakes beneath footed animals!", + hitpower=0)])]) + + # Insert levels + session.add_all( + [Level2(id='necroplex', + name='The Necroplex', + description='A complex full of pure deathzone.'), + Level2(id='evilstorm', + name='Evil Storm', + description='A storm full of pure evil.', + exits=[]), # you can't escape the evilstorm + Level2(id='central_park' + name='Central Park, NY, NY', + description="New York's friendly Central Park.")]) + + # necroplex exits + session.add_all( + [LevelExit2(name='deathwell', + from_level='necroplex', + to_level='evilstorm'), + LevelExit2(name='portal', + from_level='necroplex', + to_level='central_park')]) + + # there are no evilstorm exits because there is no exit from the + # evilstorm + + # central park exits + session.add_all( + [LevelExit2(name='portal', + from_level='central_park', + to_level='necroplex')] + + session.commit() -- cgit v1.2.3 From 356654deb878d667806c347e47500a2dd0dadb09 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Mon, 16 Jan 2012 16:40:51 -0600 Subject: Docstrings for stage 2 migrations --- mediagoblin/tests/test_sql_migrations.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 4d58cfbf..ccaf60ba 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -104,6 +104,10 @@ SET2_MODELS = [Creature2, CreaturePower2, Level2, LevelExit2] @RegisterMigration(1, FULL_MIGRATIONS) def creature_remove_is_demon(db_conn): + """ + Remove the is_demon field from the creature model. We don't need + it! + """ metadata = MetaData(bind=db_conn.engine) creature_table = Table( 'creature', metadata, @@ -113,6 +117,11 @@ def creature_remove_is_demon(db_conn): @RegisterMigration(2, FULL_MIGRATIONS) def creature_powers_new_table(db_conn): + """ + Add a new table for creature powers. Nothing needs to go in it + yet though as there wasn't anything that previously held this + information + """ metadata = MetaData(bind=db_conn.engine) creature_powers = Table( 'creature_power', metadata, @@ -127,6 +136,10 @@ def creature_powers_new_table(db_conn): @RegisterMigration(3, FULL_MIGRATIONS) def level_exits_new_table(db_conn): + """ + Make a new table for level exits and move the previously pickled + stuff over to here (then drop the old unneeded table) + """ # First, create the table # ----------------------- metadata = MetaData(bind=db_conn.engine) @@ -258,6 +271,9 @@ def creature_power_hitpower_to_float(db_conn): def _insert_migration1_objects(session): + """ + Test objects to insert for the first set of things + """ # Insert creatures session.add_all( [Creature1(name='centipede', @@ -293,6 +309,9 @@ def _insert_migration1_objects(session): def _insert_migration2_objects(session): + """ + Test objects to insert for the second set of things + """ # Insert creatures session.add_all( [Creature2( -- cgit v1.2.3 From d6cdf64b4f498fabfcf8262c3f444b1d4c2147df Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Mon, 16 Jan 2012 16:59:14 -0600 Subject: Wrote some (semi-silly) descriptions of each migration --- mediagoblin/tests/test_sql_migrations.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index ccaf60ba..4117ce0c 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -235,6 +235,11 @@ SET3_MODELS = [Creature3, CreaturePower3, Level3, LevelExit3] @RegisterMigration(4, FULL_MIGRATIONS) def creature_num_legs_to_num_limbs(db_conn): + """ + Turns out we're tracking all sorts of limbs, not "legs" + specifically. Humans would be 4 here, for instance. So we + renamed the column. + """ metadata = MetaData(bind=db_conn.engine) creature_table = Table( 'creature', metadata, @@ -244,6 +249,9 @@ def creature_num_legs_to_num_limbs(db_conn): @RegisterMigration(5, FULL_MIGRATIONS) def level_exit_index_from_and_to_level(db_conn): + """ + Index the from and to levels of the level exit table. + """ metadata = MetaData(bind=db_conn.engine) level_exit = Table( 'level_exit', metadata, @@ -254,6 +262,9 @@ def level_exit_index_from_and_to_level(db_conn): @RegisterMigration(6, FULL_MIGRATIONS) def creature_power_index_creature(db_conn): + """ + Index our foreign key relationship to the creatures + """ metadata = MetaData(bind=db_conn.engine) creature_power = Table( 'creature_power', metadata, @@ -263,6 +274,13 @@ def creature_power_index_creature(db_conn): @RegisterMigration(7, FULL_MIGRATIONS) def creature_power_hitpower_to_float(db_conn): + """ + Convert hitpower column on creature power table from integer to + float. + + Turns out we want super precise values of how much hitpower there + really is. + """ metadata = MetaData(bind=db_conn.engine) creature_power = Table( 'creature_power', metadata, -- cgit v1.2.3 From dc3db4681f0e3c99914dac99945a399a045f895f Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Thu, 19 Jan 2012 21:32:37 -0600 Subject: Insert migration objects round 3 --- mediagoblin/tests/test_sql_migrations.py | 87 ++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 4117ce0c..96324d77 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -400,3 +400,90 @@ def _insert_migration2_objects(session): to_level='necroplex')] session.commit() + + +def _insert_migration3_objects(session): + """ + Test objects to insert for the third set of things + """ + # Insert creatures + session.add_all( + [Creature3( + name='centipede', + num_limbs=100), + Creature3( + name='wolf', + num_limbs=4, + magical_powers = [ + CreaturePower3( + name="ice breath", + description="A blast of icy breath!", + hitpower=20.0), + CreaturePower3( + name="death stare", + description="A frightening stare, for sure!", + hitpower=45.0)]), + Creature3( + name='wizardsnake', + num_limbs=0, + magical_powers=[ + CreaturePower3( + name='death_rattle', + description='A rattle... of DEATH!', + hitpower=1000.0), + CreaturePower3( + name='sneaky_stare', + description="The sneakiest stare you've ever seen!" + hitpower=300.0), + CreaturePower3( + name='slithery_smoke', + description="A blast of slithery, slithery smoke.", + hitpower=10.0), + CreaturePower3( + name='treacherous_tremors', + description="The ground shakes beneath footed animals!", + hitpower=0.0)])], + # annnnnd one more to test a floating point hitpower + Creature3( + name='deity', + numb_limbs=30, + magical_powers[ + CreaturePower3( + name='smite', + description='Smitten by holy wrath!', + hitpower=9999.9)) + + # Insert levels + session.add_all( + [Level3(id='necroplex', + name='The Necroplex', + description='A complex full of pure deathzone.'), + Level3(id='evilstorm', + name='Evil Storm', + description='A storm full of pure evil.', + exits=[]), # you can't escape the evilstorm + Level3(id='central_park' + name='Central Park, NY, NY', + description="New York's friendly Central Park.")]) + + # necroplex exits + session.add_all( + [LevelExit3(name='deathwell', + from_level='necroplex', + to_level='evilstorm'), + LevelExit3(name='portal', + from_level='necroplex', + to_level='central_park')]) + + # there are no evilstorm exits because there is no exit from the + # evilstorm + + # central park exits + session.add_all( + [LevelExit3(name='portal', + from_level='central_park', + to_level='necroplex')] + + session.commit() + + -- cgit v1.2.3 From 09e2c48701f8a3fb00da68a61935e75c46a3ec3b Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sat, 21 Jan 2012 16:14:32 -0600 Subject: Wrote up some scaffolding for the actual tests --- mediagoblin/tests/test_sql_migrations.py | 56 ++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 3 deletions(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 96324d77..d94888e9 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -397,7 +397,7 @@ def _insert_migration2_objects(session): session.add_all( [LevelExit2(name='portal', from_level='central_park', - to_level='necroplex')] + to_level='necroplex')]) session.commit() @@ -451,7 +451,7 @@ def _insert_migration3_objects(session): CreaturePower3( name='smite', description='Smitten by holy wrath!', - hitpower=9999.9)) + hitpower=9999.9)))) # Insert levels session.add_all( @@ -482,8 +482,58 @@ def _insert_migration3_objects(session): session.add_all( [LevelExit3(name='portal', from_level='central_park', - to_level='necroplex')] + to_level='necroplex')]) session.commit() +def create_test_engine(): + from sqlalchemy import create_engine + engine = create_engine('sqlite:///:memory:', echo=False) + return engine + + +def test_set1_to_set3(): + # Create / connect to database + # Create tables by migrating on empty initial set + + # Install the initial set + # Check version in database + # Sanity check a few things in the database + + # Migrate + # Make sure version matches expected + # Check all things in database match expected + pass + + +def test_set2_to_set3(): + # Create / connect to database + # Create tables by migrating on empty initial set + + # Install the initial set + # Check version in database + # Sanity check a few things in the database + + # Migrate + # Make sure version matches expected + # Check all things in database match expected + pass + + +def test_set1_to_set2_to_set3(): + # Create / connect to database + # Create tables by migrating on empty initial set + + # Install the initial set + # Check version in database + # Sanity check a few things in the database + + # Migrate + # Make sure version matches expected + # Check all things in database match expected + + # Migrate again + # Make sure version matches expected again + # Check all things in database match expected again + pass -- cgit v1.2.3 From 40f0996ab9160fe586e3d8f3fd22412ce6a34d27 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Fri, 27 Jan 2012 18:19:34 -0600 Subject: A ton more work on the SQL migration unit tests... --- mediagoblin/tests/test_sql_migrations.py | 90 ++++++++++++++++++++++++++++++-- 1 file changed, 85 insertions(+), 5 deletions(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index d94888e9..4e0a89d9 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -51,7 +51,7 @@ class Level1(Base1): id = Column(Unicode, primary_key=True) name = Column(Unicode, unique=True, nullable=False, index=True) - description = Column(UnicodeText) + description = Column(Unicode) exits = Column(PickleType) SET1_MODELS = [Creature1, Level1] @@ -87,7 +87,7 @@ class Level2(Base2): id = Column(Unicode, primary_key=True) name = Column(Unicode) - description = Column(UnicodeText) + description = Column(Unicode) class LevelExit2(Base2): __tablename__ = "level_exit" @@ -217,7 +217,7 @@ class Level3(Base3): id = Column(Unicode, primary_key=True) name = Column(Unicode) - description = Column(UnicodeText) + description = Column(Unicode) class LevelExit3(Base3): __tablename__ = "level_exit" @@ -487,21 +487,101 @@ def _insert_migration3_objects(session): session.commit() +def CollectingPrinter(object): + def __init__(self): + self.collection = [] + + def __call__(self, string): + self.collection.append(string) + + @property + def combined_string(self): + return u''.join(self.collection) + + def create_test_engine(): from sqlalchemy import create_engine engine = create_engine('sqlite:///:memory:', echo=False) - return engine + sqlalchemy.orm import sessionmaker + Session = sessionmaker(bind=engine) + return engine, Session + + +def assert_col_type(column, class): + assert isinstance(column.type, class) def test_set1_to_set3(): # Create / connect to database + engine, Session = create_test_engine() # Create tables by migrating on empty initial set + printer = CollectingPrinter + migration_manager = MigrationManager( + '__main__', SET1_MODELS, SET1_MIGRATIONS, Session(), + printer) + # Check latest migration and database current migration + assert migration_manager.latest_migration == 0 + assert migration_manager.database_current_migration == None + + result = migration_manager.init_or_migrate() + # Make sure output was "inited" + assert result == u'inited' + # Check output + assert printer.combined_string == "-> Initializing __main__... done.\n" + # Check version in database + assert migration_manager.latest_migration == 0 + assert migration_manager.database_current_migration == 0 # Install the initial set + _insert_migration1_objects(Session()) + # Try to "re-migrate" with same manager settings... nothing should happen + migration_manager = MigrationManager( + '__main__', SET1_MODELS, SET1_MIGRATIONS, Session(), + printer) + assert migration_manager.init_or_migrate() == None + # Check version in database - # Sanity check a few things in the database + assert migration_manager.latest_migration == 0 + assert migration_manager.database_current_migration == 0 + + # Sanity check a few things in the database... + metadata = MetaData(bind=db_conn.engine) + + # Check the structure of the creature table + creature_table = Table( + 'creature', metadata, + autoload=True, autoload_with=db_conn.engine) + assert set(creature_table.c.keys()) == set( + ['id', 'name', 'num_legs', 'is_demon']) + assert_col_type(creature_table.c.id, Integer) + assert_col_type(creature_table.c.name, Unicode) + assert creature_table.c.name.nullable is False + assert creature_table.c.name.index is True + assert creature_table.c.name.unique is True + assert_col_type(creature_table.c.num_legs, Integer) + assert creature_table.c.num_legs.nullable is False + assert_col_type(creature_table.c.is_demon, Boolean) + + # Check the structure of the level table + level_table = Table( + 'level', metadata, + autoload=True, autoload_with=db_conn.engine) + assert set(level_table.c.keys()) == set( + ['id', 'name', 'description', 'exits']) + assert_col_type(level_table.c.id, Integer) + assert_col_type(level_table.c.name, Unicode) + assert level_table.c.name.nullable is False + assert level_table.c.name.index is True + assert level_table.c.name.unique is True + assert_col_type(level_table.c.description, Unicode) + # Skipping exits... Not sure if we can detect pickletype, not a + # big deal regardless. + + # Now check to see if stuff seems to be in there. # Migrate + # Make sure result was "migrated" + # Check output to user # Make sure version matches expected # Check all things in database match expected pass -- cgit v1.2.3 From 94eff10deb62f6f52c270d0926e0fa74e44001a7 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 29 Jan 2012 08:58:34 -0600 Subject: unicode stuff and more bits on the actual migration method --- mediagoblin/tests/test_sql_migrations.py | 303 ++++++++++++++++++++++--------- 1 file changed, 214 insertions(+), 89 deletions(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 4e0a89d9..57a83ec5 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -50,7 +50,7 @@ class Level1(Base1): __tablename__ = "level" id = Column(Unicode, primary_key=True) - name = Column(Unicode, unique=True, nullable=False, index=True) + name = Column(Unicode)x description = Column(Unicode) exits = Column(PickleType) @@ -244,7 +244,7 @@ def creature_num_legs_to_num_limbs(db_conn): creature_table = Table( 'creature', metadata, autoload=True, autoload_with=db_conn.engine) - creature_table.c.num_legs.alter(name="num_limbs") + creature_table.c.num_legs.alter(name=u"num_limbs") @RegisterMigration(5, FULL_MIGRATIONS) @@ -294,32 +294,32 @@ def _insert_migration1_objects(session): """ # Insert creatures session.add_all( - [Creature1(name='centipede', + [Creature1(name=u'centipede', num_legs=100, is_demon=False), - Creature1(name='wolf', + Creature1(name=u'wolf', num_legs=4, is_demon=False), # don't ask me what a wizardsnake is. - Creature1(name='wizardsnake', + Creature1(name=u'wizardsnake', num_legs=0, is_demon=True)]) # Insert levels session.add_all( - [Level1(id='necroplex', - name='The Necroplex', - description='A complex full of pure deathzone.', + [Level1(id=u'necroplex', + name=u'The Necroplex', + description=u'A complex full of pure deathzone.', exits={ 'deathwell': 'evilstorm', 'portal': 'central_park'}), - Level1(id='evilstorm', - name='Evil Storm', - description='A storm full of pure evil.', + Level1(id=u'evilstorm', + name=u'Evil Storm', + description=u'A storm full of pure evil.', exits={}), # you can't escape the evilstorm - Level1(id='central_park' - name='Central Park, NY, NY', - description="New York's friendly Central Park.", + Level1(id=u'central_park' + name=u'Central Park, NY, NY', + description=u"New York's friendly Central Park.", exits={ 'portal': 'necroplex'})]) @@ -333,71 +333,71 @@ def _insert_migration2_objects(session): # Insert creatures session.add_all( [Creature2( - name='centipede', + name=u'centipede', num_legs=100), Creature2( - name='wolf', + name=u'wolf', num_legs=4, magical_powers = [ CreaturePower2( - name="ice breath", - description="A blast of icy breath!", + name=u"ice breath", + description=u"A blast of icy breath!", hitpower=20), CreaturePower2( - name="death stare", - description="A frightening stare, for sure!", + name=u"death stare", + description=u"A frightening stare, for sure!", hitpower=45)]), Creature2( - name='wizardsnake', + name=u'wizardsnake', num_legs=0, magical_powers=[ CreaturePower2( - name='death_rattle', - description='A rattle... of DEATH!', + name=u'death_rattle', + description=u'A rattle... of DEATH!', hitpower=1000), CreaturePower2( - name='sneaky_stare', - description="The sneakiest stare you've ever seen!" + name=u'sneaky_stare', + description=u"The sneakiest stare you've ever seen!" hitpower=300), CreaturePower2( - name='slithery_smoke', - description="A blast of slithery, slithery smoke.", + name=u'slithery_smoke', + description=u"A blast of slithery, slithery smoke.", hitpower=10), CreaturePower2( - name='treacherous_tremors', - description="The ground shakes beneath footed animals!", + name=u'treacherous_tremors', + description=u"The ground shakes beneath footed animals!", hitpower=0)])]) # Insert levels session.add_all( - [Level2(id='necroplex', - name='The Necroplex', - description='A complex full of pure deathzone.'), - Level2(id='evilstorm', - name='Evil Storm', - description='A storm full of pure evil.', + [Level2(id=u'necroplex', + name=u'The Necroplex', + description=u'A complex full of pure deathzone.'), + Level2(id=u'evilstorm', + name=u'Evil Storm', + description=u'A storm full of pure evil.', exits=[]), # you can't escape the evilstorm - Level2(id='central_park' - name='Central Park, NY, NY', - description="New York's friendly Central Park.")]) + Level2(id=u'central_park' + name=u'Central Park, NY, NY', + description=u"New York's friendly Central Park.")]) # necroplex exits session.add_all( - [LevelExit2(name='deathwell', - from_level='necroplex', - to_level='evilstorm'), - LevelExit2(name='portal', - from_level='necroplex', - to_level='central_park')]) + [LevelExit2(name=u'deathwell', + from_level=u'necroplex', + to_level=u'evilstorm'), + LevelExit2(name=u'portal', + from_level=u'necroplex', + to_level=u'central_park')]) # there are no evilstorm exits because there is no exit from the # evilstorm # central park exits session.add_all( - [LevelExit2(name='portal', - from_level='central_park', - to_level='necroplex')]) + [LevelExit2(name=u'portal', + from_level=u'central_park', + to_level=u'necroplex')]) session.commit() @@ -409,80 +409,80 @@ def _insert_migration3_objects(session): # Insert creatures session.add_all( [Creature3( - name='centipede', + name=u'centipede', num_limbs=100), Creature3( - name='wolf', + name=u'wolf', num_limbs=4, magical_powers = [ CreaturePower3( - name="ice breath", - description="A blast of icy breath!", + name=u"ice breath", + description=u"A blast of icy breath!", hitpower=20.0), CreaturePower3( - name="death stare", - description="A frightening stare, for sure!", + name=u"death stare", + description=u"A frightening stare, for sure!", hitpower=45.0)]), Creature3( - name='wizardsnake', + name=u'wizardsnake', num_limbs=0, magical_powers=[ CreaturePower3( - name='death_rattle', - description='A rattle... of DEATH!', + name=u'death_rattle', + description=u'A rattle... of DEATH!', hitpower=1000.0), CreaturePower3( - name='sneaky_stare', - description="The sneakiest stare you've ever seen!" + name=u'sneaky_stare', + description=u"The sneakiest stare you've ever seen!" hitpower=300.0), CreaturePower3( - name='slithery_smoke', - description="A blast of slithery, slithery smoke.", + name=u'slithery_smoke', + description=u"A blast of slithery, slithery smoke.", hitpower=10.0), CreaturePower3( - name='treacherous_tremors', - description="The ground shakes beneath footed animals!", + name=u'treacherous_tremors', + description=u"The ground shakes beneath footed animals!", hitpower=0.0)])], # annnnnd one more to test a floating point hitpower Creature3( - name='deity', + name=u'deity', numb_limbs=30, magical_powers[ CreaturePower3( - name='smite', - description='Smitten by holy wrath!', + name=u'smite', + description=u'Smitten by holy wrath!', hitpower=9999.9)))) # Insert levels session.add_all( - [Level3(id='necroplex', - name='The Necroplex', - description='A complex full of pure deathzone.'), - Level3(id='evilstorm', - name='Evil Storm', - description='A storm full of pure evil.', + [Level3(id=u'necroplex', + name=u'The Necroplex', + description=u'A complex full of pure deathzone.'), + Level3(id=u'evilstorm', + name=u'Evil Storm', + description=u'A storm full of pure evil.', exits=[]), # you can't escape the evilstorm - Level3(id='central_park' - name='Central Park, NY, NY', - description="New York's friendly Central Park.")]) + Level3(id=u'central_park' + name=u'Central Park, NY, NY', + description=u"New York's friendly Central Park.")]) # necroplex exits session.add_all( - [LevelExit3(name='deathwell', - from_level='necroplex', - to_level='evilstorm'), - LevelExit3(name='portal', - from_level='necroplex', - to_level='central_park')]) + [LevelExit3(name=u'deathwell', + from_level=u'necroplex', + to_level=u'evilstorm'), + LevelExit3(name=u'portal', + from_level=u'necroplex', + to_level=u'central_park')]) # there are no evilstorm exits because there is no exit from the # evilstorm # central park exits session.add_all( - [LevelExit3(name='portal', - from_level='central_park', - to_level='necroplex')]) + [LevelExit3(name=u'portal', + from_level=u'central_park', + to_level=u'necroplex')]) session.commit() @@ -513,27 +513,38 @@ def assert_col_type(column, class): def test_set1_to_set3(): # Create / connect to database + # ---------------------------- + engine, Session = create_test_engine() + # Create tables by migrating on empty initial set + # ----------------------------------------------- + printer = CollectingPrinter migration_manager = MigrationManager( '__main__', SET1_MODELS, SET1_MIGRATIONS, Session(), printer) + # Check latest migration and database current migration assert migration_manager.latest_migration == 0 assert migration_manager.database_current_migration == None result = migration_manager.init_or_migrate() + # Make sure output was "inited" assert result == u'inited' # Check output - assert printer.combined_string == "-> Initializing __main__... done.\n" + assert printer.combined_string == ( + "-> Initializing main mediagoblin tables... done.\n") # Check version in database assert migration_manager.latest_migration == 0 assert migration_manager.database_current_migration == 0 # Install the initial set + # ----------------------- + _insert_migration1_objects(Session()) + # Try to "re-migrate" with same manager settings... nothing should happen migration_manager = MigrationManager( '__main__', SET1_MODELS, SET1_MIGRATIONS, Session(), @@ -568,22 +579,136 @@ def test_set1_to_set3(): autoload=True, autoload_with=db_conn.engine) assert set(level_table.c.keys()) == set( ['id', 'name', 'description', 'exits']) - assert_col_type(level_table.c.id, Integer) + assert_col_type(level_table.c.id, Unicode) + assert level_table.c.id.primary_key is True assert_col_type(level_table.c.name, Unicode) - assert level_table.c.name.nullable is False - assert level_table.c.name.index is True - assert level_table.c.name.unique is True assert_col_type(level_table.c.description, Unicode) # Skipping exits... Not sure if we can detect pickletype, not a # big deal regardless. # Now check to see if stuff seems to be in there. + creature = session.query(Creature1).filter_by( + name=u'centipede').one() + assert creature.num_legs == 100 + assert creature.is_demon == False + + creature = session.query(Creature1).filter_by( + name=u'wolf').one() + assert creature.num_legs == 4 + assert creature.is_demon == False + + creature = session.query(Creature1).filter_by( + name=u'wizardsnake').one() + assert creature.num_legs == 0 + assert creature.is_demon == True + + level = session.query(Level1).filter_by( + id=u'necroplex') + assert level.name == u'The Necroplex' + assert level.description == u'A complex of pure deathzone.' + assert level.exits == { + 'deathwell': 'evilstorm', + 'portal': 'central_park'} + + level = session.query(Level1).filter_by( + id=u'evilstorm') + assert level.name == u'Evil Storm' + assert level.description == u'A storm full of pure evil.' + assert level.exits == {} # You still can't escape the evilstorm! + + level = session.query(Level1).filter_by( + id=u'central_park') + assert level.name == u'Central Park, NY, NY' + assert level.description == u"New York's friendly Central Park." + assert level.exits == { + 'portal': 'necroplex'} + + # Create new migration manager, but make sure the db migration + # isn't said to be updated yet + printer = CollectingPrinter + migration_manager = MigrationManager( + '__main__', SET3_MODELS, SET3_MIGRATIONS, Session(), + printer) + + assert migration_manager.latest_migration == 3 + assert migration_manager.database_current_migration == 0 # Migrate + result = migration_manager.init_or_migrate() + # Make sure result was "migrated" - # Check output to user + assert result == u'migrated' + + # TODO: Check output to user + assert printer.combined_string == """\ +-> Updating main mediagoblin tables... + + Running migration 1, "creature_remove_is_demon"... done. + + Running migration 2, "creature_powers_new_table"... done. + + Running migration 3, "level_exits_new_table"... done.""" + # Make sure version matches expected + migration_manager = MigrationManager( + '__main__', SET3_MODELS, SET3_MIGRATIONS, Session(), + printer) + assert migration_manager.latest_migration == 3 + assert migration_manager.database_current_migration == 3 + # Check all things in database match expected + + # Check the creature table + creature_table = Table( + 'creature', metadata, + autoload=True, autoload_with=db_conn.engine) + assert set(creature_table.c.keys()) == set( + ['id', 'name', 'num_legs']) + assert_col_type(creature_table.c.id, Integer) + assert_col_type(creature_table.c.name, Unicode) + assert creature_table.c.name.nullable is False + assert creature_table.c.name.index is True + assert creature_table.c.name.unique is True + assert_col_type(creature_table.c.num_legs, Integer) + assert creature_table.c.num_legs.nullable is False + + # Check the CreaturePower table + creature_power_table = Table( + 'creature_power', metadata, + autoload=True, autoload_with=db_conn.engine) + assert set(creature_power_table.c.keys()) == set( + ['id', 'creature', 'name', 'description', 'hitpower']) + assert_col_type(creature_power_table.c.id, Integer) + assert_col_type(creature_power_table.c.creature, Integer) + assert creature_power_table.c.creature.nullable is False + assert_col_type(creature_power_table.c.name, Unicode) + assert_col_type(creature_power_table.c.description, Unicode) + assert_col_type(creature_power_table.c.hitpower, Integer) + assert creature_power_table.c.hitpower.nullable is False + + # Check the structure of the level table + level_table = Table( + 'level', metadata, + autoload=True, autoload_with=db_conn.engine) + assert set(level_table.c.keys()) == set( + ['id', 'name', 'description']) + assert_col_type(level_table.c.id, Unicode) + assert level_table.c.id.primary_key is True + assert_col_type(level_table.c.name, Unicode) + assert_col_type(level_table.c.description, Unicode) + + # Check the structure of the level_exits table + level_exit_table = Table( + 'level_exit', metadata, + autoload=True, autoload_with=db_conn.engine) + assert set(level_exit_table.c.keys()) == set( + ['id', 'name', 'from_level', 'to_level']) + assert_col_type(level_exit_table.c.id, Integer) + assert_col_type(level_exit_table.c.name, Unicode) + assert_col_type(level_exit_table.c.from_level, Unicode) + assert level_exit_table.c.from_level.nullable is False + assert_col_type(level_exit_table.c.to_level, Unicode) + assert level_exit_table.c.to_level.nullable is False + + # Now check to see if stuff seems to be in there. + pass -- cgit v1.2.3 From 690b51faa7aa8723bb08639f21b60dacd5696fcd Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 29 Jan 2012 13:14:56 -0600 Subject: Closer to the end of this migration test... --- mediagoblin/tests/test_sql_migrations.py | 72 +++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 2 deletions(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 57a83ec5..33580432 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -660,7 +660,7 @@ def test_set1_to_set3(): 'creature', metadata, autoload=True, autoload_with=db_conn.engine) assert set(creature_table.c.keys()) == set( - ['id', 'name', 'num_legs']) + ['id', 'name', 'num_limbs']) assert_col_type(creature_table.c.id, Integer) assert_col_type(creature_table.c.name, Unicode) assert creature_table.c.name.nullable is False @@ -680,7 +680,7 @@ def test_set1_to_set3(): assert creature_power_table.c.creature.nullable is False assert_col_type(creature_power_table.c.name, Unicode) assert_col_type(creature_power_table.c.description, Unicode) - assert_col_type(creature_power_table.c.hitpower, Integer) + assert_col_type(creature_power_table.c.hitpower, Float) assert creature_power_table.c.hitpower.nullable is False # Check the structure of the level table @@ -704,10 +704,26 @@ def test_set1_to_set3(): assert_col_type(level_exit_table.c.name, Unicode) assert_col_type(level_exit_table.c.from_level, Unicode) assert level_exit_table.c.from_level.nullable is False + assert level_exit_table.c.from_level.indexed is True assert_col_type(level_exit_table.c.to_level, Unicode) assert level_exit_table.c.to_level.nullable is False + assert level_exit_table.c.to_level.indexed is True # Now check to see if stuff seems to be in there. + creature = session.query(Creature1).filter_by( + name=u'centipede').one() + assert creature.num_legs == 100.0 + assert creature.creature_powers == [] + + creature = session.query(Creature1).filter_by( + name=u'wolf').one() + assert creature.num_legs == 4.0 + assert creature.creature_powers == [] + + creature = session.query(Creature1).filter_by( + name=u'wizardsnake').one() + assert creature.num_legs == 0.0 + assert creature.creature_powers == [] pass @@ -741,4 +757,56 @@ def test_set1_to_set2_to_set3(): # Migrate again # Make sure version matches expected again # Check all things in database match expected again + + ##### Set2 + # creature_table = Table( + # 'creature', metadata, + # autoload=True, autoload_with=db_conn.engine) + # assert set(creature_table.c.keys()) == set( + # ['id', 'name', 'num_legs']) + # assert_col_type(creature_table.c.id, Integer) + # assert_col_type(creature_table.c.name, Unicode) + # assert creature_table.c.name.nullable is False + # assert creature_table.c.name.index is True + # assert creature_table.c.name.unique is True + # assert_col_type(creature_table.c.num_legs, Integer) + # assert creature_table.c.num_legs.nullable is False + + # # Check the CreaturePower table + # creature_power_table = Table( + # 'creature_power', metadata, + # autoload=True, autoload_with=db_conn.engine) + # assert set(creature_power_table.c.keys()) == set( + # ['id', 'creature', 'name', 'description', 'hitpower']) + # assert_col_type(creature_power_table.c.id, Integer) + # assert_col_type(creature_power_table.c.creature, Integer) + # assert creature_power_table.c.creature.nullable is False + # assert_col_type(creature_power_table.c.name, Unicode) + # assert_col_type(creature_power_table.c.description, Unicode) + # assert_col_type(creature_power_table.c.hitpower, Integer) + # assert creature_power_table.c.hitpower.nullable is False + + # # Check the structure of the level table + # level_table = Table( + # 'level', metadata, + # autoload=True, autoload_with=db_conn.engine) + # assert set(level_table.c.keys()) == set( + # ['id', 'name', 'description']) + # assert_col_type(level_table.c.id, Unicode) + # assert level_table.c.id.primary_key is True + # assert_col_type(level_table.c.name, Unicode) + # assert_col_type(level_table.c.description, Unicode) + + # # Check the structure of the level_exits table + # level_exit_table = Table( + # 'level_exit', metadata, + # autoload=True, autoload_with=db_conn.engine) + # assert set(level_exit_table.c.keys()) == set( + # ['id', 'name', 'from_level', 'to_level']) + # assert_col_type(level_exit_table.c.id, Integer) + # assert_col_type(level_exit_table.c.name, Unicode) + # assert_col_type(level_exit_table.c.from_level, Unicode) + # assert level_exit_table.c.from_level.nullable is False + # assert_col_type(level_exit_table.c.to_level, Unicode) + pass -- cgit v1.2.3 From 5de0f4daf5cb38606485e84253706b97f78e97ee Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 29 Jan 2012 14:57:42 -0600 Subject: More stuff even yet per sql migration stuff! And still not ready! --- mediagoblin/tests/test_sql_migrations.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 33580432..4975945c 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -17,7 +17,7 @@ import copy from sqlalchemy import ( - Table, Column, MetaData, Index + Table, Column, MetaData, Index, Integer, Float, Unicode, UnicodeText, DateTime, Boolean, ForeignKey, UniqueConstraint, PickleType) from sqlalchemy.orm import sessionmaker, relationship @@ -712,17 +712,17 @@ def test_set1_to_set3(): # Now check to see if stuff seems to be in there. creature = session.query(Creature1).filter_by( name=u'centipede').one() - assert creature.num_legs == 100.0 + assert creature.num_limbs == 100.0 assert creature.creature_powers == [] creature = session.query(Creature1).filter_by( name=u'wolf').one() - assert creature.num_legs == 4.0 + assert creature.num_limbs == 4.0 assert creature.creature_powers == [] creature = session.query(Creature1).filter_by( name=u'wizardsnake').one() - assert creature.num_legs == 0.0 + assert creature.num_limbs == 0.0 assert creature.creature_powers == [] pass -- cgit v1.2.3 From caed154af020eb0fadefa955d0a15b836433e3a4 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 29 Jan 2012 15:27:45 -0600 Subject: Fixing some obvious errors caught by pyflakes --- mediagoblin/tests/test_sql_migrations.py | 46 +++++++++++++++++--------------- 1 file changed, 25 insertions(+), 21 deletions(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 4975945c..92e99ab1 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -23,6 +23,7 @@ from sqlalchemy import ( from sqlalchemy.orm import sessionmaker, relationship from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.sql import select, insert +from sqlalchemy.sql.util import MigrationManager from migrate import changeset from mediagoblin.db.sql.base import GMGTableBase @@ -50,7 +51,7 @@ class Level1(Base1): __tablename__ = "level" id = Column(Unicode, primary_key=True) - name = Column(Unicode)x + name = Column(Unicode) description = Column(Unicode) exits = Column(PickleType) @@ -231,6 +232,7 @@ class LevelExit3(Base3): SET3_MODELS = [Creature3, CreaturePower3, Level3, LevelExit3] +SET3_MIGRATIONS = FULL_MIGRATIONS @RegisterMigration(4, FULL_MIGRATIONS) @@ -256,8 +258,8 @@ def level_exit_index_from_and_to_level(db_conn): level_exit = Table( 'level_exit', metadata, autoload=True, autoload_with=db_conn.engine) - Index('ix_from_level', level_exit.c.from_level).create(engine) - Index('ix_to_exit', level_exit.c.to_exit).create(engine) + Index('ix_from_level', level_exit.c.from_level).create(db_conn.engine) + Index('ix_to_exit', level_exit.c.to_exit).create(db_conn.engine) @RegisterMigration(6, FULL_MIGRATIONS) @@ -269,7 +271,7 @@ def creature_power_index_creature(db_conn): creature_power = Table( 'creature_power', metadata, autoload=True, autoload_with=db_conn.engine) - Index('ix_creature', creature_power.c.creature).create(engine) + Index('ix_creature', creature_power.c.creature).create(db_conn.engine) @RegisterMigration(7, FULL_MIGRATIONS) @@ -317,7 +319,7 @@ def _insert_migration1_objects(session): name=u'Evil Storm', description=u'A storm full of pure evil.', exits={}), # you can't escape the evilstorm - Level1(id=u'central_park' + Level1(id=u'central_park', name=u'Central Park, NY, NY', description=u"New York's friendly Central Park.", exits={ @@ -357,7 +359,7 @@ def _insert_migration2_objects(session): hitpower=1000), CreaturePower2( name=u'sneaky_stare', - description=u"The sneakiest stare you've ever seen!" + description=u"The sneakiest stare you've ever seen!", hitpower=300), CreaturePower2( name=u'slithery_smoke', @@ -377,7 +379,7 @@ def _insert_migration2_objects(session): name=u'Evil Storm', description=u'A storm full of pure evil.', exits=[]), # you can't escape the evilstorm - Level2(id=u'central_park' + Level2(id=u'central_park', name=u'Central Park, NY, NY', description=u"New York's friendly Central Park.")]) @@ -433,7 +435,7 @@ def _insert_migration3_objects(session): hitpower=1000.0), CreaturePower3( name=u'sneaky_stare', - description=u"The sneakiest stare you've ever seen!" + description=u"The sneakiest stare you've ever seen!", hitpower=300.0), CreaturePower3( name=u'slithery_smoke', @@ -447,11 +449,11 @@ def _insert_migration3_objects(session): Creature3( name=u'deity', numb_limbs=30, - magical_powers[ + magical_powers=[ CreaturePower3( name=u'smite', description=u'Smitten by holy wrath!', - hitpower=9999.9)))) + hitpower=9999.9)])) # Insert levels session.add_all( @@ -462,7 +464,7 @@ def _insert_migration3_objects(session): name=u'Evil Storm', description=u'A storm full of pure evil.', exits=[]), # you can't escape the evilstorm - Level3(id=u'central_park' + Level3(id=u'central_park', name=u'Central Park, NY, NY', description=u"New York's friendly Central Park.")]) @@ -502,13 +504,12 @@ def CollectingPrinter(object): def create_test_engine(): from sqlalchemy import create_engine engine = create_engine('sqlite:///:memory:', echo=False) - sqlalchemy.orm import sessionmaker Session = sessionmaker(bind=engine) return engine, Session -def assert_col_type(column, class): - assert isinstance(column.type, class) +def assert_col_type(column, this_class): + assert isinstance(column.type, this_class) def test_set1_to_set3(): @@ -556,12 +557,12 @@ def test_set1_to_set3(): assert migration_manager.database_current_migration == 0 # Sanity check a few things in the database... - metadata = MetaData(bind=db_conn.engine) + metadata = MetaData(bind=engine) # Check the structure of the creature table creature_table = Table( 'creature', metadata, - autoload=True, autoload_with=db_conn.engine) + autoload=True, autoload_with=engine) assert set(creature_table.c.keys()) == set( ['id', 'name', 'num_legs', 'is_demon']) assert_col_type(creature_table.c.id, Integer) @@ -576,7 +577,7 @@ def test_set1_to_set3(): # Check the structure of the level table level_table = Table( 'level', metadata, - autoload=True, autoload_with=db_conn.engine) + autoload=True, autoload_with=engine) assert set(level_table.c.keys()) == set( ['id', 'name', 'description', 'exits']) assert_col_type(level_table.c.id, Unicode) @@ -587,6 +588,8 @@ def test_set1_to_set3(): # big deal regardless. # Now check to see if stuff seems to be in there. + session = Session() + creature = session.query(Creature1).filter_by( name=u'centipede').one() assert creature.num_legs == 100 @@ -658,7 +661,7 @@ def test_set1_to_set3(): # Check the creature table creature_table = Table( 'creature', metadata, - autoload=True, autoload_with=db_conn.engine) + autoload=True, autoload_with=engine) assert set(creature_table.c.keys()) == set( ['id', 'name', 'num_limbs']) assert_col_type(creature_table.c.id, Integer) @@ -672,7 +675,7 @@ def test_set1_to_set3(): # Check the CreaturePower table creature_power_table = Table( 'creature_power', metadata, - autoload=True, autoload_with=db_conn.engine) + autoload=True, autoload_with=engine) assert set(creature_power_table.c.keys()) == set( ['id', 'creature', 'name', 'description', 'hitpower']) assert_col_type(creature_power_table.c.id, Integer) @@ -686,7 +689,7 @@ def test_set1_to_set3(): # Check the structure of the level table level_table = Table( 'level', metadata, - autoload=True, autoload_with=db_conn.engine) + autoload=True, autoload_with=engine) assert set(level_table.c.keys()) == set( ['id', 'name', 'description']) assert_col_type(level_table.c.id, Unicode) @@ -697,7 +700,7 @@ def test_set1_to_set3(): # Check the structure of the level_exits table level_exit_table = Table( 'level_exit', metadata, - autoload=True, autoload_with=db_conn.engine) + autoload=True, autoload_with=engine) assert set(level_exit_table.c.keys()) == set( ['id', 'name', 'from_level', 'to_level']) assert_col_type(level_exit_table.c.id, Integer) @@ -710,6 +713,7 @@ def test_set1_to_set3(): assert level_exit_table.c.to_level.indexed is True # Now check to see if stuff seems to be in there. + session = Session() creature = session.query(Creature1).filter_by( name=u'centipede').one() assert creature.num_limbs == 100.0 -- cgit v1.2.3 From e8e52b3a0b353849cbdbfa70bb44270ec8211e17 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 29 Jan 2012 16:07:16 -0600 Subject: test_set1_to_set3() now has appropriate amount of code, even if it doesn't run :) --- mediagoblin/tests/test_sql_migrations.py | 45 ++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 8 deletions(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 92e99ab1..1f97b6ce 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -23,10 +23,10 @@ from sqlalchemy import ( from sqlalchemy.orm import sessionmaker, relationship from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.sql import select, insert -from sqlalchemy.sql.util import MigrationManager from migrate import changeset from mediagoblin.db.sql.base import GMGTableBase +from mediagoblin.db.sql.util import MigrationManager, RegisterMigration # This one will get filled with local migrations @@ -512,6 +512,13 @@ def assert_col_type(column, this_class): assert isinstance(column.type, this_class) +def _get_level3_exits(session, level): + return dict( + [(level_exit.name, level_exit.to_level) + for level_exit in + session.query(LevelExit3).filter_by(from_level=level.id)]) + + def test_set1_to_set3(): # Create / connect to database # ---------------------------- @@ -606,7 +613,7 @@ def test_set1_to_set3(): assert creature.is_demon == True level = session.query(Level1).filter_by( - id=u'necroplex') + id=u'necroplex').one() assert level.name == u'The Necroplex' assert level.description == u'A complex of pure deathzone.' assert level.exits == { @@ -614,13 +621,13 @@ def test_set1_to_set3(): 'portal': 'central_park'} level = session.query(Level1).filter_by( - id=u'evilstorm') + id=u'evilstorm').one() assert level.name == u'Evil Storm' assert level.description == u'A storm full of pure evil.' assert level.exits == {} # You still can't escape the evilstorm! level = session.query(Level1).filter_by( - id=u'central_park') + id=u'central_park').one() assert level.name == u'Central Park, NY, NY' assert level.description == u"New York's friendly Central Park." assert level.exits == { @@ -714,22 +721,44 @@ def test_set1_to_set3(): # Now check to see if stuff seems to be in there. session = Session() - creature = session.query(Creature1).filter_by( + creature = session.query(Creature3).filter_by( name=u'centipede').one() assert creature.num_limbs == 100.0 assert creature.creature_powers == [] - creature = session.query(Creature1).filter_by( + creature = session.query(Creature3).filter_by( name=u'wolf').one() assert creature.num_limbs == 4.0 assert creature.creature_powers == [] - creature = session.query(Creature1).filter_by( + creature = session.query(Creature3).filter_by( name=u'wizardsnake').one() assert creature.num_limbs == 0.0 assert creature.creature_powers == [] - pass + level = session.query(Level3).filter_by( + id=u'necroplex').one() + assert level.name == u'The Necroplex' + assert level.description == u'A complex of pure deathzone.' + level_exits = _get_level3_exits(session, level) + assert level_exits == { + u'deathwell': u'evilstorm', + u'portal': u'central_park'} + + level = session.query(Level3).filter_by( + id=u'evilstorm').one() + assert level.name == u'Evil Storm' + assert level.description == u'A storm full of pure evil.' + level_exits = _get_level3_exits(session, level) + assert level_exits == {} # You still can't escape the evilstorm! + + level = session.query(Level3).filter_by( + id=u'central_park').one() + assert level.name == u'Central Park, NY, NY' + assert level.description == u"New York's friendly Central Park." + level_exits = _get_level3_exits(session, level) + assert level_exits == { + 'portal': 'necroplex'} def test_set2_to_set3(): -- cgit v1.2.3 From a5e03db6c2d16581251802bcf1eeae1a7c1f2d9f Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 29 Jan 2012 16:39:08 -0600 Subject: Migration records are dicts, not lists. Fix SET1_MIGATIONS! --- mediagoblin/tests/test_sql_migrations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 1f97b6ce..cae29549 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -57,7 +57,7 @@ class Level1(Base1): SET1_MODELS = [Creature1, Level1] -SET1_MIGRATIONS = [] +SET1_MIGRATIONS = {} ####################################################### # Migration set 2: A few migrations and new model -- cgit v1.2.3 From 0f3526c601b7848a54808ec0baef832d3baaa8b7 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 29 Jan 2012 16:48:44 -0600 Subject: magical_powers relationship set on wrong table, fixed --- mediagoblin/tests/test_sql_migrations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index cae29549..7a49a4a5 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -201,6 +201,7 @@ class Creature3(Base3): id = Column(Integer, primary_key=True) name = Column(Unicode, unique=True, nullable=False, index=True) num_limbs= Column(Integer, nullable=False) + magical_powers = relationship("CreaturePower3") class CreaturePower3(Base3): __tablename__ = "creature_power" @@ -211,7 +212,6 @@ class CreaturePower3(Base3): name = Column(Unicode) description = Column(Unicode) hitpower = Column(Float, nullable=False) - magical_powers = relationship("CreaturePower3") class Level3(Base3): __tablename__ = "level" -- cgit v1.2.3 From 245e6d83a618f524be0c71d071374ddc6da291f0 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 29 Jan 2012 17:10:18 -0600 Subject: CollectingPrinter is a class, not a function! --- mediagoblin/tests/test_sql_migrations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 7a49a4a5..7dfc4b26 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -489,7 +489,7 @@ def _insert_migration3_objects(session): session.commit() -def CollectingPrinter(object): +class CollectingPrinter(object): def __init__(self): self.collection = [] -- cgit v1.2.3 From ecb4cc89900b897b26fe5f6a1f4c1d94b4a50b6a Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 29 Jan 2012 17:11:41 -0600 Subject: printer = CollectingPrinter -> printer = CollectingPrinter() --- mediagoblin/tests/test_sql_migrations.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 7dfc4b26..d9d30237 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -528,7 +528,7 @@ def test_set1_to_set3(): # Create tables by migrating on empty initial set # ----------------------------------------------- - printer = CollectingPrinter + printer = CollectingPrinter() migration_manager = MigrationManager( '__main__', SET1_MODELS, SET1_MIGRATIONS, Session(), printer) @@ -635,7 +635,7 @@ def test_set1_to_set3(): # Create new migration manager, but make sure the db migration # isn't said to be updated yet - printer = CollectingPrinter + printer = CollectingPrinter() migration_manager = MigrationManager( '__main__', SET3_MODELS, SET3_MIGRATIONS, Session(), printer) -- cgit v1.2.3 From c7fa585bffd1f2cf55455c810ce5c30c8b16d690 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 29 Jan 2012 17:21:44 -0600 Subject: assert column type from Unicode -> VARCHAR. SQLAlchemy reflection only so smart ;) --- mediagoblin/tests/test_sql_migrations.py | 58 ++++++++++++++++---------------- 1 file changed, 29 insertions(+), 29 deletions(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index d9d30237..71ea321a 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -19,7 +19,7 @@ import copy from sqlalchemy import ( Table, Column, MetaData, Index, Integer, Float, Unicode, UnicodeText, DateTime, Boolean, - ForeignKey, UniqueConstraint, PickleType) + ForeignKey, UniqueConstraint, PickleType, VARCHAR) from sqlalchemy.orm import sessionmaker, relationship from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.sql import select, insert @@ -573,10 +573,10 @@ def test_set1_to_set3(): assert set(creature_table.c.keys()) == set( ['id', 'name', 'num_legs', 'is_demon']) assert_col_type(creature_table.c.id, Integer) - assert_col_type(creature_table.c.name, Unicode) + assert_col_type(creature_table.c.name, VARCHAR) assert creature_table.c.name.nullable is False - assert creature_table.c.name.index is True - assert creature_table.c.name.unique is True + #assert creature_table.c.name.index is True + #assert creature_table.c.name.unique is True assert_col_type(creature_table.c.num_legs, Integer) assert creature_table.c.num_legs.nullable is False assert_col_type(creature_table.c.is_demon, Boolean) @@ -587,10 +587,10 @@ def test_set1_to_set3(): autoload=True, autoload_with=engine) assert set(level_table.c.keys()) == set( ['id', 'name', 'description', 'exits']) - assert_col_type(level_table.c.id, Unicode) + assert_col_type(level_table.c.id, VARCHAR) assert level_table.c.id.primary_key is True - assert_col_type(level_table.c.name, Unicode) - assert_col_type(level_table.c.description, Unicode) + assert_col_type(level_table.c.name, VARCHAR) + assert_col_type(level_table.c.description, VARCHAR) # Skipping exits... Not sure if we can detect pickletype, not a # big deal regardless. @@ -672,10 +672,10 @@ def test_set1_to_set3(): assert set(creature_table.c.keys()) == set( ['id', 'name', 'num_limbs']) assert_col_type(creature_table.c.id, Integer) - assert_col_type(creature_table.c.name, Unicode) + assert_col_type(creature_table.c.name, VARCHAR) assert creature_table.c.name.nullable is False - assert creature_table.c.name.index is True - assert creature_table.c.name.unique is True + #assert creature_table.c.name.index is True + #assert creature_table.c.name.unique is True assert_col_type(creature_table.c.num_legs, Integer) assert creature_table.c.num_legs.nullable is False @@ -688,8 +688,8 @@ def test_set1_to_set3(): assert_col_type(creature_power_table.c.id, Integer) assert_col_type(creature_power_table.c.creature, Integer) assert creature_power_table.c.creature.nullable is False - assert_col_type(creature_power_table.c.name, Unicode) - assert_col_type(creature_power_table.c.description, Unicode) + assert_col_type(creature_power_table.c.name, VARCHAR) + assert_col_type(creature_power_table.c.description, VARCHAR) assert_col_type(creature_power_table.c.hitpower, Float) assert creature_power_table.c.hitpower.nullable is False @@ -699,10 +699,10 @@ def test_set1_to_set3(): autoload=True, autoload_with=engine) assert set(level_table.c.keys()) == set( ['id', 'name', 'description']) - assert_col_type(level_table.c.id, Unicode) + assert_col_type(level_table.c.id, VARCHAR) assert level_table.c.id.primary_key is True - assert_col_type(level_table.c.name, Unicode) - assert_col_type(level_table.c.description, Unicode) + assert_col_type(level_table.c.name, VARCHAR) + assert_col_type(level_table.c.description, VARCHAR) # Check the structure of the level_exits table level_exit_table = Table( @@ -711,13 +711,13 @@ def test_set1_to_set3(): assert set(level_exit_table.c.keys()) == set( ['id', 'name', 'from_level', 'to_level']) assert_col_type(level_exit_table.c.id, Integer) - assert_col_type(level_exit_table.c.name, Unicode) - assert_col_type(level_exit_table.c.from_level, Unicode) + assert_col_type(level_exit_table.c.name, VARCHAR) + assert_col_type(level_exit_table.c.from_level, VARCHAR) assert level_exit_table.c.from_level.nullable is False - assert level_exit_table.c.from_level.indexed is True - assert_col_type(level_exit_table.c.to_level, Unicode) + #assert level_exit_table.c.from_level.index is True + assert_col_type(level_exit_table.c.to_level, VARCHAR) assert level_exit_table.c.to_level.nullable is False - assert level_exit_table.c.to_level.indexed is True + #assert level_exit_table.c.to_level.index is True # Now check to see if stuff seems to be in there. session = Session() @@ -798,7 +798,7 @@ def test_set1_to_set2_to_set3(): # assert set(creature_table.c.keys()) == set( # ['id', 'name', 'num_legs']) # assert_col_type(creature_table.c.id, Integer) - # assert_col_type(creature_table.c.name, Unicode) + # assert_col_type(creature_table.c.name, VARCHAR) # assert creature_table.c.name.nullable is False # assert creature_table.c.name.index is True # assert creature_table.c.name.unique is True @@ -814,8 +814,8 @@ def test_set1_to_set2_to_set3(): # assert_col_type(creature_power_table.c.id, Integer) # assert_col_type(creature_power_table.c.creature, Integer) # assert creature_power_table.c.creature.nullable is False - # assert_col_type(creature_power_table.c.name, Unicode) - # assert_col_type(creature_power_table.c.description, Unicode) + # assert_col_type(creature_power_table.c.name, VARCHAR) + # assert_col_type(creature_power_table.c.description, VARCHAR) # assert_col_type(creature_power_table.c.hitpower, Integer) # assert creature_power_table.c.hitpower.nullable is False @@ -825,10 +825,10 @@ def test_set1_to_set2_to_set3(): # autoload=True, autoload_with=db_conn.engine) # assert set(level_table.c.keys()) == set( # ['id', 'name', 'description']) - # assert_col_type(level_table.c.id, Unicode) + # assert_col_type(level_table.c.id, VARCHAR) # assert level_table.c.id.primary_key is True - # assert_col_type(level_table.c.name, Unicode) - # assert_col_type(level_table.c.description, Unicode) + # assert_col_type(level_table.c.name, VARCHAR) + # assert_col_type(level_table.c.description, VARCHAR) # # Check the structure of the level_exits table # level_exit_table = Table( @@ -837,9 +837,9 @@ def test_set1_to_set2_to_set3(): # assert set(level_exit_table.c.keys()) == set( # ['id', 'name', 'from_level', 'to_level']) # assert_col_type(level_exit_table.c.id, Integer) - # assert_col_type(level_exit_table.c.name, Unicode) - # assert_col_type(level_exit_table.c.from_level, Unicode) + # assert_col_type(level_exit_table.c.name, VARCHAR) + # assert_col_type(level_exit_table.c.from_level, VARCHAR) # assert level_exit_table.c.from_level.nullable is False - # assert_col_type(level_exit_table.c.to_level, Unicode) + # assert_col_type(level_exit_table.c.to_level, VARCHAR) pass -- cgit v1.2.3 From 505c0db119c9a33a2ee823b4da3859054cce48fd Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 29 Jan 2012 17:22:29 -0600 Subject: Fixed the descriptions for the necroplex! --- mediagoblin/tests/test_sql_migrations.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 71ea321a..8bb82efd 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -615,7 +615,7 @@ def test_set1_to_set3(): level = session.query(Level1).filter_by( id=u'necroplex').one() assert level.name == u'The Necroplex' - assert level.description == u'A complex of pure deathzone.' + assert level.description == u'A complex full of pure deathzone.' assert level.exits == { 'deathwell': 'evilstorm', 'portal': 'central_park'} @@ -739,7 +739,7 @@ def test_set1_to_set3(): level = session.query(Level3).filter_by( id=u'necroplex').one() assert level.name == u'The Necroplex' - assert level.description == u'A complex of pure deathzone.' + assert level.description == u'A complex full of pure deathzone.' level_exits = _get_level3_exits(session, level) assert level_exits == { u'deathwell': u'evilstorm', -- cgit v1.2.3 From be1077ac44ac4a6ad659e202b93363af2d033f96 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 29 Jan 2012 17:23:27 -0600 Subject: Migration manager's current migration should be 3, not 7, after running all migrations! --- mediagoblin/tests/test_sql_migrations.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 8bb82efd..92925e64 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -640,7 +640,7 @@ def test_set1_to_set3(): '__main__', SET3_MODELS, SET3_MIGRATIONS, Session(), printer) - assert migration_manager.latest_migration == 3 + assert migration_manager.latest_migration == 7 assert migration_manager.database_current_migration == 0 # Migrate @@ -660,8 +660,8 @@ def test_set1_to_set3(): migration_manager = MigrationManager( '__main__', SET3_MODELS, SET3_MIGRATIONS, Session(), printer) - assert migration_manager.latest_migration == 3 - assert migration_manager.database_current_migration == 3 + assert migration_manager.latest_migration == 7 + assert migration_manager.database_current_migration == 7 # Check all things in database match expected -- cgit v1.2.3 From e920b96859b37e5404415bd0573f57b81f7d14d0 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 29 Jan 2012 17:28:25 -0600 Subject: db_conn.engine -> db_conn.bind --- mediagoblin/tests/test_sql_migrations.py | 42 ++++++++++++++++---------------- 1 file changed, 21 insertions(+), 21 deletions(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 92925e64..f91d449c 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -109,10 +109,10 @@ def creature_remove_is_demon(db_conn): Remove the is_demon field from the creature model. We don't need it! """ - metadata = MetaData(bind=db_conn.engine) + metadata = MetaData(bind=db_conn.bind) creature_table = Table( 'creature', metadata, - autoload=True, autoload_with=db_conn.engine) + autoload=True, autoload_with=db_conn.bind) creature_table.drop_column('is_demon') @@ -123,7 +123,7 @@ def creature_powers_new_table(db_conn): yet though as there wasn't anything that previously held this information """ - metadata = MetaData(bind=db_conn.engine) + metadata = MetaData(bind=db_conn.bind) creature_powers = Table( 'creature_power', metadata, Column('id', Integer, primary_key=True), @@ -132,7 +132,7 @@ def creature_powers_new_table(db_conn): Column('name', Unicode), Column('description', Unicode), Column('hitpower', Integer, nullable=False)) - metadata.create_all(db_conn.engine) + metadata.create_all(db_conn.bind) @RegisterMigration(3, FULL_MIGRATIONS) @@ -143,7 +143,7 @@ def level_exits_new_table(db_conn): """ # First, create the table # ----------------------- - metadata = MetaData(bind=db_conn.engine) + metadata = MetaData(bind=db_conn.bind) level_exits = Table( 'level_exit', metadata, Column('id', Integer, primary_key=True), @@ -152,7 +152,7 @@ def level_exits_new_table(db_conn): Integer, ForeignKey('level.id'), nullable=False), Column('to_level', Integer, ForeignKey('level.id'), nullable=False)) - metadata.create_all(db_conn.engine) + metadata.create_all(db_conn.bind) # And now, convert all the old exit pickles to new level exits # ------------------------------------------------------------ @@ -242,10 +242,10 @@ def creature_num_legs_to_num_limbs(db_conn): specifically. Humans would be 4 here, for instance. So we renamed the column. """ - metadata = MetaData(bind=db_conn.engine) + metadata = MetaData(bind=db_conn.bind) creature_table = Table( 'creature', metadata, - autoload=True, autoload_with=db_conn.engine) + autoload=True, autoload_with=db_conn.bind) creature_table.c.num_legs.alter(name=u"num_limbs") @@ -254,12 +254,12 @@ def level_exit_index_from_and_to_level(db_conn): """ Index the from and to levels of the level exit table. """ - metadata = MetaData(bind=db_conn.engine) + metadata = MetaData(bind=db_conn.bind) level_exit = Table( 'level_exit', metadata, - autoload=True, autoload_with=db_conn.engine) - Index('ix_from_level', level_exit.c.from_level).create(db_conn.engine) - Index('ix_to_exit', level_exit.c.to_exit).create(db_conn.engine) + autoload=True, autoload_with=db_conn.bind) + Index('ix_from_level', level_exit.c.from_level).create(db_conn.bind) + Index('ix_to_exit', level_exit.c.to_exit).create(db_conn.bind) @RegisterMigration(6, FULL_MIGRATIONS) @@ -267,11 +267,11 @@ def creature_power_index_creature(db_conn): """ Index our foreign key relationship to the creatures """ - metadata = MetaData(bind=db_conn.engine) + metadata = MetaData(bind=db_conn.bind) creature_power = Table( 'creature_power', metadata, - autoload=True, autoload_with=db_conn.engine) - Index('ix_creature', creature_power.c.creature).create(db_conn.engine) + autoload=True, autoload_with=db_conn.bind) + Index('ix_creature', creature_power.c.creature).create(db_conn.bind) @RegisterMigration(7, FULL_MIGRATIONS) @@ -283,10 +283,10 @@ def creature_power_hitpower_to_float(db_conn): Turns out we want super precise values of how much hitpower there really is. """ - metadata = MetaData(bind=db_conn.engine) + metadata = MetaData(bind=db_conn.bind) creature_power = Table( 'creature_power', metadata, - autoload=True, autoload_with=db_conn.engine) + autoload=True, autoload_with=db_conn.bind) creature_power.c.hitpower.alter(type=Float) @@ -794,7 +794,7 @@ def test_set1_to_set2_to_set3(): ##### Set2 # creature_table = Table( # 'creature', metadata, - # autoload=True, autoload_with=db_conn.engine) + # autoload=True, autoload_with=db_conn.bind) # assert set(creature_table.c.keys()) == set( # ['id', 'name', 'num_legs']) # assert_col_type(creature_table.c.id, Integer) @@ -808,7 +808,7 @@ def test_set1_to_set2_to_set3(): # # Check the CreaturePower table # creature_power_table = Table( # 'creature_power', metadata, - # autoload=True, autoload_with=db_conn.engine) + # autoload=True, autoload_with=db_conn.bind) # assert set(creature_power_table.c.keys()) == set( # ['id', 'creature', 'name', 'description', 'hitpower']) # assert_col_type(creature_power_table.c.id, Integer) @@ -822,7 +822,7 @@ def test_set1_to_set2_to_set3(): # # Check the structure of the level table # level_table = Table( # 'level', metadata, - # autoload=True, autoload_with=db_conn.engine) + # autoload=True, autoload_with=db_conn.bind) # assert set(level_table.c.keys()) == set( # ['id', 'name', 'description']) # assert_col_type(level_table.c.id, VARCHAR) @@ -833,7 +833,7 @@ def test_set1_to_set2_to_set3(): # # Check the structure of the level_exits table # level_exit_table = Table( # 'level_exit', metadata, - # autoload=True, autoload_with=db_conn.engine) + # autoload=True, autoload_with=db_conn.bind) # assert set(level_exit_table.c.keys()) == set( # ['id', 'name', 'from_level', 'to_level']) # assert_col_type(level_exit_table.c.id, Integer) -- cgit v1.2.3 From 78d17b8055ca19b3af52f97337ccf3c6b8e610b7 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 29 Jan 2012 22:19:03 -0600 Subject: Excepting that migration 1 doesn't work(!), sqlalchemy migration branch working The reason migration 1 doesn't work, and is commented out, is because of sqlalchemy-migrate not handling certain constraints while dropping binary sqlite columns right. See also: http://code.google.com/p/sqlalchemy-migrate/issues/detail?id=143&thanks=143&ts=1327882242 --- mediagoblin/tests/test_sql_migrations.py | 108 ++++++++++++++++++++----------- 1 file changed, 72 insertions(+), 36 deletions(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index f91d449c..e1af2586 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -109,12 +109,13 @@ def creature_remove_is_demon(db_conn): Remove the is_demon field from the creature model. We don't need it! """ - metadata = MetaData(bind=db_conn.bind) - creature_table = Table( - 'creature', metadata, - autoload=True, autoload_with=db_conn.bind) - creature_table.drop_column('is_demon') - + # metadata = MetaData(bind=db_conn.bind) + # creature_table = Table( + # 'creature', metadata, + # autoload=True, autoload_with=db_conn.bind) + # creature_table.drop_column('is_demon') + pass + @RegisterMigration(2, FULL_MIGRATIONS) def creature_powers_new_table(db_conn): @@ -124,6 +125,13 @@ def creature_powers_new_table(db_conn): information """ metadata = MetaData(bind=db_conn.bind) + + # We have to access the creature table so sqlalchemy can make the + # foreign key relationship + creature_table = Table( + 'creature', metadata, + autoload=True, autoload_with=db_conn.bind) + creature_powers = Table( 'creature_power', metadata, Column('id', Integer, primary_key=True), @@ -144,40 +152,43 @@ def level_exits_new_table(db_conn): # First, create the table # ----------------------- metadata = MetaData(bind=db_conn.bind) + + # Minimal representation of level table. + # Not auto-introspecting here because of pickle table. I'm not + # sure sqlalchemy can auto-introspect pickle columns. + levels = Table( + 'level', metadata, + Column('id', Unicode, primary_key=True), + Column('name', Unicode), + Column('description', Unicode), + Column('exits', PickleType)) + level_exits = Table( 'level_exit', metadata, Column('id', Integer, primary_key=True), Column('name', Unicode), Column('from_level', - Integer, ForeignKey('level.id'), nullable=False), + Unicode, ForeignKey('level.id'), nullable=False), Column('to_level', - Integer, ForeignKey('level.id'), nullable=False)) + Unicode, ForeignKey('level.id'), nullable=False)) metadata.create_all(db_conn.bind) # And now, convert all the old exit pickles to new level exits # ------------------------------------------------------------ - # Minimal representation of level table. - # Not auto-introspecting here because of pickle table. I'm not - # sure sqlalchemy can auto-introspect pickle columns. - levels = Table( - 'level', metadata, - Column('id', Integer, primary_key=True), - Column('exits', PickleType)) - # query over and insert result = db_conn.execute( select([levels], levels.c.exits!=None)) for level in result: - this_exit = level['exits'] - - # Insert the level exit - db_conn.execute( - level_exits.insert().values( - name=this_exit['name'], - from_level=this_exit['from_level'], - to_level=this_exit['to_level'])) + + for exit_name, to_level in level['exits'].iteritems(): + # Insert the level exit + db_conn.execute( + level_exits.insert().values( + name=exit_name, + from_level=level.id, + to_level=to_level)) # Finally, drop the old level exits pickle table # ---------------------------------------------- @@ -258,8 +269,10 @@ def level_exit_index_from_and_to_level(db_conn): level_exit = Table( 'level_exit', metadata, autoload=True, autoload_with=db_conn.bind) - Index('ix_from_level', level_exit.c.from_level).create(db_conn.bind) - Index('ix_to_exit', level_exit.c.to_exit).create(db_conn.bind) + Index('ix_level_exit_from_level', + level_exit.c.from_level).create(db_conn.bind) + Index('ix_level_exit_to_level', + level_exit.c.to_level).create(db_conn.bind) @RegisterMigration(6, FULL_MIGRATIONS) @@ -271,7 +284,8 @@ def creature_power_index_creature(db_conn): creature_power = Table( 'creature_power', metadata, autoload=True, autoload_with=db_conn.bind) - Index('ix_creature', creature_power.c.creature).create(db_conn.bind) + Index('ix_creature_power_creature', + creature_power.c.creature).create(db_conn.bind) @RegisterMigration(7, FULL_MIGRATIONS) @@ -284,9 +298,23 @@ def creature_power_hitpower_to_float(db_conn): really is. """ metadata = MetaData(bind=db_conn.bind) + + # We have to access the creature table so sqlalchemy can make the + # foreign key relationship + creature_table = Table( + 'creature', metadata, + autoload=True, autoload_with=db_conn.bind) + creature_power = Table( 'creature_power', metadata, - autoload=True, autoload_with=db_conn.bind) + Column('id', Integer, primary_key=True), + Column('creature', Integer, + ForeignKey('creature.id'), nullable=False, + index=True), + Column('name', Unicode), + Column('description', Unicode), + Column('hitpower', Integer, nullable=False)) + creature_power.c.hitpower.alter(type=Float) @@ -651,10 +679,15 @@ def test_set1_to_set3(): # TODO: Check output to user assert printer.combined_string == """\ --> Updating main mediagoblin tables... +-> Updating main mediagoblin tables: + Running migration 1, "creature_remove_is_demon"... done. + Running migration 2, "creature_powers_new_table"... done. - + Running migration 3, "level_exits_new_table"... done.""" + + Running migration 3, "level_exits_new_table"... done. + + Running migration 4, "creature_num_legs_to_num_limbs"... done. + + Running migration 5, "level_exit_index_from_and_to_level"... done. + + Running migration 6, "creature_power_index_creature"... done. + + Running migration 7, "creature_power_hitpower_to_float"... done. +""" # Make sure version matches expected migration_manager = MigrationManager( @@ -666,18 +699,21 @@ def test_set1_to_set3(): # Check all things in database match expected # Check the creature table + metadata = MetaData(bind=engine) creature_table = Table( 'creature', metadata, autoload=True, autoload_with=engine) + # assert set(creature_table.c.keys()) == set( + # ['id', 'name', 'num_limbs']) assert set(creature_table.c.keys()) == set( - ['id', 'name', 'num_limbs']) + [u'id', 'name', u'num_limbs', u'is_demon']) assert_col_type(creature_table.c.id, Integer) assert_col_type(creature_table.c.name, VARCHAR) assert creature_table.c.name.nullable is False #assert creature_table.c.name.index is True #assert creature_table.c.name.unique is True - assert_col_type(creature_table.c.num_legs, Integer) - assert creature_table.c.num_legs.nullable is False + assert_col_type(creature_table.c.num_limbs, Integer) + assert creature_table.c.num_limbs.nullable is False # Check the CreaturePower table creature_power_table = Table( @@ -724,17 +760,17 @@ def test_set1_to_set3(): creature = session.query(Creature3).filter_by( name=u'centipede').one() assert creature.num_limbs == 100.0 - assert creature.creature_powers == [] + assert creature.magical_powers == [] creature = session.query(Creature3).filter_by( name=u'wolf').one() assert creature.num_limbs == 4.0 - assert creature.creature_powers == [] + assert creature.magical_powers == [] creature = session.query(Creature3).filter_by( name=u'wizardsnake').one() assert creature.num_limbs == 0.0 - assert creature.creature_powers == [] + assert creature.magical_powers == [] level = session.query(Level3).filter_by( id=u'necroplex').one() -- cgit v1.2.3 From 7f3ec607a34e727324397c2bf240f771b12a0455 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sun, 29 Jan 2012 22:19:53 -0600 Subject: Explained why migration #1 commented out. --- mediagoblin/tests/test_sql_migrations.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index e1af2586..1b7fb903 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -109,6 +109,9 @@ def creature_remove_is_demon(db_conn): Remove the is_demon field from the creature model. We don't need it! """ + # :( Commented out 'cuz of: + # http://code.google.com/p/sqlalchemy-migrate/issues/detail?id=143&thanks=143&ts=1327882242 + # metadata = MetaData(bind=db_conn.bind) # creature_table = Table( # 'creature', metadata, -- cgit v1.2.3 From d2506eebb45e13616e51230b39f68db9ae91a0c2 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sat, 18 Feb 2012 23:19:41 -0600 Subject: Commenting out the migrations that don't exist yet --- mediagoblin/tests/test_sql_migrations.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 1b7fb903..507a7725 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -800,7 +800,7 @@ def test_set1_to_set3(): 'portal': 'necroplex'} -def test_set2_to_set3(): +#def test_set2_to_set3(): # Create / connect to database # Create tables by migrating on empty initial set @@ -811,10 +811,10 @@ def test_set2_to_set3(): # Migrate # Make sure version matches expected # Check all things in database match expected - pass + # pass -def test_set1_to_set2_to_set3(): +#def test_set1_to_set2_to_set3(): # Create / connect to database # Create tables by migrating on empty initial set @@ -881,4 +881,4 @@ def test_set1_to_set2_to_set3(): # assert level_exit_table.c.from_level.nullable is False # assert_col_type(level_exit_table.c.to_level, VARCHAR) - pass + # pass -- cgit v1.2.3 From a00ac32045a7a03d91e0e64ee8470f0bd6f383d8 Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Wed, 4 Jul 2012 10:54:44 -0400 Subject: Make sure MigrationManagers always get Unicode names. If we fail to do this, SQLAlchemy complains that we're binding a non-Unicode value to a Unicode column. --- mediagoblin/tests/test_sql_migrations.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 507a7725..7fea101c 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -561,7 +561,7 @@ def test_set1_to_set3(): printer = CollectingPrinter() migration_manager = MigrationManager( - '__main__', SET1_MODELS, SET1_MIGRATIONS, Session(), + u'__main__', SET1_MODELS, SET1_MIGRATIONS, Session(), printer) # Check latest migration and database current migration @@ -586,7 +586,7 @@ def test_set1_to_set3(): # Try to "re-migrate" with same manager settings... nothing should happen migration_manager = MigrationManager( - '__main__', SET1_MODELS, SET1_MIGRATIONS, Session(), + u'__main__', SET1_MODELS, SET1_MIGRATIONS, Session(), printer) assert migration_manager.init_or_migrate() == None @@ -668,7 +668,7 @@ def test_set1_to_set3(): # isn't said to be updated yet printer = CollectingPrinter() migration_manager = MigrationManager( - '__main__', SET3_MODELS, SET3_MIGRATIONS, Session(), + u'__main__', SET3_MODELS, SET3_MIGRATIONS, Session(), printer) assert migration_manager.latest_migration == 7 @@ -694,7 +694,7 @@ def test_set1_to_set3(): # Make sure version matches expected migration_manager = MigrationManager( - '__main__', SET3_MODELS, SET3_MIGRATIONS, Session(), + u'__main__', SET3_MODELS, SET3_MIGRATIONS, Session(), printer) assert migration_manager.latest_migration == 7 assert migration_manager.database_current_migration == 7 -- cgit v1.2.3 From c7dfd4fba6407d70345c5dc9017e87e472ce6722 Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Wed, 4 Jul 2012 11:09:36 -0400 Subject: Level exits are Unicode too. --- mediagoblin/tests/test_sql_migrations.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 7fea101c..8ef46fdc 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -344,8 +344,8 @@ def _insert_migration1_objects(session): name=u'The Necroplex', description=u'A complex full of pure deathzone.', exits={ - 'deathwell': 'evilstorm', - 'portal': 'central_park'}), + u'deathwell': u'evilstorm', + u'portal': u'central_park'}), Level1(id=u'evilstorm', name=u'Evil Storm', description=u'A storm full of pure evil.', @@ -354,7 +354,7 @@ def _insert_migration1_objects(session): name=u'Central Park, NY, NY', description=u"New York's friendly Central Park.", exits={ - 'portal': 'necroplex'})]) + u'portal': u'necroplex'})]) session.commit() -- cgit v1.2.3 From 35a24fc26393b4aec1809885884112e35db7afe0 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sat, 14 Jul 2012 12:36:40 -0500 Subject: Moving the "dependency injection printer tools" over to tools/common.py --- mediagoblin/tests/test_sql_migrations.py | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 8ef46fdc..e3b55634 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -27,6 +27,7 @@ from migrate import changeset from mediagoblin.db.sql.base import GMGTableBase from mediagoblin.db.sql.util import MigrationManager, RegisterMigration +from mediagoblin.tools.common import CollectingPrinter # This one will get filled with local migrations @@ -520,18 +521,6 @@ def _insert_migration3_objects(session): session.commit() -class CollectingPrinter(object): - def __init__(self): - self.collection = [] - - def __call__(self, string): - self.collection.append(string) - - @property - def combined_string(self): - return u''.join(self.collection) - - def create_test_engine(): from sqlalchemy import create_engine engine = create_engine('sqlite:///:memory:', echo=False) -- cgit v1.2.3 From 52539acad2e945002b25d7e07991b634734eee30 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Wed, 5 Dec 2012 16:13:22 -0600 Subject: Add a UniqueConstraint add test in test_sql_migrations We should have this anyway, and Elrond needs it to help fix current broken migration thingies. --- mediagoblin/tests/test_sql_migrations.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index e3b55634..6383d096 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -322,6 +322,28 @@ def creature_power_hitpower_to_float(db_conn): creature_power.c.hitpower.alter(type=Float) +@RegisterMigration(8, FULL_MIGRATIONS) +def creature_power_name_creature_unique(db_conn): + """ + Add a unique constraint to name and creature on creature_power. + + We don't want multiple creature powers with the same name per creature! + """ + # Note: We don't actually check to see if this constraint is set + # up because at present there's no way to do so in sqlalchemy :\ + + metadata = MetaData(bind=db_conn.bind) + + creature_power = Table( + 'creature_power', metadata, + autoload=True, autoload_with=db_conn.bind) + + cons = changeset.constraint.UniqueConstraint( + 'name', 'creature', table=creature_power) + + cons.create() + + def _insert_migration1_objects(session): """ Test objects to insert for the first set of things @@ -660,7 +682,7 @@ def test_set1_to_set3(): u'__main__', SET3_MODELS, SET3_MIGRATIONS, Session(), printer) - assert migration_manager.latest_migration == 7 + assert migration_manager.latest_migration == 8 assert migration_manager.database_current_migration == 0 # Migrate @@ -679,14 +701,15 @@ def test_set1_to_set3(): + Running migration 5, "level_exit_index_from_and_to_level"... done. + Running migration 6, "creature_power_index_creature"... done. + Running migration 7, "creature_power_hitpower_to_float"... done. + + Running migration 8, "creature_power_name_creature_unique"... done. """ # Make sure version matches expected migration_manager = MigrationManager( u'__main__', SET3_MODELS, SET3_MIGRATIONS, Session(), printer) - assert migration_manager.latest_migration == 7 - assert migration_manager.database_current_migration == 7 + assert migration_manager.latest_migration == 8 + assert migration_manager.database_current_migration == 8 # Check all things in database match expected -- cgit v1.2.3 From 1e46dc2537c5ee706a55876c4dbf86129baafbbf Mon Sep 17 00:00:00 2001 From: Sebastian Spaeth Date: Fri, 30 Nov 2012 09:49:45 +0100 Subject: Move db.sql.util to db.util Now that sqlalchemy is providing the database abstractions, there is no need to hide everything in db.sql. sub-modules. It complicates the code and provides a futher layer of indirection. Move the db.sql.util.py to db.util.py and adapt the importers. --- mediagoblin/tests/test_sql_migrations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 6383d096..0e7102bc 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -26,7 +26,7 @@ from sqlalchemy.sql import select, insert from migrate import changeset from mediagoblin.db.sql.base import GMGTableBase -from mediagoblin.db.sql.util import MigrationManager, RegisterMigration +from mediagoblin.db.util import MigrationManager, RegisterMigration from mediagoblin.tools.common import CollectingPrinter -- cgit v1.2.3 From 39dc3bf8db4a0a21220560a259574da4f2c1e12a Mon Sep 17 00:00:00 2001 From: Sebastian Spaeth Date: Mon, 7 Jan 2013 13:03:51 +0100 Subject: Mv db.sql.base to db.base This concludes the db.sql.* -> db.* move. Our db abstraction layer is sqlalchemy, so there is no need to a separate db.sql.* hierarchy. All tests have been run for each of the commit series to make sure everything works at every step. --- mediagoblin/tests/test_sql_migrations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 0e7102bc..26979bdf 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -25,7 +25,7 @@ from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.sql import select, insert from migrate import changeset -from mediagoblin.db.sql.base import GMGTableBase +from mediagoblin.db.base import GMGTableBase from mediagoblin.db.util import MigrationManager, RegisterMigration from mediagoblin.tools.common import CollectingPrinter -- cgit v1.2.3 From a050e776c6d9c4970dd241e92b2f24a8c9deb36d Mon Sep 17 00:00:00 2001 From: Elrond Date: Thu, 13 Dec 2012 12:31:47 +0100 Subject: Move all the migration tools into new migration_tools.py Factor all the migration related stuff out into a new .db.sql.migration_tools. First we don't have to load this module for our normal server. Second it makes all the import dependencies a little more cleaner. --- mediagoblin/tests/test_sql_migrations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 26979bdf..4a260228 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -26,7 +26,7 @@ from sqlalchemy.sql import select, insert from migrate import changeset from mediagoblin.db.base import GMGTableBase -from mediagoblin.db.util import MigrationManager, RegisterMigration +from mediagoblin.db.sql.migration_tools import MigrationManager, RegisterMigration from mediagoblin.tools.common import CollectingPrinter -- cgit v1.2.3 From c130e3ee79affaf9e2e52a98506ffb1a7f5c9db6 Mon Sep 17 00:00:00 2001 From: Elrond Date: Tue, 8 Jan 2013 22:12:16 +0100 Subject: Move db.sql.migration_tools to db.migration_tools. Follow the new trend. --- mediagoblin/tests/test_sql_migrations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 4a260228..2fc4c043 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -26,7 +26,7 @@ from sqlalchemy.sql import select, insert from migrate import changeset from mediagoblin.db.base import GMGTableBase -from mediagoblin.db.sql.migration_tools import MigrationManager, RegisterMigration +from mediagoblin.db.migration_tools import MigrationManager, RegisterMigration from mediagoblin.tools.common import CollectingPrinter -- cgit v1.2.3 From 0536306048daa0970d2e43411ba2a9bf073e570e Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Thu, 16 May 2013 17:51:21 -0500 Subject: Always activate testing in every test module ever. Kind of a dorky way to implement this, but... --- mediagoblin/tests/test_sql_migrations.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index 2fc4c043..ff3d384f 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -28,6 +28,9 @@ from migrate import changeset from mediagoblin.db.base import GMGTableBase from mediagoblin.db.migration_tools import MigrationManager, RegisterMigration from mediagoblin.tools.common import CollectingPrinter +from mediagoblin.tools.testing import _activate_testing + +_activate_testing() # This one will get filled with local migrations -- cgit v1.2.3 From 9a9bafc078192317695a4f06233ea261fe147989 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Fri, 17 May 2013 11:12:56 -0500 Subject: Reverting "Always activate testing in every test module ever." Revert "Always activate testing in every test module ever." This reverts commit 0536306048daa0970d2e43411ba2a9bf073e570e. --- mediagoblin/tests/test_sql_migrations.py | 3 --- 1 file changed, 3 deletions(-) (limited to 'mediagoblin/tests/test_sql_migrations.py') diff --git a/mediagoblin/tests/test_sql_migrations.py b/mediagoblin/tests/test_sql_migrations.py index ff3d384f..2fc4c043 100644 --- a/mediagoblin/tests/test_sql_migrations.py +++ b/mediagoblin/tests/test_sql_migrations.py @@ -28,9 +28,6 @@ from migrate import changeset from mediagoblin.db.base import GMGTableBase from mediagoblin.db.migration_tools import MigrationManager, RegisterMigration from mediagoblin.tools.common import CollectingPrinter -from mediagoblin.tools.testing import _activate_testing - -_activate_testing() # This one will get filled with local migrations -- cgit v1.2.3