order_test.go 913 Bytes
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
package query

import (
	"strings"
	"testing"
)

func testKeyOrder(t *testing.T, f Order, keys []string, expect []string) {
	e := make([]Entry, len(keys))
	for i, k := range keys {
		e[i] = Entry{Key: k}
	}

	res := ResultsWithEntries(Query{}, e)
	res = NaiveOrder(res, f)
16 17 18 19 20
	actualE, err := res.Rest()
	if err != nil {
		t.Fatal(err)
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
	actual := make([]string, len(actualE))
	for i, e := range actualE {
		actual[i] = e.Key
	}

	if len(actual) != len(expect) {
		t.Error("expect != actual.", expect, actual)
	}

	if strings.Join(actual, "") != strings.Join(expect, "") {
		t.Error("expect != actual.", expect, actual)
	}
}

func TestOrderByKey(t *testing.T) {

	testKeyOrder(t, OrderByKey{}, sampleKeys, []string{
		"/a",
		"/ab",
		"/ab/c",
		"/ab/cd",
		"/abce",
		"/abcf",
	})
	testKeyOrder(t, OrderByKeyDescending{}, sampleKeys, []string{
		"/abcf",
		"/abce",
		"/ab/cd",
		"/ab/c",
		"/ab",
		"/a",
	})
}