dicedb-rs 0.1.3

Rust SDK for DiceDb.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
use crate::client::Client;
use crate::commands::Command;
use crate::commands::CommandExecutor;
use crate::commands::DelInput;
use crate::commands::ExpireAtOption;
use crate::commands::ExpireOption;
use crate::commands::GetexOption;
use crate::commands::SetOption;
use crate::commands::SetValue;
use crate::commands::Value;
use crate::errors::StreamError;

type Result<T> = std::result::Result<T, StreamError>;

impl<'a> Into<DelInput<'a>> for Vec<&'a str> {
    fn into(self) -> DelInput<'a> {
        DelInput::Multiple(self)
    }
}

impl<'a> Into<DelInput<'a>> for &'a str {
    fn into(self) -> DelInput<'a> {
        DelInput::Single(self)
    }
}

impl Client {
    /// Decrements the integer at `key` by one. Creates `key` as -1 if absent. Errors on wrong type
    /// or non-integer string. Limited to 64-bit signed integers.
    ///
    /// # Arguments
    /// * `key` - The key to decrement.
    /// # Returns
    /// * [`Value`] - The new value of `key`.
    /// # Errors
    /// * [`StreamError`] - If an error occured in the communication stream.
    pub fn decr(&mut self, key: &str) -> Result<Value> {
        let resp = self.command_client.execute_command(Command::DECR {
            key: key.to_string(),
        })?;
        Ok(resp)
    }
    // DECRBY command decrements the integer at ‘key’ by the delta specified. Creates ‘key’ with value (-delta) if absent. Errors on wrong type or non-integer string. Limited to 64-bit signed integers.
    /// Decrements the integer at `key` by `delta`. Creates `key` as `-delta` if absent. Errors on
    /// wrong type
    /// or non-integer string. Limited to 64-bit signed integers.
    /// # Arguments
    /// * `key` - The key to decrement.
    /// * `delta` - The amount to decrement by.
    /// # Returns
    /// * [`Value`] - The new value of `key`.
    /// # Errors
    /// * [`StreamError`] - If an error occured in the communication stream.
    pub fn decrby(&mut self, key: &str, delta: i64) -> Result<Value> {
        let resp = self.command_client.execute_command(Command::DECRBY {
            key: key.to_string(),
            delta,
        })?;
        Ok(resp)
    }

    // DEL command deletes all the specified keys and returns the number of keys deleted on success. &
    /// Deletes all the specified keys and returns the number of keys deleted on success.
    /// # Arguments
    /// * `keys` - The keys to delete, either a single key or multiple keys.
    /// # Returns
    /// * [`Value`] - The number of keys deleted.
    /// # Errors
    /// * [`StreamError`] - If an error occured in the communication stream.
    pub fn del<'a, T: Into<DelInput<'a>>>(&mut self, keys: T) -> Result<Value> {
        let del_input: DelInput<'_> = keys.into();
        let keys = match del_input {
            DelInput::Single(key) => vec![key].iter().map(|&x| x.to_string()).collect(),
            DelInput::Multiple(keys) => keys.iter().map(|&x| x.to_string()).collect(),
        };
        let resp = self.command_client.execute_command(Command::DEL { keys })?;
        Ok(resp)
    }

    /// Echos a message with the server, ie. returns the message passed to it.
    /// # Arguments
    /// * `message` - The message to return.
    /// # Returns
    /// * [`Value`] - The message.
    /// # Errors
    /// * [`StreamError`] - If an error occured in the communication stream.
    pub fn echo(&mut self, message: &str) -> Result<Value> {
        let resp = self.command_client.execute_command(Command::ECHO {
            message: message.to_string(),
        })?;
        Ok(resp)
    }

