Refactor tests, sample config to use [rattail.db] config section.

This commit is contained in:
Lance Edgar 2014-05-30 22:25:31 -07:00
parent 87c70c4375
commit a04712524d
2 changed files with 36 additions and 39 deletions

View file

@ -34,23 +34,6 @@ configure_logging = True
#git.executable = "C:\Program Files\Git\bin\git.exe"
[edbob.db]
# Keys which name the database engines configured within this section. Without
# this option, the 'default' key is assumed; but it must be included if the
# option is defined.
#keys = default, host, other
# Connection details for the default database engine. Configuration is
# (mostly) defined by the SQLAlchemy library; for details see the parameters at
# http://docs.sqlalchemy.org/en/latest/core/engines.html#sqlalchemy.create_engine
#default.url = postgresql://user:pass@localhost/rattail
# Connection details for additional database engines, as needed.
#host.url = postgresql://user:pass@host-server/rattail
#other.url = mysql://user:pass@another-server/rattail
[edbob.errors]
# Mako template used to generate the email body for an unhandled exception. If
@ -114,15 +97,29 @@ subject.errors = [Rattail] Error
[rattail.db]
# Keys which name the database engines configured within this section. Without
# this option, the 'default' key is assumed; but it must be included if the
# option is defined.
#keys = default, host, other
# Connection details for the default database engine. Configuration is
# (mostly) defined by the SQLAlchemy library; for details see the parameters at
# http://docs.sqlalchemy.org/en/latest/core/engines.html#sqlalchemy.create_engine
#default.url = postgresql://user:pass@localhost/rattail
# Connection details for additional database engines, as needed.
#host.url = postgresql://user:pass@host-server/rattail
#other.url = mysql://user:pass@another-server/rattail
# Whether to record "change" data for the local database (i.e. the 'default'
# engine). This should only be enabled for a parent/master database, as it is
# used to track which data the database synchronization service will sync to
# the child/slave databases.
#record_changes = True
# Database engine keys (as found in the ``[edbob.db]`` section above) which
# correspond to child/slave databases in the hierarchy. These databases will
# be kept in sync by the database synchronization service, if used.
# Database engine keys (as found in the `keys` option, above) which correspond
# to child/slave databases in the hierarchy. These databases will be kept in
# sync by the database synchronization service, if used.
#syncs = north, south
# Class specification to use for the primary "data synchronizer" instance.

View file

@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from unittest import TestCase
@ -14,9 +15,8 @@ from rattail import db
class TestConfigureSessionFactory(TestCase):
def setUp(self):
self.config = AppConfigParser('rattail')
self.config.add_section('edbob.db')
self.config.add_section('rattail.db')
self.config = AppConfigParser(u'rattail')
self.config.add_section(u'rattail.db')
self.Session = sessionmaker()
def test_session_is_not_bound_if_no_engine_is_defined_by_config(self):
@ -26,56 +26,56 @@ class TestConfigureSessionFactory(TestCase):
session.close()
def test_session_is_correctly_bound_if_engine_is_defined_by_config(self):
self.config.set('edbob.db', 'sqlalchemy.url', 'sqlite:////a/very/custom/db')
self.config.set(u'rattail.db', u'sqlalchemy.url', u'sqlite:////a/very/custom/db')
session = self.Session()
self.assertTrue(session.bind is None)
session.close()
db.configure_session_factory(self.config, self.Session)
session = self.Session()
self.assertTrue(isinstance(session.bind, Engine))
self.assertEqual(str(session.bind.url), 'sqlite:////a/very/custom/db')
self.assertEqual(unicode(session.bind.url), u'sqlite:////a/very/custom/db')
session.close()
def test_global_session_is_configured_by_default(self):
self.config.set('edbob.db', 'sqlalchemy.url', 'sqlite:////path/to/rattail.sqlite')
self.config.set(u'rattail.db', u'sqlalchemy.url', u'sqlite:////path/to/rattail.sqlite')
session = db.Session()
self.assertTrue(session.bind is None)
session.close()
db.configure_session_factory(self.config)
session = db.Session()
self.assertTrue(isinstance(session.bind, Engine))
self.assertEqual(str(session.bind.url), 'sqlite:////path/to/rattail.sqlite')
self.assertEqual(unicode(session.bind.url), u'sqlite:////path/to/rattail.sqlite')
session.close()
# Must undo that configuration, this thing is global.
db.Session.configure(bind=None)
@patch('rattail.db.changes.record_changes')
@patch(u'rattail.db.changes.record_changes')
def test_changes_will_not_be_recorded_by_default(self, record_changes):
self.config.set('edbob.db', 'sqlalchemy.url', 'sqlite://')
self.config.set(u'rattail.db', u'sqlalchemy.url', u'sqlite://')
db.configure_session_factory(self.config, self.Session)
self.assertFalse(record_changes.called)
@patch('rattail.db.changes.record_changes')
@patch(u'rattail.db.changes.record_changes')
def test_changes_will_be_recorded_by_so_configured(self, record_changes):
self.config.set('edbob.db', 'sqlalchemy.url', 'sqlite://')
self.config.set('rattail.db', 'changes.record', 'true')
self.config.set(u'rattail.db', u'sqlalchemy.url', u'sqlite://')
self.config.set(u'rattail.db', u'changes.record', u'true')
db.configure_session_factory(self.config, self.Session)
# Role changes are ignored by default.
record_changes.assert_called_once_with(self.Session, True)
@patch('rattail.db.changes.record_changes')
@patch(u'rattail.db.changes.record_changes')
def test_changes_will_still_be_recorded_with_deprecated_config(self, record_changes):
self.config.set('edbob.db', 'sqlalchemy.url', 'sqlite://')
self.config.set('rattail.db', 'changes.record', 'true')
self.config.set(u'rattail.db', u'sqlalchemy.url', u'sqlite://')
self.config.set(u'rattail.db', u'changes.record', u'true')
db.configure_session_factory(self.config, self.Session)
# Role changes are ignored by default.
record_changes.assert_called_once_with(self.Session, True)
@patch('rattail.db.changes.record_changes')
@patch(u'rattail.db.changes.record_changes')
def test_config_determines_if_role_changes_are_ignored(self, record_changes):
self.config.set('edbob.db', 'sqlalchemy.url', 'sqlite://')
self.config.set('rattail.db', 'changes.record', 'true')
self.config.set('rattail.db', 'changes.ignore_roles', 'false')
self.config.set(u'rattail.db', u'sqlalchemy.url', u'sqlite://')
self.config.set(u'rattail.db', u'changes.record', u'true')
self.config.set(u'rattail.db', u'changes.ignore_roles', u'false')
db.configure_session_factory(self.config, self.Session)
# Role changes are ignored by default; False means config works.
record_changes.assert_called_once_with(self.Session, False)