Ticket #16173: unittest.diff

File unittest.diff, 2.6 KB (added by Silvan Spross, 13 years ago)

tried to create a unittest for this

  • tests/modeltests/many_to_one/tests.py

     
    44from django.test import TestCase
    55from django.core.exceptions import FieldError
    66
    7 from models import Article, Reporter
     7from models import Article, Reporter, ArticleWithDummyManager
     8from models import SuccessfulExecutedException
    89
    910
    1011class ManyToOneTests(TestCase):
     
    372373        # recursive don't cause recursion depth problems under deepcopy.
    373374        self.r.cached_query = Article.objects.filter(reporter=self.r)
    374375        self.assertEqual(repr(deepcopy(self.r)), "<Reporter: John Smith>")
     376       
     377    def test_reverse_single_related_manager(self):
     378        # This unit test prooves if the dummy managers get methode will be
     379        # executed. Related to the ticket #16173.
     380        r1 = Reporter.objects.create(first_name='John', last_name='Doh', email='john@doh.com')
     381        a1 = ArticleWithDummyManager.objects.create(id=None, headline="Article with dummy manager",
     382                                    pub_date=datetime(2011, 6, 9), reporter_id=r1.id)
     383        self.assertRaises(a1.reporter, SuccessfulExecutedException)
     384       
     385 No newline at end of file
  • tests/modeltests/many_to_one/models.py

     
    2424
    2525    class Meta:
    2626        ordering = ('headline',)
     27       
     28class SuccessfulExecutedException(Exception):
     29    pass
     30
     31class ArticleDummyManager(models.Manager):
     32    use_for_related_fields = True
     33   
     34    def get(self, **kwargs):
     35        raise SuccessfulExecutedException
     36       
     37class ArticleWithDummyManager(Article):
     38    default_manager = ArticleDummyManager()
  • django/db/models/fields/related.py

     
    311311            rel_mgr = self.field.rel.to._default_manager
    312312            db = router.db_for_read(self.field.rel.to, instance=instance)
    313313            if getattr(rel_mgr, 'use_for_related_fields', False):
    314                 rel_obj = rel_mgr.using(db).get(**params)
     314                rel_obj = rel_mgr.db_manager(db).get(**params)
    315315            else:
    316316                rel_obj = QuerySet(self.field.rel.to).using(db).get(**params)
    317317            setattr(instance, cache_name, rel_obj)
Back to Top