Ticket #16340: 16340.patch

File 16340.patch, 2.3 KB (added by Jonas Obrist, 13 years ago)

new patch (same as https://github.com/ojii/django/compare/django:master...ojii:16340)

  • django/db/models/query.py

    From 758d99697f94a9f210e22a38c96c511d7ee0b7a2 Mon Sep 17 00:00:00 2001
    From: Jonas Obrist <ojiidotch@gmail.com>
    Date: Sat, 24 Dec 2011 23:29:38 +0100
    Subject: [PATCH] updated patch 3 of 16340 to trunk
    
    ---
     django/db/models/query.py               |    4 +++-
     tests/modeltests/get_or_create/tests.py |   13 +++++++++++++
     2 files changed, 16 insertions(+), 1 deletions(-)
    
    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index fd531dd..e70e431 100644
    a b  
    44
    55import copy
    66import itertools
     7import sys
    78
    89from django.db import connections, router, transaction, IntegrityError
    910from django.db.models.fields import AutoField
    def get_or_create(self, **kwargs):  
    450451                return obj, True
    451452            except IntegrityError, e:
    452453                transaction.savepoint_rollback(sid, using=self.db)
     454                exc_info = sys.exc_info()
    453455                try:
    454456                    return self.get(**lookup), False
    455457                except self.model.DoesNotExist:
    456                     raise e
     458                    raise exc_info[1], None, exc_info[2]
    457459
    458460    def latest(self, field_name=None):
    459461        """
  • tests/modeltests/get_or_create/tests.py

    diff --git a/tests/modeltests/get_or_create/tests.py b/tests/modeltests/get_or_create/tests.py
    index 4cf4450..64dfdbf 100644
    a b  
    11from __future__ import absolute_import
    22
    33from datetime import date
     4import sys
     5import traceback
    46
    57from django.db import IntegrityError
    68from django.test import TestCase
    def test_get_or_create(self):  
    5254            ManualPrimaryKeyTest.objects.get_or_create, id=1, data="Different"
    5355        )
    5456        self.assertEqual(ManualPrimaryKeyTest.objects.get(id=1).data, "Original")
     57       
     58        # get_or_create should raise IntegrityErrors with the full traceback.
     59        # This is tested by checking that a known method call is in the traceback.
     60        # We cannot use assertRaises/assertRaises here because we need to inspect
     61        # the actual traceback.
     62        try:
     63            ManualPrimaryKeyTest.objects.get_or_create(id=1, data="Different")
     64        except IntegrityError, e:
     65            formatted_traceback = traceback.format_exc()
     66            self.assertIn('obj.save', formatted_traceback)
     67
Back to Top