diff --git a/.gitignore b/.gitignore index 0ea2c9a8c..b8da9b483 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ **/build **/_build **/deps +elixir-api/cover # Dependencies .pnp.* diff --git a/elixir-api/Makefile b/elixir-api/Makefile index ea633bd25..4965cd8ed 100644 --- a/elixir-api/Makefile +++ b/elixir-api/Makefile @@ -23,7 +23,7 @@ run: # Run docker container for 'production' dataset build test: SHELL:=/bin/bash test: install test: - @. .env && MIX_ENV=test mix test + @. .env && MIX_ENV=test mix test --cover cleanup: SHELL:=/bin/bash cleanup: diff --git a/elixir-api/lib/routes/api/base.ex b/elixir-api/lib/routes/api/base.ex index 375b69008..498666522 100644 --- a/elixir-api/lib/routes/api/base.ex +++ b/elixir-api/lib/routes/api/base.ex @@ -6,6 +6,7 @@ defmodule Router.Api.Base do defmacro __using__(opts \\ []) do auth = Keyword.get(opts, :auth, false) qp = Keyword.get(opts, :query_params, true) + parse = Keyword.get(opts, :parse, false) quote do use Plug.Router @@ -31,6 +32,10 @@ defmodule Router.Api.Base do plug(:fetch_query_params) end + if unquote(parse) do + plug(Plug.Parsers, parsers: [:json], json_decoder: Poison) + end + plug(:dispatch) unquote(opts) diff --git a/elixir-api/lib/routes/api/v1/inc.ex b/elixir-api/lib/routes/api/v1/inc.ex index 0e9c86935..f7d11ba32 100644 --- a/elixir-api/lib/routes/api/v1/inc.ex +++ b/elixir-api/lib/routes/api/v1/inc.ex @@ -1,5 +1,5 @@ defmodule Router.Api.V1.Inc do - use Router.Api.Base + use Router.Api.Base, parse: true options "/" do conn @@ -13,7 +13,7 @@ defmodule Router.Api.V1.Inc do @spec handle_inc(Plug.Conn.t(), binary(), map()) :: Plug.Conn.t() def handle_inc(conn, id, body) when is_map(body) do if body["target"] == "views" do - query = "*[_id = '#{id}'][0]" + query = "*[_id == '#{id}'][0]" {:ok, _pid} = try_increment_view_count(query) receive do @@ -43,11 +43,10 @@ defmodule Router.Api.V1.Inc do post "/:id" do if is_binary(conn.params["id"]) do try do - {:ok, body, conn} = read_body(conn) - - conn |> handle_inc(conn.params["id"], body |> Poison.decode!()) + handle_inc(conn, conn.params["id"], conn.body_params) rescue - _ -> + e -> + IO.puts("caught error: #{inspect(e)}") conn |> error_res(400, "Bad Request", "Invalid body content") end else diff --git a/elixir-api/mix.exs b/elixir-api/mix.exs index c264218d7..2765d7da4 100644 --- a/elixir-api/mix.exs +++ b/elixir-api/mix.exs @@ -9,7 +9,16 @@ defmodule Hexerei.MixProject do start_permanent: Mix.env() == :prod, deps: deps(), escript: escript(), - releases: releases() + releases: releases(), + test_coverage: [ + summary: [ + threshold: 50 + ], + ignore_modules: [ + Router.Api.Base, + Hexerei.HTTP.DefaultClient + ] + ] ] end