1

I have the following test

Class FriendsTest(TestCase):

    # always login user
    def setUp(self):
        self.user = User.objects.all().order_by('date_joined')[0]
        self.client.login(facebook_id = self.user.get_profile().facebook_id)

    def test_test(self):
        r = self.client.get(reverse('web.views.friends.main'))
        self.assertEqual(r.status_code, 200)

    def test_test2(self):
        r = self.client.get(reverse('web.views.friends.main'))
        self.assertTemplateUsed(r, 'web/friends.html')
        # and some test for GET data here.

the test works if I only have one test method (test_test). But when I have more than one method (test_test2), I keep getting error "IntegrityError: Could not load web.CustomProfile(pk=1): (1062, "Duplicate entry '1' for key 'id_user'")".

the error trace is below. I would really appreciate any help.

.Problem installing fixture '/Users/ruixia/public_html/x/project/web/fixtures/data.json': Traceback (most recent call last):

File "/Users/ruixia/virt_env/x/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 196, in handle obj.save(using=using)

File "/Users/ruixia/virt_env/x/lib/python2.7/site-packages/django/core/serializers/base.py", line 165, in save models.Model.save_base(self.object, using=using, raw=True)

File "/Users/ruixia/virt_env/x/lib/python2.7/site-packages/django/db/models/base.py", line 551, in save_base result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)

File "/Users/ruixia/virt_env/x/lib/python2.7/site-packages/django/db/models/manager.py", line 203, in _insert return insert_query(self.model, objs, fields, **kwargs)

File "/Users/ruixia/virt_env/x/lib/python2.7/site-packages/django/db/models/query.py", line 1576, in insert_query return query.get_compiler(using=using).execute_sql(return_id)

File "/Users/ruixia/virt_env/x/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 910, in execute_sql cursor.execute(sql, params)

File "/Users/ruixia/virt_env/x/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 114, in execute return self.cursor.execute(query, args)

File "/Users/ruixia/virt_env/x/lib/python2.7/site-packages/MySQLdb/cursors.py", line 201, in execute self.errorhandler(self, exc, value)

File "/Users/ruixia/virt_env/x/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler raise errorclass, errorvalue IntegrityError: Could not load web.CustomProfile(pk=1): (1062, "Duplicate entry '1' for key 'id_user'")

David Robinson
  • 77,383
  • 16
  • 167
  • 187
Rui Xia
  • 175
  • 4
  • 20
  • What is in the file `Users/ruixia/public_html/x/project/web/fixtures/data.json`? – David Robinson Jan 06 '13 at 01:59
  • @DavidRobinson the json is dumped from my dev server with manage.py dumpdata – Rui Xia Jan 06 '13 at 02:13
  • If you remove the content (but leave in the definition) of all three functions (comment them out, replace with `pass`), does the error still occur? What if you comment out only `test_test` and `test_test2`, or only `setUp`? – David Robinson Jan 06 '13 at 02:18
  • I think I found the answer at http://stackoverflow.com/questions/3499791/how-do-i-prevent-fixtures-from-conflicting-with-django-post-save-signal-code – Rui Xia Jan 06 '13 at 02:19

0 Answers0