feat(handler): implement default sort configuration for models

* Add SetDefaultSort method to configure default sort order
* Implement getDefaultSort method to retrieve configured defaults
* Update handleRead to apply default sort when none specified
* Add tests for default sort functionality
This commit is contained in:
Hein
2026-07-29 10:05:24 +02:00
parent a70e3e02d0
commit 7c737afc5a
6 changed files with 203 additions and 0 deletions
+30
View File
@@ -41,6 +41,36 @@ func TestSetFallbackHandler(t *testing.T) {
}
}
func TestSetDefaultSort(t *testing.T) {
handler := NewHandler(nil, nil)
// No default configured yet
if got := handler.getDefaultSort("public", "users"); got != nil {
t.Errorf("Expected no default sort, got %v", got)
}
// Global default
global := []common.SortOption{{Column: "created_at", Direction: "desc"}}
handler.SetDefaultSort("", "", global...)
if got := handler.getDefaultSort("public", "users"); !reflect.DeepEqual(got, global) {
t.Errorf("Expected global default sort %v, got %v", global, got)
}
// Per-model default overrides the global default
perModel := []common.SortOption{{Column: "name", Direction: "asc"}}
handler.SetDefaultSort("public", "users", perModel...)
if got := handler.getDefaultSort("public", "users"); !reflect.DeepEqual(got, perModel) {
t.Errorf("Expected per-model default sort %v, got %v", perModel, got)
}
// Other models still fall back to the global default
if got := handler.getDefaultSort("public", "orders"); !reflect.DeepEqual(got, global) {
t.Errorf("Expected global default sort %v for unrelated model, got %v", global, got)
}
}
func TestGetDatabase(t *testing.T) {
handler := NewHandler(nil, nil)
db := handler.GetDatabase()