def test_orderby(): resource_cls = mock.Mock(**{ 'Meta.order_fields': {'one', 'two'} }) rs = ResultSet(resource_cls=resource_cls) one_forwards = rs.order_by('one')._build_query() assert one_forwards['sort_by'] == 'one' assert one_forwards['sort_direction'] == 'asc' one_backwards = rs.order_by('-one')._build_query() assert one_backwards['sort_by'] == 'one' assert one_backwards['sort_direction'] == 'desc' two_forwards = rs.order_by('two')._build_query() assert two_forwards['sort_by'] == 'two' assert two_forwards['sort_direction'] == 'asc' two_backwards = rs.order_by('-two')._build_query() assert two_backwards['sort_by'] == 'two' assert two_backwards['sort_direction'] == 'desc' with assert_raises(ValueError): rs.order_by('invalid') with assert_raises(ValueError): rs.order_by('-invalid')
def test_immutable(): rs = ResultSet( resource_cls=mock.Mock(**{'Meta.order_fields': {'foo'}}) ) rs_filtered = rs.filter(foo='bar') rs_ordered = rs.order_by('foo') assert rs is not rs_filtered assert rs is not rs_ordered
def test_orderby(): resource_cls = mock.Mock(**{'Meta.order_fields': {'one', 'two'}}) rs = ResultSet(resource_cls=resource_cls) one_forwards = rs.order_by('one')._build_query() assert one_forwards['sort_by'] == 'one' assert one_forwards['sort_direction'] == 'asc' one_backwards = rs.order_by('-one')._build_query() assert one_backwards['sort_by'] == 'one' assert one_backwards['sort_direction'] == 'desc' two_forwards = rs.order_by('two')._build_query() assert two_forwards['sort_by'] == 'two' assert two_forwards['sort_direction'] == 'asc' two_backwards = rs.order_by('-two')._build_query() assert two_backwards['sort_by'] == 'two' assert two_backwards['sort_direction'] == 'desc' with assert_raises(ValueError): rs.order_by('invalid') with assert_raises(ValueError): rs.order_by('-invalid')
def test_immutable(): rs = ResultSet(resource_cls=mock.Mock(**{'Meta.order_fields': {'foo'}})) rs_filtered = rs.filter(foo='bar') rs_ordered = rs.order_by('foo') assert rs is not rs_filtered assert rs is not rs_ordered