Re: TestCase failing when using transaction.atomic() inside test

2020-05-12 Thread Uri Kogan
Continuing my investigation I got the a following which does not work but should: from django.contrib.auth.models import User from django.test import TransactionTestCase from django.db import transaction class FooTest(TransactionTestCase): def test_bar(self): with

Re: TestCase failing when using transaction.atomic() inside test

2020-05-12 Thread Uri Kogan
The documentation states that "you cannot test that a block of code is executing within a transaction" while I am looking to "test a block of code that has a transaction". In any case, the issue here is that "transaction.atomic" does not work when nested while it should clearly work. On

Re: TestCase failing when using transaction.atomic() inside test

2020-05-12 Thread Aldian Fazrihady
Probably you need to use TransactionTestCase, which is the parent of TestCase instead: https://docs.djangoproject.com/en/3.0/topics/testing/tools/#transactiontestcase That page also mentions a specific problem when using transaction on TestCase: ``` A consequence of this, however, is that some

Re: TestCase failing when using transaction.atomic() inside test

2020-05-12 Thread Uri Kogan
That not that simple in my case because. I'll show what I mean: from django.contrib.auth.models import User from django.test import TestCase from rolepermissions.roles import assign_role class FooTest(TestCase): def test_bar(self): u = User.objects.create_user(username="abc",

Re: TestCase failing when using transaction.atomic() inside test

2020-05-12 Thread Aldian Fazrihady
When testing django apps, my unit test usually accesses the django app via django test client: https://docs.djangoproject.com/en/3.0/topics/testing/tools/#the-test-client. Django test client simulates http access, so my Django tests always run my Django code starting from Django views. Even though

Re: TestCase failing when using transaction.atomic() inside test

2020-05-11 Thread Uri Kogan
While this can be done in my code, there are libraries that the project use that have "transaction.atomic" in them. For example, pretty popular django-role-permissions. >From what I see in the documentation, there should be no problem to use transactions within transactions in TestCase. On

Re: TestCase failing when using transaction.atomic() inside test

2020-05-11 Thread Aldian Fazrihady
I don't think the subclass of TestCase need to use transaction.atomic. Why can't you just remove the transaction.atomic? Regards, Aldian Fazrihady http://aldianfazrihady.com Pada tanggal Sel, 12 Mei 2020 04.02, Uri Kogan menulis: > Hello, > > I am using TestCase and trying to create an object

TestCase failing when using transaction.atomic() inside test

2020-05-11 Thread Uri Kogan
Hello, I am using TestCase and trying to create an object during test. There is a log activated on MySQL server, so I see all the queries being executed there. This "transaction.atomic" sets a SAVEPOINT in the database thinking that the transaction is already started. The problem is that there