/[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 45 - (show annotations)
Mon Mar 23 10:42:29 2009 UTC (10 years, 4 months ago) by dpavlin
File MIME type: application/x-troff
File size: 4614 byte(s)
mget
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use Test::More tests => 95;
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 diag "Commands operating on string values";
19
20 ok( $o->set( foo => 'bar' ), 'set foo => bar' );
21
22 eval { $o->set( foo => 'bar', 1 ) };
23 ok( $@, 'set foo => bar new again failed' );
24
25 cmp_ok( $o->get( 'foo' ), 'eq', 'bar', 'get foo = bar' );
26
27 ok( $o->set( foo => 'baz' ), 'set foo => baz' );
28
29 cmp_ok( $o->get( 'foo' ), 'eq', 'baz', 'get foo = baz' );
30
31 ok( $o->set( 'test-undef' => 42 ), 'set test-undef' );
32 ok( $o->set( 'test-undef' => undef ), 'set undef' );
33 ok( ! defined $o->get( 'test-undef' ), 'get undef' );
34 diag $o->exists( 'test-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 diag "key: $key";
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 diag "dbsize: $nr_keys";
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 diag "Connection handling";
166
167 ok( $o->quit, 'quit' );

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26