Example #1
0
    def test_transaction_connection_handling(self):
        patch = 'peewee.Database'
        db = SqliteDatabase(':memory:')
        with mock.patch(patch, wraps=db) as patched_db:
            with transaction(patched_db):
                patched_db.begin.assert_called_once_with()
                self.assertEqual(patched_db.commit.call_count, 0)
                self.assertEqual(patched_db.rollback.call_count, 0)

            patched_db.begin.assert_called_once_with()
            patched_db.commit.assert_called_once_with()
            self.assertEqual(patched_db.rollback.call_count, 0)

        with mock.patch(patch, wraps=db) as patched_db:
            def _test_patched():
                patched_db.commit.side_effect = ValueError
                with transaction(patched_db):
                    pass

            self.assertRaises(ValueError, _test_patched)
            patched_db.begin.assert_called_once_with()
            patched_db.commit.assert_called_once_with()
            patched_db.rollback.assert_called_once_with()
    def test_transaction_connection_handling(self):
        patch = 'peewee.Database'
        db = SqliteDatabase(':memory:')
        with mock.patch(patch, wraps=db) as patched_db:
            with transaction(patched_db):
                patched_db.begin.assert_called_once_with()
                self.assertEqual(patched_db.commit.call_count, 0)
                self.assertEqual(patched_db.rollback.call_count, 0)

            patched_db.begin.assert_called_once_with()
            patched_db.commit.assert_called_once_with()
            self.assertEqual(patched_db.rollback.call_count, 0)

        with mock.patch(patch, wraps=db) as patched_db:
            def _test_patched():
                patched_db.commit.side_effect = ValueError
                with transaction(patched_db):
                    pass

            self.assertRaises(ValueError, _test_patched)
            patched_db.begin.assert_called_once_with()
            patched_db.commit.assert_called_once_with()
            patched_db.rollback.assert_called_once_with()
Example #3
0
    def test_print_header(self):
        cmdline = '-i -e sqlite /path/to/database.db'

        with capture_output() as output:
            with mock.patch('pwiz.datetime.datetime') as mock_datetime:
                now = mock_datetime.now.return_value
                now.strftime.return_value = 'February 03, 2015 15:30PM'
                print_header(cmdline, self.introspector)

        self.assertEqual(output.data.strip(), (
            '# Code generated by:\n'
            '# python -m pwiz %s\n'
            '# Date: February 03, 2015 15:30PM\n'
            '# Database: peewee_test.db\n'
            '# Peewee version: %s') % (cmdline, peewee_version))
Example #4
0
    def test_print_header(self):
        cmdline = '-i -e sqlite /path/to/database.db'

        with capture_output() as output:
            with mock.patch('pwiz.datetime.datetime') as mock_datetime:
                now = mock_datetime.now.return_value
                now.strftime.return_value = 'February 03, 2015 15:30PM'
                print_header(cmdline, self.introspector)

        self.assertEqual(output.data.strip(),
                         ('# Code generated by:\n'
                          '# python -m pwiz %s\n'
                          '# Date: February 03, 2015 15:30PM\n'
                          '# Database: peewee_test.db\n'
                          '# Peewee version: %s') % (cmdline, peewee_version))
    def test_atomic_nesting(self):
        db = SqliteDatabase(':memory:')
        db_patches = mock.patch.multiple(
            db,
            begin=mock.DEFAULT,
            commit=mock.DEFAULT,
            execute_sql=mock.DEFAULT,
            rollback=mock.DEFAULT)

        with mock.patch('peewee.Database', wraps=db) as patched_db:
            with db_patches as db_mocks:
                begin = db_mocks['begin']
                commit = db_mocks['commit']
                execute_sql = db_mocks['execute_sql']
                rollback = db_mocks['rollback']

                with _atomic(patched_db):
                    patched_db.transaction.assert_called_once_with(None)
                    begin.assert_called_once_with(lock_type=None)
                    self.assertEqual(patched_db.savepoint.call_count, 0)

                    with _atomic(patched_db):
                        patched_db.transaction.assert_called_once_with(None)
                        begin.assert_called_once_with(lock_type=None)
                        patched_db.savepoint.assert_called_once_with()
                        self.assertEqual(commit.call_count, 0)
                        self.assertEqual(rollback.call_count, 0)

                        with _atomic(patched_db):
                            (patched_db.transaction
                             .assert_called_once_with(None))
                            begin.assert_called_once_with(lock_type=None)
                            self.assertEqual(
                                patched_db.savepoint.call_count,
                                2)

                    begin.assert_called_once_with(lock_type=None)
                    self.assertEqual(commit.call_count, 0)
                    self.assertEqual(rollback.call_count, 0)

                commit.assert_called_once_with()
                self.assertEqual(rollback.call_count, 0)
