package appuser import ( "strings" "testing" ) func TestNormalizeListQuerySort(t *testing.T) { query := normalizeListQuery(listQuery{Page: 0, PageSize: 1000, SortBy: "coins", SortDirection: "asc"}) if query.Page != 1 || query.PageSize != 100 || query.SortBy != "coin" || query.SortDirection != "asc" { t.Fatalf("coin sort query mismatch: %+v", query) } query = normalizeListQuery(listQuery{SortBy: "createdAtMs", SortDirection: "unknown"}) if query.SortBy != "created_at" || query.SortDirection != "desc" { t.Fatalf("created sort default mismatch: %+v", query) } query = normalizeListQuery(listQuery{Country: " ph ", RegionID: -3, RegionIDSet: true, StartMs: -1, EndMs: -2}) if query.Country != "PH" || query.RegionIDSet || query.StartMs != 0 || query.EndMs != 0 { t.Fatalf("filter normalization mismatch: %+v", query) } } func TestAppUserListWhereSQLFilters(t *testing.T) { whereSQL, args := appUserListWhereSQL("lalu", normalizeListQuery(listQuery{ Country: "ph", Keyword: "hunter", RegionID: 3, RegionIDSet: true, StartMs: 1000, EndMs: 2000, Status: "active", })) for _, want := range []string{ "u.status = ?", "CAST(u.user_id AS CHAR) LIKE ?", "UPPER(u.country) = ?", "u.region_id = ?", "u.created_at_ms >= ?", "u.created_at_ms < ?", } { if !strings.Contains(whereSQL, want) { t.Fatalf("where sql missing %q: %s", want, whereSQL) } } if len(args) != 9 || args[0] != "lalu" || args[5] != "PH" || args[6] != int64(3) || args[7] != int64(1000) || args[8] != int64(2000) { t.Fatalf("where args mismatch: %+v", args) } whereSQL, args = appUserListWhereSQL("lalu", normalizeListQuery(listQuery{RegionID: 0, RegionIDSet: true})) if !strings.Contains(whereSQL, "COALESCE(u.region_id, 0) = 0") || !strings.Contains(whereSQL, "NOT EXISTS") { t.Fatalf("global region sql mismatch: %s", whereSQL) } if len(args) != 1 || args[0] != "lalu" { t.Fatalf("global region args mismatch: %+v", args) } } func TestAppUserOrderSQL(t *testing.T) { asc := appUserOrderSQL(listQuery{SortBy: "created_at", SortDirection: "asc"}) if asc != "ORDER BY u.created_at_ms ASC, u.user_id ASC" { t.Fatalf("created asc order mismatch: %s", asc) } desc := appUserOrderSQL(listQuery{SortBy: "created_at", SortDirection: "desc"}) if desc != "ORDER BY u.created_at_ms DESC, u.user_id DESC" { t.Fatalf("created desc order mismatch: %s", desc) } } func TestSortAppUsersByCoin(t *testing.T) { items := []AppUser{ {UserID: "1001", Coin: 10}, {UserID: "1003", Coin: 10}, {UserID: "1002", Coin: 80}, } sortAppUsersByCoin(items, "desc") if got := []string{items[0].UserID, items[1].UserID, items[2].UserID}; got[0] != "1002" || got[1] != "1003" || got[2] != "1001" { t.Fatalf("coin desc order mismatch: %+v", got) } sortAppUsersByCoin(items, "asc") if got := []string{items[0].UserID, items[1].UserID, items[2].UserID}; got[0] != "1001" || got[1] != "1003" || got[2] != "1002" { t.Fatalf("coin asc order mismatch: %+v", got) } }