/[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 38 - (show annotations)
Sun Mar 22 18:17:05 2009 UTC (11 years, 9 months ago) by dpavlin
File MIME type: application/x-troff
File size: 4313 byte(s)
select
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use Test::More tests => 88;
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 my @keys;
49
50 foreach ( 0 .. $key_next ) {
51 my $key = 'key-' . $_;
52 push @keys, $key;
53 ok( $o->set( $key => $_ ), "set $key" );
54 ok( $o->exists( $key ), "exists $key" );
55 cmp_ok( $o->get( $key ), 'eq', $_, "get $key" );
56 cmp_ok( $o->incr( 'key-next' ), '==', $_ + 1, 'incr' );
57 cmp_ok( $o->decr( 'key-left' ), '==', $key_next - $_ - 1, 'decr' );
58 }
59
60 cmp_ok( $o->get( 'key-next' ), '==', $key_next + 1, 'key-next' );
61
62 ok( $o->set('test-incrby', 0), 'test-incrby' );
63 ok( $o->set('test-decrby', 0), 'test-decry' );
64 foreach ( 1 .. 3 ) {
65 cmp_ok( $o->incr('test-incrby', 3), '==', $_ * 3, 'incrby 3' );
66 cmp_ok( $o->decr('test-decrby', 7), '==', -( $_ * 7 ), 'decrby 7' );
67 }
68
69 ok( $o->del( $_ ), "del $_" ) foreach map { "key-$_" } ( 'next', 'left' );
70 ok( ! $o->del('non-existing' ), 'del non-existing' );
71
72 cmp_ok( $o->type('foo'), 'eq', 'string', 'type' );
73
74 cmp_ok( $o->keys('key-*'), '==', $key_next + 1, 'key-*' );
75 is_deeply( [ $o->keys('key-*') ], [ @keys ], 'keys' );
76
77 ok( my $key = $o->randomkey, 'randomkey' );
78 diag "key: $key";
79
80 ok( $o->rename( 'test-incrby', 'test-renamed' ), 'rename' );
81 ok( $o->exists( 'test-renamed' ), 'exists test-renamed' );
82
83 eval { $o->rename( 'test-decrby', 'test-renamed', 1 ) };
84 ok( $@, 'rename to existing key' );
85
86 ok( my $nr_keys = $o->dbsize, 'dbsize' );
87 diag "dbsize: $nr_keys";
88
89 diag "Commands operating on lists";
90
91 my $list = 'test-list';
92
93 $o->del($list) && diag "cleanup $list from last run";
94
95 ok( $o->rpush( $list => "r$_" ), 'rpush' ) foreach ( 1 .. 3 );
96
97 ok( $o->lpush( $list => "l$_" ), 'lpush' ) foreach ( 1 .. 2 );
98
99 cmp_ok( $o->type($list), 'eq', 'list', 'type' );
100 cmp_ok( $o->llen($list), '==', 5, 'llen' );
101
102 is_deeply( [ $o->lrange( $list, 0, 1 ) ], [ 'l2', 'l1' ], 'lrange' );
103
104 ok( $o->ltrim( $list, 1, 2 ), 'ltrim' );
105 cmp_ok( $o->llen($list), '==', 2, 'llen after ltrim' );
106
107 cmp_ok( $o->lindex( $list, 0 ), 'eq', 'l1', 'lindex' );
108 cmp_ok( $o->lindex( $list, 1 ), 'eq', 'r1', 'lindex' );
109
110 ok( $o->lset( $list, 0, 'foo' ), 'lset' );
111 cmp_ok( $o->lindex( $list, 0 ), 'eq', 'foo', 'verified' );
112
113 ok( $o->lrem( $list, 1, 'foo' ), 'lrem' );
114 cmp_ok( $o->llen( $list ), '==', 1, 'llen after lrem' );
115
116 cmp_ok( $o->lpop( $list ), 'eq', 'r1', 'lpop' );
117
118 ok( ! $o->rpop( $list ), 'rpop' );
119
120
121 diag "Commands operating on sets";
122
123 my $set = 'test-set';
124 $o->del($set);
125
126 ok( $o->sadd( $set, 'foo' ), 'sadd' );
127 ok( ! $o->sadd( $set, 'foo' ), 'sadd' );
128 cmp_ok( $o->scard( $set ), '==', 1, 'scard' );
129 ok( $o->sismember( $set, 'foo' ), 'sismember' );
130
131 cmp_ok( $o->type( $set ), 'eq', 'set', 'type is set' );
132
133 ok( $o->srem( $set, 'foo' ), 'srem' );
134 ok( ! $o->srem( $set, 'foo' ), 'srem again' );
135 cmp_ok( $o->scard( $set ), '==', 0, 'scard' );
136
137 $o->sadd( 'test-set1', $_ ) foreach ( 'foo', 'bar', 'baz' );
138 $o->sadd( 'test-set2', $_ ) foreach ( 'foo', 'baz', 'xxx' );
139
140 my $inter = [ 'baz', 'foo' ];
141
142 is_deeply( [ $o->sinter( 'test-set1', 'test-set2' ) ], $inter, 'siter' );
143
144 ok( $o->sinterstore( 'test-set-inter', 'test-set1', 'test-set2' ), 'sinterstore' );
145
146 cmp_ok( $o->scard( 'test-set-inter' ), '==', $#$inter + 1, 'cardinality of intersection' );
147
148
149 diag "Multiple databases handling commands";
150
151 ok( $o->select( 1 ), 'select' );
152 ok( $o->select( 0 ), 'select' );
153
154
155 diag "Connection handling";
156
157 ok( $o->quit, 'quit' );

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26