Example #6
0
    def test_atomic_nesting(self):
        db = SqliteDatabase(':memory:')
        db_patches = mock.patch.multiple(
            db,
            begin=mock.DEFAULT,
            commit=mock.DEFAULT,
            execute_sql=mock.DEFAULT,
            rollback=mock.DEFAULT)

        with mock.patch('peewee.Database', wraps=db) as patched_db:
            with db_patches as db_mocks:
                begin = db_mocks['begin']
                commit = db_mocks['commit']
                execute_sql = db_mocks['execute_sql']
                rollback = db_mocks['rollback']

                with _atomic(patched_db):
                    patched_db.transaction.assert_called_once_with()
                    begin.assert_called_once_with()
                    self.assertEqual(patched_db.savepoint.call_count, 0)

                    with _atomic(patched_db):
                        patched_db.transaction.assert_called_once_with()
                        begin.assert_called_once_with()
                        patched_db.savepoint.assert_called_once_with()
                        self.assertEqual(commit.call_count, 0)
                        self.assertEqual(rollback.call_count, 0)

                        with _atomic(patched_db):
                            patched_db.transaction.assert_called_once_with()
                            begin.assert_called_once_with()
                            self.assertEqual(
                                patched_db.savepoint.call_count,
                                2)

                    begin.assert_called_once_with()
                    self.assertEqual(commit.call_count, 0)
                    self.assertEqual(rollback.call_count, 0)

                commit.assert_called_once_with()
                self.assertEqual(rollback.call_count, 0)
Example #7
0
    def test_atomic_nesting(self):
        db = SqliteDatabase(":memory:")
        db_patches = mock.patch.multiple(
            db, begin=mock.DEFAULT, commit=mock.DEFAULT, execute_sql=mock.DEFAULT, rollback=mock.DEFAULT
        )

        with mock.patch("peewee.Database", wraps=db) as patched_db:
            with db_patches as db_mocks:
                begin = db_mocks["begin"]
                commit = db_mocks["commit"]
                execute_sql = db_mocks["execute_sql"]
                rollback = db_mocks["rollback"]

                with _atomic(patched_db):
                    patched_db.transaction.assert_called_once_with(None)
                    begin.assert_called_once_with(lock_type=None)
                    self.assertEqual(patched_db.savepoint.call_count, 0)

                    with _atomic(patched_db):
                        patched_db.transaction.assert_called_once_with(None)
                        begin.assert_called_once_with(lock_type=None)
                        patched_db.savepoint.assert_called_once_with()
                        self.assertEqual(commit.call_count, 0)
                        self.assertEqual(rollback.call_count, 0)

                        with _atomic(patched_db):
                            (patched_db.transaction.assert_called_once_with(None))
                            begin.assert_called_once_with(lock_type=None)
                            self.assertEqual(patched_db.savepoint.call_count, 2)

                    begin.assert_called_once_with(lock_type=None)
                    self.assertEqual(commit.call_count, 0)
                    self.assertEqual(rollback.call_count, 0)

                commit.assert_called_once_with()
                self.assertEqual(rollback.call_count, 0)