]>
git.saurik.com Git - redis.git/blob - client-libraries/perl/t/01-Redis.t
6 use Test
::More tests
=> 106;
7 use Data
::Dump qw
/dump/;
15 ok
( my $o = Redis-
>new(), 'new' );
17 ok
( $o->ping, 'ping' );
20 diag
"Commands operating on string values";
22 ok
( $o->set( foo
=> 'bar' ), 'set foo => bar' );
24 ok
( ! $o->setnx( foo
=> 'bar' ), 'setnx foo => bar fails' );
26 cmp_ok
( $o->get( 'foo' ), 'eq', 'bar', 'get foo = bar' );
28 ok
( $o->set( foo
=> 'baz' ), 'set foo => baz' );
30 cmp_ok
( $o->get( 'foo' ), 'eq', 'baz', 'get foo = baz' );
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' );
37 $o->del('non-existant');
39 ok
( ! $o->exists( 'non-existant' ), 'exists non-existant' );
40 ok
( ! $o->get( 'non-existant' ), 'get non-existant' );
42 ok
( $o->set('key-next' => 0), 'key-next = 0' );
46 ok
( $o->set('key-left' => $key_next), 'key-left' );
48 is_deeply
( [ $o->mget( 'foo', 'key-next', 'key-left' ) ], [ 'baz', 0, 3 ], 'mget' );
52 foreach my $id ( 0 .. $key_next ) {
53 my $key = 'key-' . $id;
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' );
62 cmp_ok
( $o->get( 'key-next' ), '==', $key_next + 1, 'key-next' );
64 ok
( $o->set('test-incrby', 0), 'test-incrby' );
65 ok
( $o->set('test-decrby', 0), 'test-decry' );
67 cmp_ok
( $o->incrby('test-incrby', 3), '==', $_ * 3, 'incrby 3' );
68 cmp_ok
( $o->decrby('test-decrby', 7), '==', -( $_ * 7 ), 'decrby 7' );
71 ok
( $o->del( $_ ), "del $_" ) foreach map { "key-$_" } ( 'next', 'left' );
72 ok
( ! $o->del('non-existing' ), 'del non-existing' );
74 cmp_ok
( $o->type('foo'), 'eq', 'string', 'type' );
76 cmp_ok
( $o->keys('key-*'), '==', $key_next + 1, 'key-*' );
77 is_deeply
( [ $o->keys('key-*') ], [ @keys ], 'keys' );
79 ok
( my $key = $o->randomkey, 'randomkey' );
81 ok
( $o->rename( 'test-incrby', 'test-renamed' ), 'rename' );
82 ok
( $o->exists( 'test-renamed' ), 'exists test-renamed' );
84 eval { $o->rename( 'test-decrby', 'test-renamed', 1 ) };
85 ok
( $@, 'rename to existing key' );
87 ok
( my $nr_keys = $o->dbsize, 'dbsize' );
90 diag
"Commands operating on lists";
92 my $list = 'test-list';
94 $o->del($list) && diag
"cleanup $list from last run";
96 ok
( $o->rpush( $list => "r$_" ), 'rpush' ) foreach ( 1 .. 3 );
98 ok
( $o->lpush( $list => "l$_" ), 'lpush' ) foreach ( 1 .. 2 );
100 cmp_ok
( $o->type($list), 'eq', 'list', 'type' );
101 cmp_ok
( $o->llen($list), '==', 5, 'llen' );
103 is_deeply
( [ $o->lrange( $list, 0, 1 ) ], [ 'l2', 'l1' ], 'lrange' );
105 ok
( $o->ltrim( $list, 1, 2 ), 'ltrim' );
106 cmp_ok
( $o->llen($list), '==', 2, 'llen after ltrim' );
108 cmp_ok
( $o->lindex( $list, 0 ), 'eq', 'l1', 'lindex' );
109 cmp_ok
( $o->lindex( $list, 1 ), 'eq', 'r1', 'lindex' );
111 ok
( $o->lset( $list, 0, 'foo' ), 'lset' );
112 cmp_ok
( $o->lindex( $list, 0 ), 'eq', 'foo', 'verified' );
114 ok
( $o->lrem( $list, 1, 'foo' ), 'lrem' );
115 cmp_ok
( $o->llen( $list ), '==', 1, 'llen after lrem' );
117 cmp_ok
( $o->lpop( $list ), 'eq', 'r1', 'lpop' );
119 ok
( ! $o->rpop( $list ), 'rpop' );
122 diag
"Commands operating on sets";
124 my $set = 'test-set';
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' );
132 cmp_ok
( $o->type( $set ), 'eq', 'set', 'type is set' );
134 ok
( $o->srem( $set, 'foo' ), 'srem' );
135 ok
( ! $o->srem( $set, 'foo' ), 'srem again' );
136 cmp_ok
( $o->scard( $set ), '==', 0, 'scard' );
138 $o->sadd( 'test-set1', $_ ) foreach ( 'foo', 'bar', 'baz' );
139 $o->sadd( 'test-set2', $_ ) foreach ( 'foo', 'baz', 'xxx' );
141 my $inter = [ 'baz', 'foo' ];
143 is_deeply
( [ $o->sinter( 'test-set1', 'test-set2' ) ], $inter, 'siter' );
145 ok
( $o->sinterstore( 'test-set-inter', 'test-set1', 'test-set2' ), 'sinterstore' );
147 cmp_ok
( $o->scard( 'test-set-inter' ), '==', $#$inter + 1, 'cardinality of intersection' );
150 diag
"Multiple databases handling commands";
152 ok
( $o->select( 1 ), 'select' );
153 ok
( $o->select( 0 ), 'select' );
155 ok
( $o->move( 'foo', 1 ), 'move' );
156 ok
( ! $o->exists( 'foo' ), 'gone' );
158 ok
( $o->select( 1 ), 'select' );
159 ok
( $o->exists( 'foo' ), 'exists' );
161 ok
( $o->flushdb, 'flushdb' );
162 cmp_ok
( $o->dbsize, '==', 0, 'empty' );
167 ok
( $o->lpush( 'test-sort', $_ ), "put $_" ) foreach ( 1 .. 4 );
168 cmp_ok
( $o->llen( 'test-sort' ), '==', 4, 'llen' );
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' );
174 diag
"Persistence control commands";
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";
182 diag
"Remote server control commands";
184 ok
( my $info = $o->info, 'info' );
187 diag
"Connection handling";
189 ok
( $o->quit, 'quit' );