Skip to content

Instantly share code, notes, and snippets.

@pablasso
Last active February 29, 2016 23:37
Show Gist options
  • Save pablasso/da34b3587d1ba6519acc to your computer and use it in GitHub Desktop.
Save pablasso/da34b3587d1ba6519acc to your computer and use it in GitHub Desktop.
test_transaction.py
from django.core.management.base import BaseCommand
from django.db import transaction
class Command(BaseCommand):
args = ''
help = ''
def handle(self, **options):
for i in range(0, 5):
self.__class__.test_func(i)
print 'Ok.'
@classmethod
@transaction.commit_manually
def test_func(cls, i):
try:
cls.save_release(i)
cls.save_market(i)
transaction.commit()
except Exception, e:
print(str(e))
print('...rolling back')
transaction.rollback()
@classmethod
def save_release(cls, i):
from api.models import Release, WizelineUser
release = Release()
release.name = 'this is a test %s' % i
release.creator = WizelineUser.objects.get(email='pablasso@wizeline.com')
release.organization = release.creator.organizations.all()[0]
release.save()
@classmethod
def save_market(cls, i):
if i % 2 != 0:
raise Exception('omg an error')
from api.models import MarketNeed
market_need = MarketNeed()
market_need.title = 'this is a test %s' % i
market_need.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment