Source

django-denormalize / denormalize / tests / test_django / test_collections.py

Full commit
from ...models import *
from ..common import ModelTestCase, CollectionTest
from . import models

# TODO: check if the SQL queries are efficient (all data should be fetched in one query)

class BookCollection(DocumentCollection):
    model = models.Book
    name = "books"
    select_related = ['publisher', 'extra_info']
    prefetch_related = ['chapter_set', 'authors', 'tags', 'publisher__links',
        'category_set', 'publisher__tags']
    exclude = ['authors__email', 'summary']
    add_model_name = True

    def dump_obj(self, model, obj, path):
        # Customization: for tags, use the name string as the representation
        if model.name == 'Tag':
            return obj.name
        # Here only the url
        if model.name == 'PublisherLink':
            return obj.url
        return super(BookCollection, self).dump_obj(model, obj, path)


class DjangoCollectionTest(ModelTestCase, CollectionTest):
    def get_test_objects(self):
        return {'book': Book.objects.get(title=u"Cooking for Geeks")}

    def get_models(self):
        return models


class DjangoORMTest(ModelTestCase):
    def test_reverse_field_registration(self):
        self.assertTrue(hasattr(models.A, 'b_set'))
        self.assertTrue(models.A._meta.get_field_by_name('b'))

        self.assertTrue(hasattr(models.A, 'c_set'))
        self.assertTrue(models.A._meta.get_field_by_name('c'))