/[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 48 - (show annotations)
Mon Mar 23 11:33:21 2009 UTC (11 years ago) by dpavlin
File MIME type: application/x-troff
File size: 4917 byte(s)
save
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use Test::More tests => 103;
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 diag $o->exists( 'test-undef' );
36 ok( $o->exists( 'test-undef' ), 'exists undef' );
37
38 $o->del('non-existant');
39
40 ok( ! $o->exists( 'non-existant' ), 'exists non-existant' );
41 ok( ! $o->get( 'non-existant' ), 'get non-existant' );
42
43 ok( $o->set('key-next' => 0), 'key-next = 0' );
44
45 my $key_next = 3;
46
47 ok( $o->set('key-left' => $key_next), 'key-left' );
48
49 is_deeply( [ $o->mget( 'foo', 'key-next', 'key-left' ) ], [ 'baz', 0, 3 ], 'mget' );
50
51 my @keys;
52
53 foreach ( 0 .. $key_next ) {
54 my $key = 'key-' . $_;
55 push @keys, $key;
56 ok( $o->set( $key => $_ ), "set $key" );
57 ok( $o->exists( $key ), "exists $key" );
58 cmp_ok( $o->get( $key ), 'eq', $_, "get $key" );
59 cmp_ok( $o->incr( 'key-next' ), '==', $_ + 1, 'incr' );
60 cmp_ok( $o->decr( 'key-left' ), '==', $key_next - $_ - 1, 'decr' );
61 }
62
63 cmp_ok( $o->get( 'key-next' ), '==', $key_next + 1, 'key-next' );
64
65 ok( $o->set('test-incrby', 0), 'test-incrby' );
66 ok( $o->set('test-decrby', 0), 'test-decry' );
67 foreach ( 1 .. 3 ) {
68 cmp_ok( $o->incr('test-incrby', 3), '==', $_ * 3, 'incrby 3' );
69 cmp_ok( $o->decr('test-decrby', 7), '==', -( $_ * 7 ), 'decrby 7' );
70 }
71
72 ok( $o->del( $_ ), "del $_" ) foreach map { "key-$_" } ( 'next', 'left' );
73 ok( ! $o->del('non-existing' ), 'del non-existing' );
74
75 cmp_ok( $o->type('foo'), 'eq', 'string', 'type' );
76
77 cmp_ok( $o->keys('key-*'), '==', $key_next + 1, 'key-*' );
78 is_deeply( [ $o->keys('key-*') ], [ @keys ], 'keys' );
79
80 ok( my $key = $o->randomkey, 'randomkey' );
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
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
166 diag "Sorting";
167
168 ok( $o->lpush( 'test-sort', $_ ), "put $_" ) foreach ( 1 .. 4 );
169 cmp_ok( $o->llen( 'test-sort' ), '==', 4, 'llen' );
170
171 is_deeply( [ $o->sort( 'test-sort' ) ], [ 1,2,3,4 ], 'sort' );
172 is_deeply( [ $o->sort( 'test-sort DESC' ) ], [ 4,3,2,1 ], 'sort DESC' );
173
174
175 diag "Persistence control commands";
176
177 ok( $o->save, 'save' );
178
179
180 diag "Connection handling";
181
182 ok( $o->quit, 'quit' );

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26