    /// Checks if the specified keys exist.
    /// # Arguments
    /// * `key` - The key to check.
    /// * `additional_keys` - Additional keys to check. If empty, only `key` is checked.
    /// # Returns
    /// * [`Value`] - The number of keys that exist.
    /// # Errors
    /// * [`StreamError`] - If an error occured in the communication stream.
    pub fn exists(&mut self, key: &str, additional_keys: Vec<&str>) -> Result<Value> {
        let resp = self.command_client.execute_command(Command::EXISTS {
            key: key.to_string(),
            additional_keys: additional_keys.iter().map(|&x| x.to_string()).collect(),
        })?;
        Ok(resp)
    }
    // EXPIRE sets an expiry (in seconds) on a specified key. After the expiry time has elapsed, the key will be automatically deleted.
    //
    //     If you want to delete the expirtation time on the key, you can use the PERSIST command.
    //
    // The command returns 1 if the expiry was set, and 0 if the key already had an expiry set. The command supports the following options:
    //
    //     NX: Set the expiration only if the key does not already have an expiration time.
    //     XX: Set the expiration only if the key already has an expiration time.
    //
    /// Sets an expiry (in seconds) on a specified key. After the expiry time has elapsed, the key
    /// will be automatically deleted.
    /// # Arguments
    /// * `key` - The key to set the expiry on.
    /// * `seconds` - The number of seconds until the key expires.
    /// * `option`: [`ExpireOption`] - The option to specify conditions for setting the expiry.
    /// # Returns
    /// * [`Value`] - 1 if the expiry was set, 0 if expire was not set.
    /// # Errors
    /// * [`StreamError`] - If an error occured in the communication stream.
    pub fn expire(&mut self, key: &str, seconds: i64, option: ExpireOption) -> Result<Value> {
        let resp = self.command_client.execute_command(Command::EXPIRE {
            key: key.to_string(),
            seconds,
            option,
        })?;
        Ok(resp)
    }

    /// Sets the expiration time of a key as an absolute Unix timestamp (in seconds). After the
    /// expiry
    /// time has elapsed, the key will be automatically deleted.
    /// # Arguments
    /// * `key` - The key to set the expiry on.
    /// * `timestamp` - The Unix timestamp in seconds.
    /// * `option`: [`ExpireAtOption`] - The option to specify conditions for setting the expiry.
    /// # Returns
    /// * [`Value`] - 1 if the expiry was set or updated, 0 if the expiration time was not changed.
    /// # Errors
    /// * [`StreamError`] - If an error occured in the communication stream.
    pub fn expireat(&mut self, key: &str, timestamp: i64, option: ExpireAtOption) -> Result<Value> {
        let resp = self.command_client.execute_command(Command::EXPIREAT {
            key: key.to_string(),
            timestamp,
            option,
        })?;
        Ok(resp)
    }

    /// Returns the absolute Unix timestamp in seconds at which the given key will expire.
    /// # Arguments
    /// * `key` - The key to get the expiry time of.
    /// # Returns
    /// * [`Value`] - The Unix timestamp in seconds.
    /// # Errors
    /// * [`StreamError`] - If an error occured in the communication stream.
    pub fn expiretime(&mut self, key: &str) -> Result<Value> {
        let resp = self.command_client.execute_command(Command::EXPIRETIME {
            key: key.to_string(),
        })?;
        Ok(resp)
    }

    /// Deletes all keys present in the database.
    pub fn flushdb(&mut self) -> Result<Value> {
        let resp = self.command_client.execute_command(Command::FLUSHDB)?;
        Ok(resp)
    }
    // GET returns the value for the key in args.
    //
    // The command returns (nil) if the key does not exist.
    /// Returns the value for the given key.
    /// # Arguments
    /// * `key` - The key to get the value of.
    /// # Returns
    /// * [`Value`] - The value of the key. Returns a valid  [`Value::VNull`] variant if the key does not exist.
    /// # Errors
    /// * [`StreamError`] - If an error occured in the communication stream.
    pub fn get(&mut self, key: &str) -> Result<Value> {
        let resp = self.command_client.execute_command(Command::GET {
            key: key.to_string(),
        })?;
        Ok(resp)
    }
    /// Returns the value for the given key and then deletes the key.
    /// # Arguments
    /// * `key` - The key to get the value of and delete.
    /// # Returns
    /// * [`Value`] - The value of the key. Returns a valid  [`Value::VNull`] variant if the key
    /// does not exist.
    pub fn getdel(&mut self, key: &str) -> Result<Value> {
        let resp = self.command_client.execute_command(Command::GETDEL {
            key: key.to_string(),
        })?;
        Ok(resp)
    }

