/[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

Annotation of /t/01-Redis.t

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26