Django: confusion in testing

In the unit tests I wrote, I provide test equipment for each test chamber. However, I keep getting a duplicate (IntegrityError) when it runs in the test suite.

So, I’m wondering, shouldn't the database be created and populated at the beginning of each TestCase, and after each test is done in this test chamber, so that I can provide a different fixture for each test database?


Edit: added test code

class DashboardTestCase(TestCase):
    fixtures = ['initial.json']

    def setUp(self):
        u = User(username='su')
        u.save()
        self.name = "Test Dashboard"
        self.slug = "test-dashboard"
        self.description = "Test Description"
        self.fiscal_year = "2011"
        self.region = "Test Region"
        self.review_date = "2010-08-01"
        self.date_completed = "2009-03-15"
        self.prepared_by = "Test User"
        self.dashboard = Dashboard(name=self.name, description=self.description, fiscal_year=self.fiscal_year,
                                    region=self.region, review_date=self.review_date, date_completed=self.date_completed,
                                    prepared_by=self.prepared_by)
        self.dashboard.save()

    def testDashboardName(self):
        self.assertEqual(self.dashboard.name, self.name)
    def testDashboardDescription(self):
        self.assertEqual(self.dashboard.description, self.description)
    def testDashboardFiscalYear(self):
        self.assertEqual(self.dashboard.fiscal_year, self.fiscal_year)
    def testDashboardRegion(self):
        self.assertEqual(self.dashboard.region, self.region)
    def testDashboardReviewDate(self):
        self.assertEqual(self.dashboard.review_date, self.review_date)
    def testDashboardDateCompleted(self):
        self.assertEqual(self.dashboard.date_completed, self.date_completed)
    def testDashboardPreparedBy(self):
        self.assertEqual(self.dashboard.prepared_by, self.prepared_by)
    def testDashboardSlug(self):
        self.assertEqual(self.dashboard.slug, self.slug)
    def testDashboardSlugClash(self):
        # Create another dashboard with exactly the same name.
        self.dashboard2 = Dashboard(name='Test Dashboard')
        self.dashboard2.save()
        self.assertEqual(self.dashboard2.slug, 'test-dashboard1')

And the trace:

Installing json fixture 'initial_data' from '/Users/Eric/Documents/DjangoProjects/MMR/../MMR/dashboard/fixtures'.
Problem installing fixture '/Users/Eric/Documents/DjangoProjects/MMR/../MMR/dashboard/fixtures/initial_data.json': Traceback (most recent call last):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/commands/loaddata.py", line 169, in handle
    obj.save(using=using)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/serializers/base.py", line 165, in save
    models.Model.save_base(self.object, using=using, raw=True)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/base.py", line 501, in save_base
    rows = manager.using(using).filter(pk=pk_val)._update(values)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/query.py", line 491, in _update
    return query.get_compiler(self.db).execute_sql(None)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 861, in execute_sql
    cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 727, in execute_sql
    cursor.execute(sql, params)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 86, in execute
    return self.cursor.execute(query, args)
  File "build/bdist.macosx-10.5-i386/egg/MySQLdb/cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)
  File "build/bdist.macosx-10.5-i386/egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
IntegrityError: (1062, "Duplicate entry 'dashboard-action' for key 'app_label'")
+3
source share
1 answer

So, I’m interested, shouldn't the database be created and populated at the beginning of each test system

- . , ./manage.py test. . , , . .

(IntegrityError),

, , initial_data, . initial_data syncdb, . , initial_data .

- , .

Problem installing fixture '/Users/Eric/Documents/DjangoProjects/MMR/../MMR/dashboard/fixtures/initial_data.json':

, (initial.json). , - initial_data.json, .

:

"Duplicate entry 'dashboard-action' for key 'app_label'"
  • , / , app_label
  • initial_data.json , app_label.
  • .

- . initial.json initial_data.json. initial_data.json, . fixtures = ... , initial_data.json.

2

, Django. . , .

+2

Source: https://habr.com/ru/post/1760556/


All Articles