|  | 
|  | 1 | +//go:build conftests | 
|  | 2 | +// +build conftests | 
|  | 3 | + | 
|  | 4 | +/* | 
|  | 5 | +Copyright 2021 The Dapr Authors | 
|  | 6 | +Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 7 | +you may not use this file except in compliance with the License. | 
|  | 8 | +You may obtain a copy of the License at | 
|  | 9 | +    http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 10 | +Unless required by applicable law or agreed to in writing, software | 
|  | 11 | +distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | +See the License for the specific language governing permissions and | 
|  | 14 | +limitations under the License. | 
|  | 15 | +*/ | 
|  | 16 | + | 
|  | 17 | +package clickhouse | 
|  | 18 | + | 
|  | 19 | +import ( | 
|  | 20 | +	"fmt" | 
|  | 21 | +	"testing" | 
|  | 22 | + | 
|  | 23 | +	"github.com/stretchr/testify/assert" | 
|  | 24 | +	"github.com/stretchr/testify/require" | 
|  | 25 | + | 
|  | 26 | +	"github.com/dapr/components-contrib/state" | 
|  | 27 | +	"github.com/dapr/components-contrib/state/clickhouse" | 
|  | 28 | +	"github.com/dapr/components-contrib/tests/certification/flow" | 
|  | 29 | +	"github.com/dapr/components-contrib/tests/certification/flow/dockercompose" | 
|  | 30 | +	"github.com/dapr/components-contrib/tests/certification/flow/sidecar" | 
|  | 31 | +	"github.com/dapr/kit/logger" | 
|  | 32 | +) | 
|  | 33 | + | 
|  | 34 | +const (   | 
|  | 35 | +	componentName = "clickhouse-store" | 
|  | 36 | +) | 
|  | 37 | + | 
|  | 38 | +func TestClickHouseStateStore(t *testing.T) { | 
|  | 39 | +	flow.New(t, "Test ClickHouse state store certification").   | 
|  | 40 | +		Step(dockercompose.Run("docker-compose.yml")).   | 
|  | 41 | +		Step("verify clickhouse store operations", testClickHouseStateStore()).   | 
|  | 42 | +		Run() | 
|  | 43 | +} | 
|  | 44 | + | 
|  | 45 | +func testClickHouseStateStore() flow.Runnable { | 
|  | 46 | +	return func(ctx flow.Context) error { | 
|  | 47 | +		// Create a new ClickHouse state store instance | 
|  | 48 | +		store := clickhouse.NewClickHouseStateStore(logger.NewLogger("clickhouse-store-test")) | 
|  | 49 | +		 | 
|  | 50 | +		// Initialize the state store | 
|  | 51 | +		metadata := state.Metadata{} | 
|  | 52 | +		metadata.Properties = map[string]string{ | 
|  | 53 | +			"clickhouseURL": "tcp://localhost:9000", | 
|  | 54 | +			"databaseName": "dapr_test", | 
|  | 55 | +			"tableName":    "state_test", | 
|  | 56 | +			"username":     "default", | 
|  | 57 | +			"password":     "", | 
|  | 58 | +		} | 
|  | 59 | +		 | 
|  | 60 | +		err := store.Init(ctx, metadata) | 
|  | 61 | +		require.NoError(ctx.T, err) | 
|  | 62 | +		 | 
|  | 63 | +		// Set a value | 
|  | 64 | +		setReq := &state.SetRequest{ | 
|  | 65 | +			Key:   "test-key", | 
|  | 66 | +			Value: []byte("test-value"), | 
|  | 67 | +		} | 
|  | 68 | +		err = store.Set(ctx, setReq) | 
|  | 69 | +		require.NoError(ctx.T, err) | 
|  | 70 | +		 | 
|  | 71 | +		// Get the value | 
|  | 72 | +		getReq := &state.GetRequest{ | 
|  | 73 | +			Key: "test-key", | 
|  | 74 | +		} | 
|  | 75 | +		getResp, err := store.Get(ctx, getReq) | 
|  | 76 | +		require.NoError(ctx.T, err) | 
|  | 77 | +		assert.Equal(ctx.T, "test-value", string(getResp.Data)) | 
|  | 78 | +		 | 
|  | 79 | +		// Delete the value | 
|  | 80 | +		delReq := &state.DeleteRequest{ | 
|  | 81 | +			Key: "test-key", | 
|  | 82 | +		} | 
|  | 83 | +		err = store.Delete(ctx, delReq) | 
|  | 84 | +		require.NoError(ctx.T, err) | 
|  | 85 | +		 | 
|  | 86 | +		// Verify the value is deleted | 
|  | 87 | +		getResp, err = store.Get(ctx, getReq) | 
|  | 88 | +		require.NoError(ctx.T, err) | 
|  | 89 | +		assert.Nil(ctx.T, getResp.Data) | 
|  | 90 | +		 | 
|  | 91 | +		// Test TTL | 
|  | 92 | +		ttlSetReq := &state.SetRequest{ | 
|  | 93 | +			Key:   "test-ttl-key", | 
|  | 94 | +			Value: []byte("test-ttl-value"), | 
|  | 95 | +			Metadata: map[string]string{ | 
|  | 96 | +				"ttlInSeconds": "1", // 1 second TTL | 
|  | 97 | +			}, | 
|  | 98 | +		} | 
|  | 99 | +		err = store.Set(ctx, ttlSetReq) | 
|  | 100 | +		require.NoError(ctx.T, err) | 
|  | 101 | +		 | 
|  | 102 | +		// Wait for TTL to expire (2 seconds to be safe) | 
|  | 103 | +		fmt.Println("Waiting for TTL to expire...") | 
|  | 104 | +		flow.Sleep(2 * flow.Second) | 
|  | 105 | +		 | 
|  | 106 | +		// Verify the value is expired | 
|  | 107 | +		ttlGetReq := &state.GetRequest{ | 
|  | 108 | +			Key: "test-ttl-key", | 
|  | 109 | +		} | 
|  | 110 | +		ttlGetResp, err := store.Get(ctx, ttlGetReq) | 
|  | 111 | +		require.NoError(ctx.T, err) | 
|  | 112 | +		assert.Nil(ctx.T, ttlGetResp.Data) | 
|  | 113 | +		 | 
|  | 114 | +		// Test ETag | 
|  | 115 | +		etagSetReq := &state.SetRequest{ | 
|  | 116 | +			Key:   "test-etag-key", | 
|  | 117 | +			Value: []byte("test-etag-value"), | 
|  | 118 | +		} | 
|  | 119 | +		err = store.Set(ctx, etagSetReq) | 
|  | 120 | +		require.NoError(ctx.T, err) | 
|  | 121 | +		 | 
|  | 122 | +		// Get the value with ETag | 
|  | 123 | +		etagGetReq := &state.GetRequest{ | 
|  | 124 | +			Key: "test-etag-key", | 
|  | 125 | +		} | 
|  | 126 | +		etagGetResp, err := store.Get(ctx, etagGetReq) | 
|  | 127 | +		require.NoError(ctx.T, err) | 
|  | 128 | +		assert.NotNil(ctx.T, etagGetResp.ETag) | 
|  | 129 | +		 | 
|  | 130 | +		// Update with correct ETag | 
|  | 131 | +		etagUpdateReq := &state.SetRequest{ | 
|  | 132 | +			Key:   "test-etag-key", | 
|  | 133 | +			Value: []byte("test-etag-value-updated"), | 
|  | 134 | +			ETag:  etagGetResp.ETag, | 
|  | 135 | +		} | 
|  | 136 | +		err = store.Set(ctx, etagUpdateReq) | 
|  | 137 | +		require.NoError(ctx.T, err) | 
|  | 138 | +		 | 
|  | 139 | +		// Update with incorrect ETag | 
|  | 140 | +		badETag := "bad-etag" | 
|  | 141 | +		etagBadUpdateReq := &state.SetRequest{ | 
|  | 142 | +			Key:   "test-etag-key", | 
|  | 143 | +			Value: []byte("test-etag-value-updated-again"), | 
|  | 144 | +			ETag:  &badETag, | 
|  | 145 | +		} | 
|  | 146 | +		err = store.Set(ctx, etagBadUpdateReq) | 
|  | 147 | +		require.Error(ctx.T, err) | 
|  | 148 | +		 | 
|  | 149 | +		// Clean up | 
|  | 150 | +		err = store.Delete(ctx, &state.DeleteRequest{Key: "test-etag-key"}) | 
|  | 151 | +		require.NoError(ctx.T, err) | 
|  | 152 | +		 | 
|  | 153 | +		return nil | 
|  | 154 | +	} | 
|  | 155 | +} | 
0 commit comments