From bd25781813eb39f0379845c0c0031b7b10ac2a49 Mon Sep 17 00:00:00 2001 From: Francois Blackburn Date: Fri, 13 Mar 2026 14:41:37 -0400 Subject: [PATCH] mallow: add sort_insensitive_columns to ListSchema why: this define which columns should be ordered case insensitively or not. --- xivo/mallow_helpers.py | 10 ++++++++- xivo/tests/test_mallow_helpers.py | 34 ++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/xivo/mallow_helpers.py b/xivo/mallow_helpers.py index 371e2e3..cda0671 100644 --- a/xivo/mallow_helpers.py +++ b/xivo/mallow_helpers.py @@ -1,4 +1,4 @@ -# Copyright 2016-2025 The Wazo Authors (see the AUTHORS file) +# Copyright 2016-2026 The Wazo Authors (see the AUTHORS file) # SPDX-License-Identifier: GPL-3.0-or-later from __future__ import annotations @@ -41,6 +41,7 @@ class ListSchema(marshmallow.Schema): sort_columns: list[str] = [] searchable_columns: list[str] = [] default_direction: Literal['asc', 'desc'] = 'asc' + sort_insensitive_columns: list[str] = [] direction = fields.String(validate=validate.OneOf(['asc', 'desc'])) order = fields.String() @@ -78,6 +79,13 @@ def add_searchable_fields( return data + @marshmallow.post_load() + def add_order_insensitive_field( + self, data: dict[str, Any], **kwargs: Any + ) -> dict[str, Any]: + data['order_insensitive'] = data['order'] in self.sort_insensitive_columns + return data + class Schema(marshmallow.Schema): class Meta: diff --git a/xivo/tests/test_mallow_helpers.py b/xivo/tests/test_mallow_helpers.py index 384175f..6f6e3f1 100644 --- a/xivo/tests/test_mallow_helpers.py +++ b/xivo/tests/test_mallow_helpers.py @@ -1,4 +1,4 @@ -# Copyright 2018-2023 The Wazo Authors (see the AUTHORS file) +# Copyright 2018-2026 The Wazo Authors (see the AUTHORS file) # SPDX-License-Identifier: GPL-3.0-or-later import unittest @@ -99,3 +99,35 @@ def test_default_values(self): result, has_entries(direction='asc', order=None, limit=None, offset=0, search=None), ) + + def test_order_insensitive_default_is_false(self): + result = ListSchema().load({}) + + assert_that(result, has_entries(order_insensitive=False)) + + def test_order_insensitive_when_order_in_sort_insensitive_columns(self): + class Schema(ListSchema): + sort_columns = ['name', 'email'] + sort_insensitive_columns = ['name'] + + result = Schema().load({'order': 'name'}) + + assert_that(result, has_entries(order_insensitive=True)) + + def test_order_insensitive_when_order_not_in_sort_insensitive_columns(self): + class Schema(ListSchema): + sort_columns = ['name', 'email'] + sort_insensitive_columns = ['name'] + + result = Schema().load({'order': 'email'}) + + assert_that(result, has_entries(order_insensitive=False)) + + def test_order_insensitive_when_sort_insensitive_columns_is_empty(self): + class Schema(ListSchema): + sort_columns = ['name'] + sort_insensitive_columns = [] + + result = Schema().load({'order': 'name'}) + + assert_that(result, has_entries(order_insensitive=False))