Source

riak / src / jiak.erl

Diff from to

File src/jiak.erl

 -export([default_jiak_bucket_props/0]).
 -export([standard_sibling_merge/1]).
 
+-include_lib("eunit/include/eunit.hrl").
+
 %% @spec local_client() -> {ok, jiak_client()}|error_term()
 %% @doc Open a Riak client for modifying Jiak objects.
 %% @see riak:local_client/0
     L = sets:to_list(
           sets:union(sets:from_list(Lin), sets:from_list(Lacc))),
     {M, {{struct, O}, L}}.
+
+%%
+%% Testing
+%%
+
+sib_sort_test() ->
+    Dated = fun(D,N) ->
+                    {dict:store(<<"X-Riak-Last-Modified">>,
+                                httpd_util:rfc1123_date(D),
+                                dict:new()),
+                     N}
+            end,
+    A = Dated({{2009,8,28},{14,0,0}}, a),
+    B = Dated({{2009,8,28},{14,0,1}}, b),
+    C = Dated({{2009,8,28},{14,1,0}}, c),
+    D = Dated({{2009,8,28},{15,0,0}}, d),
+    E = Dated({{2009,8,29},{14,0,0}}, e),
+    F = Dated({{2009,9,28},{14,0,0}}, f),
+    G = Dated({{2010,8,28},{14,0,0}}, g),
+    [G,F,E,D,C,B,A] = sort_sibs([D,G,B,A,C,F,E]).
+
+merge_sibs_test_() ->
+    [fun merge_output_structure_t/0,
+     fun merge_metadata_t/0,
+     fun merge_fields_t/0,
+     fun merge_links_t/0].
+
+%% merge didn't fail
+merge_output_structure_t() ->
+    {_CM, {{struct, _CO}, _CL}} =
+        merge_sibs({dict:new(),{{struct,[]},[]}},
+                   {dict:new(),{{struct,[]},[]}}).
+
+%% metadata properly constructed
+merge_metadata_t() ->
+    {CM,_} = merge_sibs({dict:store(b, b, dict:store(c, b, dict:new())),
+                         {{struct,[]},[]}},
+                        {dict:store(a, a, dict:store(c, a, dict:new())),
+                         {{struct,[]},[]}}),
+    3 = dict:size(CM),
+    a = dict:fetch(a, CM),
+    b = dict:fetch(b, CM),
+    a = dict:fetch(c, CM).
+    
+%% fields properly constructed
+merge_fields_t() ->
+    {_,{{struct,CO},_}} = merge_sibs({dict:new(),
+                                      {{struct, [{b,b},{c,b}]},[]}},
+                                     {dict:new(),
+                                      {{struct, [{a,a},{c,a}]},[]}}),
+    3 = length(CO),
+    a = proplists:get_value(a, CO),
+    b = proplists:get_value(b, CO),
+    a = proplists:get_value(c, CO).
+
+%% links properly constructed
+merge_links_t() ->
+    {_,{_,CL}} = merge_sibs({dict:new(),{{struct,[]},[b,c]}},
+                            {dict:new(),{{struct,[]},[a,c]}}),
+    3 = length(CL),
+    true = lists:member(a, CL),
+    true = lists:member(b, CL),
+    true = lists:member(c, CL).
+
+standard_sibling_merge_test() ->
+    A = {dict:store(<<"X-Riak-Last-Modified">>,
+                    httpd_util:rfc1123_date({{2009,8,28},{14,0,0}}),
+                    dict:store(a,a,dict:store(x,a,dict:store(y,a,dict:new())))),
+         {{struct,[{a,a},{x,a},{y,a}]},[a,z]}},
+    B = {dict:store(<<"X-Riak-Last-Modified">>,
+                    httpd_util:rfc1123_date({{2009,8,28},{14,0,1}}),
+                    dict:store(b,b,dict:store(x,b,dict:store(z,b,dict:new())))),
+         {{struct,[{b,b},{x,b},{z,b}]},[b,z]}},
+    C = {dict:store(<<"X-Riak-Last-Modified">>,
+                    httpd_util:rfc1123_date({{2009,8,28},{14,0,2}}),
+                    dict:store(c,c,dict:store(y,c,dict:store(z,c,dict:new())))),
+         {{struct,[{c,c},{y,c},{z,c}]},[c,z]}},
+
+    %% structure okay
+    {MM, {{struct, MF}, ML}} = standard_sibling_merge([A,B,C]),
+    
+    %% metadata okay
+    7 = dict:size(MM),
+    ?assertEqual(httpd_util:rfc1123_date({{2009,8,28},{14,0,2}}),
+                 dict:fetch(<<"X-Riak-Last-Modified">>, MM)),
+    [a,b,c,b,c,c] = [ dict:fetch(Q, MM) || Q <- [a,b,c,x,y,z] ],
+    
+    %% fields okay
+    6 = length(MF),
+    [a,b,c,b,c,c] = [ proplists:get_value(Q, MF) || Q <- [a,b,c,x,y,z] ],
+    
+    %% links okay
+    4 = length(ML),
+    true = lists:all(fun(Q) -> lists:member(Q, ML) end, [a,b,c,z]).