Skip to content

Commit

Permalink
chore: Add test coverage for API
Browse files Browse the repository at this point in the history
  • Loading branch information
kiosion committed Nov 8, 2023
1 parent ab98385 commit 453512f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
**/build
**/_build
**/deps
elixir-api/cover

# Dependencies
.pnp.*
Expand Down
2 changes: 1 addition & 1 deletion elixir-api/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions elixir-api/lib/routes/api/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
11 changes: 5 additions & 6 deletions elixir-api/lib/routes/api/v1/inc.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Router.Api.V1.Inc do
use Router.Api.Base
use Router.Api.Base, parse: true

options "/" do
conn
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion elixir-api/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 453512f

Please sign in to comment.