3 |
use warnings; |
use warnings; |
4 |
use strict; |
use strict; |
5 |
|
|
6 |
use Test::More tests => 44; |
use Test::More tests => 95; |
7 |
|
|
8 |
use lib 'lib'; |
use lib 'lib'; |
9 |
|
|
15 |
|
|
16 |
ok( $o->ping, 'ping' ); |
ok( $o->ping, 'ping' ); |
17 |
|
|
18 |
|
diag "Commands operating on string values"; |
19 |
|
|
20 |
ok( $o->set( foo => 'bar' ), 'set foo => bar' ); |
ok( $o->set( foo => 'bar' ), 'set foo => bar' ); |
21 |
|
|
22 |
eval { $o->set( foo => 'bar', 1 ) }; |
eval { $o->set( foo => 'bar', 1 ) }; |
28 |
|
|
29 |
cmp_ok( $o->get( 'foo' ), 'eq', 'baz', 'get foo = baz' ); |
cmp_ok( $o->get( 'foo' ), 'eq', 'baz', 'get foo = baz' ); |
30 |
|
|
31 |
|
ok( $o->set( 'test-undef' => 42 ), 'set test-undef' ); |
32 |
|
ok( $o->set( 'test-undef' => undef ), 'set undef' ); |
33 |
|
ok( ! defined $o->get( 'test-undef' ), 'get undef' ); |
34 |
|
diag $o->exists( 'test-undef' ); |
35 |
|
ok( $o->exists( 'test-undef' ), 'exists undef' ); |
36 |
|
|
37 |
$o->del('non-existant'); |
$o->del('non-existant'); |
38 |
|
|
39 |
ok( ! $o->exists( 'non-existant' ), 'exists non-existant' ); |
ok( ! $o->exists( 'non-existant' ), 'exists non-existant' ); |
45 |
|
|
46 |
ok( $o->set('key-left' => $key_next), 'key-left' ); |
ok( $o->set('key-left' => $key_next), 'key-left' ); |
47 |
|
|
48 |
|
is_deeply( [ $o->mget( 'foo', 'key-next', 'key-left' ) ], [ 'baz', 0, 3 ], 'mget' ); |
49 |
|
|
50 |
|
my @keys; |
51 |
|
|
52 |
foreach ( 0 .. $key_next ) { |
foreach ( 0 .. $key_next ) { |
53 |
ok( $o->set( "key-$_" => $_ ), "set key-$_" ); |
my $key = 'key-' . $_; |
54 |
ok( $o->exists( "key-$_" ), "exists key-$_" ); |
push @keys, $key; |
55 |
cmp_ok( $o->get( "key-$_" ), 'eq', $_, "get key-$_" ); |
ok( $o->set( $key => $_ ), "set $key" ); |
56 |
|
ok( $o->exists( $key ), "exists $key" ); |
57 |
|
cmp_ok( $o->get( $key ), 'eq', $_, "get $key" ); |
58 |
cmp_ok( $o->incr( 'key-next' ), '==', $_ + 1, 'incr' ); |
cmp_ok( $o->incr( 'key-next' ), '==', $_ + 1, 'incr' ); |
59 |
cmp_ok( $o->decr( 'key-left' ), '==', $key_next - $_ - 1, 'decr' ); |
cmp_ok( $o->decr( 'key-left' ), '==', $key_next - $_ - 1, 'decr' ); |
60 |
} |
} |
68 |
cmp_ok( $o->decr('test-decrby', 7), '==', -( $_ * 7 ), 'decrby 7' ); |
cmp_ok( $o->decr('test-decrby', 7), '==', -( $_ * 7 ), 'decrby 7' ); |
69 |
} |
} |
70 |
|
|
71 |
ok( $o->del('key-next' ), 'del' ); |
ok( $o->del( $_ ), "del $_" ) foreach map { "key-$_" } ( 'next', 'left' ); |
72 |
|
ok( ! $o->del('non-existing' ), 'del non-existing' ); |
73 |
|
|
74 |
cmp_ok( $o->type('foo'), 'eq', 'string', 'type' ); |
cmp_ok( $o->type('foo'), 'eq', 'string', 'type' ); |
75 |
|
|
76 |
|
cmp_ok( $o->keys('key-*'), '==', $key_next + 1, 'key-*' ); |
77 |
|
is_deeply( [ $o->keys('key-*') ], [ @keys ], 'keys' ); |
78 |
|
|
79 |
|
ok( my $key = $o->randomkey, 'randomkey' ); |
80 |
|
diag "key: $key"; |
81 |
|
|
82 |
|
ok( $o->rename( 'test-incrby', 'test-renamed' ), 'rename' ); |
83 |
|
ok( $o->exists( 'test-renamed' ), 'exists test-renamed' ); |
84 |
|
|
85 |
|
eval { $o->rename( 'test-decrby', 'test-renamed', 1 ) }; |
86 |
|
ok( $@, 'rename to existing key' ); |
87 |
|
|
88 |
|
ok( my $nr_keys = $o->dbsize, 'dbsize' ); |
89 |
|
diag "dbsize: $nr_keys"; |
90 |
|
|
91 |
|
diag "Commands operating on lists"; |
92 |
|
|
93 |
|
my $list = 'test-list'; |
94 |
|
|
95 |
|
$o->del($list) && diag "cleanup $list from last run"; |
96 |
|
|
97 |
|
ok( $o->rpush( $list => "r$_" ), 'rpush' ) foreach ( 1 .. 3 ); |
98 |
|
|
99 |
|
ok( $o->lpush( $list => "l$_" ), 'lpush' ) foreach ( 1 .. 2 ); |
100 |
|
|
101 |
|
cmp_ok( $o->type($list), 'eq', 'list', 'type' ); |
102 |
|
cmp_ok( $o->llen($list), '==', 5, 'llen' ); |
103 |
|
|
104 |
|
is_deeply( [ $o->lrange( $list, 0, 1 ) ], [ 'l2', 'l1' ], 'lrange' ); |
105 |
|
|
106 |
|
ok( $o->ltrim( $list, 1, 2 ), 'ltrim' ); |
107 |
|
cmp_ok( $o->llen($list), '==', 2, 'llen after ltrim' ); |
108 |
|
|
109 |
|
cmp_ok( $o->lindex( $list, 0 ), 'eq', 'l1', 'lindex' ); |
110 |
|
cmp_ok( $o->lindex( $list, 1 ), 'eq', 'r1', 'lindex' ); |
111 |
|
|
112 |
|
ok( $o->lset( $list, 0, 'foo' ), 'lset' ); |
113 |
|
cmp_ok( $o->lindex( $list, 0 ), 'eq', 'foo', 'verified' ); |
114 |
|
|
115 |
|
ok( $o->lrem( $list, 1, 'foo' ), 'lrem' ); |
116 |
|
cmp_ok( $o->llen( $list ), '==', 1, 'llen after lrem' ); |
117 |
|
|
118 |
|
cmp_ok( $o->lpop( $list ), 'eq', 'r1', 'lpop' ); |
119 |
|
|
120 |
|
ok( ! $o->rpop( $list ), 'rpop' ); |
121 |
|
|
122 |
|
|
123 |
|
diag "Commands operating on sets"; |
124 |
|
|
125 |
|
my $set = 'test-set'; |
126 |
|
$o->del($set); |
127 |
|
|
128 |
|
ok( $o->sadd( $set, 'foo' ), 'sadd' ); |
129 |
|
ok( ! $o->sadd( $set, 'foo' ), 'sadd' ); |
130 |
|
cmp_ok( $o->scard( $set ), '==', 1, 'scard' ); |
131 |
|
ok( $o->sismember( $set, 'foo' ), 'sismember' ); |
132 |
|
|
133 |
|
cmp_ok( $o->type( $set ), 'eq', 'set', 'type is set' ); |
134 |
|
|
135 |
|
ok( $o->srem( $set, 'foo' ), 'srem' ); |
136 |
|
ok( ! $o->srem( $set, 'foo' ), 'srem again' ); |
137 |
|
cmp_ok( $o->scard( $set ), '==', 0, 'scard' ); |
138 |
|
|
139 |
|
$o->sadd( 'test-set1', $_ ) foreach ( 'foo', 'bar', 'baz' ); |
140 |
|
$o->sadd( 'test-set2', $_ ) foreach ( 'foo', 'baz', 'xxx' ); |
141 |
|
|
142 |
|
my $inter = [ 'baz', 'foo' ]; |
143 |
|
|
144 |
|
is_deeply( [ $o->sinter( 'test-set1', 'test-set2' ) ], $inter, 'siter' ); |
145 |
|
|
146 |
|
ok( $o->sinterstore( 'test-set-inter', 'test-set1', 'test-set2' ), 'sinterstore' ); |
147 |
|
|
148 |
|
cmp_ok( $o->scard( 'test-set-inter' ), '==', $#$inter + 1, 'cardinality of intersection' ); |
149 |
|
|
150 |
|
|
151 |
|
diag "Multiple databases handling commands"; |
152 |
|
|
153 |
|
ok( $o->select( 1 ), 'select' ); |
154 |
|
ok( $o->select( 0 ), 'select' ); |
155 |
|
|
156 |
|
ok( $o->move( 'foo', 1 ), 'move' ); |
157 |
|
ok( ! $o->exists( 'foo' ), 'gone' ); |
158 |
|
|
159 |
|
ok( $o->select( 1 ), 'select' ); |
160 |
|
ok( $o->exists( 'foo' ), 'exists' ); |
161 |
|
|
162 |
|
ok( $o->flushdb, 'flushdb' ); |
163 |
|
cmp_ok( $o->dbsize, '==', 0, 'empty' ); |
164 |
|
|
165 |
|
diag "Connection handling"; |
166 |
|
|
167 |
ok( $o->quit, 'quit' ); |
ok( $o->quit, 'quit' ); |