def _makeSqlite(parsed): from norm.sqlite import sqlite connstr = mkConnStr(parsed) db = sqlite.connect(connstr) db.row_factory = sqlite.Row runner = BlockingRunner(db) runner.db_scheme = 'sqlite' return defer.succeed(runner)
def test_close(self): """ You can close the runner """ mock = MagicMock() runner = BlockingRunner(mock) d = runner.close() d.addCallback(lambda _: mock.close.assert_called_once_with()) return d
def test_runOperation(self): """ Should run an interaction that runs the query but doesn't return results. """ db = sqlite3.connect(':memory:') runner = BlockingRunner(db) d = runner.runOperation('create table foo (name text)') def done(_): db.execute('insert into foo (name) values (?)', ('name1',)) return d.addCallback(done)
def test_runQuery(self): """ Should run an interaction that runs the query and returns results. """ db = sqlite3.connect(':memory:') db.execute('create table foo (name text)') db.execute('insert into foo (name) values (?)', ('name1',)) db.execute('insert into foo (name) values (?)', ('name2',)) runner = BlockingRunner(db) d = runner.runQuery('select name from foo order by name') def check(result): self.assertEqual(map(tuple,result), [ ('name1',), ('name2',), ]) return d.addCallback(check)
def test_runInteraction_error(self): """ If there's an error in the interaction, do a rollback """ db = sqlite3.connect(':memory:') mock = create_autospec(db) runner = BlockingRunner(mock) def interaction(cursor): raise Exception('foo') def check(result): mock.rollback.assert_called_once_with() d = runner.runInteraction(interaction) return d.addErrback(check)
def test_runInteraction(self): """ Should call the function with an instance of cursorFactory """ db = sqlite3.connect(':memory:') mock = create_autospec(db) runner = BlockingRunner(mock) def interaction(cursor, *args, **kwargs): self.assertTrue(isinstance(cursor, BlockingCursor)) self.assertEqual(args, (1,2,3)) self.assertEqual(kwargs, {'foo': 'bar'}) return 'result' def check(result): self.assertEqual(result, 'result') mock.commit.assert_called_once_with() d = runner.runInteraction(interaction, 1, 2, 3, foo='bar') return d.addCallback(check)