Test Database का व्यवहार
In this page:
एक separate test database क्यों होता है
अगर tests आपके real database में लिखते, तो वे real data corrupt कर सकते थे या junk rows पीछे छोड़ सकते थे। Django इसे suite शुरू होने से पहले एक throwaway database बनाकर solve करता है।
उदाहरण: Why a separate test database exists
If tests wrote to your real database, they could corrupt real data or leave behind junk rows. Django solves this by building a throwaway database before the suite starts.
python manage.py test
⚠️ Run this in your own terminal or Node.js environment.
हर test के लिए automatic rollback
TestCase हर test method को एक transaction में wrap करता है और बाद में इसे rollback कर देता है, ताकि एक test में बनाए गए objects कभी दूसरे test में leak न हों।
उदाहरण: Automatic rollback per test
TestCase wraps each test method in a transaction and rolls it back afterward, so objects created in one test never leak into the next.
from django.test import TestCase
from myapp.models import Book
class IsolationTests(TestCase):
def test_one_creates_a_book(self):
Book.objects.create(title='Temp Book')
self.assertEqual(Book.objects.count(), 1)
def test_two_starts_with_no_books(self):
self.assertEqual(Book.objects.count(), 0)
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
SQLite और test database
DATABASES[default] के रूप में SQLite के साथ, Django का test runner default रूप से test database को memory में बना सकता है, जिससे पूरी suite बिना किसी extra setup के तेज़ी से run होती है।
उदाहरण: SQLite and the test database
With SQLite as DATABASES[default], Django's test runner can create the test database in memory by default, making the whole suite run quickly with zero extra setup.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- यह चिंता करना कि tests run करने से real production/development data modify या delete हो जाएगा — Django का test database पूरी तरह अलग होता है और बाद में फेंक दिया जाता है।
- यह मान लेना कि एक test method में बनाया गया data दूसरे में visible है — हर test method के database changes अपने आप rollback होते हैं।
- यह न समझना कि test database वही engine उपयोग करता है जो
DATABASES[default](यहां SQLite) उपयोग करता है, इसलिए SQLite project के लिए कोई extra configuration ज़रूरी नहीं।
- Tests run करने से पहले, Django एक separate test database बनाता है, default रूप से test_ prefix के नाम से।
- हर individual test method एक database transaction के अंदर run होता है जो test खत्म होने पर rollback हो जाता है।
- पूरी suite खत्म होने के बाद, Django test database को अपने आप destroy कर देता है।
- SQLite projects के लिए, test database पूरी तरह memory में भी run हो सकता है, जिससे tests तेज़ हो जाते हैं।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: