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