Ticket #5968: 5968-3.diff

File 5968-3.diff, 5.6 KB (added by jamesp, 13 years ago)

Adding tests

  • django/contrib/databrowse/sites.py

    diff --git a/django/contrib/databrowse/sites.py b/django/contrib/databrowse/sites.py
    index 172ca52..eed8071 100644
    a b  
    11from django import http
    22from django.db import models
     3from django.db.models.base import ModelBase
    34from django.contrib.databrowse.datastructures import EasyModel
    45from django.shortcuts import render_to_response
    56from django.utils.safestring import mark_safe
    class DatabrowseSite(object):  
    8586        If a model is already registered, this will raise AlreadyRegistered.
    8687        """
    8788        databrowse_class = databrowse_class or DefaultModelDatabrowse
    88         if issubclass(model_or_iterable, models.Model):
     89        if isinstance(model_or_iterable, models.base.ModelBase):
    8990            model_or_iterable = [model_or_iterable]
    9091        for model in model_or_iterable:
    9192            if model in self.registry:
    class DatabrowseSite(object):  
    9899
    99100        If a model isn't already registered, this will raise NotRegistered.
    100101        """
    101         if issubclass(model_or_iterable, models.Model):
     102        if isinstance(model_or_iterable, models.base.ModelBase):
    102103            model_or_iterable = [model_or_iterable]
    103104        for model in model_or_iterable:
    104105            if model not in self.registry:
  • docs/ref/contrib/databrowse.txt

    diff --git a/docs/ref/contrib/databrowse.txt b/docs/ref/contrib/databrowse.txt
    index 33c8228..fac541b 100644
    a b How to use Databrowse  
    3939    2. Register a number of models with the Databrowse site::
    4040
    4141           from django.contrib import databrowse
    42            from myapp.models import SomeModel, SomeOtherModel
     42           from myapp.models import SomeModel, SomeOtherModel, YetAnotherModel
    4343
    4444           databrowse.site.register(SomeModel)
    45            databrowse.site.register(SomeOtherModel)
     45           databrowse.site.register([SomeOtherModel, YetAnotherModel])
    4646
    4747       Note that you should register the model *classes*, not instances.
    4848
     49       The register function accepts an individual model class or a list of classes.
     50
    4951       It doesn't matter where you put this, as long as it gets executed at some
    5052       point. A good place for it is in your :doc:`URLconf file
    5153       </topics/http/urls>` (``urls.py``).
  • new file tests/regressiontests/bug5968/models.py

    diff --git a/tests/regressiontests/bug5968/__init__.py b/tests/regressiontests/bug5968/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/bug5968/models.py b/tests/regressiontests/bug5968/models.py
    new file mode 100644
    index 0000000..3788b04
    - +  
     1from django.db import models
     2
     3class SomeModel(models.Model):
     4    some_field = models.CharField(max_length=50)
     5
     6    def __unicode__(self):
     7        return self.some_field
     8
     9class SomeOtherModel(models.Model):
     10    some_other_field = models.CharField(max_length=50)
     11
     12    def __unicode__(self):
     13        return self.some_other_field
     14
     15class YetAnotherModel(models.Model):
     16    yet_another_field = models.CharField(max_length=50)
     17
     18    def __unicode__(self):
     19        return self.yet_another_field
  • new file tests/regressiontests/bug5968/tests.py

    diff --git a/tests/regressiontests/bug5968/tests.py b/tests/regressiontests/bug5968/tests.py
    new file mode 100644
    index 0000000..cc796a0
    - +  
     1from django.contrib import databrowse
     2from django.test import TestCase
     3
     4from models import SomeModel, SomeOtherModel, YetAnotherModel
     5
     6class Bug5968Test(TestCase):
     7    """
     8    Test for bug #5968 - allow an iterable containing models to be passed to
     9    databrowse.site.register/unregister.
     10
     11    """
     12
     13    def setUp(self):
     14        sm1 = SomeModel.objects.create(some_field="Some Data 1")
     15        sm2 = SomeModel.objects.create(some_field="Some Data 2")
     16        som1 = SomeOtherModel.objects.create(some_other_field="Some Other Data 1")
     17        som2 = SomeOtherModel.objects.create(some_other_field="Some Other Data 2")
     18        yam1 = YetAnotherModel.objects.create(yet_another_field="Yet Some Other Data 1")
     19        yam2 = YetAnotherModel.objects.create(yet_another_field="Yet Some Other Data 2")
     20
     21    def test_databrowse_registration(self):
     22        databrowse.site.register(SomeModel)
     23        self.assertEqual(SomeModel in databrowse.site.registry, True)
     24        databrowse.site.register([SomeOtherModel, YetAnotherModel])
     25        self.assertEqual(SomeOtherModel in databrowse.site.registry and
     26            YetAnotherModel in databrowse.site.registry, True)
     27
     28        self.assertRaisesMessage(
     29            databrowse.sites.AlreadyRegistered,
     30            'The model SomeModel is already registered',
     31            databrowse.site.register, [SomeModel, SomeOtherModel]
     32        )
     33
     34        databrowse.site.unregister(SomeOtherModel)
     35        self.assertEqual(SomeOtherModel not in databrowse.site.registry, True)
     36        databrowse.site.unregister([SomeModel, YetAnotherModel])
     37        self.assertEqual(SomeModel not in databrowse.site.registry and
     38            YetAnotherModel not in databrowse.site.registry, True)
     39
     40        self.assertRaisesMessage(
     41            databrowse.sites.NotRegistered,
     42            'The model SomeModel is not registered',
     43            databrowse.site.unregister, [SomeModel, SomeOtherModel]
     44        )
     45
     46        self.assertRaisesMessage(
     47            databrowse.sites.AlreadyRegistered,
     48            'The model SomeModel is already registered',
     49            databrowse.site.register, [SomeModel, SomeModel]
     50        )
     51
     52        databrowse.site.unregister(SomeModel)
     53        databrowse.site.register([SomeModel])
     54        self.assertEqual(SomeModel in databrowse.site.registry, True)
     55
Back to Top