/[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 45 - (hide annotations)
Mon Mar 23 10:42:29 2009 UTC (11 years ago) by dpavlin
File MIME type: application/x-troff
File size: 4614 byte(s)
mget
1 dpavlin 2 #!/usr/bin/perl
2    
3     use warnings;
4     use strict;
5    
6 dpavlin 45 use Test::More tests => 95;
7 dpavlin 2
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 dpavlin 38 diag "Commands operating on string values";
19    
20 dpavlin 5 ok( $o->set( foo => 'bar' ), 'set foo => bar' );
21 dpavlin 3
22 dpavlin 5 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 dpavlin 28 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 dpavlin 10 $o->del('non-existant');
38    
39 dpavlin 9 ok( ! $o->exists( 'non-existant' ), 'exists non-existant' );
40 dpavlin 4 ok( ! $o->get( 'non-existant' ), 'get non-existant' );
41    
42 dpavlin 7 ok( $o->set('key-next' => 0), 'key-next = 0' );
43    
44     my $key_next = 3;
45    
46 dpavlin 8 ok( $o->set('key-left' => $key_next), 'key-left' );
47    
48 dpavlin 45 is_deeply( [ $o->mget( 'foo', 'key-next', 'key-left' ) ], [ 'baz', 0, 3 ], 'mget' );
49    
50 dpavlin 12 my @keys;
51    
52 dpavlin 7 foreach ( 0 .. $key_next ) {
53 dpavlin 12 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 dpavlin 7 cmp_ok( $o->incr( 'key-next' ), '==', $_ + 1, 'incr' );
59 dpavlin 8 cmp_ok( $o->decr( 'key-left' ), '==', $key_next - $_ - 1, 'decr' );
60 dpavlin 4 }
61    
62 dpavlin 7 cmp_ok( $o->get( 'key-next' ), '==', $key_next + 1, 'key-next' );
63    
64     ok( $o->set('test-incrby', 0), 'test-incrby' );
65 dpavlin 8 ok( $o->set('test-decrby', 0), 'test-decry' );
66 dpavlin 7 foreach ( 1 .. 3 ) {
67     cmp_ok( $o->incr('test-incrby', 3), '==', $_ * 3, 'incrby 3' );
68 dpavlin 8 cmp_ok( $o->decr('test-decrby', 7), '==', -( $_ * 7 ), 'decrby 7' );
69 dpavlin 7 }
70    
71 dpavlin 12 ok( $o->del( $_ ), "del $_" ) foreach map { "key-$_" } ( 'next', 'left' );
72     ok( ! $o->del('non-existing' ), 'del non-existing' );
73 dpavlin 10
74 dpavlin 11 cmp_ok( $o->type('foo'), 'eq', 'string', 'type' );
75    
76 dpavlin 12 cmp_ok( $o->keys('key-*'), '==', $key_next + 1, 'key-*' );
77     is_deeply( [ $o->keys('key-*') ], [ @keys ], 'keys' );
78    
79 dpavlin 13 ok( my $key = $o->randomkey, 'randomkey' );
80     diag "key: $key";
81    
82 dpavlin 16 ok( $o->rename( 'test-incrby', 'test-renamed' ), 'rename' );
83 dpavlin 14 ok( $o->exists( 'test-renamed' ), 'exists test-renamed' );
84    
85 dpavlin 16 eval { $o->rename( 'test-decrby', 'test-renamed', 1 ) };
86 dpavlin 14 ok( $@, 'rename to existing key' );
87    
88 dpavlin 17 ok( my $nr_keys = $o->dbsize, 'dbsize' );
89     diag "dbsize: $nr_keys";
90    
91 dpavlin 18 diag "Commands operating on lists";
92    
93 dpavlin 22 my $list = 'test-list';
94 dpavlin 18
95 dpavlin 22 $o->del($list) && diag "cleanup $list from last run";
96 dpavlin 18
97 dpavlin 22 ok( $o->rpush( $list => "r$_" ), 'rpush' ) foreach ( 1 .. 3 );
98 dpavlin 18
99 dpavlin 22 ok( $o->lpush( $list => "l$_" ), 'lpush' ) foreach ( 1 .. 2 );
100 dpavlin 18
101 dpavlin 25 cmp_ok( $o->type($list), 'eq', 'list', 'type' );
102 dpavlin 22 cmp_ok( $o->llen($list), '==', 5, 'llen' );
103    
104     is_deeply( [ $o->lrange( $list, 0, 1 ) ], [ 'l2', 'l1' ], 'lrange' );
105    
106 dpavlin 23 ok( $o->ltrim( $list, 1, 2 ), 'ltrim' );
107 dpavlin 22 cmp_ok( $o->llen($list), '==', 2, 'llen after ltrim' );
108    
109 dpavlin 23 cmp_ok( $o->lindex( $list, 0 ), 'eq', 'l1', 'lindex' );
110     cmp_ok( $o->lindex( $list, 1 ), 'eq', 'r1', 'lindex' );
111    
112 dpavlin 24 ok( $o->lset( $list, 0, 'foo' ), 'lset' );
113     cmp_ok( $o->lindex( $list, 0 ), 'eq', 'foo', 'verified' );
114    
115 dpavlin 26 ok( $o->lrem( $list, 1, 'foo' ), 'lrem' );
116     cmp_ok( $o->llen( $list ), '==', 1, 'llen after lrem' );
117    
118 dpavlin 27 cmp_ok( $o->lpop( $list ), 'eq', 'r1', 'lpop' );
119    
120     ok( ! $o->rpop( $list ), 'rpop' );
121    
122 dpavlin 30
123 dpavlin 38 diag "Commands operating on sets";
124    
125 dpavlin 30 my $set = 'test-set';
126     $o->del($set);
127    
128     ok( $o->sadd( $set, 'foo' ), 'sadd' );
129     ok( ! $o->sadd( $set, 'foo' ), 'sadd' );
130 dpavlin 31 cmp_ok( $o->scard( $set ), '==', 1, 'scard' );
131 dpavlin 32 ok( $o->sismember( $set, 'foo' ), 'sismember' );
132 dpavlin 30
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 dpavlin 31 cmp_ok( $o->scard( $set ), '==', 0, 'scard' );
138 dpavlin 30
139 dpavlin 33 $o->sadd( 'test-set1', $_ ) foreach ( 'foo', 'bar', 'baz' );
140     $o->sadd( 'test-set2', $_ ) foreach ( 'foo', 'baz', 'xxx' );
141    
142 dpavlin 34 my $inter = [ 'baz', 'foo' ];
143 dpavlin 33
144 dpavlin 34 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 dpavlin 38
151     diag "Multiple databases handling commands";
152    
153     ok( $o->select( 1 ), 'select' );
154     ok( $o->select( 0 ), 'select' );
155    
156 dpavlin 40 ok( $o->move( 'foo', 1 ), 'move' );
157     ok( ! $o->exists( 'foo' ), 'gone' );
158 dpavlin 38
159 dpavlin 40 ok( $o->select( 1 ), 'select' );
160     ok( $o->exists( 'foo' ), 'exists' );
161    
162 dpavlin 41 ok( $o->flushdb, 'flushdb' );
163     cmp_ok( $o->dbsize, '==', 0, 'empty' );
164    
165 dpavlin 38 diag "Connection handling";
166    
167 dpavlin 2 ok( $o->quit, 'quit' );

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26