1package paged23import (4 "context"5 "path/filepath"6 "testing"78 "src.rest/cat/012/internal/fileio"9 "src.rest/cat/012/internal/sheet"10)1112// runPages runs the pages' jobs until none is left, reporting how many13// ran.14func runPages(p *Pages) int {15 n := 016 for jobs := p.Jobs(); len(jobs) > 0; jobs = p.Jobs() {17 for _, j := range jobs {18 j.Run(context.Background())19 p.Store(j)20 n++21 }22 }23 return n24}2526// openSales writes n rows to a Parquet file and opens it as a source.27func openSales(t *testing.T, n int) fileio.Source {28 t.Helper()29 name := filepath.Join(t.TempDir(), "sales.parquet")30 writeSales(t, name, n, 0)31 h, err := fileio.OpenSource(context.Background(), fileio.SourceSpec{Path: name})32 if err != nil {33 t.Fatal(err)34 }35 t.Cleanup(func() { h.Close() })36 return h37}3839func TestPagesKeepTheLatest(t *testing.T) {40 h := openSales(t, 100*PageRows)41 p := NewPages(h, 0, nil)42 defer p.Close()43 if runPages(p) != 1 {44 t.Fatal("the view wasn't built once")45 }46 if n, ok := p.Rows(); !ok || n != 100*PageRows {47 t.Fatalf("%d rows, %v", n, ok)48 }49 for i := range int64(90) {50 row := i * PageRows51 p.Want(row, row)52 if got := runPages(p); got != 1 {53 t.Fatalf("page %d took %d reads", i, got)54 }55 if num, vals, ok := p.Row(row); !ok || num != row || vals[0].V.Num != float64(row) {56 t.Fatalf("row %d: %d %v %v", row, num, vals, ok)57 }58 }59 if len(p.pages) > keepPages {60 t.Errorf("%d pages kept", len(p.pages))61 }62 if _, _, ok := p.Row(0); ok {63 t.Error("the least used page is kept past the budget")64 }65}6667func TestPagesSortedAndFiltered(t *testing.T) {68 h := openSales(t, 1000)69 o := &sheet.SourceOrder{70 Sort: []sheet.SourceSort{{Col: 0, Desc: true}},71 Filter: []sheet.SourceFilter{{Col: 1, Cond: sheet.Condition{Op: sheet.CondExactly, Arg: "north"}}},72 }73 p := NewPages(h, 0, o)74 defer p.Close()75 if !p.Building() {76 t.Fatal("an ordered view isn't built")77 }78 runPages(p)79 n, ok := p.Rows()80 if !ok || n == 0 || n >= 1000 {81 t.Fatalf("%d rows pass, %v", n, ok)82 }83 p.Want(0, n-1)84 runPages(p)85 prev := int64(1 << 62)86 for i := range n {87 num, vals, ok := p.Row(i)88 if !ok || num >= prev || !(vals[1].V.Str == "north" || vals[1].V.Str == "North") {89 t.Fatalf("row %d of the view: %d %v %v", i, num, vals, ok)90 }91 prev = num92 }93 if !p.Same(h, 0, o) || p.Same(h, 0, nil) || !p.Of(h, 0) {94 t.Error("Same and Of")95 }96}