Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: fdef3a9760
Fetching contributors…

Cannot retrieve contributors at this time

file 93 lines (65 sloc) 1.643 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#!/usr/bin/env perl

use strict;
use warnings;

use lib 't';

use Test::More tests => 38;
use Data::Dumper;

use RedisClientTest;
use Redis::Client::Hash;


use_ok 'RedisClientTest';

eval {
    tie my %hash, 'Redis::Client::Hash';
};

like $@, qr/^Attribute/;
undef $@;

eval {
    tie my %hash, 'Redis::Client::Hash', key => 'blorb';
};

like $@, qr/^Attribute.+client/;
undef $@;

SKIP: {
    my $redis = RedisClientTest->server;

    skip 'No Redis server available', 35 unless $redis;
    
    ok $redis;
    isa_ok $redis, 'Redis::Client';

    my $val = 0;
    for( 'A' .. 'E' ) {
        my $result = $redis->hset( 'perl_redis_test_hash', $_ => ++$val );
        is $result, 1;
    }

    tie my %hash, 'Redis::Client::Hash', key => 'perl_redis_test_hash', client => $redis;

    for( 'F', 'G', 'H' ) {
        $hash{$_} = ++$val;
    }

    is $hash{F}, 6;
    is $hash{G}, 7;
    is $hash{H}, 8;

    is $redis->hget( 'perl_redis_test_hash', 'H' ), 8;

    my @keys = sort { $a cmp $b } keys %hash;

    my $i = 0;
    for ( 'A' .. 'H' ) {
        is $keys[$i++], $_;
    }

    ok exists $hash{A};
    ok !exists $hash{narf};

    my $dval = delete $hash{C};
    is $dval, 3;
    is $hash{C}, undef;

    my @vals = sort { $a <=> $b } values %hash;
    is $vals[0], 1;
    is $vals[1], 2;
    is $vals[2], 4;
    is $vals[3], 5;
    is $vals[4], 6;
    is $vals[5], 7;
    is $vals[6], 8;

    %hash = ( );

    my @keys2 = keys %hash;
    ok @keys2 == 0;

    my @vals2 = values %hash;
    ok @vals2 == 0;

    $hash{foo} = 42;
    ok exists $hash{foo};
    is $hash{foo}, 42;

    ok $redis->del( 'perl_redis_test_hash' );
}
Something went wrong with that request. Please try again.