X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=t%2F01-Redis.t;h=81577731a1eeb51022629fc2e517060f088e5834;hb=fbd7d22719c793e16bf4230b0b3ef4cef3403fc4;hp=211f743e7ca792bc7b6f530e061383ba02ff2ddb;hpb=1c54c8b4b9091cabeacaa663c64deb3c6e8d72eb;p=perl-Redis.git diff --git a/t/01-Redis.t b/t/01-Redis.t index 211f743..8157773 100755 --- a/t/01-Redis.t +++ b/t/01-Redis.t @@ -3,8 +3,9 @@ use warnings; use strict; -use Test::More tests => 106; -use Data::Dump qw/dump/; +use Test::More tests => 111; +use Test::Exception; +use Data::Dumper; use lib 'lib'; @@ -16,23 +17,29 @@ ok( my $o = Redis->new(), 'new' ); ok( $o->ping, 'ping' ); +ok( $o = Redis->new( server => 'localhost:6379' ), 'new with server' ); diag "Commands operating on string values"; ok( $o->set( foo => 'bar' ), 'set foo => bar' ); -eval { $o->set( foo => 'bar', 1 ) }; -ok( $@, 'set foo => bar new again failed' ); +ok( ! $o->setnx( foo => 'bar' ), 'setnx foo => bar fails' ); cmp_ok( $o->get( 'foo' ), 'eq', 'bar', 'get foo = bar' ); +ok( $o->set( foo => '' ), 'set foo => ""' ); + +cmp_ok( $o->get( 'foo' ), 'eq', '', 'get foo = ""' ); + ok( $o->set( foo => 'baz' ), 'set foo => baz' ); cmp_ok( $o->get( 'foo' ), 'eq', 'baz', 'get foo = baz' ); +my $euro = "\x{20ac}"; +ok( $o->set( utf8 => $euro ), 'set utf8' ); +cmp_ok( $o->get( 'utf8' ), 'eq', $euro, 'get utf8' ); + ok( $o->set( 'test-undef' => 42 ), 'set test-undef' ); -ok( $o->set( 'test-undef' => undef ), 'set undef' ); -ok( ! defined $o->get( 'test-undef' ), 'get undef' ); ok( $o->exists( 'test-undef' ), 'exists undef' ); $o->del('non-existant'); @@ -50,14 +57,14 @@ is_deeply( [ $o->mget( 'foo', 'key-next', 'key-left' ) ], [ 'baz', 0, 3 ], 'mget my @keys; -foreach ( 0 .. $key_next ) { - my $key = 'key-' . $_; +foreach my $id ( 0 .. $key_next ) { + my $key = 'key-' . $id; push @keys, $key; - ok( $o->set( $key => $_ ), "set $key" ); + ok( $o->set( $key => $id ), "set $key" ); ok( $o->exists( $key ), "exists $key" ); - cmp_ok( $o->get( $key ), 'eq', $_, "get $key" ); - cmp_ok( $o->incr( 'key-next' ), '==', $_ + 1, 'incr' ); - cmp_ok( $o->decr( 'key-left' ), '==', $key_next - $_ - 1, 'decr' ); + cmp_ok( $o->get( $key ), 'eq', $id, "get $key" ); + cmp_ok( $o->incr( 'key-next' ), '==', $id + 1, 'incr' ); + cmp_ok( $o->decr( 'key-left' ), '==', $key_next - $id - 1, 'decr' ); } cmp_ok( $o->get( 'key-next' ), '==', $key_next + 1, 'key-next' ); @@ -65,8 +72,8 @@ cmp_ok( $o->get( 'key-next' ), '==', $key_next + 1, 'key-next' ); ok( $o->set('test-incrby', 0), 'test-incrby' ); ok( $o->set('test-decrby', 0), 'test-decry' ); foreach ( 1 .. 3 ) { - cmp_ok( $o->incr('test-incrby', 3), '==', $_ * 3, 'incrby 3' ); - cmp_ok( $o->decr('test-decrby', 7), '==', -( $_ * 7 ), 'decrby 7' ); + cmp_ok( $o->incrby('test-incrby', 3), '==', $_ * 3, 'incrby 3' ); + cmp_ok( $o->decrby('test-decrby', 7), '==', -( $_ * 7 ), 'decrby 7' ); } ok( $o->del( $_ ), "del $_" ) foreach map { "key-$_" } ( 'next', 'left' ); @@ -139,7 +146,7 @@ cmp_ok( $o->scard( $set ), '==', 0, 'scard' ); $o->sadd( 'test-set1', $_ ) foreach ( 'foo', 'bar', 'baz' ); $o->sadd( 'test-set2', $_ ) foreach ( 'foo', 'baz', 'xxx' ); -my $inter = [ 'baz', 'foo' ]; +my $inter = [ 'foo', 'baz' ]; is_deeply( [ $o->sinter( 'test-set1', 'test-set2' ) ], $inter, 'siter' ); @@ -169,7 +176,7 @@ ok( $o->lpush( 'test-sort', $_ ), "put $_" ) foreach ( 1 .. 4 ); cmp_ok( $o->llen( 'test-sort' ), '==', 4, 'llen' ); is_deeply( [ $o->sort( 'test-sort' ) ], [ 1,2,3,4 ], 'sort' ); -is_deeply( [ $o->sort( 'test-sort DESC' ) ], [ 4,3,2,1 ], 'sort DESC' ); +is_deeply( [ $o->sort( 'test-sort', 'DESC' ) ], [ 4,3,2,1 ], 'sort DESC' ); diag "Persistence control commands"; @@ -183,8 +190,13 @@ diag "shutdown not tested"; diag "Remote server control commands"; ok( my $info = $o->info, 'info' ); -diag dump( $info ); +isa_ok( $info, 'HASH' ); +diag Dumper( $info ); diag "Connection handling"; ok( $o->quit, 'quit' ); + +throws_ok sub { Redis->new(server => '127.0.0.1:1') }, + qr/Could not connect to Redis server at 127[.]0[.]0[.]1:1:/, + 'Failed connection throws exception';