|
| 1 | +package table |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "google.golang.org/protobuf/proto" |
| 8 | + |
| 9 | + "github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Table" |
| 10 | + |
| 11 | + "github.com/ydb-platform/ydb-go-sdk/v3/table" |
| 12 | + "github.com/ydb-platform/ydb-go-sdk/v3/table/options" |
| 13 | + "github.com/ydb-platform/ydb-go-sdk/v3/testutil" |
| 14 | +) |
| 15 | + |
| 16 | +func TestQueryCachePolicyKeepInCache(t *testing.T) { |
| 17 | + for _, test := range [...]struct { |
| 18 | + name string |
| 19 | + queryCachePolicyOption []options.QueryCachePolicyOption |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "with server cache", |
| 23 | + queryCachePolicyOption: []options.QueryCachePolicyOption{ |
| 24 | + options.WithQueryCachePolicyKeepInCache(), |
| 25 | + }, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "no server cache", |
| 29 | + queryCachePolicyOption: []options.QueryCachePolicyOption{}, |
| 30 | + }, |
| 31 | + } { |
| 32 | + t.Run(test.name, func(t *testing.T) { |
| 33 | + b := StubBuilder{ |
| 34 | + T: t, |
| 35 | + cc: testutil.NewDB( |
| 36 | + testutil.WithInvokeHandlers( |
| 37 | + testutil.InvokeHandlers{ |
| 38 | + // nolint:unparam |
| 39 | + testutil.TableExecuteDataQuery: func(request interface{}) (proto.Message, error) { |
| 40 | + r, ok := request.(*Ydb_Table.ExecuteDataQueryRequest) |
| 41 | + if !ok { |
| 42 | + t.Fatalf("cannot cast request '%T' to *Ydb_Table.ExecuteDataQueryRequest", request) |
| 43 | + } |
| 44 | + if len(test.queryCachePolicyOption) > 0 { |
| 45 | + if !r.QueryCachePolicy.GetKeepInCache() { |
| 46 | + t.Fatalf("keep-in-cache policy must be true, got: %v", r.QueryCachePolicy.GetKeepInCache()) |
| 47 | + } |
| 48 | + } else { |
| 49 | + if r.QueryCachePolicy.GetKeepInCache() { |
| 50 | + t.Fatalf("keep-in-cache policy must be false, got: %v", r.QueryCachePolicy.GetKeepInCache()) |
| 51 | + } |
| 52 | + } |
| 53 | + return &Ydb_Table.ExecuteQueryResult{ |
| 54 | + TxMeta: &Ydb_Table.TransactionMeta{ |
| 55 | + Id: "", |
| 56 | + }, |
| 57 | + }, nil |
| 58 | + }, |
| 59 | + // nolint:unparam |
| 60 | + testutil.TableCreateSession: func(interface{}) (proto.Message, error) { |
| 61 | + return &Ydb_Table.CreateSessionResult{ |
| 62 | + SessionId: testutil.SessionID(), |
| 63 | + }, nil |
| 64 | + }, |
| 65 | + }, |
| 66 | + ), |
| 67 | + ), |
| 68 | + } |
| 69 | + s, err := b.createSession(context.Background()) |
| 70 | + if err != nil { |
| 71 | + t.Fatal(err) |
| 72 | + } |
| 73 | + _, _, err = s.Execute( |
| 74 | + context.Background(), table.TxControl( |
| 75 | + table.BeginTx( |
| 76 | + table.WithOnlineReadOnly(), |
| 77 | + ), |
| 78 | + table.CommitTx(), |
| 79 | + ), |
| 80 | + "SELECT 1", |
| 81 | + table.NewQueryParameters(), |
| 82 | + options.WithQueryCachePolicy(test.queryCachePolicyOption...), |
| 83 | + ) |
| 84 | + if err != nil { |
| 85 | + t.Fatal(err) |
| 86 | + } |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
0 commit comments