github.com/supabase/cli@v1.168.1/internal/inspect/cache/cache_test.go (about)

     1  package cache
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/jackc/pgconn"
     8  	"github.com/spf13/afero"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/supabase/cli/internal/inspect"
    11  	"github.com/supabase/cli/internal/testing/pgtest"
    12  )
    13  
    14  var dbConfig = pgconn.Config{
    15  	Host:     "127.0.0.1",
    16  	Port:     5432,
    17  	User:     "admin",
    18  	Password: "password",
    19  	Database: "postgres",
    20  }
    21  
    22  func TestCacheCommand(t *testing.T) {
    23  	t.Run("inspects cache hit rate", func(t *testing.T) {
    24  		// Setup in-memory fs
    25  		fsys := afero.NewMemMapFs()
    26  		// Setup mock postgres
    27  		conn := pgtest.NewConn()
    28  		defer conn.Close(t)
    29  		conn.Query(inspect.CACHE_QUERY).
    30  			Reply("SELECT 1", Result{
    31  				Name:  "index hit rate",
    32  				Ratio: 0.9,
    33  			})
    34  		// Run test
    35  		err := Run(context.Background(), dbConfig, fsys, conn.Intercept)
    36  		// Check error
    37  		assert.NoError(t, err)
    38  	})
    39  
    40  	t.Run("throws error on empty result", func(t *testing.T) {
    41  		// Setup in-memory fs
    42  		fsys := afero.NewMemMapFs()
    43  		// Setup mock postgres
    44  		conn := pgtest.NewConn()
    45  		defer conn.Close(t)
    46  		conn.Query(inspect.CACHE_QUERY).
    47  			Reply("SELECT 1", []interface{}{})
    48  		// Run test
    49  		err := Run(context.Background(), dbConfig, fsys, conn.Intercept)
    50  		// Check error
    51  		assert.ErrorContains(t, err, "cannot find field Name in returned row")
    52  	})
    53  }