def sortable_backwards_compatibility(): # Table.Meta.sortable (not set) class SimpleTable(tables.Table): name = tables.Column() table = SimpleTable([]) with warns(DeprecationWarning): assert table.columns['name'].sortable is True # Table.Meta.sortable = False with warns(DeprecationWarning): class SimpleTable(tables.Table): name = tables.Column() class Meta: sortable = False table = SimpleTable([]) with warns(DeprecationWarning): assert table.columns['name'].sortable is False # backwards compatible assert table.columns['name'].orderable is False # Table.Meta.sortable = True with warns(DeprecationWarning): class SimpleTable(tables.Table): name = tables.Column() class Meta: sortable = True table = SimpleTable([]) with warns(DeprecationWarning): assert table.columns['name'].sortable is True # backwards compatible assert table.columns['name'].orderable is True
def orderable(): # Table.Meta.orderable = False class SimpleTable(tables.Table): name = tables.Column() table = SimpleTable([]) assert table.columns["name"].orderable is True # Table.Meta.orderable = False class SimpleTable(tables.Table): name = tables.Column() class Meta: orderable = False table = SimpleTable([]) assert table.columns["name"].orderable is False with warns(DeprecationWarning): assert table.columns["name"].sortable is False # backwards compatible # Table.Meta.orderable = True class SimpleTable(tables.Table): name = tables.Column() class Meta: orderable = True table = SimpleTable([]) with warns(DeprecationWarning): assert table.columns["name"].sortable is True # backwards compatible assert table.columns["name"].orderable is True
def warns(): with attest.warns(UserWarning) as captured: warnings.warn("foo") warnings.warn("bar", DeprecationWarning) assert len(captured) == 1 assert unicode(captured[0]) == "foo" with attest.raises(AssertionError): with attest.warns(UserWarning): pass with attest.raises(AssertionError): with attest.warns(UserWarning, DeprecationWarning): warnings.warn("foo") with attest.warns(UserWarning, DeprecationWarning, any=True): warnings.warn("foo") if hasattr(warnings, "catch_warnings"): # not available in Python 2.5 with warnings.catch_warnings(): warnings.simplefilter("error", UserWarning) with attest.warns(UserWarning): warnings.warn("foo") with attest.raises(UserWarning): warnings.warn("bar")
def orderable(): # Table.Meta.orderable = False class SimpleTable(tables.Table): name = tables.Column() table = SimpleTable([]) assert table.columns['name'].orderable is True # Table.Meta.orderable = False class SimpleTable(tables.Table): name = tables.Column() class Meta: orderable = False table = SimpleTable([]) assert table.columns['name'].orderable is False with warns(DeprecationWarning): assert table.columns['name'].sortable is False # backwards compatible # Table.Meta.orderable = True class SimpleTable(tables.Table): name = tables.Column() class Meta: orderable = True table = SimpleTable([]) with warns(DeprecationWarning): assert table.columns['name'].sortable is True # backwards compatible assert table.columns['name'].orderable is True
def new_attrs_should_be_supported(): with warns(DeprecationWarning): class TestTable(tables.Table): col1 = tables.CheckBoxColumn( attrs=Attrs(th__input={"th_key": "th_value"}, td__input={"td_key": "td_value"})) col2 = tables.CheckBoxColumn(attrs=Attrs(input={"key": "value"})) table = TestTable([{"col1": "data", "col2": "data"}]) assert attrs(table.columns["col1"].header) == { "type": "checkbox", "th_key": "th_value" } assert attrs(table.rows[0]["col1"]) == { "type": "checkbox", "td_key": "td_value", "value": "data", "name": "col1" } assert attrs(table.columns["col2"].header) == { "type": "checkbox", "key": "value" } assert attrs(table.rows[0]["col2"]) == { "type": "checkbox", "key": "value", "value": "data", "name": "col2" }
def column_is_checked(): with warns(DeprecationWarning): class TestTable(tables.Table): col = tables.CheckBoxColumn(attrs={"name": "col"}, checked='is_selected') table = TestTable([{ 'col': '1', 'is_selected': True }, { 'col': '2', 'is_selected': False }]) assert attrs(table.rows[0]["col"]) == { "type": "checkbox", "value": "1", "name": "col", "checked": "checked" } assert attrs(table.rows[1]["col"]) == { "type": "checkbox", "value": "2", "name": "col" }
def old_style_attrs_should_still_work(): with warns(DeprecationWarning): class TestTable(tables.Table): col = tables.LinkColumn("occupation", kwargs={"pk": A("col")}, attrs={"title": "Occupation Title"}) table = TestTable([{"col": 0}]) assert attrs(table.rows[0]["col"]) == {"href": reverse("occupation", kwargs={"pk": 0}), "title": "Occupation Title"}
def attrs_should_be_translated_for_backwards_compatibility(): with warns(DeprecationWarning): class TestTable(tables.Table): col = tables.CheckBoxColumn(header_attrs={"th_key": "th_value"}, attrs={"td_key": "td_value"}) table = TestTable([{"col": "data"}]) assert attrs(table.columns["col"].header) == {"type": "checkbox", "th_key": "th_value"} assert attrs(table.rows[0]["col"]) == {"type": "checkbox", "td_key": "td_value", "value": "data", "name": "col"}
def new_attrs_should_be_supported(): with warns(DeprecationWarning): class TestTable(tables.Table): col1 = tables.CheckBoxColumn(attrs=Attrs(th__input={"th_key": "th_value"}, td__input={"td_key": "td_value"})) col2 = tables.CheckBoxColumn(attrs=Attrs(input={"key": "value"})) table = TestTable([{"col1": "data", "col2": "data"}]) assert attrs(table.columns["col1"].header) == {"type": "checkbox", "th_key": "th_value"} assert attrs(table.rows[0]["col1"]) == {"type": "checkbox", "td_key": "td_value", "value": "data", "name": "col1"} assert attrs(table.columns["col2"].header) == {"type": "checkbox", "key": "value"} assert attrs(table.rows[0]["col2"]) == {"type": "checkbox", "key": "value", "value": "data", "name": "col2"}
def old_style_attrs_should_still_work(): with warns(DeprecationWarning): class TestTable(tables.Table): col = tables.LinkColumn('occupation', kwargs={"pk": A('col')}, attrs={"title": "Occupation Title"}) table = TestTable([{"col": 0}]) assert attrs(table.rows[0]["col"]) == { "href": reverse("occupation", kwargs={"pk": 0}), "title": "Occupation Title" }
def attrs_should_be_translated_for_backwards_compatibility(): with warns(DeprecationWarning): class TestTable(tables.Table): col = tables.CheckBoxColumn(header_attrs={"th_key": "th_value"}, attrs={"td_key": "td_value"}) table = TestTable([{"col": "data"}]) assert attrs(table.columns["col"].header) == { "type": "checkbox", "th_key": "th_value" } assert attrs(table.rows[0]["col"]) == { "type": "checkbox", "td_key": "td_value", "value": "data", "name": "col" }
def column_is_checked_callback(): with warns(DeprecationWarning): def is_selected(value, record): if value == '1': return True return False class TestTable(tables.Table): col = tables.CheckBoxColumn(attrs={"name": "col"}, checked=is_selected) table = TestTable([{'col': '1'}, {'col': '2'}]) assert attrs(table.rows[0]["col"]) == { "type": "checkbox", "value": "1", "name": "col", "checked": "checked" } assert attrs(table.rows[1]["col"]) == { "type": "checkbox", "value": "2", "name": "col" }
def ordering(): # fallback to Table.Meta assert ('alpha', ) == OrderedTable( [], order_by=None).order_by == OrderedTable([]).order_by # values of order_by are wrapped in tuples before being returned assert OrderedTable([], order_by='alpha').order_by == ('alpha', ) assert OrderedTable([], order_by=('beta', )).order_by == ('beta', ) table = OrderedTable([]) table.order_by = [] assert () == table.order_by == OrderedTable([], order_by=[]).order_by table = OrderedTable([]) table.order_by = () assert () == table.order_by == OrderedTable([], order_by=()).order_by table = OrderedTable([]) table.order_by = '' assert () == table.order_by == OrderedTable([], order_by='').order_by # apply an ordering table = UnorderedTable([]) table.order_by = 'alpha' assert ('alpha', ) == UnorderedTable( [], order_by='alpha').order_by == table.order_by table = OrderedTable([]) table.order_by = 'alpha' assert ('alpha', ) == OrderedTable( [], order_by='alpha').order_by == table.order_by # let's check the data table = OrderedTable(MEMORY_DATA, order_by='beta') assert 3 == table.rows[0]['i'] table = OrderedTable(MEMORY_DATA, order_by='-beta') assert 1 == table.rows[0]['i'] # allow fallback to Table.Meta.order_by table = OrderedTable(MEMORY_DATA) assert 1 == table.rows[0]['i'] # column's can't be ordered if they're not allowed to be class TestTable2(tables.Table): a = tables.Column(orderable=False) b = tables.Column() table = TestTable2([], order_by='a') assert table.order_by == () table = TestTable2([], order_by='b') assert table.order_by == ('b', ) # ordering disabled by default class TestTable3(tables.Table): a = tables.Column(orderable=True) b = tables.Column() class Meta: orderable = False table = TestTable3([], order_by='a') assert table.order_by == ('a', ) table = TestTable3([], order_by='b') assert table.order_by == () table = TestTable3([], orderable=True, order_by='b') assert table.order_by == ('b', ) with warns(DeprecationWarning) as captured: tables.Column(sortable=True) tables.Column(sortable=False) class TestTable4(tables.Table): class Meta: sortable = True class TestTable4(tables.Table): class Meta: sortable = False assert len(captured) == 4
def ordering(): # fallback to Table.Meta assert ('alpha', ) == OrderedTable([], order_by=None).order_by == OrderedTable([]).order_by # values of order_by are wrapped in tuples before being returned assert OrderedTable([], order_by='alpha').order_by == ('alpha', ) assert OrderedTable([], order_by=('beta',)).order_by == ('beta', ) table = OrderedTable([]) table.order_by = [] assert () == table.order_by == OrderedTable([], order_by=[]).order_by table = OrderedTable([]) table.order_by = () assert () == table.order_by == OrderedTable([], order_by=()).order_by table = OrderedTable([]) table.order_by = '' assert () == table.order_by == OrderedTable([], order_by='').order_by # apply an ordering table = UnorderedTable([]) table.order_by = 'alpha' assert ('alpha', ) == UnorderedTable([], order_by='alpha').order_by == table.order_by table = OrderedTable([]) table.order_by = 'alpha' assert ('alpha', ) == OrderedTable([], order_by='alpha').order_by == table.order_by # let's check the data table = OrderedTable(MEMORY_DATA, order_by='beta') assert 3 == table.rows[0]['i'] table = OrderedTable(MEMORY_DATA, order_by='-beta') assert 1 == table.rows[0]['i'] # allow fallback to Table.Meta.order_by table = OrderedTable(MEMORY_DATA) assert 1 == table.rows[0]['i'] # column's can't be ordered if they're not allowed to be class TestTable2(tables.Table): a = tables.Column(orderable=False) b = tables.Column() table = TestTable2([], order_by='a') assert table.order_by == () table = TestTable2([], order_by='b') assert table.order_by == ('b', ) # ordering disabled by default class TestTable3(tables.Table): a = tables.Column(orderable=True) b = tables.Column() class Meta: orderable = False table = TestTable3([], order_by='a') assert table.order_by == ('a', ) table = TestTable3([], order_by='b') assert table.order_by == () table = TestTable3([], orderable=True, order_by='b') assert table.order_by == ('b', ) with warns(DeprecationWarning) as captured: tables.Column(sortable=True) tables.Column(sortable=False) class TestTable4(tables.Table): class Meta: sortable = True class TestTable4(tables.Table): class Meta: sortable = False assert len(captured) == 4