2525 show_schema /1 ,
2626 add_to_schema /1 ,
2727 remove_from_schema /1 ]).
28+ -compile (export_all ).
29+ -ifdef (TEST ).
30+ -include_lib (" eunit/include/eunit.hrl" ).
31+ -endif .
2832
2933-type field_data () :: {dynamicfield | field , list (), list ()}.
3034
@@ -71,7 +75,7 @@ switch_to_new_search([]) ->
7175-spec create_schema ([string ()|string ()]) -> ok | error .
7276create_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 ().
132136make_field (dynamicfield , FieldName , Options ) ->
133- {dynamicfield , [{name , FieldName }|merge (Options , ? FIELD_DEFAULTS )], [] };
137+ {dynamicfield , [{name , FieldName }|merge (Options , ? FIELD_DEFAULTS )]};
134138make_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 ()) ->
@@ -163,7 +167,7 @@ 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,51 @@ read_schema(Path) ->
178182merge (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+ MissingSchema = filename :join ([? YZ_PRIV , " foo.xml" ]),
194+ GoodSchema = filename :join ([? YZ_PRIV , " default_schema.xml" ]),
195+ GoodOutput = read_schema (GoodSchema ),
196+ ? assertMatch ({ok , _ }, GoodOutput ),
197+ {ok , RawSchema } = GoodOutput ,
198+ ? assert (is_binary (RawSchema )),
199+ ? assertThrow ({fileReadError , enoent }, read_schema (MissingSchema )).
200+
201+ parse_options_test () ->
202+ EmptyOptions = [],
203+ GoodOptions = [" foo=bar" , " bar=baz" , " foobar=barbaz" ],
204+ BadOptions = [" hey" , " foo" ],
205+ ? assertEqual ([], parse_options (EmptyOptions )),
206+ % % first errored option will be thrown
207+ ? assertThrow ({error , {invalid_option , " hey" }}, parse_options (BadOptions )),
208+ ? assertEqual ([{" foobar" , " barbaz" }, {" bar" , " baz" }, {" foo" , " bar" }],
209+ parse_options (GoodOptions )).
210+
211+ merge_test () ->
212+ Overriding = [{type , " integer" }, {stored , " true" }, {indexed , " true" }],
213+ Other = ? FIELD_DEFAULTS ,
214+ Merged = merge (Overriding , Other ),
215+ Expected = [{indexed , " true" }, {multiValued , " true" }, {stored , " true" },
216+ {type , " integer" }],
217+ ? assertEqual (Expected , Merged ).
218+
219+ make_field_test () ->
220+ Field = make_field (field , " person" ,
221+ parse_options ([" foo=bar" , " bar=baz" ])),
222+ DynamicField = make_field (dynamicfield , " person" , []),
223+ ? assertMatch ({field , [_ |_ ]}, Field ),
224+ ? assertMatch ({dynamicfield , [_ |_ ]}, DynamicField ),
225+ {_ , FieldItems } = Field ,
226+ {_ , DynamicFieldItems } = DynamicField ,
227+ % % + 2 new options + 1 for the field name
228+ ? assertEqual (length (FieldItems ), length (? FIELD_DEFAULTS ) + 3 ),
229+ % % + 1 for the field name
230+ ? assertEqual (length (DynamicFieldItems ), length (? FIELD_DEFAULTS ) + 1 ).
231+
232+ -endif .
0 commit comments