Skip to content

Commit

Permalink
Improving tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marinho committed Jul 6, 2010
1 parent 75b4faf commit e078097
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 22 deletions.
2 changes: 1 addition & 1 deletion geraldo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ def get_version():
FIELD_ACTION_MIN, FIELD_ACTION_MAX, FIELD_ACTION_SUM,\
FIELD_ACTION_DISTINCT_COUNT, BAND_WIDTH
from graphics import RoundRect, Rect, Line, Circle, Arc, Ellipse, Image
from exceptions import EmptyQueryset, ObjectNotFound, ManyObjectsFound
from exceptions import EmptyQueryset, ObjectNotFound, ManyObjectsFound, AbortEvent
from cross_reference import CrossReferenceMatrix

1 change: 1 addition & 0 deletions geraldo/tests/02-widgets.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Value, Count, Avg, Min, Max and Sum. Default action is "Value".
>>> from geraldo import ObjectValue, FIELD_ACTION_VALUE

>>> ObjectValue.attribute_name
>>> ObjectValue.expression
>>> ObjectValue.action == FIELD_ACTION_VALUE
True

Expand Down
38 changes: 21 additions & 17 deletions geraldo/tests/12-matplotlib.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,28 @@ Getting data

Building the chart

>>> import pylab
>>> from numpy import numarray

>>> user_ids = report.queryset.filter(id__in=[1,5,7,12]).values('id','username')
>>> labels = [user['username'] for user in user_ids]
>>> data = [user['id'] for user in user_ids]
>>> left = numarray.array(range(len(data))) + 0.5
>>>
>>> tmp = pylab.bar(left, data, width=0.5)
>>> tmp = pylab.yticks(range(15))
>>> tmp = pylab.xticks(left+0.25, labels)
>>> tmp = pylab.title('Users IDs')
>>> pylab.gca().get_xaxis().tick_bottom()
>>> pylab.gca().get_yaxis().tick_left()
>>> pylab.savefig(os.path.join(cur_dir, 'output/matplotlib.png'), dpi=50)
>>> try:
... import pylab
... from numpy import numarray
... except ImportError:
... numarray = pylab = None

>>> if pylab and numarray:
... user_ids = report.queryset.filter(id__in=[1,5,7,12]).values('id','username')
... labels = [user['username'] for user in user_ids]
... data = [user['id'] for user in user_ids]
... left = numarray.array(range(len(data))) + 0.5
...
... tmp = pylab.bar(left, data, width=0.5)
... tmp = pylab.yticks(range(15))
... tmp = pylab.xticks(left+0.25, labels)
... tmp = pylab.title('Users IDs')
... pylab.gca().get_xaxis().tick_bottom()
... pylab.gca().get_yaxis().tick_left()
... pylab.savefig(os.path.join(cur_dir, 'output/matplotlib.png'), dpi=50)

PDF generation

>>> report.generate_by(PDFGenerator, filename=os.path.join(cur_dir, 'output/charts-matplotlib-report.pdf'))

>>> if pylab and numarray:
... report.generate_by(PDFGenerator, filename=os.path.join(cur_dir, 'output/charts-matplotlib-report.pdf'))

37 changes: 36 additions & 1 deletion geraldo/tests/22-events-system.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Element (widgets, graphics, etc.)
>>> from geraldo.utils import A4, cm, TA_CENTER, TA_RIGHT

>>> from geraldo import Report, ReportBand, Label, ObjectValue, SystemField,\
... FIELD_ACTION_COUNT, BAND_WIDTH
... FIELD_ACTION_COUNT, BAND_WIDTH, AbortEvent

Report class

Expand Down Expand Up @@ -58,6 +58,8 @@ Report class
... elements = [
... ObjectValue(attribute_name='id', top=0, left=0, name='other-field-with-event'),
... ObjectValue(attribute_name='name', top=0, left=3*cm, name='field-with-event'),
... ObjectValue(expression='age * unknown_field', top=0, left=7*cm, name='field-with-exception'),
... ObjectValue(expression='name', top=0, left=9*cm, name='field-with-abort-event'),
... ]

