53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package cache
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/jackc/pgconn"
|
|
"github.com/spf13/afero"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/supabase/cli/pkg/pgtest"
|
|
)
|
|
|
|
var dbConfig = pgconn.Config{
|
|
Host: "127.0.0.1",
|
|
Port: 5432,
|
|
User: "admin",
|
|
Password: "password",
|
|
Database: "postgres",
|
|
}
|
|
|
|
func TestCacheCommand(t *testing.T) {
|
|
t.Run("inspects cache hit rate", func(t *testing.T) {
|
|
// Setup in-memory fs
|
|
fsys := afero.NewMemMapFs()
|
|
// Setup mock postgres
|
|
conn := pgtest.NewConn()
|
|
defer conn.Close(t)
|
|
conn.Query(CacheQuery).
|
|
Reply("SELECT 1", Result{
|
|
Name: "index hit rate",
|
|
Ratio: 0.9,
|
|
})
|
|
// Run test
|
|
err := Run(context.Background(), dbConfig, fsys, conn.Intercept)
|
|
// Check error
|
|
assert.NoError(t, err)
|
|
})
|
|
|
|
t.Run("throws error on empty result", func(t *testing.T) {
|
|
// Setup in-memory fs
|
|
fsys := afero.NewMemMapFs()
|
|
// Setup mock postgres
|
|
conn := pgtest.NewConn()
|
|
defer conn.Close(t)
|
|
conn.Query(CacheQuery).
|
|
Reply("SELECT 1", []interface{}{})
|
|
// Run test
|
|
err := Run(context.Background(), dbConfig, fsys, conn.Intercept)
|
|
// Check error
|
|
assert.ErrorContains(t, err, "cannot find field Name in returned row")
|
|
})
|
|
}
|