    /// Returns the value for the given key and optionally sets its expiration.
    /// # Arguments
    /// * `key` - The key to get the value of.
    /// * `option`: [`GetexOption`] - The option to specify conditions for setting the expiry.
    /// # Returns
    /// * [`Value`] - The value of the key. Returns a valid  [`Value::VNull`] variant if the key
    /// does not exist.
    /// # Errors
    /// * [`StreamError`] - If an error occured in the communication stream.
    pub fn getex(&mut self, key: &str, option: GetexOption) -> Result<Value> {
        let resp = self.command_client.execute_command(Command::GETEX {
            key: key.to_string(),
            ex: option,
        })?;
        Ok(resp)
    }
    /// Increments the integer at `key` by one. Creates `key` as 1 if absent.    
    /// /// # Arguments
    /// * `key` - The key to increment.
    /// # Returns
    /// * [`Value`] - The new value of `key`.
    /// # Errors
    /// * [`StreamError`] - If an error occured in the communication stream, or if the key is not
    /// an integer.
    pub fn incr(&mut self, key: &str) -> Result<Value> {
        let resp = self.command_client.execute_command(Command::INCR {
            key: key.to_string(),
        })?;
        Ok(resp)
    }
    /// Increments the integer at `key` by `delta`. Creates `key` as `delta` if absent.
    /// # Arguments
    /// * `key` - The key to increment.
    /// * `delta` - The amount to increment by.
    /// # Returns
    /// * [`Value`] - The new value of `key`, or an error if the key is not an integer.
    pub fn incrby(&mut self, key: &str, delta: i64) -> Result<Value> {
        let resp = self.command_client.execute_command(Command::INCRBY {
            key: key.to_string(),
            delta,
        })?;
        Ok(resp)
    }
    /// Returns PONG if no argument is provided, otherwise it returns PONG with the message
    /// argument.
    /// # Returns
    /// * [`Value`] - The response from the server, with PONG if no argument is provided.
    /// # Errors
    /// * [`StreamError`] - If an error occured in the communication stream.
    pub fn ping(&mut self) -> Result<Value> {
        let resp = self.command_client.execute_command(Command::PING)?;
        Ok(resp)
    }
    /// Sets the value of a key.
    /// # Arguments
    /// * `key` - The key to set the value of.
    /// * `value` - The value to set.
    /// # Returns
    /// * [`Value`] - A response from the server with an OK if succes.
    /// # Errors
    /// * [`StreamError`] - If an error occured in the communication stream.
    pub fn set<T: Into<SetValue>>(&mut self, key: &str, value: T) -> Result<Value> {
        let resp = self.command_client.execute_command(Command::SET {
            key: key.to_string(),
            value: value.into(),
            option: crate::commands::SetOption::None,
            get: false,
        })?;
        Ok(resp)
    }

    /// Sets the value of a key and returns the previous value.
    /// # Arguments
    /// * `key` - The key to set the value of.
    /// * `value` - The value to set.
    /// # Returns
    /// * [`Value`] - The previous value of the key.
    /// # Errors
    /// * [`StreamError`] - If an error occured in the communication stream.
    pub fn setget<T: Into<SetValue>>(&mut self, key: &str, value: T) -> Result<Value> {
        let resp = self.command_client.execute_command(Command::SET {
            key: key.to_string(),
            value: value.into(),
            option: crate::commands::SetOption::None,
            get: true,
        })?;
        Ok(resp)
    }

    /// Sets the value of a key with an expiration time.
    /// # Arguments
    /// * `key` - The key to set the value of.
    /// * `value` - The value to set.
    /// * `option`: [`SetOption`] - The option to specify conditions for setting the expiry.
    /// # Returns
    /// * [`Value`] - A response from the server with an OK if succes.
    /// # Errors
    /// * [`StreamError`] - If an error occured in the communication stream.
    pub fn setex<T: Into<SetValue>>(
        &mut self,
        key: &str,
        value: T,
        option: SetOption,
    ) -> Result<Value> {
        let resp = self.command_client.execute_command(Command::SET {
            key: key.to_string(),
            value: value.into(),
            option,
            get: false,
        })?;
        Ok(resp)
    }
    /// Returns the remaining time to live (in seconds) of a key that has an expiration set.
    /// # Arguments
    /// * `key` - The key to get the time to live of.
    /// # Returns
    /// * [`Value`] - The remaining time to live in seconds.
    /// # Errors
    /// * [`StreamError`] - If an error occured in the communication stream.
    pub fn ttl(&mut self, key: &str) -> Result<Value> {
        let resp = self.command_client.execute_command(Command::TTL {
            key: key.to_string(),
        })?;
        Ok(resp)
    }

