]>
Commit | Line | Data |
---|---|---|
f78fd11b | 1 | #!/usr/bin/perl |
2 | ||
3 | use warnings; | |
4 | use strict; | |
5 | ||
6 | use Test::More tests => 106; | |
7 | use Data::Dump qw/dump/; | |
8 | ||
9 | use lib 'lib'; | |
10 | ||
11 | BEGIN { | |
12 | use_ok( 'Redis' ); | |
13 | } | |
14 | ||
15 | ok( my $o = Redis->new(), 'new' ); | |
16 | ||
17 | ok( $o->ping, 'ping' ); | |
18 | ||
19 | ||
20 | diag "Commands operating on string values"; | |
21 | ||
22 | ok( $o->set( foo => 'bar' ), 'set foo => bar' ); | |
23 | ||
24 | ok( ! $o->setnx( foo => 'bar' ), 'setnx foo => bar fails' ); | |
25 | ||
26 | cmp_ok( $o->get( 'foo' ), 'eq', 'bar', 'get foo = bar' ); | |
27 | ||
28 | ok( $o->set( foo => 'baz' ), 'set foo => baz' ); | |
29 | ||
30 | cmp_ok( $o->get( 'foo' ), 'eq', 'baz', 'get foo = baz' ); | |
31 | ||
32 | ok( $o->set( 'test-undef' => 42 ), 'set test-undef' ); | |
33 | ok( $o->set( 'test-undef' => undef ), 'set undef' ); | |
34 | ok( ! defined $o->get( 'test-undef' ), 'get undef' ); | |
35 | ok( $o->exists( 'test-undef' ), 'exists undef' ); | |
36 | ||
37 | $o->del('non-existant'); | |
38 | ||
39 | ok( ! $o->exists( 'non-existant' ), 'exists non-existant' ); | |
40 | ok( ! $o->get( 'non-existant' ), 'get non-existant' ); | |
41 | ||
42 | ok( $o->set('key-next' => 0), 'key-next = 0' ); | |
43 | ||
44 | my $key_next = 3; | |
45 | ||
46 | 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 my $id ( 0 .. $key_next ) { | |
53 | my $key = 'key-' . $id; | |
54 | push @keys, $key; | |
55 | ok( $o->set( $key => $id ), "set $key" ); | |
56 | ok( $o->exists( $key ), "exists $key" ); | |
57 | cmp_ok( $o->get( $key ), 'eq', $id, "get $key" ); | |
58 | cmp_ok( $o->incr( 'key-next' ), '==', $id + 1, 'incr' ); | |
59 | cmp_ok( $o->decr( 'key-left' ), '==', $key_next - $id - 1, 'decr' ); | |
60 | } | |
61 | ||
62 | cmp_ok( $o->get( 'key-next' ), '==', $key_next + 1, 'key-next' ); | |
63 | ||
64 | ok( $o->set('test-incrby', 0), 'test-incrby' ); | |
65 | ok( $o->set('test-decrby', 0), 'test-decry' ); | |
66 | foreach ( 1 .. 3 ) { | |
67 | cmp_ok( $o->incrby('test-incrby', 3), '==', $_ * 3, 'incrby 3' ); | |
68 | cmp_ok( $o->decrby('test-decrby', 7), '==', -( $_ * 7 ), 'decrby 7' ); | |
69 | } | |
70 | ||
71 | 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' ); | |
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 | ||
81 | ok( $o->rename( 'test-incrby', 'test-renamed' ), 'rename' ); | |
82 | ok( $o->exists( 'test-renamed' ), 'exists test-renamed' ); | |
83 | ||
84 | eval { $o->rename( 'test-decrby', 'test-renamed', 1 ) }; | |
85 | ok( $@, 'rename to existing key' ); | |
86 | ||
87 | ok( my $nr_keys = $o->dbsize, 'dbsize' ); | |
88 | ||
89 | ||
90 | diag "Commands operating on lists"; | |
91 | ||
92 | my $list = 'test-list'; | |
93 | ||
94 | $o->del($list) && diag "cleanup $list from last run"; | |
95 | ||
96 | ok( $o->rpush( $list => "r$_" ), 'rpush' ) foreach ( 1 .. 3 ); | |
97 | ||
98 | ok( $o->lpush( $list => "l$_" ), 'lpush' ) foreach ( 1 .. 2 ); | |
99 | ||
100 | cmp_ok( $o->type($list), 'eq', 'list', 'type' ); | |
101 | cmp_ok( $o->llen($list), '==', 5, 'llen' ); | |
102 | ||
103 | is_deeply( [ $o->lrange( $list, 0, 1 ) ], [ 'l2', 'l1' ], 'lrange' ); | |
104 | ||
105 | ok( $o->ltrim( $list, 1, 2 ), 'ltrim' ); | |
106 | cmp_ok( $o->llen($list), '==', 2, 'llen after ltrim' ); | |
107 | ||
108 | cmp_ok( $o->lindex( $list, 0 ), 'eq', 'l1', 'lindex' ); | |
109 | cmp_ok( $o->lindex( $list, 1 ), 'eq', 'r1', 'lindex' ); | |
110 | ||
111 | ok( $o->lset( $list, 0, 'foo' ), 'lset' ); | |
112 | cmp_ok( $o->lindex( $list, 0 ), 'eq', 'foo', 'verified' ); | |
113 | ||
114 | ok( $o->lrem( $list, 1, 'foo' ), 'lrem' ); | |
115 | cmp_ok( $o->llen( $list ), '==', 1, 'llen after lrem' ); | |
116 | ||
117 | cmp_ok( $o->lpop( $list ), 'eq', 'r1', 'lpop' ); | |
118 | ||
119 | ok( ! $o->rpop( $list ), 'rpop' ); | |
120 | ||
121 | ||
122 | diag "Commands operating on sets"; | |
123 | ||
124 | my $set = 'test-set'; | |
125 | $o->del($set); | |
126 | ||
127 | ok( $o->sadd( $set, 'foo' ), 'sadd' ); | |
128 | ok( ! $o->sadd( $set, 'foo' ), 'sadd' ); | |
129 | cmp_ok( $o->scard( $set ), '==', 1, 'scard' ); | |
130 | ok( $o->sismember( $set, 'foo' ), 'sismember' ); | |
131 | ||
132 | cmp_ok( $o->type( $set ), 'eq', 'set', 'type is set' ); | |
133 | ||
134 | ok( $o->srem( $set, 'foo' ), 'srem' ); | |
135 | ok( ! $o->srem( $set, 'foo' ), 'srem again' ); | |
136 | cmp_ok( $o->scard( $set ), '==', 0, 'scard' ); | |
137 | ||
138 | $o->sadd( 'test-set1', $_ ) foreach ( 'foo', 'bar', 'baz' ); | |
139 | $o->sadd( 'test-set2', $_ ) foreach ( 'foo', 'baz', 'xxx' ); | |
140 | ||
141 | my $inter = [ 'baz', 'foo' ]; | |
142 | ||
143 | is_deeply( [ $o->sinter( 'test-set1', 'test-set2' ) ], $inter, 'siter' ); | |
144 | ||
145 | ok( $o->sinterstore( 'test-set-inter', 'test-set1', 'test-set2' ), 'sinterstore' ); | |
146 | ||
147 | cmp_ok( $o->scard( 'test-set-inter' ), '==', $#$inter + 1, 'cardinality of intersection' ); | |
148 | ||
149 | ||
150 | diag "Multiple databases handling commands"; | |
151 | ||
152 | ok( $o->select( 1 ), 'select' ); | |
153 | ok( $o->select( 0 ), 'select' ); | |
154 | ||
155 | ok( $o->move( 'foo', 1 ), 'move' ); | |
156 | ok( ! $o->exists( 'foo' ), 'gone' ); | |
157 | ||
158 | ok( $o->select( 1 ), 'select' ); | |
159 | ok( $o->exists( 'foo' ), 'exists' ); | |
160 | ||
161 | ok( $o->flushdb, 'flushdb' ); | |
162 | cmp_ok( $o->dbsize, '==', 0, 'empty' ); | |
163 | ||
164 | ||
165 | diag "Sorting"; | |
166 | ||
167 | ok( $o->lpush( 'test-sort', $_ ), "put $_" ) foreach ( 1 .. 4 ); | |
168 | cmp_ok( $o->llen( 'test-sort' ), '==', 4, 'llen' ); | |
169 | ||
170 | is_deeply( [ $o->sort( 'test-sort' ) ], [ 1,2,3,4 ], 'sort' ); | |
171 | is_deeply( [ $o->sort( 'test-sort DESC' ) ], [ 4,3,2,1 ], 'sort DESC' ); | |
172 | ||
173 | ||
174 | diag "Persistence control commands"; | |
175 | ||
176 | ok( $o->save, 'save' ); | |
177 | ok( $o->bgsave, 'bgsave' ); | |
178 | ok( $o->lastsave, 'lastsave' ); | |
179 | #ok( $o->shutdown, 'shutdown' ); | |
180 | diag "shutdown not tested"; | |
181 | ||
182 | diag "Remote server control commands"; | |
183 | ||
184 | ok( my $info = $o->info, 'info' ); | |
185 | diag dump( $info ); | |
186 | ||
187 | diag "Connection handling"; | |
188 | ||
189 | ok( $o->quit, 'quit' ); |