@@ -25,6 +25,22 @@ func testQueryRequest() (string, []byte) {
2525 return query , queryBytes
2626}
2727
28+ func testQueryRequestWithDateFucntion () (string , []byte ) {
29+ query := `SELECT
30+ user_id,
31+ username,
32+ last_login,
33+ NOW() AS current_time
34+ FROM
35+ users
36+ WHERE
37+ last_login >= CURRENT_DATE;`
38+ queryMsg := pgproto3.Query {String : query }
39+ // Encode the data to base64.
40+ queryBytes , _ := queryMsg .Encode (nil )
41+ return query , queryBytes
42+ }
43+
2844func testStartupRequest () []byte {
2945 startupMsg := pgproto3.StartupMessage {
3046 ProtocolVersion : 196608 ,
@@ -180,3 +196,76 @@ func Test_Plugin(t *testing.T) {
180196 assert .Equal (t , resultMap ["response" ], response )
181197 assert .Contains (t , resultMap , sdkAct .Signals )
182198}
199+
200+ func TestPluginDateFunctionInQuery (t * testing.T ) {
201+ // Initialize a new mock Redis server.
202+ mockRedisServer := miniredis .RunT (t )
203+ redisURL := "redis://" + mockRedisServer .Addr () + "/0"
204+ redisConfig , err := redis .ParseURL (redisURL )
205+ redisClient := redis .NewClient (redisConfig )
206+
207+ cacheUpdateChannel := make (chan * v1.Struct , 10 )
208+
209+ // Create and initialize a new plugin.
210+ logger := hclog .New (& hclog.LoggerOptions {
211+ Level : logging .GetLogLevel ("error" ),
212+ Output : os .Stdout ,
213+ })
214+ plugin := NewCachePlugin (Plugin {
215+ Logger : logger ,
216+ RedisURL : redisURL ,
217+ RedisClient : redisClient ,
218+ UpdateCacheChannel : cacheUpdateChannel ,
219+ })
220+
221+ // Use a WaitGroup to wait for the goroutine to finish.
222+ var wg sync.WaitGroup
223+ wg .Add (1 )
224+ go func () {
225+ defer wg .Done ()
226+ plugin .Impl .UpdateCache (context .Background ())
227+ }()
228+
229+ // Test the plugin's OnTrafficFromClient method with a StartupMessage.
230+ clientArgs := map [string ]interface {}{
231+ "request" : testStartupRequest (),
232+ "client" : map [string ]interface {}{
233+ "local" : "localhost:15432" ,
234+ "remote" : "localhost:45320" ,
235+ },
236+ "server" : map [string ]interface {}{
237+ "local" : "localhost:54321" ,
238+ "remote" : "localhost:5432" ,
239+ },
240+ "error" : "" ,
241+ }
242+ clientRequest , err := v1 .NewStruct (clientArgs )
243+ plugin .Impl .OnTrafficFromClient (context .Background (), clientRequest )
244+
245+ // Test the plugin's OnTrafficFromServer method with a query request.
246+ _ , queryRequest := testQueryRequestWithDateFucntion ()
247+ queryResponse , err := base64 .StdEncoding .DecodeString ("VAAAABsAAWlkAAAAQAQAAQAAABcABP////8AAEQAAAALAAEAAAABMUMAAAANU0VMRUNUIDEAWgAAAAVJ" )
248+ assert .Nil (t , err )
249+ queryArgs := map [string ]interface {}{
250+ "request" : queryRequest ,
251+ "response" : queryResponse ,
252+ "client" : map [string ]interface {}{
253+ "local" : "localhost:15432" ,
254+ "remote" : "localhost:45320" ,
255+ },
256+ "server" : map [string ]interface {}{
257+ "local" : "localhost:54321" ,
258+ "remote" : "localhost:5432" ,
259+ },
260+ "error" : "" ,
261+ }
262+ serverRequest , err := v1 .NewStruct (queryArgs )
263+ plugin .Impl .OnTrafficFromServer (context .Background (), serverRequest )
264+
265+ // Close the channel and wait for the cache updater to return gracefully.
266+ close (cacheUpdateChannel )
267+ wg .Wait ()
268+
269+ keys , _ := redisClient .Keys (context .Background (), "*" ).Result ()
270+ assert .Equal (t , 1 , len (keys )) // Only one key (representing the database name) should be present.
271+ }
0 commit comments