46 lines
1.6 KiB
Go
46 lines
1.6 KiB
Go
package appuser
|
|
|
|
import "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)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|