mirror of
https://github.com/hanzoai/orm.git
synced 2026-10-30 15:01:22 +00:00
Typed[T].Count swapped the projection for COUNT(*) and kept every other
clause. SQLite accepted the ORDER BY left beside the aggregate; PostgreSQL
refuses it ("must appear in the GROUP BY clause"), so every ordered Count
failed on hanzo-sql alone. A grouped query answered one group's count, and
LIMIT and DISTINCT were ignored on both. Count now runs the query itself as
a subquery of SELECT COUNT(*), with the order dropped.
It was found by running the relational tests on PostgreSQL, which they now
do on every run: internal/pgtest starts a throwaway server from the
binaries on the machine, on a Unix socket in a temporary directory, and a
machine without one says so by name.
replicated/go.mod was untidy against the root module, which failed its
build gate on main.
Co-authored-by: Hanzo Dev <[email protected]>
65 lines
1.5 KiB
Go
65 lines
1.5 KiB
Go
package orm_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/hanzoai/orm"
|
|
"github.com/hanzoai/orm/query"
|
|
)
|
|
|
|
type userSummary struct {
|
|
ID string
|
|
Label string
|
|
}
|
|
|
|
func TestTypedMapGenericMethod(t *testing.T) {
|
|
relational(t, func(t *testing.T, db *query.DB) {
|
|
ctx := context.Background()
|
|
|
|
typed := orm.Select[typedUser](db, "users").OrderBy("id ASC")
|
|
|
|
// Call Map with a closure transforming typedUser to userSummary.
|
|
// Go 1.27 infers U = userSummary automatically from closure return type.
|
|
summaries, err := typed.Map(ctx, func(u typedUser) userSummary {
|
|
return userSummary{
|
|
ID: u.ID,
|
|
Label: fmt.Sprintf("%s (%s)", u.ID, u.Email),
|
|
}
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("Map failed: %v", err)
|
|
}
|
|
|
|
if len(summaries) != 3 {
|
|
t.Fatalf("expected 3 summaries, got %d", len(summaries))
|
|
}
|
|
if summaries[0].Label != "u1 ([email protected])" {
|
|
t.Errorf("unexpected summary[0]: %+v", summaries[0])
|
|
}
|
|
if summaries[1].Label != "u2 ([email protected])" {
|
|
t.Errorf("unexpected summary[1]: %+v", summaries[1])
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestTypedOneMapGenericMethod(t *testing.T) {
|
|
relational(t, func(t *testing.T, db *query.DB) {
|
|
ctx := context.Background()
|
|
|
|
typed := orm.Select[typedUser](db, "users").OrderBy("id ASC")
|
|
|
|
// Call OneMap transforming single typedUser to string
|
|
email, err := typed.OneMap(ctx, func(u typedUser) string {
|
|
return u.Email
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("OneMap failed: %v", err)
|
|
}
|
|
if email == nil || *email != "[email protected]" {
|
|
t.Errorf("expected [email protected], got %v", email)
|
|
}
|
|
})
|
|
}
|