Thursday, June 29, 2017

Re: Django ORM and TestCase

Hi,

In TestCase Django wraps all tests within transaction and makes commit and rollback as no-op.

So only code that is within transaction (your test) will see the data.

After test is finished a rollback is issued to remove all the data inserted to db.

And there is no way to read that data outside transaction since its never committed.


29.6.2017 22.01 "howie9393" <howardanguyen@gmail.com> kirjoitti:
I was wondering how django.test.TestCase and the ORM interact. If I create a databased backed object in a TestCase, the ORM can "see" the object when I query for it, as expected.


from django.test import TestCase
class MyTests(TestCase):
   
def test_where_is_my_obj(self):
       
MyObj.objects.create(...)
       
self.assert(MyObj.objects.count(), 1) # this is true as expected


But let's say I put a breakpoint right before the assert statement. If I were to connect to the test database directly using let's say "mysql -u myuser -p test_database_that_django_made", the tables are empty. The MyObj table has no rows. How does the ORM know about this uncommited object? And, how can I see this object in the database shell?

Note: I can get the mysql shell to "see" this object if I use TransactionTestCase instead of TestCase but TransactionTestCase is extremely slow. Also, I'm just like to know how Django "sees" this object in TestCase. Thanks!!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/41694eab-8800-49af-9124-6e241208d40d%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHn91oeEdPp95%2B2ZLigv8JEoBx7nJa-QGLvHUbiqaY421AUZSQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment