Files @ 177478f7d054
Branch filter:

Location: rattail-project/rattail/tests/db/test_init.py - annotation

lance
Database config/init overhaul.

This contains some not-very-atomic changes:

* Get rid of `get_session_class()` function and return to global `Session`
class approach.
* Primary database `Session` is now configured as part of command
initialization, by default.
* Make `config` object available to subcommands, and `Daemon` instances
(the beginning of the end for `edbob.config`!).
* Add `--stdout` and `--stderr` arguments to primary `Command`. These are
in turn made available to subcommands.
* Overhauled some subcommand logic per new patterns.
* Get rid of a few other random references to `edbob`.
* Added and improved several tests.
* Added ability to run tests using arbitrary database engine.

from unittest import TestCase

from mock import patch

from sqlalchemy.orm import sessionmaker
from sqlalchemy.engine import Engine

from edbob.configuration import AppConfigParser

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.Session = sessionmaker()

    def test_session_is_not_bound_if_no_engine_is_defined_by_config(self):
        db.configure_session_factory(self.config, self.Session)
        engine = self.Session.kw['bind']
        self.assertTrue(engine is None)

    def test_session_is_correctly_bound_if_engine_is_defined_by_config(self):
        self.config.set('edbob.db', 'sqlalchemy.url', 'postgresql://rattail:rattail@localhost/rattail')
        self.assertTrue(self.Session.kw['bind'] is None)
        db.configure_session_factory(self.config, self.Session)
        engine = self.Session.kw['bind']
        self.assertTrue(isinstance(engine, Engine))
        self.assertEqual(str(engine.url), 'postgresql://rattail:rattail@localhost/rattail')

    def test_global_session_is_configured_by_default(self):
        self.config.set('edbob.db', 'sqlalchemy.url', 'sqlite:////path/to/rattail.sqlite')
        self.assertTrue(db.Session.kw['bind'] is None)
        db.configure_session_factory(self.config)
        engine = db.Session.kw['bind']
        self.assertTrue(isinstance(engine, Engine))
        self.assertEqual(str(engine.url), 'sqlite:////path/to/rattail.sqlite')
        # Must undo that configuration, this thing is global.
        db.Session.configure(bind=None)

    @patch('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://')
        db.configure_session_factory(self.config, self.Session)
        self.assertFalse(record_changes.called)

    @patch('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')
        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')
    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', 'record_changes', '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')
    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')
        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)