testcommands.pp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2025 by the Free Pascal development team
  4. Simple Redis test program, donated by Mario Ray Mahardhika
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. program testcommands;
  12. uses
  13. sysutils, redis;
  14. const
  15. MapKey = 'key';
  16. MapValue = 'value';
  17. ListKey = 'listkey';
  18. ListValue = 'v';
  19. var
  20. GTCPClient: TAbstractTCPClient;
  21. GRedis: TRedis;
  22. GRESP: TRESP;
  23. i: Integer;
  24. begin
  25. GTCPClient := TSSocketsTCPClient.Create(Redis.DefaultHost, Redis.DefaultPort, Redis.DefaultConnectTimeout, Redis.DefaultCanReadTimeout);
  26. GRedis := TRedis.Create(GTCPClient);
  27. Writeln('Testing SET');
  28. GRESP := GRedis.SendCommand(['SET', MapKey, MapValue]);
  29. if GRESP.RESPType = rtError then begin
  30. WriteLn(StdErr, GRESP.ErrorType + ': ' + GRESP.StrValue);
  31. Halt(1);
  32. end else begin
  33. WriteLn(GRESP.StrValue);
  34. end;
  35. Writeln('Testing GET:');
  36. GRESP := GRedis.SendCommand(['GET', MapKey]);
  37. if GRESP.RESPType = rtError then begin
  38. WriteLn(StdErr, GRESP.ErrorType + ': ' + GRESP.StrValue);
  39. Halt(1);
  40. end else begin
  41. WriteLn(GRESP.StrValue);
  42. end;
  43. Writeln('Testing LPUSH:');
  44. for i := 1 to 3 do begin
  45. GRESP := GRedis.SendCommand(['LPUSH', ListKey, ListValue+IntTostr(i)]);
  46. if GRESP.RESPType = rtError then begin
  47. WriteLn(StdErr, GRESP.ErrorType + ': ' + GRESP.StrValue);
  48. Halt(1);
  49. end else begin
  50. WriteLn(GRESP.IntValue);
  51. end;
  52. end;
  53. Writeln('Testing LRANGE:');
  54. GRESP := GRedis.SendCommand(['LRANGE', ListKey, '0', '-1']);
  55. if GRESP.RESPType = rtError then begin
  56. WriteLn(StdErr, GRESP.ErrorType + ': ' + GRESP.StrValue);
  57. Halt(1);
  58. end else begin
  59. for i := 0 to GRESP.ElementCount - 1 do begin
  60. if i > 0 then Write(', ');
  61. Write(GRESP.Elements[i].StrValue);
  62. end;
  63. WriteLn;
  64. end;
  65. Writeln('Testing GET on array (will result in error):');
  66. GRESP := GRedis.SendCommand(['GET', ListKey]);
  67. if GRESP.RESPType = rtError then begin
  68. WriteLn(StdErr, GRESP.ErrorType + ': ' + GRESP.StrValue+' (this is expected)');
  69. end else begin
  70. WriteLn('Unexpected reply:', GRESP.StrValue);
  71. end;
  72. Writeln('Testing RPOP:');
  73. for i := 1 to 3 do begin
  74. GRESP := GRedis.SendCommand(['RPOP', ListKey]);
  75. if GRESP.RESPType = rtError then begin
  76. WriteLn(StdErr, GRESP.ErrorType + ': ' + GRESP.StrValue);
  77. Halt(1);
  78. end else begin
  79. WriteLn(GRESP.StrValue);
  80. end;
  81. end;
  82. Writeln('Testing DEL (',MapKey,') :');
  83. GRESP := GRedis.SendCommand(['DEL', MapKey]);
  84. if GRESP.RESPType = rtError then begin
  85. WriteLn(StdErr, GRESP.ErrorType + ': ' + GRESP.StrValue);
  86. Halt(1);
  87. end else begin
  88. WriteLn(GRESP.IntValue);
  89. end;
  90. Writeln('Testing DEL (',ListKey,') :');
  91. GRESP := GRedis.SendCommand(['DEL', ListKey]);
  92. if GRESP.RESPType = rtError then begin
  93. WriteLn(StdErr, GRESP.ErrorType + ': ' + GRESP.StrValue);
  94. Halt(1);
  95. end else begin
  96. WriteLn(GRESP.IntValue);
  97. end;
  98. GRedis.Free;
  99. GTCPClient.Free;
  100. end.