From c897978a313a247aede60a4c5c9dc91ac7ca9b51 Mon Sep 17 00:00:00 2001 From: Bradley Ayers Date: Sat, 21 May 2011 22:53:54 +1000 Subject: [PATCH] LinkColumn now properly handles null foreign keys. resolves issue #7 --- django_tables/columns.py | 2 ++ tests/__init__.py | 5 +++-- tests/columns.py | 33 ++++++++++++++++++++++++++++----- tests/models.py | 28 +++++++++------------------- tests/testapp/models.py | 2 +- 5 files changed, 43 insertions(+), 27 deletions(-) diff --git a/django_tables/columns.py b/django_tables/columns.py index b546ba8..a899799 100644 --- a/django_tables/columns.py +++ b/django_tables/columns.py @@ -221,6 +221,8 @@ class LinkColumn(Column): self.attrs = attrs or {} def render(self, value, record, bound_column): + if value is None: + return self.default # The following params + if statements create the arguments required to # pass to Django's reverse() function. params = {} diff --git a/tests/__init__.py b/tests/__init__.py index 4f2c71b..dec96d7 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -3,7 +3,6 @@ from attest import AssertImportHook, Tests # Django's django.utils.module_loading.module_has_submodule is busted AssertImportHook.disable() - from django.conf import settings # It's important to configure prior to importing the tests, as some of them @@ -22,6 +21,9 @@ settings.configure( ROOT_URLCONF = 'tests.testapp.urls', ) +from django.test.simple import DjangoTestSuiteRunner +runner = DjangoTestSuiteRunner() +runner.setup_databases() from .core import core from .templates import templates @@ -30,5 +32,4 @@ from .utils import utils from .rows import rows from .columns import columns - everything = Tests([core, templates, models, utils, rows, columns]) diff --git a/tests/columns.py b/tests/columns.py index ba87e6e..939a588 100644 --- a/tests/columns.py +++ b/tests/columns.py @@ -1,16 +1,17 @@ # -*- coding: utf-8 -*- """Test the core table functionality.""" from attest import Tests, Assert +from django_attest import TransactionTestContext from django.test.client import RequestFactory from django.template import Context, Template import django_tables as tables from django_tables import utils, A +from .testapp.models import Person -columns = Tests() +general = Tests() - -@columns.test +@general.test def sortable(): class SimpleTable(tables.Table): name = tables.Column() @@ -31,8 +32,11 @@ def sortable(): Assert(SimpleTable([]).columns['name'].sortable) is True -@columns.test -def link_column(): +linkcolumn = Tests() +linkcolumn.context(TransactionTestContext()) + +@linkcolumn.test +def unicode(): """Test LinkColumn""" # test unicode values + headings class UnicodeTable(tables.Table): @@ -54,4 +58,23 @@ def link_column(): Assert(u'Chr…s' in html) Assert(u'DÒble' in html) + # Test handling queryset data with a null foreign key + + +@linkcolumn.test +def null_foreign_key(): + """ + + """ + class PersonTable(tables.Table): + first_name = tables.Column() + last_name = tables.Column() + occupation = tables.LinkColumn('occupation', args=[A('occupation.pk')]) + + Person.objects.create(first_name='bradley', last_name='ayers') + + table = PersonTable(Person.objects.all()) + table.as_html() + +columns = Tests([general, linkcolumn]) diff --git a/tests/models.py b/tests/models.py index 6075f6f..e68daab 100644 --- a/tests/models.py +++ b/tests/models.py @@ -1,39 +1,29 @@ +import itertools from django.conf import settings -from django.test.simple import DjangoTestSuiteRunner from django.test.client import RequestFactory from django.template import Template, Context -from django.core.paginator import * import django_tables as tables -import itertools -from django_attest import TestContext +from django_attest import TransactionTestContext from attest import Tests, Assert from .testapp.models import Person, Occupation models = Tests() -models.context(TestContext()) +models.context(TransactionTestContext()) -runner = DjangoTestSuiteRunner() -runner.setup_databases() +class PersonTable(tables.Table): + first_name = tables.Column() + last_name = tables.Column() + occupation = tables.Column() -@models.context -def samples(): - class PersonTable(tables.Table): - first_name = tables.Column() - last_name = tables.Column() - occupation = tables.Column() - # Test data +@models.test +def boundrows_iteration(): occupation = Occupation.objects.create(name='Programmer') Person.objects.create(first_name='Bradley', last_name='Ayers', occupation=occupation) Person.objects.create(first_name='Chris', last_name='Doble', occupation=occupation) - yield PersonTable - - -@models.test -def boundrows_iteration(client, PersonTable): table = PersonTable(Person.objects.all()) records = [row.record for row in table.rows] expecteds = Person.objects.all() diff --git a/tests/testapp/models.py b/tests/testapp/models.py index f08923b..61b0d95 100644 --- a/tests/testapp/models.py +++ b/tests/testapp/models.py @@ -4,7 +4,7 @@ from django.db import models class Person(models.Model): first_name = models.CharField(max_length=200) last_name = models.CharField(max_length=200) - occupation = models.ForeignKey('Occupation', related_name='people') + occupation = models.ForeignKey('Occupation', related_name='people', null=True) def __unicode__(self): return self.first_name -- 2.26.2