    /// Returns the type of the value stored at `key` as a string.
    /// # Arguments
    /// * `key` - The key to get the type of.
    /// # Returns
    /// * [`Value`] - The type of the value stored at `key`, as a [`Value::VStr`] variant.
    /// # Errors
    /// * [`StreamError`] - If an error occured in the communication stream.
    pub fn dtype(&mut self, key: &str) -> Result<Value> {
        let resp = self.command_client.execute_command(Command::TYPE {
            key: key.to_string(),
        })?;
        Ok(resp)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    const HOST: &str = "localhost";
    const PORT: u16 = 7379;

    #[test]
    fn test_key_w_spaces() {
        // NOTE: Today this is legal, but should it?
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "test ilegal key";
        let value = SetValue::Str("ilegal key?".to_string());
        let result = client.set(key, value.clone());
        assert!(result.is_ok());
        let value_get = client.get(key).unwrap();
        assert_eq!(value_get, Value::VStr("ilegal key?".to_string()));
    }

    #[test]
    fn test_key_w_underscores() {
        // NOTE: Today this is legal, but should it?
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "test_ilegal_key";
        let value = SetValue::Str("ilegal key with underscores?".to_string());
        let result = client.set(key, value.clone());
        assert!(result.is_ok());
        let value_get = client.get(key).unwrap();
        assert_eq!(
            value_get,
            Value::VStr("ilegal key with underscores?".to_string())
        );
    }

    #[test]
    fn test_key_w_newline() {
        // NOTE: Today this is legal, but should it?
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "test\nilegal\nkey";
        let value = SetValue::Str("ilegal key with newlines?".to_string());
        let result = client.set(key, value.clone());
        assert!(result.is_ok());
        let value_get = client.get(key).unwrap();
        assert_eq!(
            value_get,
            Value::VStr("ilegal key with newlines?".to_string())
        );
    }

    #[test]
    fn test_key_w_weird_symbols() {
        // NOTE: Today this is legal, but should it?
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "test!@#$«»%^&*()_+\t";
        let value = SetValue::Str("ilegal key with weird symbols?".to_string());
        let result = client.set(key, value.clone());
        assert!(result.is_ok());
        let value_get = client.get(key).unwrap();
        assert_eq!(
            value_get,
            Value::VStr("ilegal key with weird symbols?".to_string())
        );
    }

    #[test]
    fn test_key_w_underscores_cause_problems_with_exists() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "test_ilegal_key_exists";
        let value = SetValue::Str("ilegal key with underscores?".to_string());
        let result = client.set(key, value.clone());
        assert!(result.is_ok());
        let value_get = client.exists(key, vec![key, key]).unwrap();
        assert_eq!(value_get, Value::VInt(9)); // BUG: There is probably a bug with how additional
                                               // keys are handled in the exists command.
    }

