def test_headers(self): G(SimpleDemoModel, n=2, char='abc', integer1=10, integer2=20) ds = Datasource.as_datasource(model=SimpleDemoModel) r = BaseReport.as_report(datasource=ds, list_display=['integer2', 'char', 'integer1']) self.assertSequenceEqual(['Integer #2', 'Character', 'Integer #1'], r.headers) ds = Datasource.as_datasource(model=SimpleDemoModel, columns=['integer2', 'char', 'integer1']) r = BaseReport.as_report(datasource=ds) self.assertSequenceEqual(['Integer #2', 'Character', 'Integer #1'], r.headers)
def test_list_columns(self): G(SimpleDemoModel, n=2, char='abc', integer1=1, integer2=3) ds = Datasource.as_datasource(model=SimpleDemoModel, columns=[('char', Column), ('integer1', Column)]) self.assertSequenceEqual([c.name for c in ds.columns], ['char', 'integer1']) self.assertSequenceEqual([c.title for c in ds.columns], ['Character', 'Integer #1']) self.assertSequenceEqual(ds, [(u'abc', 1), (u'abc', 1)])
def test_by_custom_column(self): itms = [ User(username='******' % x, first_name='fn%s' % int(x % 2), last_name='ln%s' % int(x % 2), is_staff=bool(x % 2)) for x in range(6) ] ds = Datasource.as_datasource( model=User, columns=('is_staff', 'username', StringFormatColumn( 'group', format='{0.last_name} {0.first_name}')), queryset=FakeQuerySet(model=User, items=itms)) TestReport = type( 'Report', (BaseReport, ), { 'datasource': ds, 'group_by': ('group', 'username'), 'list_display': ('group', 'username') }) report = TestReport.as_report() self.assertSequenceEqual(sorted(dict(report.get_groups()).keys()), ['ln0 fn0', 'ln1 fn1']) self.assertSequenceEqual(list(report), [('ln0 fn0', 'username0'), ('ln0 fn0', 'username2'), ('ln0 fn0', 'username4'), ('ln1 fn1', 'username1'), ('ln1 fn1', 'username3'), ('ln1 fn1', 'username5')])
def test_get_column_values(self): G(SimpleDemoModel, n=10, data_fixture=SequentialDataFixture(0)) ds = Datasource.as_datasource(model=SimpleDemoModel) r = BaseReport.as_report(datasource=ds, list_display=['integer2', 'char', 'integer1']) self.assertSequenceEqual([1, 3, 5, 7, 9, 11, 13, 15, 17, 19], r.get_column_values('integer1'))
def test_col_to_css_class(): c = Column('user.first_name') r = RowValue('Test', column=c) assert col_to_css_class(r) == 'ereports_engine_columns_column' e = RowValueError('Test') assert col_to_css_class(e) == 'Test' with pytest.raises(ValueError): col_to_css_class('Wrong') ds = Datasource.as_datasource( model=User, queryset=FakeQuerySet( model=User, items=[User(username='******'), User(username='******')])) TestReport = type('TestReport', (BaseReport, ), { 'model': User, 'datasource': ds }) report = TestReport.as_report() assert col_to_css_class( report[0].username) == 'ereports_engine_columns_charcolumn' assert col_to_css_class( report[0]['username']) == 'ereports_engine_columns_charcolumn'
def test_subtotal(): G(SimpleDemoModel, n=2, char='abc', integer1=10, integer2=20) ds = Datasource.as_datasource(model=SimpleDemoModel) r = BaseReport.as_report(datasource=ds) assert subtotal(r, r, 'integer1') == 20 assert subtotal(r, int, 'integer1') == 0
def test_by_custom_column(self): itms = [User(username='******' % x, first_name='fn%s' % int(x % 2), last_name='ln%s' % int(x % 2), is_staff=bool(x % 2)) for x in range(6)] ds = Datasource.as_datasource(model=User, columns=('is_staff', 'username', StringFormatColumn('group', format='{0.last_name} {0.first_name}')), queryset=FakeQuerySet(model=User, items=itms)) TestReport = type('Report', (BaseReport,), {'datasource': ds, 'group_by': ('group', 'username'), 'list_display': ('group', 'username')}) report = TestReport.as_report() self.assertSequenceEqual(sorted(dict(report.get_groups()).keys()), ['ln0 fn0', 'ln1 fn1']) self.assertSequenceEqual(list(report), [('ln0 fn0', 'username0'), ('ln0 fn0', 'username2'), ('ln0 fn0', 'username4'), ('ln1 fn1', 'username1'), ('ln1 fn1', 'username3'), ('ln1 fn1', 'username5')])
def test_post_filter(self): instances = G(SimpleDemoModel, n=5, char='abc') ds = Datasource.as_datasource(queryset=SimpleDemoModel.objects.all(), columns=['id', 'char']) ds.add_filters(id__gt=instances[2].pk) self.assertSequenceEqual(ds, [(instances[3].pk, u'abc'), (instances[4].pk, u'abc')])
def test_total(): G(SimpleDemoModel, n=2, char='abc', integer1=10, integer2=20) ds = Datasource.as_datasource(model=SimpleDemoModel) r = BaseReport.as_report(datasource=ds) assert total(r, 'integer2') == 40 assert total(r, 'char') == 0
def test_callback_group(self): itms = [ User(username='******' % x, is_staff=bool(x % 2)) for x in range(6) ] ds = Datasource.as_datasource(model=User, queryset=FakeQuerySet(model=User, items=itms)) def is_staff(rec): return str(rec.is_staff) TestReport = type( 'TestReport', (BaseReport, ), { 'model': User, 'list_display': ('is_staff', 'username'), 'group_by': (is_staff, 'username'), 'datasource': ds }) report = TestReport.as_report() self.assertItemsEqual( dict(report.get_groups()).keys(), ['True', 'False']) self.assertSequenceEqual(list(report), [(False, 'username0'), (False, 'username2'), (False, 'username4'), (True, 'username1'), (True, 'username3'), (True, 'username5')])
def test_columns(self): G(SimpleDemoModel, n=2, char='abc', integer1=1) ds = Datasource.as_datasource(model=SimpleDemoModel, columns=[Column('char', 'AAA'), Column('integer1')]) self.assertSequenceEqual([c.name for c in ds.columns], ['char', 'integer1']) self.assertSequenceEqual([c.title for c in ds.columns], ['AAA', 'Integer #1']) self.assertSequenceEqual(ds, [(u'abc', 1), (u'abc', 1)])
def test_custom_column_from_string(self): G(SimpleDemoModel, n=2, char='abc', integer1=1, integer2=3) ds = Datasource.as_datasource(model=SimpleDemoModel, extra_column=lambda obj: 'extra_value', columns=[Column('integer1'), 'integer2', CalcColumn(['integer1', 'integer2'])]) self.assertSequenceEqual(ds, [(1, 3, 4), (1, 3, 4)])
def test_custom_column(self): G(SimpleDemoModel, n=2, char='abc', integer1=1, integer2=3) ds = Datasource.as_datasource(model=SimpleDemoModel, columns=[ Column('integer1'), Column('integer2'), CalcColumn(['integer1', 'integer2']) ]) self.assertSequenceEqual(ds, [(1, 3, 4), (1, 3, 4)])
def test_custom_column_from_string(self): G(SimpleDemoModel, n=2, char='abc', integer1=1, integer2=3) ds = Datasource.as_datasource(model=SimpleDemoModel, extra_column=lambda obj: 'extra_value', columns=[ Column('integer1'), 'integer2', CalcColumn(['integer1', 'integer2']) ]) self.assertSequenceEqual(ds, [(1, 3, 4), (1, 3, 4)])
def test_datasource_custom_list_display(self): G(SimpleDemoModel, n=2, char='abc', integer1=10, integer2=20) ds = Datasource.as_datasource(model=SimpleDemoModel) r = BaseReport.as_report(datasource=ds, list_display=['integer2', 'char', 'integer1']) self.assertSequenceEqual(r[1], (20, u'abc', 10)) self.assertSequenceEqual(r[:1], [(20, u'abc', 10)])
def test_filter_queryset(self): instances = G(SimpleDemoModel, n=5, char='abc') self.assertEquals(len(instances), 5) ds = Datasource.as_datasource(queryset=SimpleDemoModel.objects.all(), columns=['id', 'char']) ds.add_filters(id__gt=instances[2].pk) self.assertSequenceEqual(ds, [(instances[3].pk, u'abc'), (instances[4].pk, u'abc')])
def test_datasource_std_list_display(self): G(SimpleDemoModel, n=2, char='abc', integer1=10, integer2=20) ds = Datasource.as_datasource(model=SimpleDemoModel, columns=['integer2', 'char', 'integer1']) r = BaseReport.as_report(datasource=ds) self.assertSequenceEqual([c.name for c in ds.columns], r.display_order()) self.assertSequenceEqual(r[1], (20, u'abc', 10))
def test_create_from_model(self): instances = G(SimpleDemoModel, n=2, char='1', integer1=1, integer2=2, boolean=True) self.assertEquals(len(instances), 2) ds = Datasource.as_datasource(model=SimpleDemoModel) self.assertSequenceEqual([c.name for c in ds.columns], ['id', 'char', 'integer1', 'integer2', 'boolean']) self.assertSequenceEqual([c.title for c in ds.columns], ['ID', 'Character', 'Integer #1', 'Integer #2', 'Boolean']) self.assertSequenceEqual(ds, [(instances[0].pk, u'1', 1, 2, True), (instances[1].pk, u'1', 1, 2, True)])
def test_create_from_queryset(self): instances = G(SimpleDemoModel, n=2, char='abc') self.assertEquals(len(instances), 2) ds = Datasource.as_datasource(queryset=SimpleDemoModel.objects.all(), columns=['id', 'char']) self.assertSequenceEqual([c.name for c in ds.columns], ['id', 'char']) self.assertSequenceEqual([c.title for c in ds.columns], ['ID', 'Character']) self.assertSequenceEqual(ds, [(instances[0].pk, u'abc'), (instances[1].pk, u'abc')])
def test_datasource(self): instances = G(SimpleDemoModel, n=2, char='abc', integer1=10, integer2=20, boolean=True) ds = Datasource.as_datasource(model=SimpleDemoModel) r = BaseReport.as_report(datasource=ds) self.assertSequenceEqual(r[1].values(), (instances[1].pk, u'abc', 10, 20, True))
def test_columns(self): G(SimpleDemoModel, n=2, char='abc', integer1=1) ds = Datasource.as_datasource( model=SimpleDemoModel, columns=[Column('char', 'AAA'), Column('integer1')]) self.assertSequenceEqual([c.name for c in ds.columns], ['char', 'integer1']) self.assertSequenceEqual([c.title for c in ds.columns], ['AAA', 'Integer #1']) self.assertSequenceEqual(ds, [(u'abc', 1), (u'abc', 1)])
def test_custom_column_callable(self): G(SimpleDemoModel, n=2, char='abc', integer1=10, integer2=20) def _custom_callable(obj, ds): return obj.integer1 + 100 ds = Datasource.as_datasource(model=SimpleDemoModel, columns=[Column('integer1'), Column('integer2'), ColumnCallable(_custom_callable)]) self.assertSequenceEqual(ds, [(10, 20, 110), (10, 20, 110)])
def test_optional_column_optional(self): G(DemoOptionalModel, n=2, name='abc', user=None) ds = Datasource.as_datasource( model=DemoOptionalModel, columns=[Column('name'), OptionalColumn('user.first_name')]) self.assertSequenceEqual([c.title for c in ds.columns], ['name', 'first name']) self.assertSequenceEqual(ds, [( 'abc', '', ), ('abc', '')])
def test_custom_column_callable(self): G(SimpleDemoModel, n=2, char='abc', integer1=10, integer2=20) def _custom_callable(obj, ds): return obj.integer1 + 100 ds = Datasource.as_datasource(model=SimpleDemoModel, columns=[ Column('integer1'), Column('integer2'), ColumnCallable(_custom_callable) ]) self.assertSequenceEqual(ds, [(10, 20, 110), (10, 20, 110)])
def test_custom_filter(self): G(SimpleDemoModel, n=10, char='abc') ds = Datasource.as_datasource(model=SimpleDemoModel, columns=[Column('filter_source')]) ds._get_queryset = mock.Mock(wraps=ds._get_queryset) ds._create_result_cache = mock.Mock(wraps=ds._create_result_cache) def filter_odd(row): if not row.filter_source.value % 2: raise RecordFilteredError ds.add_custom_filter(filter_odd) list(ds) list(ds) self.assertSequenceEqual(ds, [(1,), (3,), (5,), (7,), (9,)]) assert ds._get_queryset.call_count == 1 assert ds._create_result_cache.call_count == 1
def test_custom_filter(self): G(SimpleDemoModel, n=10, char='abc') ds = Datasource.as_datasource(model=SimpleDemoModel, columns=[Column('filter_source')]) ds._get_queryset = mock.Mock(wraps=ds._get_queryset) ds._create_result_cache = mock.Mock(wraps=ds._create_result_cache) def filter_odd(row): if not row.filter_source.value % 2: raise RecordFilteredError ds.add_custom_filter(filter_odd) list(ds) list(ds) self.assertSequenceEqual(ds, [(1, ), (3, ), (5, ), (7, ), (9, )]) assert ds._get_queryset.call_count == 1 assert ds._create_result_cache.call_count == 1
def test_format_raw_value(): c = DecimalColumn('salary.currency') r = RowValue(Decimal("1000"), column=c) assert format_raw_value(r) == "1,000.00" r = RowValue("1000", column=c) assert format_raw_value(r) == "1000" ds = Datasource.as_datasource(model=User, queryset=FakeQuerySet(model=User, items=[User(username='******'), User(username='******')])) TestReport = type('TestReport', (BaseReport,), {'model': User, 'datasource': ds}) report = TestReport.as_report() assert format_raw_value(report[0].username) == 'username1' assert format_raw_value(report[0]['username']) == 'username1'
def test_simple_group(self): itms = [User(username='******' % x, is_staff=bool(x % 2)) for x in range(6)] ds = Datasource.as_datasource(model=User, queryset=FakeQuerySet(model=User, items=itms)) TestReport = type('TestReport', (BaseReport,), {'model': User, 'list_display': ('is_staff', 'username'), 'group_by': ('is_staff', 'username'), 'datasource': ds}) report = TestReport.as_report() self.assertSequenceEqual(list(report), [(False, 'username0'), (False, 'username2'), (False, 'username4'), (True, 'username1'), (True, 'username3'), (True, 'username5')])
def test_create_from_model(self): instances = G(SimpleDemoModel, n=2, char='1', integer1=1, integer2=2, boolean=True) self.assertEquals(len(instances), 2) ds = Datasource.as_datasource(model=SimpleDemoModel) self.assertSequenceEqual( [c.name for c in ds.columns], ['id', 'char', 'integer1', 'integer2', 'boolean']) self.assertSequenceEqual( [c.title for c in ds.columns], ['ID', 'Character', 'Integer #1', 'Integer #2', 'Boolean']) self.assertSequenceEqual(ds, [(instances[0].pk, u'1', 1, 2, True), (instances[1].pk, u'1', 1, 2, True)])
def as_report(cls, **initkwargs): for key, value in initkwargs.items(): if not hasattr(cls, key) and not callable(value): raise TypeError(u"%s() received an invalid keyword %r" % (cls.__name__, key)) model = initkwargs.get('model', cls.model) datasource = initkwargs.get('datasource', cls.datasource) use_cache = initkwargs.get('use_cache', cls.use_cache) extras = initkwargs.get('extras', cls.extras) if datasource is not None: initkwargs['model'] = datasource.model initkwargs['datasource'] = datasource._clone(extras=extras) elif model is not None: initkwargs['datasource'] = Datasource.as_datasource(model=model, use_cache=use_cache) else: raise ImproperlyConfigured(u"%(cls)s is missing a datasource. Define " u"%(cls)s.model, %(cls)s.datasource" % {'cls': cls.__name__}) return cls(**initkwargs)
def test_col_to_css_class(): c = Column('user.first_name') r = RowValue('Test', column=c) assert col_to_css_class(r) == 'ereports_engine_columns_column' e = RowValueError('Test') assert col_to_css_class(e) == 'Test' with pytest.raises(ValueError): col_to_css_class('Wrong') ds = Datasource.as_datasource(model=User, queryset=FakeQuerySet(model=User, items=[User(username='******'), User(username='******')])) TestReport = type('TestReport', (BaseReport,), {'model': User, 'datasource': ds}) report = TestReport.as_report() assert col_to_css_class(report[0].username) == 'ereports_engine_columns_charcolumn' assert col_to_css_class(report[0]['username']) == 'ereports_engine_columns_charcolumn'
def test_callback_group(self): itms = [User(username='******' % x, is_staff=bool(x % 2)) for x in range(6)] ds = Datasource.as_datasource(model=User, queryset=FakeQuerySet(model=User, items=itms)) def is_staff(rec): return str(rec.is_staff) TestReport = type('TestReport', (BaseReport,), {'model': User, 'list_display': ('is_staff', 'username'), 'group_by': (is_staff, 'username'), 'datasource': ds}) report = TestReport.as_report() self.assertItemsEqual(dict(report.get_groups()).keys(), ['True', 'False']) self.assertSequenceEqual(list(report), [(False, 'username0'), (False, 'username2'), (False, 'username4'), (True, 'username1'), (True, 'username3'), (True, 'username5')])
def as_report(cls, **initkwargs): for key, value in initkwargs.items(): if not hasattr(cls, key) and not callable(value): raise TypeError(u"%s() received an invalid keyword %r" % (cls.__name__, key)) model = initkwargs.get('model', cls.model) datasource = initkwargs.get('datasource', cls.datasource) use_cache = initkwargs.get('use_cache', cls.use_cache) extras = initkwargs.get('extras', cls.extras) if datasource is not None: initkwargs['model'] = datasource.model initkwargs['datasource'] = datasource._clone(extras=extras) elif model is not None: initkwargs['datasource'] = Datasource.as_datasource( model=model, use_cache=use_cache) else: raise ImproperlyConfigured( u"%(cls)s is missing a datasource. Define " u"%(cls)s.model, %(cls)s.datasource" % {'cls': cls.__name__}) return cls(**initkwargs)
def test_format_raw_value(): c = DecimalColumn('salary.currency') r = RowValue(Decimal("1000"), column=c) assert format_raw_value(r) == "1,000.00" r = RowValue("1000", column=c) assert format_raw_value(r) == "1000" ds = Datasource.as_datasource( model=User, queryset=FakeQuerySet( model=User, items=[User(username='******'), User(username='******')])) TestReport = type('TestReport', (BaseReport, ), { 'model': User, 'datasource': ds }) report = TestReport.as_report() assert format_raw_value(report[0].username) == 'username1' assert format_raw_value(report[0]['username']) == 'username1'
def test_simple_group(self): itms = [ User(username='******' % x, is_staff=bool(x % 2)) for x in range(6) ] ds = Datasource.as_datasource(model=User, queryset=FakeQuerySet(model=User, items=itms)) TestReport = type( 'TestReport', (BaseReport, ), { 'model': User, 'list_display': ('is_staff', 'username'), 'group_by': ('is_staff', 'username'), 'datasource': ds }) report = TestReport.as_report() self.assertSequenceEqual(list(report), [(False, 'username0'), (False, 'username2'), (False, 'username4'), (True, 'username1'), (True, 'username3'), (True, 'username5')])
def test_manipulator(self): G(SimpleDemoModel, n=2, char='abc', integer1=1) ds = Datasource.as_datasource( model=SimpleDemoModel, columns=[Column('char', manipulator=lambda v: v.upper())]) self.assertSequenceEqual(ds, [(u'ABC', ), (u'ABC', )])
def test_manipulator(self): G(SimpleDemoModel, n=2, char='abc', integer1=1) ds = Datasource.as_datasource(model=SimpleDemoModel, columns=[Column('char', manipulator=lambda v: v.upper())]) self.assertSequenceEqual(ds, [(u'ABC', ), (u'ABC', )])
def test_get_col_by_name(self): G(SimpleDemoModel, n=2, char='abc') ds = Datasource.as_datasource(queryset=SimpleDemoModel.objects.all()) self.assertSequenceEqual([u'abc', u'abc'], [row.char for row in ds]) self.assertSequenceEqual([u'abc', u'abc'], [row['char'] for row in ds])
def test_booleanyesno_column_yes(self): G(SimpleDemoModel, n=2, boolean=True) ds = Datasource.as_datasource(model=SimpleDemoModel, columns=[BooleanColumn('boolean')]) self.assertSequenceEqual([c.title for c in ds.columns], ['Boolean']) self.assertSequenceEqual(ds, [(True,), (True,)])
def test_optional_column_optional(self): G(DemoOptionalModel, n=2, name='abc', user=None) ds = Datasource.as_datasource(model=DemoOptionalModel, columns=[Column('name'), OptionalColumn('user.first_name')]) self.assertSequenceEqual([c.title for c in ds.columns], ['name', 'first name']) self.assertSequenceEqual(ds, [('abc', '',), ('abc', '')])
def test_booleanyesno_column_yes(self): G(SimpleDemoModel, n=2, boolean=True) ds = Datasource.as_datasource(model=SimpleDemoModel, columns=[BooleanColumn('boolean')]) self.assertSequenceEqual([c.title for c in ds.columns], ['Boolean']) self.assertSequenceEqual(ds, [(True, ), (True, )])
def test_cachemanager(self): ds = Datasource.as_datasource(model=SimpleDemoModel, use_cache=False) self.assertIsInstance(ds.cache_manager, DummyCacheManager) ds = Datasource.as_datasource(model=SimpleDemoModel, use_cache=True) self.assertIsInstance(ds.cache_manager, DatasourceCacheManager)
def test_custom_column(self): G(SimpleDemoModel, n=2, char='abc', integer1=1, integer2=3) ds = Datasource.as_datasource(model=SimpleDemoModel, columns=[Column('integer1'), Column('integer2'), CalcColumn(['integer1', 'integer2'])]) self.assertSequenceEqual(ds, [(1, 3, 4), (1, 3, 4)])