Because there is open transaction and changed data is readable within that
transaction. And since each test runs in own transaction ORM can see the
changes within each test.

Transaction is like a (new) file in text editor. You can edit text and only
you can see the edited text. Only if you save the file it will be possible
that someone else can read that text and changes you made.

6.7.2017 7.58 ip. "howie9393" <howardangu...@gmail.com> kirjoitti:

Thanks for the reply! So how does the Django ORM see that uncommitted data
in the database when we run the MyObj.objects.count()?


On Thursday, June 29, 2017 at 2:34:28 PM UTC-7, Jani Tiainen wrote:

> 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" <howard...@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...@googlegroups.com.
> To post to this group, send email to django...@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/ms
> gid/django-users/41694eab-8800-49af-9124-6e241208d40d%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/41694eab-8800-49af-9124-6e241208d40d%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> 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+unsubscr...@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/274b93c6-f616-47d3-b535-6b38d771b18b%40googlegroups.com
<https://groups.google.com/d/msgid/django-users/274b93c6-f616-47d3-b535-6b38d771b18b%40googlegroups.com?utm_medium=email&utm_source=footer>
.

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+unsubscr...@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/CAHn91ofdEaY%2Bzhtjicx2wPoHn4qfN-xOGPotJB3AN66yOxThPw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to