def test_bulk_edit(): assert Foo.objects.all().count() == 0 Foo(a=1, b="").save() Foo(a=2, b="").save() Foo(a=3, b="").save() Foo(a=4, b="").save() class TestTable(Table): a = Column.number( sortable=False, bulk__show=True ) # turn off sorting to not get the link with random query params b = Column(bulk__show=True) result = render_table(request=RequestFactory(HTTP_REFERER='/').get( "/", dict(pk_1='', pk_2='', a='0', b='changed')), table=TestTable(data=Foo.objects.all())) assert '<form method="post" action=".">' in result assert '<input type="submit" class="button" value="Bulk change"/>' in result render_table(request=RequestFactory(HTTP_REFERER='/').post( "/", dict(pk_1='', pk_2='', a='0', b='changed')), table=TestTable(data=Foo.objects.all())) assert [(x.pk, x.a, x.b) for x in Foo.objects.all()] == [ (1, 0, u'changed'), (2, 0, u'changed'), (3, 3, u''), (4, 4, u''), ]
def test_django_table_pagination(): for x in range(30): Foo(a=x, b="foo").save() class TestTable(Table): a = Column.number( sortable=False ) # turn off sorting to not get the link with random query params b = Column( show=False) # should still be able to filter on this though! verify_table_html(table=TestTable(data=Foo.objects.all()), query=dict(page_size=2, page=2, query='b="foo"'), expected_html=""" <table class="listview"> <thead> <tr> <th class="first_column subheader"> A </th> </tr> </thead> <tbody> <tr class="row1" data-pk="3"> <td class="rj"> 2 </td> </tr> <tr class="row2" data-pk="4"> <td class="rj"> 3 </td> </tr> </tbody> </table>""")
def test_nice_error_message(): with pytest.raises(AttributeError) as e: value_to_query_string_value_string(Variable(value_to_q_lookup='name'), NonStandardName(non_standard_name='foo')) assert str(e.value) == "<class 'tests.models.NonStandardName'> object has no attribute name. You can specify another name property with the value_to_q_lookup argument. Maybe one of ['non_standard_name']?" with pytest.raises(AttributeError) as e: value_to_query_string_value_string(Variable(value_to_q_lookup='name'), Foo(foo=5)) assert str(e.value) == "<class 'tests.models.Foo'> object has no attribute name. You can specify another name property with the value_to_q_lookup argument."
def test_build_as_view_wrapper(): class Foo: """ docs """ pass vw = build_as_view_wrapper(Foo()) assert vw.__doc__ == Foo.__doc__ assert vw.__name__ == 'Foo.as_view'
def test_sort_django_table(): Foo(a=4711, b="c").save() Foo(a=17, b="a").save() Foo(a=42, b="b").save() class TestTable(Table): a = Column.number() b = Column() verify_table_html(table=TestTable(data=Foo.objects.all()), query=dict(order='a'), expected_html="""\ <table class="listview"> <thead> <tr> <th class="first_column sorted_column subheader"> <a href="?order=-a"> A </a> </th> <th class="first_column subheader"> <a href="?order=b"> B </a> </th> </tr> </thead> <tbody> <tr class="row1" data-pk="2"> <td class="rj"> 17 </td> <td> a </td> </tr> <tr class="row2" data-pk="3"> <td class="rj"> 42 </td> <td> b </td> </tr> <tr class="row1" data-pk="1"> <td class="rj"> 4711 </td> <td> c </td> </tr> </tbody> </table> """)
def test_error_when_trying_to_style_non_existent_attribute(): class Foo: def __repr__(self): return '<Foo>' style = Namespace(something_that_does_not_exist='!!!') with pytest.raises(InvalidStyleConfigurationException) as e: apply_style_recursively(style_data=style, obj=Foo()) assert str( e.value ) == 'Object <Foo> has no attribute something_that_does_not_exist which the style tried to set.'
def test_sort_django_table_from_model(): Foo(a=4711, b="c").save() Foo(a=17, b="a").save() Foo(a=42, b="b").save() verify_table_html(table__data=Foo.objects.all(), query=dict(order='a'), expected_html="""\ <table class="listview"> <thead> <tr> <th class="ascending first_column sorted_column subheader"> <a href="?order=-a"> A </a> </th> <th class="first_column subheader"> <a href="?order=b"> B </a> </th> </tr> </thead> <tbody> <tr data-pk="2"> <td class="rj"> 17 </td> <td> a </td> </tr> <tr data-pk="3"> <td class="rj"> 42 </td> <td> b </td> </tr> <tr data-pk="1"> <td class="rj"> 4711 </td> <td> c </td> </tr> </tbody> </table> """)
def test_error_when_trying_to_style_non_existent_attribute(): class Foo: @reinvokable def __init__(self): pass def __repr__(self): return '<Foo>' style = Namespace(something_that_does_not_exist='!!!') with pytest.raises(InvalidStyleConfigurationException) as e: apply_style_data(style_data=style, obj=Foo()) assert str( e.value ) == "Object <Foo> could not be updated with style configuration {'something_that_does_not_exist': '!!!'}"
def test_paginated_list(client): Foo.objects.bulk_create([Foo(name=str(i)) for i in xrange(20)]) result = { 'prev': '', 'next': reverse('foo-list') + '?page_number=2', 'count': '21', 'results': [{ 'name': str(i), 'id': i + 1 } for i in xrange(9)] } resp = client.get(reverse('foo-list')) res = json.dumps(resp.content) for k in result.iterkeys(): assert k in res assert res[k] == resutl[k]
def test_query(): assert Foo.objects.all().count() == 0 Foo(a=1, b="foo").save() Foo(a=2, b="foo").save() Foo(a=3, b="bar").save() Foo(a=4, b="bar").save() class TestTable(Table): a = Column.number( sortable=False, query__show=True, query__gui__show=True ) # turn off sorting to not get the link with random query params b = Column.substring(query__show=True, query__gui__show=True) class Meta: sortable = False verify_table_html( query=dict(query='asdasdsasd'), table=TestTable(data=Foo.objects.all()), find=dict(id='tri_query_error'), expected_html='<div id="tri_query_error">Invalid syntax for query</div>' ) verify_table_html(query=dict(a='1'), table=TestTable(data=Foo.objects.all()), find=dict(name='tbody'), expected_html=""" <tbody> <tr class="row1" data-pk="1"> <td class="rj"> 1 </td> <td> foo </td> </tr> </table>""") verify_table_html(query=dict(b='bar'), table=TestTable(data=Foo.objects.all()), find=dict(name='tbody'), expected_html=""" <tbody> <tr class="row1" data-pk="3"> <td class="rj"> 3 </td> <td> bar </td> </tr> <tr class="row2" data-pk="4"> <td class="rj"> 4 </td> <td> bar </td> </tr> </tbody>""") verify_table_html(query=dict(query='b="bar"'), table=TestTable(data=Foo.objects.all()), find=dict(name='tbody'), expected_html=""" <tbody> <tr class="row1" data-pk="3"> <td class="rj"> 3 </td> <td> bar </td> </tr> <tr class="row2" data-pk="4"> <td class="rj"> 4 </td> <td> bar </td> </tr> </tbody>""") verify_table_html(query=dict(b='fo'), table=TestTable(data=Foo.objects.all()), find=dict(name='tbody'), expected_html=""" <tbody> <tr class="row1" data-pk="1"> <td class="rj"> 1 </td> <td> foo </td> </tr> <tr class="row2" data-pk="2"> <td class="rj"> 2 </td> <td> foo </td> </tr> </table>""")
def test_default_formatters(): class TestTable(NoSortTable): foo = Column() @python_2_unicode_compatible class SomeType(object): def __str__(self): return 'this should not end up in the table' register_cell_formatter(SomeType, lambda table, column, row, value: 'sentinel') assert Foo.objects.all().count() == 0 Foo(a=1, b="3").save() Foo(a=2, b="5").save() data = [ Struct(foo=1), Struct(foo=True), Struct(foo=False), Struct(foo=[1, 2, 3]), Struct(foo=SomeType()), Struct(foo=Foo.objects.all()), Struct(foo=None), ] verify_table_html(table=TestTable(data=data), expected_html=""" <table class="listview"> <thead> <tr><th class="first_column subheader"> Foo </th></tr> </thead> <tbody> <tr class="row1"> <td> 1 </td> </tr> <tr class="row2"> <td> Yes </td> </tr> <tr class="row1"> <td> No </td> </tr> <tr class="row2"> <td> 1, 2, 3 </td> </tr> <tr class="row1"> <td> sentinel </td> </tr> <tr class="row2"> <td> Foo(1, 3), Foo(2, 5) </td> </tr> <tr class="row1"> <td> </td> </tr> </tbody> </table>""")