From a7a704bfcb7574e28073e87d7d6b3293d91779cc Mon Sep 17 00:00:00 2001 From: =?utf8?q?Michael=20Elsd=C3=B6rfer?= Date: Sat, 14 Jun 2008 17:34:59 +0000 Subject: [PATCH] using py.test for testing; added some tests; minor model bugfixes; --- README | 15 +++++++ django_tables/columns.py | 4 +- django_tables/models.py | 7 ++-- tests/__init__.py | 3 ++ tests/{test.py => test_basic.py} | 65 +++------------------------- tests/test_models.py | 72 ++++++++++++++++++++++++++++++++ tests/test_templates.py | 56 +++++++++++++++++++++++++ 7 files changed, 157 insertions(+), 65 deletions(-) create mode 100644 tests/__init__.py rename tests/{test.py => test_basic.py} (67%) create mode 100644 tests/test_models.py create mode 100644 tests/test_templates.py diff --git a/README b/README index 9408857..704019a 100644 --- a/README +++ b/README @@ -1,3 +1,18 @@ +django-tables +============= + +Installation +------------ + +Adding django-tables to your INSTALLED_APPS settings is optional, it'll get +you the ability to load some template utilities via {% load tables %}, but +apart from that, ``import django_tables as tables`` should get you going. + +Running the test suite +---------------------- + +The test suite uses py.test (from http://codespeak.net/py/dist/test.html). + Tables and Pagination --------------------- diff --git a/django_tables/columns.py b/django_tables/columns.py index 164bcd5..1ee5e84 100644 --- a/django_tables/columns.py +++ b/django_tables/columns.py @@ -1,5 +1,5 @@ __all__ = ( - 'Column', + 'Column', 'TextColumn', 'NumberColumn', ) class Column(object): @@ -32,7 +32,7 @@ class Column(object): self.default = default self.visible = visible self.sortable = sortable - + self.creation_counter = Column.creation_counter Column.creation_counter += 1 diff --git a/django_tables/models.py b/django_tables/models.py index bc820d2..c0f8d1b 100644 --- a/django_tables/models.py +++ b/django_tables/models.py @@ -1,4 +1,5 @@ -from tables import BaseTable, DeclarativeColumnsMetaclass +from django.utils.datastructures import SortedDict +from tables import BaseTable, DeclarativeColumnsMetaclass, Column __all__ = ('BaseModelTable', 'ModelTable') @@ -23,7 +24,7 @@ def columns_for_model(model, columns=None, exclude=None): field_list = [] opts = model._meta for f in opts.fields + opts.many_to_many: - if (fields and not f.name in fields) or \ + if (columns and not f.name in columns) or \ (exclude and f.name in exclude): continue column = Column() @@ -45,7 +46,7 @@ class ModelTableMetaclass(DeclarativeColumnsMetaclass): # if a model is defined, then build a list of default columns and # let the declared columns override them. if opts.model: - columns = columns_for_model(opts.model, opts.fields, opts.exclude) + columns = columns_for_model(opts.model, opts.columns, opts.exclude) columns.update(self.declared_columns) self.base_columns = columns return self diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..9ec883c --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,3 @@ +# make django-tables available for import for tests +import os, sys +sys.path.append(os.path.join(os.path.dirname(__file__), '..')) \ No newline at end of file diff --git a/tests/test.py b/tests/test_basic.py similarity index 67% rename from tests/test.py rename to tests/test_basic.py index 29ee0db..3fe7fe6 100644 --- a/tests/test.py +++ b/tests/test_basic.py @@ -1,6 +1,7 @@ -from django.core.paginator import Paginator -import os, sys -sys.path.append(os.path.join(os.path.dirname(__file__), '..')) +"""Test the base table functionality. + +This includes the core, as well as static data, non-model tables. +""" import django_tables as tables @@ -43,7 +44,6 @@ def test_declaration(): assert 'motto' in StateTable1.base_columns assert 'motto' in StateTable2.base_columns - # test more with actual models def test_basic(): class BookTable(tables.Table): @@ -93,59 +93,4 @@ def test_sort(): books.columns['language'].sortable = False books.order_by = 'language' assert not books.order_by - test_order(('language', 'pages'), [1,3,2,4]) # as if: 'pages' - -def test_for_templates(): - class BookTable(tables.Table): - id = tables.Column() - name = tables.Column() - books = BookTable([ - {'id': 1, 'name': 'Foo: Bar'}, - ]) - - # cast to a string we get a value ready to be passed to the querystring - books.order_by = ('name',) - assert str(books.order_by) == 'name' - books.order_by = ('name', '-id') - assert str(books.order_by) == 'name,-id' - -test_declaration() -test_basic() -test_sort() -test_for_templates() - - -""" - - - {% for column in book.columns %} - {{ column }} -{% for row in book %} - - {% for value in row %} - - {% endfor %} - -{% endfor %} -
{{ column }}
{{ value }]
- -OR: - - -{% for row in book %} - - {% if book.columns.name.visible %} - - {% endif %} - {% if book.columns.score.visible %} - - {% endif %} - -{% endfor %} -
{{ row.name }]{{ row.score }]
- - -""" \ No newline at end of file + test_order(('language', 'pages'), [1,3,2,4]) # as if: 'pages' \ No newline at end of file diff --git a/tests/test_models.py b/tests/test_models.py new file mode 100644 index 0000000..48191e7 --- /dev/null +++ b/tests/test_models.py @@ -0,0 +1,72 @@ +"""Test ModelTable specific functionality. + +Sets up a temporary Django project using a memory SQLite database. +""" + +from django.conf import settings + +def setup_module(module): + settings.configure(**{ + 'DATABASE_ENGINE': 'sqlite3', + 'DATABASE_NAME': ':memory:', + }) + + from django.db import models + + class City(models.Model): + name = models.TextField() + population = models.IntegerField() + module.City = City + + class Country(models.Model): + name = models.TextField() + population = models.IntegerField() + capital = models.ForeignKey(City) + tld = models.TextField(verbose_name='Domain Extension', max_length=2) + module.Country = Country + + +def test_nothing(): + pass + +import django_tables as tables + +def test_declaration(): + """Test declaration, declared columns and default model field columns. + """ + + class CountryTable(tables.ModelTable): + class Meta: + model = Country + + assert len(CountryTable.base_columns) == 5 + assert 'name' in CountryTable.base_columns + assert not hasattr(CountryTable, 'name') + + # Override one model column, add another custom one, exclude one + class CountryTable(tables.ModelTable): + capital = tables.TextColumn(verbose_name='Name of capital') + projected = tables.Column(verbose_name="Projected Population") + class Meta: + model = Country + exclude = ['tld'] + + assert len(CountryTable.base_columns) == 5 + assert 'projected' in CountryTable.base_columns + assert 'capital' in CountryTable.base_columns + assert not 'tld' in CountryTable.base_columns + + # Inheritance (with a different model) + field restrictions + class CityTable(CountryTable): + class Meta: + model = City + columns = ['id', 'name'] + exclude = ['capital'] + + print CityTable.base_columns + assert len(CityTable.base_columns) == 4 + assert 'id' in CityTable.base_columns + assert 'name' in CityTable.base_columns + assert 'projected' in CityTable.base_columns # declared in parent + assert not 'population' in CityTable.base_columns # not in Meta:columns + assert 'capital' in CityTable.base_columns # in exclude, but only works on model fields (is that the right behaviour?) \ No newline at end of file diff --git a/tests/test_templates.py b/tests/test_templates.py new file mode 100644 index 0000000..7d2e5bc --- /dev/null +++ b/tests/test_templates.py @@ -0,0 +1,56 @@ +"""Test template specific functionality. + +Make sure tables expose their functionality to templates right. +""" + +import django_tables as tables + +def test_for_templates(): + class BookTable(tables.Table): + id = tables.Column() + name = tables.Column() + books = BookTable([ + {'id': 1, 'name': 'Foo: Bar'}, + ]) + + # cast to a string we get a value ready to be passed to the querystring + books.order_by = ('name',) + assert str(books.order_by) == 'name' + books.order_by = ('name', '-id') + assert str(books.order_by) == 'name,-id' + + +""" + + + {% for column in book.columns %} + {{ column }} +{% for row in book %} + + {% for value in row %} + + {% endfor %} + +{% endfor %} +
{{ column }}
{{ value }]
+ +OR: + + +{% for row in book %} + + {% if book.columns.name.visible %} + + {% endif %} + {% if book.columns.score.visible %} + + {% endif %} + +{% endfor %} +
{{ row.name }]{{ row.score }]
+ + +""" \ No newline at end of file -- 2.26.2