Merge lp:~abompard/mailman/failing-test into lp:mailman

Proposed by Aurélien Bompard
Status: Merged
Approved by: Barry Warsaw
Approved revision: 7265
Merged at revision: 7265
Proposed branch: lp:~abompard/mailman/failing-test
Merge into: lp:mailman
Diff against target: 42 lines (+13/-8)
1 file modified
src/mailman/database/tests/test_factory.py (+13/-8)
To merge this branch: bzr merge lp:~abompard/mailman/failing-test
Reviewer Review Type Date Requested Status
Barry Warsaw Approve
Review via email: mp+243033@code.launchpad.net

Description of the change

Fix a failing schemamanager test

To post a comment you must log in.
Revision history for this message
Barry Warsaw (barry) wrote :

Thanks! I made a few small changes.

* in TestSchemaManager.setUp(), I used md.sorted_tables so I didn't have to call .values():

        for table in md.sorted_tables:
            table.drop(config.db.engine)

* In test_initial() I used assertIn() on md.tables (since if it's a mapping, `in` should automatically compare against .keys()):

        self.assertIn('alembic_version', md.tables)

I don't quite understand the code though, and I would like to add some comments to the test.

* After calling setup_database(), why would you expect that alembic.command.upgrade was not called?

* After calling setup_database(), would you expect alembic_version table to exist, or would it only exist after the md.reflect() call?

review: Approve
Revision history for this message
Aurélien Bompard (abompard) wrote :

> * After calling setup_database(), why would you expect that alembic.command.upgrade was not called?

Because when there is no existing database, I'm not using alembic.command.upgrade, I'm creating all tables in SQLAlchemy with Metadata.create_all and them I'm stamping the schema at Alembic's latest version with alembic.command.stamp. So I'm checking that I'm not going through a different code path.

> * After calling setup_database(), would you expect alembic_version table to exist, or would it only exist after the md.reflect() call?

The table is created by the Alembic stamp command, but it's not in Mailman's metadata (we have no model for it since we don't use it), so I have to detect its presence using md.reflect().

Does this answer your questions?

Revision history for this message
Barry Warsaw (barry) wrote :

On Nov 29, 2014, at 08:38 AM, Aurélien Bompard wrote:

>Does this answer your questions?

It does, thanks! I will update the comments.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/mailman/database/tests/test_factory.py'
2--- src/mailman/database/tests/test_factory.py 2014-10-13 19:24:24 +0000
3+++ src/mailman/database/tests/test_factory.py 2014-11-27 13:31:21 +0000
4@@ -51,9 +51,9 @@
5 Model.metadata.drop_all(config.db.engine)
6 md = MetaData()
7 md.reflect(bind=config.db.engine)
8- for tablename in ('alembic_version', 'version'):
9- if tablename in md.tables:
10- md.tables[tablename].drop(config.db.engine)
11+ # Drop leftover tables (alembic & storm schema versions)
12+ for table in md.tables.values():
13+ table.drop(config.db.engine)
14 self.schema_mgr = SchemaManager(config.db)
15
16 def tearDown(self):
17@@ -114,15 +114,20 @@
18 self.assertFalse(alembic_command.stamp.called)
19 self.assertFalse(alembic_command.upgrade.called)
20
21- @patch('alembic.command')
22- def test_initial(self, alembic_command):
23+ @patch('alembic.command.upgrade')
24+ def test_initial(self, alembic_command_upgrade):
25 # No existing database.
26 self.assertFalse(self._table_exists('mailinglist'))
27 self.assertFalse(self._table_exists('alembic_version'))
28- self.schema_mgr.setup_database()
29- self.assertFalse(alembic_command.upgrade.called)
30+ head_rev = self.schema_mgr.setup_database()
31+ self.assertFalse(alembic_command_upgrade.called)
32 self.assertTrue(self._table_exists('mailinglist'))
33- self.assertTrue(self._table_exists('alembic_version'))
34+ md = MetaData()
35+ md.reflect(bind=config.db.engine)
36+ self.assertTrue('alembic_version' in md.tables.keys())
37+ current_rev = config.db.engine.execute(
38+ md.tables["alembic_version"].select()).scalar()
39+ self.assertEqual(current_rev, head_rev)
40
41 @patch('alembic.command.stamp')
42 def test_storm(self, alembic_command_stamp):