Permalink
Browse files

Fix the JSON extractor, add unit test

  • Loading branch information...
1 parent 36b3a1b commit 1a2bc1a84722f12a1b8e636b49583bc3f454834f @rzezeski committed Oct 25, 2012
Showing with 53 additions and 3 deletions.
  1. +1 −1 Makefile
  2. +1 −0 rebar.config
  3. +1 −2 src/yz_json_extractor.erl
  4. +19 −0 test/test.json
  5. +31 −0 test/yz_json_extractor_tests.erl
View
@@ -1,6 +1,6 @@
REBAR = $(shell pwd)/rebar
-.PHONY: deps rel stagedevrel
+.PHONY: deps rel stagedevrel test
all: deps compile
View
@@ -1,5 +1,6 @@
{cover_enabled, true}.
{erl_opts, [debug_info, {parse_transform, lager_transform}]}.
+{eunit_opts, [verbose]}.
{deps,
[
@@ -93,8 +93,7 @@ extract_field(CurrentName) ->
-spec extract_element(binary()) -> fun((binary(), state()) -> state()).
extract_element(CurrentName) ->
fun(Element, S) ->
- Fields = S#state.fields,
- S#state{fields=[{CurrentName, Element}|Fields]}
+ extract_fields(CurrentName, Element, S)
end.
-spec new_field_name(binary() | undefined, binary(), binary()) -> binary().
View
@@ -0,0 +1,19 @@
+{"name":"ryan",
+ "age":29,
+ "pets":["smokey", "bandit"],
+ "books":[
+ {"title":"Introduction to Information Retrieval",
+ "authors":["Christopher D. Manning",
+ "Prabhakar Raghavan",
+ "Hinrich Schütze"]},
+ {"title":"Principles of Distributed Database Systems",
+ "authors":["M. Tamer Özsu", "Patrick Valduriez"]}
+ ],
+ "type":null,
+ "alive":true,
+ "married":false,
+ "a_number":1.1e6,
+ "lucky_numbers":[13,17,21],
+ "misc":{},
+ "kids":[]
+}
@@ -0,0 +1,31 @@
+-module(yz_json_extractor_tests).
+-compile(export_all).
+
+-include_lib("eunit/include/eunit.hrl").
+
+json_extract_test() ->
+ {ok, TestJSON} = file:read_file("../test/test.json"),
+ Result = yz_json_extractor:extract(TestJSON),
+ Expect =
+ [{<<"name">>,<<"ryan">>},
+ {<<"age">>,29},
+ {<<"pets">>,<<"smokey">>},
+ {<<"pets">>,<<"bandit">>},
+ {<<"books_title">>,<<"Introduction to Information Retrieval">>},
+ {<<"books_title">>,<<"Principles of Distributed Database Systems">>},
+ {<<"books_authors">>,<<"Christopher D. Manning">>},
+ {<<"books_authors">>,<<"Prabhakar Raghavan">>},
+ {<<"books_authors">>,<<"Hinrich Schütze">>},
+ {<<"books_authors">>,<<"M. Tamer Özsu">>},
+ {<<"books_authors">>,<<"Patrick Valduriez">>},
+ {<<"alive">>,true},
+ {<<"married">>,false},
+ {<<"a_number">>,1.1e6},
+ {<<"lucky_numbers">>,13},
+ {<<"lucky_numbers">>,17},
+ {<<"lucky_numbers">>,21}],
+
+ %% Do one at a time so failure is easier to understand
+ ?assertEqual(length(Expect), length(Result)),
+ Pairs = lists:zip(lists:sort(Expect), lists:sort(Result)),
+ [?assertEqual(E,R) || {E,R} <- Pairs].

0 comments on commit 1a2bc1a

Please sign in to comment.