On Wed, Aug 5, 2009 at 11:31 AM, Peter Bengtsson<pete...@gmail.com> wrote:
>
> I have found that when running this as a normal server, the rollback
> DOES work. It's just in tests it doesn't work.
>
> On 5 Aug, 17:27, Peter Bengtsson <pete...@gmail.com> wrote:
>> Here's the models:
>>
>> # models.py
>> class Article(models.Model):
>>     title = models.CharField(max_length=100)
>>
>> # urls.py
>> urlpatterns = patterns('',
>>     (r'^delete-rolledback/$', delete_rolledback),
>> )
>>
>> # views.py
>> def delete_rolledback(request):
>>     transaction.enter_transaction_management()
>>     transaction.managed(True)
>>     qs = Article.objects.all()
>>     for article in qs:
>>         if article.title.lower().startswith(request.GET.get
>> ('startswith')):
>>             article.delete()
>>             break
>>     transaction.rollback()
>>     return HttpResponse("Rolled back!")
>>
>> # tests.py
>> from django.test import TestCase
>>
>> from news.models import Article
>>
>> class SimpleTest(TestCase):
>>     def setUp(self):
>>         Article.objects.create(title=u'Abraham')
>>         Article.objects.create(title=u'Ben')
>>         Article.objects.create(title=u'Ancor')
>>         Article.objects.create(title=u'Wat')
>>
>>         super(SimpleTest, self).setUp()
>>
>>     def test_deletion(self):
>>         count_before = Article.objects.count()
>>         assert count_before == 4
>>         r = self.client.get('/news/delete-rolledback/',
>>                             dict(startswith='a'))
>>         assert r.content.count("Rolled back")
>>         count_after = Article.objects.count()
>>         assert count_after == 4, count_after
>>
>> When I run these tests (with postgres or with sqlite) I get this
>> assertion error:
>>
>> Traceback (most recent call last):
>> ...
>> AssertionError: 3
>>
>> If someone more clued up than me could take a look at this and confirm
>> that it is a Django bug I can start looking into explaining what's
>> wrong and possibilities of a patch.
>> I've found this ticket (http://code.djangoproject.com/ticket/4758)
>> where someone has experienced similar problems when using Oracle. But
>> only for Oracle; he claims the same code *works* with Postgres.
> >
>

Your tests test transaction behavior so you need to subclass
TransactionTestCase instead of jsut TestCase.

Alex

-- 
"I disapprove of what you say, but I will defend to the death your
right to say it." -- Voltaire
"The people's good is the highest law." -- Cicero
"Code can always be simpler than you think, but never as simple as you
want" -- Me

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to