|
| 1 | +package tpuf_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/bamo/tpuf-go" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func TestWarmCache(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + namespace string |
| 18 | + httpResponse *http.Response |
| 19 | + httpError error |
| 20 | + expectedError string |
| 21 | + expectedMethod string |
| 22 | + expectedURL string |
| 23 | + expectedResult *tpuf.WarmCacheResult |
| 24 | + }{ |
| 25 | + { |
| 26 | + name: "successful warm cache", |
| 27 | + namespace: "test-namespace", |
| 28 | + httpResponse: &http.Response{ |
| 29 | + StatusCode: http.StatusOK, |
| 30 | + Body: io.NopCloser(bytes.NewBufferString(`{ |
| 31 | + "status": "ACCEPTED", |
| 32 | + "message": "cache starting to warm" |
| 33 | + }`)), |
| 34 | + }, |
| 35 | + expectedMethod: http.MethodGet, |
| 36 | + expectedURL: "https://api.turbopuffer.com/v1/namespaces/test-namespace/hint_cache_warm", |
| 37 | + expectedResult: &tpuf.WarmCacheResult{ |
| 38 | + Status: "ACCEPTED", |
| 39 | + Message: "cache starting to warm", |
| 40 | + }, |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + for _, tt := range tests { |
| 45 | + t.Run(tt.name, func(t *testing.T) { |
| 46 | + client := &tpuf.Client{ |
| 47 | + ApiToken: "test-token", |
| 48 | + HttpClient: &fakeHttpClient{ |
| 49 | + doFunc: func(req *http.Request) (*http.Response, error) { |
| 50 | + assert.Equal(t, tt.expectedMethod, req.Method, "unexpected request method") |
| 51 | + assert.Equal(t, tt.expectedURL, req.URL.String(), "unexpected request URL") |
| 52 | + |
| 53 | + return tt.httpResponse, tt.httpError |
| 54 | + }, |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + result, err := client.WarmCache(context.Background(), tt.namespace) |
| 59 | + if tt.expectedError == "" { |
| 60 | + assert.NoError(t, err) |
| 61 | + assert.Equal(t, tt.expectedResult, result, "unexpected warm cache result") |
| 62 | + } else { |
| 63 | + assert.EqualError(t, err, tt.expectedError) |
| 64 | + assert.Nil(t, result) |
| 65 | + } |
| 66 | + }) |
| 67 | + } |
| 68 | +} |
0 commit comments