def test_get(self): obt = OrderByTuple(('a', 'b', 'c')) sentinel = object() assert obt.get('b', sentinel) is obt['b'] # keying assert obt.get('-', sentinel) is sentinel assert obt.get(0, sentinel) is obt['a'] # indexing assert obt.get(3, sentinel) is sentinel
def test_get(self): obt = OrderByTuple(("a", "b", "c")) sentinel = object() assert obt.get("b", sentinel) is obt["b"] # keying assert obt.get("-", sentinel) is sentinel assert obt.get(0, sentinel) is obt["a"] # indexing assert obt.get(3, sentinel) is sentinel
def __init__(self, verbose_name=None, accessor=None, default=None, visible=True, orderable=None, attrs=None, order_by=None, empty_values=None, localize=None, footer=None): if not (accessor is None or isinstance(accessor, six.string_types) or callable(accessor)): raise TypeError('accessor must be a string or callable, not %s' % type(accessor).__name__) if callable(accessor) and default is not None: raise TypeError('accessor must be string when default is used, not callable') self.accessor = Accessor(accessor) if accessor else None self._default = default self.verbose_name = verbose_name self.visible = visible self.orderable = orderable self.attrs = attrs or {} # massage order_by into an OrderByTuple or None order_by = (order_by, ) if isinstance(order_by, six.string_types) else order_by self.order_by = OrderByTuple(order_by) if order_by is not None else None if empty_values is not None: self.empty_values = empty_values self.localize = localize self.creation_counter = Column.creation_counter Column.creation_counter += 1 self._footer = footer
def __init__(self, verbose_name=None, accessor=None, default=None, visible=True, orderable=None, attrs=None, order_by=None, sortable=None, empty_values=None, localize=None): if not (accessor is None or isinstance(accessor, six.string_types) or callable(accessor)): raise TypeError('accessor must be a string or callable, not %s' % type(accessor).__name__) if callable(accessor) and default is not None: raise TypeError('accessor must be string when default is used, not callable') self.accessor = A(accessor) if accessor else None self._default = default self.verbose_name = verbose_name self.visible = visible if sortable is not None: warnings.warn('`sortable` is deprecated, use `orderable` instead.', DeprecationWarning) # if orderable hasn't been specified, we'll use sortable's value if orderable is None: orderable = sortable self.orderable = orderable self.attrs = attrs or {} # massage order_by into an OrderByTuple or None order_by = (order_by, ) if isinstance(order_by, six.string_types) else order_by self.order_by = OrderByTuple(order_by) if order_by is not None else None if empty_values is not None: self.empty_values = empty_values self.localize = localize self.creation_counter = Column.creation_counter Column.creation_counter += 1
def __init__( self, verbose_name=None, accessor=None, default=None, visible=True, orderable=None, attrs=None, order_by=None, empty_values=None, localize=None, footer=None, exclude_from_export=False, linkify=False, initial_sort_descending=False, ): if not (accessor is None or isinstance(accessor, six.string_types) or callable(accessor)): raise TypeError("accessor must be a string or callable, not %s" % type(accessor).__name__) if callable(accessor) and default is not None: raise TypeError( "accessor must be string when default is used, not callable") self.accessor = Accessor(accessor) if accessor else None self._default = default self.verbose_name = verbose_name self.visible = visible self.orderable = orderable self.attrs = attrs or getattr(self, "attrs", {}) # massage order_by into an OrderByTuple or None order_by = (order_by, ) if isinstance(order_by, six.string_types) else order_by self.order_by = OrderByTuple( order_by) if order_by is not None else None if empty_values is not None: self.empty_values = empty_values self.localize = localize self._footer = footer self.exclude_from_export = exclude_from_export link_kwargs = None if callable(linkify) or hasattr(self, "get_url"): link_kwargs = dict( url=linkify if callable(linkify) else self.get_url) elif isinstance(linkify, (dict, tuple)): link_kwargs = dict(reverse_args=linkify) elif linkify is True: link_kwargs = dict(accessor=self.accessor) if link_kwargs is not None: self.link = LinkTransform(attrs=self.attrs.get("a", {}), **link_kwargs) self.initial_sort_descending = initial_sort_descending self.creation_counter = Column.creation_counter Column.creation_counter += 1
def test_intexing(self): obt = OrderByTuple(('a', 'b', 'c')) assert obt[0] == OrderBy('a') assert obt['b'] == OrderBy('b') with self.assertRaises(KeyError): obt['d'] with self.assertRaises(TypeError): obt[('tuple', )]
def test_intexing(self): obt = OrderByTuple(("a", "b", "c")) assert obt[0] == OrderBy("a") assert obt["b"] == OrderBy("b") with self.assertRaises(KeyError): obt["d"] with self.assertRaises(TypeError): obt[("tuple", )]
def orderbytuple(): obt = OrderByTuple('abc') Assert(obt) == (OrderBy('a'), OrderBy('b'), OrderBy('c')) Assert(obt[0]) == OrderBy('a') Assert(obt['b']) == OrderBy('b') with Assert.raises(IndexError) as error: obt['d'] with Assert.raises(TypeError) as error: obt[('tuple', )]
def test_orderbytuple_sort_key_multiple(): obt = OrderByTuple(('a', '-b')) items = [ {"a": 1, "b": 2}, {"a": 1, "b": 3}, ] assert sorted(items, key=obt.key) == [ {"a": 1, "b": 3}, {"a": 1, "b": 2}, ]
def test_sort_key_multiple(self): obt = OrderByTuple(('a', '-b')) items = [ {'a': 1, 'b': 2}, {'a': 1, 'b': 3}, ] assert sorted(items, key=obt.key) == [ {'a': 1, 'b': 3}, {'a': 1, 'b': 2}, ]
def test_sort_key_multiple(self): obt = OrderByTuple(("a", "-b")) items = [{"a": 1, "b": 2}, {"a": 1, "b": 3}] assert sorted(items, key=obt.key) == [{ "a": 1, "b": 3 }, { "a": 1, "b": 2 }]
def order_by(self): """ Returns an `.OrderByTuple` of appropriately prefixed data source keys used to sort this column. See `.order_by_alias` for details. """ if self.column.order_by is not None: order_by = self.column.order_by else: # default to using column accessor as data source sort key order_by = OrderByTuple((self.accessor, )) return order_by.opposite if self.order_by_alias.is_descending else order_by
def orderbytuple(): obt = OrderByTuple(('a', 'b', 'c')) assert obt == (OrderBy('a'), OrderBy('b'), OrderBy('c')) # indexing assert obt[0] == OrderBy('a') assert obt['b'] == OrderBy('b') with raises(KeyError): obt['d'] with raises(TypeError): obt[('tuple', )] # .get sentinel = object() assert obt.get('b', sentinel) is obt['b'] # keying assert obt.get('-', sentinel) is sentinel assert obt.get(0, sentinel) is obt['a'] # indexing assert obt.get(3, sentinel) is sentinel # .opposite assert OrderByTuple(('a', '-b', 'c')).opposite == ('-a', 'b', '-c') # in assert 'a' in obt and '-a' in obt
def test_orderbytuple(): obt = OrderByTuple(('a', 'b', 'c')) assert obt == (OrderBy('a'), OrderBy('b'), OrderBy('c')) # indexing assert obt[0] == OrderBy('a') assert obt['b'] == OrderBy('b') with pytest.raises(KeyError): obt['d'] with pytest.raises(TypeError): obt[('tuple', )] # .get sentinel = object() assert obt.get('b', sentinel) is obt['b'] # keying assert obt.get('-', sentinel) is sentinel assert obt.get(0, sentinel) is obt['a'] # indexing assert obt.get(3, sentinel) is sentinel # .opposite assert OrderByTuple(('a', '-b', 'c')).opposite == ('-a', 'b', '-c') # in assert 'a' in obt and '-a' in obt
def test_sort_key_empty_comes_first(self): obt = OrderByTuple(('a')) items = [ {'a': 1}, {'a': ''}, {'a': 2}, ] if six.PY3: assert sorted(items, key=obt.key) == [ {'a': ''}, {'a': 1}, {'a': 2}, ] else: assert sorted(items, key=obt.key) == [ {'a': 1}, {'a': 2}, {'a': ''}, ]
def test_orderbytuple_sort_key_empty_comes_first(): obt = OrderByTuple(('a')) items = [ {"a": 1}, {"a": ""}, {"a": 2}, ] if six.PY3: assert sorted(items, key=obt.key) == [ {"a": ""}, {"a": 1}, {"a": 2}, ] else: assert sorted(items, key=obt.key) == [ {"a": 1}, {"a": 2}, {"a": ""}, ]
def test_sort_key_empty_comes_first(self): obt = OrderByTuple(("a")) items = [{"a": 1}, {"a": ""}, {"a": 2}] if six.PY3: assert sorted(items, key=obt.key) == [{ "a": "" }, { "a": 1 }, { "a": 2 }] else: assert sorted(items, key=obt.key) == [{ "a": 1 }, { "a": 2 }, { "a": "" }]
def test_in(self): obt = OrderByTuple(('a', 'b', 'c')) assert 'a' in obt and '-a' in obt
def test_basic(self): obt = OrderByTuple(('a', 'b', 'c')) assert obt == (OrderBy('a'), OrderBy('b'), OrderBy('c'))
def test_in(self): obt = OrderByTuple(("a", "b", "c")) assert "a" in obt and "-a" in obt
def test_opposite(self): assert OrderByTuple(("a", "-b", "c")).opposite == ("-a", "b", "-c")
def test_opposite(self): assert OrderByTuple(('a', '-b', 'c')).opposite == ('-a', 'b', '-c')
def test_basic(self): obt = OrderByTuple(("a", "b", "c")) assert obt == (OrderBy("a"), OrderBy("b"), OrderBy("c"))