/[Redis.pre-github]/t/01-Redis.t
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /t/01-Redis.t

Parent Directory Parent Directory | Revision Log Revision Log


Revision 50 - (show annotations)
Mon Mar 23 11:40:50 2009 UTC (10 years, 4 months ago) by dpavlin
File MIME type: application/x-troff
File size: 4975 byte(s)
bgsave, lastsave, shutdown
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use Test::More tests => 106;
7
8 use lib 'lib';
9
10 BEGIN {
11 use_ok( 'Redis' );
12 }
13
14 ok( my $o = Redis->new(), 'new' );
15
16 ok( $o->ping, 'ping' );
17
18
19 diag "Commands operating on string values";
20
21 ok( $o->set( foo => 'bar' ), 'set foo => bar' );
22
23 eval { $o->set( foo => 'bar', 1 ) };
24 ok( $@, 'set foo => bar new again failed' );
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 ( 0 .. $key_next ) {
53 my $key = 'key-' . $_;
54 push @keys, $key;
55 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' );
59 cmp_ok( $o->decr( 'key-left' ), '==', $key_next - $_ - 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->incr('test-incrby', 3), '==', $_ * 3, 'incrby 3' );
68 cmp_ok( $o->decr('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
181 diag "Connection handling";
182
183 ok( $o->quit, 'quit' );

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26