Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: fef214ddab
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 131 lines (112 sloc) 6.175 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
-module(erldis_tests).

-include_lib("eunit/include/eunit.hrl").
-include("erldis.hrl").

quit_test() ->
{ok, Client} = erldis:connect("localhost", 6379),
?assertEqual(shutdown, erldis:quit(Client)),
false = is_process_alive(Client).

utils_test() ->
?assertEqual(<<"1">>, erldis_binaries:to_binary(1)),
?assertEqual(<<"atom">>, erldis_binaries:to_binary(atom)),
?assertEqual(<<"1 2 3">>, erldis_client:format([[1, 2, 3]])),
?assertEqual(<<"1 2 3\r\n4 5 6">>, erldis_client:format([[1,2,3], [4,5,6]])).

basic_test() ->
{ok, Client} = erldis:connect("localhost", 6379),
?assertEqual(ok, erldis:flushdb(Client)),

?assertEqual(nil, erldis:get(Client, <<"pippo">>)),
ok = erldis:set(Client, <<"hello">>, <<"kitty!">>),
?assert(erldis:setnx(Client, <<"foo">>, <<"bar">>)),
?assertNot(erldis:setnx(Client, <<"foo">>, <<"bar">>)),

?assert(erldis:exists(Client, <<"hello">>)),
?assert(erldis:exists(Client, <<"foo">>)),
?assertEqual(<<"bar">>, erldis:get(Client, <<"foo">>)),
?assertEqual([<<"kitty!">>, <<"bar">>], erldis:mget(Client, [<<"hello">>, <<"foo">>])),
?assertEqual([<<"foo">>], erldis:keys(Client, <<"f*">>)),

?assert(erldis:del(Client, <<"hello">>)),
?assert(erldis:del(Client, <<"foo">>)),
?assertNot(erldis:exists(Client, <<"hello">>)),
?assertNot(erldis:exists(Client, <<"foo">>)),

?assertEqual(shutdown, erldis:quit(Client)).

set_test() ->
{ok, Client} = erldis:connect("localhost", 6379),
?assertEqual(ok, erldis:flushdb(Client)),
erldis:sadd(Client, <<"set">>, <<"toto">>),
?assertEqual([<<"toto">>], erldis:smembers(Client, <<"set">>)),
erldis:srem(Client, <<"set">>, <<"toto">>),
?assertEqual([], erldis:smembers(Client, <<"set">>)),
?assertEqual(shutdown, erldis:quit(Client)).

list_test() ->
{ok, Client} = erldis:connect("localhost", 6379),
?assertEqual(ok, erldis:flushdb(Client)),

?assertEqual([], erldis:lrange(Client, <<"foo">>, 1, 2)),
ok = erldis:rpush(Client, <<"a_list">>, <<"1">>),
ok = erldis:rpush(Client, <<"a_list">>, <<"2">>),
ok = erldis:rpush(Client, <<"a_list">>, <<"3">>),
ok = erldis:rpush(Client, <<"a_list">>, <<"1">>),
?assertEqual(1, erldis:lrem(Client, <<"a_list">>, 1, <<"1">>)),
?assertEqual([<<"2">>, <<"3">>, <<"1">>], erldis:lrange(Client, <<"a_list">>, 0, 2)),
?assertEqual([<<"1">>, <<"2">>, <<"3">>], erldis:sort(Client, <<"a_list">>)),
?assertEqual([<<"3">>, <<"2">>, <<"1">>], erldis:sort(Client, <<"a_list">>, <<"DESC">>)),
?assertEqual([<<"1">>, <<"2">>], erldis:sort(Client, <<"a_list">>, <<"LIMIT 0 2 ASC">>)),

?assertEqual(shutdown, erldis:quit(Client)).

zset_test() ->
{ok, Client} = erldis:connect("localhost", 6379),
?assertEqual(ok, erldis:flushdb(Client)),

?assertEqual(0, erldis:zcard(Client, <<"foo">>)),
?assertEqual([], erldis:zrange(Client, <<"foo">>, 0, 1)),
?assertEqual(0, erldis:zscore(Client, <<"foo">>, <<"elem1">>)),