    #[test]
    fn test_case_sensitive_keys() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "UPPERcase";
        let value = SetValue::Str("case sensitive key?".to_string());
        let result = client.set(key, value.clone());
        assert!(result.is_ok());
        let get = client.get("uppercase").unwrap();
        assert_eq!(get, Value::VNull);
        let value_get = client.get(key).unwrap();
        assert_eq!(value_get, Value::VStr("case sensitive key?".to_string()));
    }

    #[test]
    fn test_decr() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testdecr";
        let value = SetValue::Int(1);
        client.set(key, value.clone()).unwrap();
        let result = client.decr(key).unwrap();
        assert_eq!(result, Value::VInt(0));
    }

    #[test]
    fn test_decrby() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testdecrby";
        let value = SetValue::Int(3);
        client.set(key, value.clone()).unwrap();
        let result = client.decrby(key, 2).unwrap();
        assert_eq!(result, Value::VInt(1));
    }

    #[test]
    fn test_decrby_overflow() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testdecrbyoverflow";
        let value = SetValue::Int(i64::MIN);
        client.set(key, value.clone()).unwrap();
        let result = client.decrby(key, 1).unwrap();
        assert_eq!(result, Value::VInt(i64::MAX));
    }

    #[test]
    fn test_decr_min_underflow() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testdecrmin";
        let value = SetValue::Int(i64::MIN);
        client.set(key, value.clone()).unwrap();
        let result = client.decr(key).unwrap();
        assert_eq!(result, Value::VInt(i64::MAX));
    }

    #[test]
    fn test_del() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testdel";
        let value = SetValue::Str("test".to_string());
        client.set(key, value.clone()).unwrap();
        let result = client.del(vec![key]).unwrap();
        assert_eq!(result, Value::VInt(1));

        let value_get = client.get(key).unwrap();
        assert_eq!(value_get, Value::VNull);
    }

    #[test]
    fn test_expire() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testexpire";
        let value = SetValue::Str("test".to_string());
        client.set(key, value.clone()).unwrap();
        let result = client.expire(key, 1, ExpireOption::None).unwrap();
        assert_eq!(result, Value::VInt(1));

        std::thread::sleep(std::time::Duration::from_secs(2));
        let value_get = client.get(key).unwrap();
        assert_eq!(value_get, Value::VNull);
    }

    #[test]
    fn test_expire_nx() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testexpirenx";
        let value = SetValue::Str("test".to_string());
        client.set(key, value.clone()).unwrap();
        let result = client.expire(key, 1, ExpireOption::NX).unwrap();
        assert_eq!(result, Value::VInt(1));

        let result = client.expire(key, 100, ExpireOption::NX).unwrap();
        assert_eq!(result, Value::VInt(0));

        std::thread::sleep(std::time::Duration::from_secs(2));
        let value_get = client.get(key).unwrap();
        assert_eq!(value_get, Value::VNull);
    }

    #[test]
    fn test_expire_xx() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testexpirexx";
        let value = SetValue::Str("test".to_string());
        client.set(key, value.clone()).unwrap();

        let result = client.expire(key, 100, ExpireOption::XX).unwrap();
        assert_eq!(result, Value::VInt(0));

        let result = client.expire(key, 100, ExpireOption::None).unwrap();
        assert_eq!(result, Value::VInt(1));

        let result = client.expire(key, 1, ExpireOption::XX).unwrap();
        assert_eq!(result, Value::VInt(1));

        std::thread::sleep(std::time::Duration::from_secs(3));
        let value_get = client.get(key).unwrap();
        assert_eq!(value_get, Value::VNull);
    }

    #[test]
    fn test_existsmany() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key1 = "testexistsmany1";
        client.set(key1, "test").unwrap();
        let key2 = "testexistsmany2";
        client.set(key2, "test").unwrap();
        let key3 = "testexistsmany3";
        let result = client.exists(key1, vec![key2, key3]).unwrap();
        assert_eq!(result, Value::VInt(3));
    }

    #[test]
    fn test_exists_one() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key1 = "testexists1";
        client.set(key1, "test").unwrap();
        let result = client.exists(key1, vec![]).unwrap();
        assert_eq!(result, Value::VInt(1));
    }

    #[test]
    fn test_exists_two() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key1 = "testexiststwo1";
        client.set(key1, "test").unwrap();
        let key2 = "testexiststwo2";
        client.set(key2, "test").unwrap();
        let result = client.exists(key1, vec![key2]).unwrap();
        assert_eq!(result, Value::VInt(2));
    }

    #[test]
    fn test_expireat() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testexpireat";
        let value = SetValue::Str("test".to_string());
        client.set(key, value.clone()).unwrap();

        let timestamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs()
            + 1;

        let result = client
            .expireat(key, timestamp as i64, ExpireAtOption::None)
            .unwrap();
        assert_eq!(result, Value::VInt(1));

        std::thread::sleep(std::time::Duration::from_secs(2));
        let value_get = client.get(key).unwrap();
        assert_eq!(value_get, Value::VNull);
    }

    #[test]
    fn test_expireat_nx() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testexpireatnx";
        let value = SetValue::Str("test".to_string());
        client.set(key, value.clone()).unwrap();

        let timestamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs()
            + 1;

        let result = client
            .expireat(key, timestamp as i64, ExpireAtOption::NX)
            .unwrap();
        assert_eq!(result, Value::VInt(1));

        let result = client
            .expireat(key, timestamp as i64, ExpireAtOption::NX)
            .unwrap();
        assert_eq!(result, Value::VInt(0));

        std::thread::sleep(std::time::Duration::from_secs(2));
        let value_get = client.get(key).unwrap();
        assert_eq!(value_get, Value::VNull);
    }

    #[test]
    fn test_expireat_xx() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testexpireatxx";
        let value = SetValue::Str("test".to_string());
        client.set(key, value.clone()).unwrap();

        let timestamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs()
            + 1;

        let result = client
            .expireat(key, timestamp as i64, ExpireAtOption::XX)
            .unwrap();
        assert_eq!(result, Value::VInt(0));

        let result = client
            .expireat(key, timestamp as i64, ExpireAtOption::None)
            .unwrap();
        assert_eq!(result, Value::VInt(1));

        let result = client
            .expireat(key, timestamp as i64, ExpireAtOption::XX)
            .unwrap();
        assert_eq!(result, Value::VInt(1));

        std::thread::sleep(std::time::Duration::from_secs(2));
        let value_get = client.get(key).unwrap();
        assert_eq!(value_get, Value::VNull);
    }

    #[test]
    fn test_expireat_gt() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testexpireatgt";
        let value = SetValue::Str("test".to_string());
        client.set(key, value.clone()).unwrap();

        let timestamp_2sec = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs()
            + 2;

        let timestamp_1sec = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs()
            + 1;

        let result = client
            .expireat(key, timestamp_2sec as i64, ExpireAtOption::GT)
            .unwrap();
        assert_eq!(result, Value::VInt(0));

        let result = client
            .expireat(key, timestamp_1sec as i64, ExpireAtOption::None)
            .unwrap();
        assert_eq!(result, Value::VInt(1));

        let result = client
            .expireat(key, timestamp_2sec as i64, ExpireAtOption::GT)
            .unwrap();
        assert_eq!(result, Value::VInt(1));

        let result = client
            .expireat(key, timestamp_1sec as i64, ExpireAtOption::GT)
            .unwrap();
        assert_eq!(result, Value::VInt(0));
    }

    #[test]
    fn test_expireat_lt() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testexpireatlt";
        let value = SetValue::Str("test".to_string());
        client.set(key, value.clone()).unwrap();

        let timestamp_2sec = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs()
            + 2;

        let timestamp_1sec = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs()
            + 1;

        let result = client
            .expireat(key, timestamp_1sec as i64, ExpireAtOption::LT)
            .unwrap();
        assert_eq!(result, Value::VInt(0));

        let result = client
            .expireat(key, timestamp_2sec as i64, ExpireAtOption::None)
            .unwrap();
        assert_eq!(result, Value::VInt(1));

        let result = client
            .expireat(key, timestamp_1sec as i64, ExpireAtOption::LT)
            .unwrap();
        assert_eq!(result, Value::VInt(1));

        let result = client
            .expireat(key, timestamp_2sec as i64, ExpireAtOption::LT)
            .unwrap();
        assert_eq!(result, Value::VInt(0));
    }

    #[test]
    fn test_expiretime() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testexpiretime";
        let value = SetValue::Str("test".to_string());
        client.set(key, value.clone()).unwrap();
        let expire_result = client.expire(key, 1, ExpireOption::None).unwrap();
        let expire_time = client.expiretime(key).unwrap();
        assert_eq!(expire_result, Value::VInt(1));
        let now_epoch = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs()
            + 1;
        assert_eq!(expire_time, Value::VInt(now_epoch as i64));
    }

    #[test]
    #[ignore] // We ignore this test, as it will flush the database and cause other tests to fail
    fn test_flushdb() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testflushdb";
        let value = SetValue::Str("test".to_string());
        client.set(key, value.clone()).unwrap();
        let result = client.flushdb().unwrap();
        assert_eq!(result, Value::VStr("OK".to_string()));

        let value_get = client.get(key).unwrap();
        assert_eq!(value_get, Value::VNull);
    }

    #[test]
    fn test_get_set() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testgetset";
        let value = SetValue::Str("test".to_string());
        client.set(key, value.clone()).unwrap();
        let result = client.get(key).unwrap();
        assert_eq!(result, value.into());
    }

    #[test]
    fn test_set_with_get() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testsetwithget";
        let value = SetValue::Str("test".to_string());
        let result = client.set(key, value.clone()).unwrap();
        assert_eq!(result, Value::VStr("OK".to_string()));
        let new_value = SetValue::Str("new test".to_string());
        let result = client.setget(key, new_value.clone()).unwrap();
        assert_eq!(result, value.into());
    }

    #[test]
    fn test_ping_pong() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let result = client.ping().unwrap();
        assert_eq!(result, Value::VStr("PONG".to_string()));
    }

    #[test]
    fn test_echo() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let message = "hello";
        let result = client.echo(message).unwrap();
        assert_eq!(result, Value::VStr(message.to_string()));
    }

    #[test]
    fn test_getdel() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testgetdel";
        let value = SetValue::Str("test".to_string());
        client.set(key, value.clone()).unwrap();
        let result = client.getdel(key).unwrap();
        assert_eq!(result, value.into());

        let value_get = client.get(key).unwrap();
        assert_eq!(value_get, Value::VNull);
    }

    #[test]
    fn test_getex() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testgetex";
        let value = SetValue::Str("test".to_string());
        client.set(key, value.clone()).unwrap();
        let result = client.getex(key, GetexOption::EX(1)).unwrap();
        assert_eq!(result, value.into());

        std::thread::sleep(std::time::Duration::from_secs(2));

        let value_get = client.get(key).unwrap();
        assert_eq!(value_get, Value::VNull);
    }

    #[test]
    fn test_incr() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testincr";
        let value = SetValue::Int(1);
        client.set(key, value.clone()).unwrap();
        let result = client.incr(key).unwrap();
        assert_eq!(result, Value::VInt(2));
    }

    #[test]
    fn test_incrby() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testincrby";
        let value = SetValue::Int(1);
        client.set(key, value.clone()).unwrap();
        let result = client.incrby(key, 2).unwrap();
        assert_eq!(result, Value::VInt(3));
    }

    #[test]
    fn test_incr_overflow() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testincroverflow";
        let value = SetValue::Int(i64::MAX);
        client.set(key, value.clone()).unwrap();
        let result = client.incr(key).unwrap();
        assert_eq!(result, Value::VInt(i64::MIN));
    }

    #[test]
    fn test_ttl() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testttl";
        let value = SetValue::Str("test".to_string());
        let result = client.setex(key, value.clone(), SetOption::EX(1)).unwrap();
        assert_eq!(result, Value::VStr("OK".to_string()));
        let ttl = client.ttl(key).unwrap();
        // This test is susceptible to failing for timing reasons if not given a acceptable range
        let withinacceptable = match ttl {
            Value::VInt(v) if v <= 2 && v >= 0 => true,
            _ => false,
        };
        assert_eq!(withinacceptable, true);
    }

    #[test]
    fn test_type_str() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testtypestr";
        let value = SetValue::Str("test".to_string());
        client.set(key, value.clone()).unwrap();
        let result = client.dtype(key).unwrap();
        assert_eq!(result, Value::VStr("string".to_string()));
    }

    #[test]
    fn test_type_int() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testtypeint";
        let value = SetValue::Int(1);
        client.set(key, value.clone()).unwrap();
        let result = client.dtype(key).unwrap();
        assert_eq!(result, Value::VStr("int".to_string()));
    }

    #[test]
    fn test_type_null() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testtypenull";
        let result = client.dtype(key).unwrap();
        assert_eq!(result, Value::VStr("none".to_string()));
    }

    #[test]
    fn test_type_float() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testtypefloat";
        let value = SetValue::Float(1.3);
        client.set(key, value.clone()).unwrap();
        let result = client.dtype(key).unwrap();
        assert_eq!(result, Value::VStr("float".to_string()));
    }

    #[test]
    fn test_get_set_float() {
        let mut client = Client::new(HOST.to_string(), PORT).unwrap();
        let key = "testgetsetfloat";
        let value = SetValue::Float(1.3);
        client.set(key, value.clone()).unwrap();
        let result = client.get(key);
        assert!(result.is_err()); // BUG: Known bug, cant get float values atm.
    }
}