Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#29] example using lists types #31

Merged
merged 1 commit into from
Jun 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions test/sumo_test_user.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
-module(sumo_test_user).

-behaviour(sumo_doc).

-type user() :: #{id => binary(),
attributes => list()}.

%% API
-export([
new/2,
id/1,
attributes/1
]).

%% sumo_doc callbacks
-export([sumo_schema/0, sumo_wakeup/1, sumo_sleep/1]).

%%%=============================================================================
%%% sumo_doc callbacks
%%%=============================================================================

-spec sumo_schema() -> sumo:schema().
sumo_schema() ->
sumo:new_schema(users, [
sumo:new_field(id, string, [id, not_null]),
sumo:new_field(attributes, custom, [{type, list}])
]).

-spec sumo_sleep(user()) -> sumo:model().
sumo_sleep(User) ->
User.

-spec sumo_wakeup(sumo:model()) -> user().
sumo_wakeup(Doc) ->
Doc.

%%%=============================================================================
%%% API
%%%=============================================================================

-spec new(binary(), list()) -> user().
new(Id, Attributes) ->
#{id => Id,
attributes => Attributes}.

-spec id(user()) -> string().
id(#{id := Val}) ->
Val.

-spec attributes(user()) -> list().
attributes(#{attributes := Val}) ->
Val.
3 changes: 2 additions & 1 deletion test/test.config
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
]},
{docs, [
{people, sumo_test_riak, #{module => sumo_test_people_riak}},
{purchases, sumo_test_riak, #{module => sumo_test_purchase_order}}
{purchases, sumo_test_riak, #{module => sumo_test_purchase_order}},
{users, sumo_test_riak, #{module => sumo_test_user}}
]},

{events, [
Expand Down
43 changes: 43 additions & 0 deletions test/users_SUITE.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
-module(users_SUITE).

%% CT
-export([
all/0,
init_per_suite/1,
end_per_suite/1,
find/1
]).

-type config() :: [{atom(), term()}].

%%%=============================================================================
%%% Common Test
%%%=============================================================================

-spec all() -> [atom()].
all() ->
[find].

-spec init_per_suite(config()) -> config().
init_per_suite(Config) ->
{ok, _} = application:ensure_all_started(sumo_db_riak),
Config.

-spec end_per_suite(config()) -> config().
end_per_suite(Config) ->
sumo:delete_all(users),
ok = sumo_db_riak:stop(),
Config.

-spec find(config()) -> ok.
find(_Config) ->
Id1 = <<"first">>,
Id2 = <<"second">>,
User1 = sumo_test_user:new(Id1, [#{this => is_a_map}, #{this => is_another}]),
User2 = sumo_test_user:new(Id2, ["A", "B"]),
sumo:persist(users, User1),
sumo:persist(users, User2),

User1 = sumo:fetch(users, Id1),
User2 = sumo:fetch(users, Id2),
ok.