Skip to content

Commit c727d52

Browse files
minor updates and addition of eunit tests for console file private functions
1 parent 2d114fb commit c727d52

File tree

1 file changed

+59
-6
lines changed

1 file changed

+59
-6
lines changed

src/yz_console.erl

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
2626
add_to_schema/1,
2727
remove_from_schema/1]).
2828

29-
-type field_data() :: {dynamicfield | field, list(), list()}.
29+
-ifdef(TEST).
30+
-include_lib("eunit/include/eunit.hrl").
31+
-endif.
32+
33+
-type field_data() :: {dynamicfield | field, list()}.
3034

3135
-define(LIST_TO_ATOM(L), list_to_atom(L)).
3236
-define(LIST_TO_BINARY(L), list_to_binary(L)).
@@ -71,7 +75,7 @@ switch_to_new_search([]) ->
7175
-spec create_schema([string()|string()]) -> ok | error.
7276
create_schema([Name, Path]) ->
7377
try
74-
RawSchema = read_schema(Path),
78+
{ok, RawSchema} = read_schema(Path),
7579
FMTName = ?LIST_TO_ATOM(Name),
7680
case yz_schema:store(?LIST_TO_BINARY(Name), RawSchema) of
7781
ok ->
@@ -130,9 +134,9 @@ remove_from_schema([Name, FieldName]) ->
130134
%% @doc Create field tagged tuple.
131135
-spec make_field(atom(), string(), list()) -> field_data().
132136
make_field(dynamicfield, FieldName, Options) ->
133-
{dynamicfield, [{name, FieldName}|merge(Options, ?FIELD_DEFAULTS)], []};
137+
{dynamicfield, [{name, FieldName}|merge(Options, ?FIELD_DEFAULTS)]};
134138
make_field(field, FieldName, Options) ->
135-
{field, [{name, FieldName}|merge(Options, ?FIELD_DEFAULTS)], []}.
139+
{field, [{name, FieldName}|merge(Options, ?FIELD_DEFAULTS)]}.
136140

137141
%% @doc Update schema with change(s).
138142
-spec update_schema(add | remove, string(), field_data() | string()) ->
@@ -158,12 +162,12 @@ parse_options([H|T], Acc) ->
158162
end.
159163

160164
%% @doc Reads and returns `RawSchema` from file path.
161-
-spec read_schema(string()) -> raw_schema() | schema_err().
165+
-spec read_schema(string()) -> {ok, raw_schema()} | schema_err().
162166
read_schema(Path) ->
163167
AbsPath = filename:absname(Path),
164168
case file:read_file(AbsPath) of
165169
{ok, RawSchema} ->
166-
RawSchema;
170+
{ok, RawSchema};
167171
{error, enoent} ->
168172
io:format("No such file or directory: ~s~n", [Path]),
169173
throw({fileReadError, enoent});
@@ -178,3 +182,52 @@ read_schema(Path) ->
178182
merge(Overriding, Other) ->
179183
lists:ukeymerge(1, lists:ukeysort(1, Overriding),
180184
lists:ukeysort(1, Other)).
185+
186+
%% ===================================================================
187+
%% EUnit tests
188+
%% ===================================================================
189+
190+
-ifdef(TEST).
191+
192+
read_schema_test() ->
193+
{ok, CurrentDir} = file:get_cwd(),
194+
MissingSchema = CurrentDir ++ "/foo.xml",
195+
GoodSchema = filename:join([?YZ_PRIV, "default_schema.xml"]),
196+
GoodOutput = read_schema(GoodSchema),
197+
?assertMatch({ok, _}, GoodOutput),
198+
{ok, RawSchema} = GoodOutput,
199+
?assert(is_binary(RawSchema)),
200+
?assertThrow({fileReadError, enoent}, read_schema(MissingSchema)).
201+
202+
parse_options_test() ->
203+
EmptyOptions = [],
204+
GoodOptions = ["foo=bar", "bar=baz", "foobar=barbaz"],
205+
BadOptions = ["hey", "foo"],
206+
?assertEqual([], parse_options(EmptyOptions)),
207+
%% first errored option will be thrown
208+
?assertThrow({error, {invalid_option, "hey"}}, parse_options(BadOptions)),
209+
?assertEqual([{"foobar", "barbaz"}, {"bar", "baz"}, {"foo", "bar"}],
210+
parse_options(GoodOptions)).
211+
212+
merge_test() ->
213+
Overriding = [{type, "integer"}, {stored, "true"}, {indexed, "true"}],
214+
Other = ?FIELD_DEFAULTS,
215+
Merged = merge(Overriding, Other),
216+
Expected = [{indexed, "true"}, {multiValued, "true"}, {stored, "true"},
217+
{type, "integer"}],
218+
?assertEqual(Expected, Merged).
219+
220+
make_field_test() ->
221+
Field = make_field(field, "person",
222+
parse_options(["foo=bar", "bar=baz"])),
223+
DynamicField = make_field(dynamicfield, "person", []),
224+
?assertMatch({field, [_|_]}, Field),
225+
?assertMatch({dynamicfield, [_|_]}, DynamicField),
226+
{_, FieldItems} = Field,
227+
{_, DynamicFieldItems} = DynamicField,
228+
%% + 2 new options + 1 for the field name
229+
?assertEqual(length(FieldItems), length(?FIELD_DEFAULTS) + 3),
230+
%% + 1 for the field name
231+
?assertEqual(length(DynamicFieldItems), length(?FIELD_DEFAULTS) + 1).
232+
233+
-endif.

0 commit comments

Comments
 (0)