?assertEqual(true, erldis:zadd(Client, <<"foo">>, 5, <<"elem1">>)),
?assertEqual([<<"elem1">>], erldis:zrange(Client, <<"foo">>, 0, 1)),
?assertEqual([<<"elem1">>], erldis:zrevrange(Client, <<"foo">>, 0, 1)),
?assertEqual([{<<"elem1">>, 5}], erldis:zrange_withscores(Client, <<"foo">>, 0, 1)),
?assertEqual([{<<"elem1">>, 5}], erldis:zrevrange_withscores(Client, <<"foo">>, 0, 1)),
?assertEqual(false, erldis:zadd(Client, <<"foo">>, 6, <<"elem1">>)),
?assertEqual(1, erldis:zcard(Client, <<"foo">>)),
?assertEqual(6, erldis:zscore(Client, <<"foo">>, <<"elem1">>)),
?assertEqual(8, erldis:zincrby(Client, <<"foo">>, 2, <<"elem1">>)),
% can use list keys & values too
?assertEqual(true, erldis:zadd(Client, "foo", 1.5, "a-elem")),
?assertEqual(2, erldis:zcard(Client, "foo")),
?assertEqual(1.5, erldis:zscore(Client, "foo", "a-elem")),
?assertEqual([<<"a-elem">>, <<"elem1">>], erldis:zrange(Client, "foo", 0, 2)),
?assertEqual([<<"elem1">>, <<"a-elem">>], erldis:zrevrange(Client, "foo", 0, 2)),
?assertEqual([{<<"a-elem">>, 1.5}, {<<"elem1">>, 8}], erldis:zrange_withscores(Client, "foo", 0, 2)),
?assertEqual([{<<"elem1">>, 8}, {<<"a-elem">>, 1.5}], erldis:zrevrange_withscores(Client, "foo", 0, 2)),
?assertEqual([<<"a-elem">>], erldis:zrangebyscore(Client, "foo", 1.0, 2.0)),
?assertEqual([<<"a-elem">>], erldis:zrangebyscore(Client, "foo", 1, 10, 0, 1)),
?assertEqual([<<"a-elem">>, <<"elem1">>], erldis:zrangebyscore(Client, "foo", 1, 10, 0, 2)),
?assertEqual([<<"elem1">>], erldis:zrangebyscore(Client, "foo", 1, 10, 1, 2)),
?assertEqual([], erldis:zrangebyscore(Client, "foo", 1, 10, 2, 2)),
?assertEqual(1, erldis:zremrangebyscore(Client, "foo", 1, 2)),
?assertEqual(false, erldis:zrem(Client, "foo", "a-elem")),
?assertEqual(1, erldis:zcard(Client, "foo")),
?assertEqual([<<"elem1">>], erldis:zrevrange(Client, "foo", 0, 1)),

?assertEqual(true, erldis:zrem(Client, <<"foo">>, <<"elem1">>)),
?assertEqual(false, erldis:zrem(Client, <<"foo">>, <<"elem1">>)),
?assertEqual(0, erldis:zcard(Client, <<"foo">>)),
?assertEqual([], erldis:zrange(Client, <<"foo">>, 0, 2)),

?assertEqual(shutdown, erldis:quit(Client)).

% inline_tests(Client) ->
% [?_assertMatch(ok, erldis:set(Client, <<"hello">>, <<"kitty!">>)),
% ?_assertMatch(false, erldis:setnx(Client, <<"hello">>, <<"kitty!">>)),
% ?_assertMatch(true, erldis:exists(Client, <<"hello">>)),
% ?_assertMatch(true, erldis:del(Client, <<"hello">>)),
% ?_assertMatch(false, erldis:exists(Client, <<"hello">>)),
%
% ?_assertMatch(true, erldis:setnx(Client, <<"hello">>, <<"kitty!">>)),
% ?_assertMatch(true, erldis:exists(Client, <<"hello">>)),
% ?_assertMatch("kitty!">>, erldis:get(Client, <<"hello">>)),
% ?_assertMatch(true, erldis:del(Client, <<"hello">>)),
%
%
% ?_assertMatch(1, erldis:incr(Client, <<"pippo">>))
% ,?_assertMatch(2, erldis:incr(Client, <<"pippo">>))
% ,?_assertMatch(1, erldis:decr(Client, <<"pippo">>))
% ,?_assertMatch(0, erldis:decr(Client, <<"pippo">>))
% ,?_assertMatch(-1, erldis:decr(Client, <<"pippo">>))
%
% ,?_assertMatch(6, erldis:incrby(Client, <<"pippo">>, 7))
% ,?_assertMatch(2, erldis:decrby(Client, <<"pippo">>, 4))
% ,?_assertMatch(-2, erldis:decrby(Client, <<"pippo">>, 4))
% ,?_assertMatch(true, erldis:del(Client, <<"pippo">>))
% ].
Something went wrong with that request. Please try again.