-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding Transact testing utility with transact pytest fixture (#119)
- Loading branch information
Showing
3 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import absolute_import, print_function, unicode_literals | ||
|
||
from sqlalchemy import event | ||
from sqlalchemy.engine import Engine | ||
|
||
from .db import databases | ||
|
||
|
||
class CommitException(BaseException): | ||
pass | ||
|
||
|
||
class Transact(object): | ||
@staticmethod | ||
def _commit(connection, *args, **kwargs): | ||
raise CommitException("Commits are not allowed") | ||
|
||
def start(self): | ||
self.hook() | ||
|
||
def stop(self): | ||
self.end() | ||
self.unhook() | ||
|
||
def hook(self): | ||
if not event.contains(Engine, "commit", self._commit): | ||
event.listen(Engine, "commit", self._commit) | ||
if not event.contains(Engine, "commit_twophase", self._commit): | ||
event.listen(Engine, "commit_twophase", self._commit) | ||
|
||
def unhook(self): | ||
if event.contains(Engine, "commit", self._commit): | ||
event.remove(Engine, "commit", self._commit) | ||
if event.contains(Engine, "commit_twophase", self._commit): | ||
event.remove(Engine, "commit_twophase", self._commit) | ||
|
||
def end(self): | ||
databases.rollback() | ||
databases.remove() | ||
|
||
def __enter__(self): | ||
self.start() | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
self.stop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters