Repository: DiceDB/dice
File: integration_tests/commands/resp/pfcountwatch_test.go (Go)

Code before:
}

func TestPFCountWATCHWithSDK(t *testing.T) {
	publisher := getLocalSdk()
	subscribers := []WatchSubscriber{{client: getLocalSdk()}, {client: getLocalSdk()}, {client: getLocalSdk()}}

	publisher.Del(context.Background(), pfcountWatchKey)

	channels := make([]<-chan *dicedb.WatchResult, len(subscribers))
	for i, subscriber := range subscribers {
		watch := subscriber.client.WatchConn(context.Background())
		subscribers[i].watch = watch
		assert.Assert(t, watch != nil)
		firstMsg, err := watch.Watch(context.Background(), "PFCOUNT", pfcountWatchKey)
		assert.NilError(t, err)
		assert.Equal(t, firstMsg.Command, "PFCOUNT")
		channels[i] = watch.Channel()
	}

	for _, tc := range PFCountWatchSDKTestCases {
		err := publisher.PFAdd(context.Background(), tc.key, tc.val).Err()
		assert.NilError(t, err)

		for _, channel := range channels {
			v := <-channel

			assert.Equal(t, "PFCOUNT", v.Command)         // command
			assert.Equal(t, "1580567186", v.Fingerprint) // Fingerprint
			assert.DeepEqual(t, tc.result, v.Data)       // data
		}
	}

	for _, tc := range pfcountWatchSDKhWithPFMergeTestCases {
		publisher.Del(context.Background(), tc.dest_key_1, tc.dest_key_2)
		publisher.PFAdd(context.Background(), tc.dest_key_1, tc.dest_value_1).Err()
		publisher.PFAdd(context.Background(), tc.dest_key_2, tc.dest_value_2).Err()
		publisher.PFMerge(context.Background(), pfcountWatchKey, tc.dest_key_1, tc.dest_key_2).Err()

		for _, channel := range channels {
			v := <-channel

			assert.Equal(t, "PFCOUNT", v.Command)         // command
			assert.Equal(t, "1580567186", v.Fingerprint) // Fingerprint
			assert.DeepEqual(t, tc.result, v.Data)       // data
		}
	}
}

Review comment (suggestion):
Ideally we should have constants defined separately.
```suggestion
pfCountWatchKey  = "pfcount_watch_key" // assuming this was defined elsewhere
defaultTimeout   = 5 * time.Second
watchFingerprint = "1580567186"
```

Code after:
	pfcountWatchQuerySDK       = "PFCOUNT.WATCH %s"
	pfcountWatchFingerPrintSDK = "1832643469"
	defaultTimeout             = 5 * time.Second
)

type pfcountWatchSDKTestCase struct {
	key    string
	val    any
	result int64
}

type pfcountWatchSDKWithPFMergeTestCase struct {
	destKey1    string
	destValue1  []string
	destKey2 	string
	destValue2  []string
	result 		int64
}

var PFCountWatchSDKTestCases = []pfcountWatchSDKTestCase{
	{pfcountWatchKeySDK, "value1", int64(1)},
	{pfcountWatchKeySDK, "value2", int64(2)},
	{pfcountWatchKeySDK, "value3", int64(3)},
	{pfcountWatchKeySDK, "value4", int64(4)},
}

var pfcountWatchSDKhWithPFMergeTestCases = []pfcountWatchSDKWithPFMergeTestCase{
	{"DEST_KEY_1", []string{"a", "b", "c", "d", "e", "f"}, "DEST_KEY_2", []string{"f", "g"}, int64(11)},
	{"DEST_KEY_3", []string{"a", "b", "c", "d", "e", "f"}, "DEST_KEY_4", []string{"a1", "b1", "c1", "d1", "e1", "f1"}, int64(17)},
	{"DEST_KEY_5", []string{"f", "g"}, "DEST_KEY_6", []string{"f", "g"}, int64(17)},
}

func TestPFCountWATCHWithSDK(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) 
	defer cancel()

	publisher := getLocalSdk()
	subscribers := setupSubscribersSDK(3)
	defer cleanupSubscribersSDK(subscribers)

	publisher.Del(ctx, pfcountWatchKey)

	channels := setUpWatchChannelsSDK(t, ctx, subscribers)

	t.Run("Basic PFCount Operations", func(t *testing.T) { 
			testPFCountAddSDK(t, ctx, channels, publisher) 
		},
	)

	t.Run("PFCount Operations including PFMerge", func(t *testing.T) { 
			testPFCountMergeSDK(t, ctx, channels, publisher)