machi/test/machi_pb_test.erl

91 lines
3 KiB
Erlang

%% -------------------------------------------------------------------
%%
%% Machi: a small village of replicated files
%%
%% Copyright (c) 2014-2015 Basho Technologies, Inc. All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------
-module(machi_pb_test).
-include("machi_pb.hrl").
-include("machi_projection.hrl").
-compile(export_all).
-ifdef(TEST).
-ifndef(PULSE).
-include_lib("eunit/include/eunit.hrl").
%% We don't expect any problems with these functions: nearly all
%% code executed is compiled from the PB definition file(s) and so
%% ought to be bug-free. Errors are likely to be caused by
%% changes in the spec but without corresponding changes to
%% message types/names here.
smoke_requests_test() ->
Echo0 = #mpb_request{req_id= <<"x">>,
echo=#mpb_echoreq{}},
Echo0 = encdec_request(Echo0),
Echo1 = #mpb_request{req_id= <<"x">>,
echo=#mpb_echoreq{message="Yo!"}},
Echo1 = encdec_request(Echo1),
ok.
smoke_responses_test() ->
R1 = #mpb_response{req_id= <<"x">>,
generic=#mpb_errorresp{code=7,
msg="foo",
extra= <<"bar">>}},
R1 = encdec_response(R1),
Files2 = [#mpb_fileinfo{file_size=X, file_name="foo."++integer_to_list(X)}
|| X <- lists:seq(1, 5)],
R2 = #mpb_response{req_id= <<"x">>,
list_files=#mpb_listfilesresp{status='OK',
files=Files2}},
R2 = encdec_response(R2),
ok.
smoke_p_srvr_test() ->
P1 = #p_srvr{name=a, address="localhost", port=5555,
props=[{dir,"./data.a"}]},
P1 = machi_pb_wrap:dec_p_srvr(
list_to_binary(machi_pb_wrap:enc_p_srvr(P1))),
ok.
smoke_projection_v1_test() ->
P1 = #p_srvr{name=a, address="localhost", port=5555,
props=[{dir,"./data.a"}]},
D = orddict:from_list([ {P1#p_srvr.name, P1} ]),
Proj1 = machi_projection:new(a, D, [a], [], [], [{property, 42}]),
Proj1 = machi_pb_wrap:dec_projection_v1(
machi_pb_wrap:enc_projection_v1(Proj1)),
ok.
encdec_request(M) ->
machi_pb:decode_mpb_request(
list_to_binary(machi_pb:encode_mpb_request(M))).
encdec_response(M) ->
machi_pb:decode_mpb_response(
list_to_binary(machi_pb:encode_mpb_response(M))).
-endif. % !PULSE
-endif. % TEST