/[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 69 - (show annotations)
Wed Mar 17 18:22:09 2010 UTC (9 years, 6 months ago) by dpavlin
File MIME type: application/x-troff
File size: 5334 byte(s)
added use bytes to support utf-8 encoded strings

We are not round-tripping utf-8 encoding strings correctly in
this version. We will get string back wothout perl utf-8 flag

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26