>>> class MyObject(object):
Expand Down Expand Up @@ -112,6 +114,20 @@ Report on new page event

>>> report.on_new_page = report_on_new_page

Band before print event

>>> def band_before_print(self, generator):
... print 'Before print band'

>>> report.band_page_header.before_print = band_before_print

Band after print event

>>> def band_after_print(self, generator):
... print 'After print band'

>>> report.band_page_header.after_print = band_after_print

Element with before print event

>>> def element_before_print(self, generator):
Expand All @@ -136,12 +152,31 @@ Element with after print event, that works

>>> report.find_by_name('other-field-with-event').after_print = element_after_print_2

Element with bad expression

>>> def element_on_expression_error(self, exception, expression, instance):
... return 'invalid'

>>> report.find_by_name('field-with-exception').on_expression_error = element_on_expression_error

Element with bad expression

>>> def element_abort_event(self, generator):
... if self.text[:1] == 'N':
... raise AbortEvent()
... else:
... self.text = self.text[:1]

>>> report.find_by_name('field-with-abort-event').before_print = element_abort_event

PDF generation

>>> from geraldo.generators import PDFGenerator

>>> report.generate_by(PDFGenerator, filename=os.path.join(cur_dir, 'output/generated-with-events.pdf'))
Printing page # 1!
Before print band
After print band
Starting generating!
After print ID 1
After print ID 2
Expand Down
30 changes: 30 additions & 0 deletions geraldo/tests/28-expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ the "expression" attribute, that support complex math expressions and calculated
This test aims to test all kinds of expressions, including them on summary, grouppings,
subreports and detail bands even on math calculation, etc.

>>> from decimal import Decimal
>>> import os
>>> cur_dir = os.path.dirname(os.path.abspath(__file__))

Expand Down Expand Up @@ -58,6 +59,35 @@ TODO in the future
>>> #run_expression(objects, 'sum(age * weight)')
>>> #run_expression(objects, 'sum(age * weight) + age / weight')

Testing decimal <-> conversion (useful sometimes)

>>> values = [None, 1, 2.0, Decimal('3')]

>>> el1 = ObjectValue(converts_decimal_to_float=False, converts_float_to_decimal=False)
>>> el2 = ObjectValue(converts_decimal_to_float=True, converts_float_to_decimal=False)
>>> el3 = ObjectValue(converts_decimal_to_float=False, converts_float_to_decimal=True)

>>> el1._clean_empty_values(values)
[0, 1, 2.0, Decimal('3')]

>>> el2._clean_empty_values(values)
[0, 1, 2.0, 3.0]

>>> el3._clean_empty_values(values)
[0, 1, Decimal('2.0'), Decimal('3')]

>>> try:
... sum(el1._clean_empty_values(values))
... except TypeError, e:
... e.message
"unsupported operand type(s) for +: 'float' and 'Decimal'"

>>> sum(el2._clean_empty_values(values))
6.0

>>> sum(el3._clean_empty_values(values))
Decimal('6.0')

Report class
------------

Expand Down
10 changes: 7 additions & 3 deletions geraldo/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def get_queryset_values(self, attribute_name=None):
return map(lambda obj: self.get_object_value(obj, attribute_name), objects)

def _clean_empty_values(self, values):
def _clean(val):
def clean(val):
if not val:
return 0
elif isinstance(val, decimal.Decimal) and self.converts_decimal_to_float:
Expand All @@ -188,7 +188,7 @@ def _clean(val):

return val

return map(_clean, values)
return map(clean, values)

def action_value(self, attribute_name=None):
return self.get_object_value(attribute_name=attribute_name)
Expand Down Expand Up @@ -249,7 +249,11 @@ def _text(self):
self._cached_text = unicode(value)

return self.display_format % self._cached_text
text = property(lambda self: self._text())

def _set_text(self, value):
self._cached_text = value

text = property(lambda self: self._text(), _set_text)

def clone(self):
new = super(ObjectValue, self).clone()
Expand Down

0 comments on commit e078097

Please sign in to comment.