From 59f4e653f836a6eef7a6a67659ffb8ab540ee8b0 Mon Sep 17 00:00:00 2001 From: SimonLab Date: Mon, 20 Apr 2020 15:20:58 +0100 Subject: [PATCH] Update tests to check tags on captures, #14 --- .travis.yml | 22 ++++++++++++++++++++++ lib/app_api/captures.ex | 11 ++++++++++- mix.exs | 16 +++++++++++++--- mix.lock | 1 + test/app_api/captures_test.exs | 17 +++++++---------- 5 files changed, 53 insertions(+), 14 deletions(-) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..9ca03be --- /dev/null +++ b/.travis.yml @@ -0,0 +1,22 @@ +language: elixir +elixir: + - 1.9 +otp_release: + - 22.1.8 +env: + - MIX_ENV=test +cache: + directories: + - _build + - deps +services: + - postgresql +env: + global: + - MIX_ENV=test +before_script: + - mix do ecto.create, ecto.migrate +script: + - mix cover +after_success: + - bash <(curl -s https://codecov.io/bash) \ No newline at end of file diff --git a/lib/app_api/captures.ex b/lib/app_api/captures.ex index 5f36bab..f50b13f 100644 --- a/lib/app_api/captures.ex +++ b/lib/app_api/captures.ex @@ -8,6 +8,7 @@ defmodule AppApi.Captures do alias AppApi.Captures.Capture alias AppApi.Timers.Timer + alias AppApi.Tags.Tag @doc """ Returns the list of captures. @@ -20,6 +21,8 @@ defmodule AppApi.Captures do """ def list_captures do Repo.all(Capture) + |> Repo.preload(timers: from(t in Timer, order_by: [desc: t.inserted_at])) + |> Repo.preload(tags: from(t in Tag, order_by: [asc: t.text])) end @doc """ @@ -39,6 +42,7 @@ defmodule AppApi.Captures do def get_capture!(id) do Repo.get!(Capture, id) |> Repo.preload(timers: from(t in Timer, order_by: [desc: t.inserted_at])) + |> Repo.preload(tags: from(t in Tag, order_by: [asc: t.text])) end def get_capture_by_id_person(id_person) do @@ -46,7 +50,10 @@ defmodule AppApi.Captures do from c in Capture, where: c.id_person == ^id_person, order_by: [desc: c.inserted_at], - preload: [timers: ^from(t in Timer, order_by: [desc: t.inserted_at])] + preload: [ + timers: ^from(t in Timer, order_by: [desc: t.inserted_at]), + tags: ^from(t in Tag, order_by: [asc: t.text]) + ] Repo.all(query) end @@ -68,6 +75,7 @@ defmodule AppApi.Captures do |> Capture.changeset(attrs) |> Repo.insert!() |> Repo.preload(timers: from(t in Timer, order_by: [desc: t.inserted_at])) + |> Repo.preload(tags: from(t in Tag, order_by: [asc: t.text])) end @doc """ @@ -87,6 +95,7 @@ defmodule AppApi.Captures do |> Capture.changeset(attrs) |> Repo.update!() |> Repo.preload(timers: from(t in Timer, order_by: [desc: t.inserted_at])) + |> Repo.preload(tags: from(t in Tag, order_by: [asc: t.text])) end @doc """ diff --git a/mix.exs b/mix.exs index fa3480e..ca732b3 100644 --- a/mix.exs +++ b/mix.exs @@ -10,7 +10,14 @@ defmodule AppApi.MixProject do compilers: [:phoenix, :gettext] ++ Mix.compilers(), start_permanent: Mix.env() == :prod, aliases: aliases(), - deps: deps() + deps: deps(), + test_coverage: [tool: ExCoveralls], + preferred_cli_env: [ + coveralls: :test, + "coveralls.detail": :test, + "coveralls.post": :test, + "coveralls.html": :test + ] ] end @@ -43,7 +50,8 @@ defmodule AppApi.MixProject do {:plug_cowboy, "~> 2.0"}, {:cors_plug, "~> 2.0"}, {:httpoison, "~> 1.6"}, - {:poison, "~> 4.0"} + {:poison, "~> 4.0"}, + {:excoveralls, "~> 0.12.2", only: [:dev, :test]} ] end @@ -57,7 +65,9 @@ defmodule AppApi.MixProject do [ "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"], "ecto.reset": ["ecto.drop", "ecto.setup"], - test: ["ecto.create --quiet", "ecto.migrate", "test"] + test: ["ecto.create --quiet", "ecto.migrate", "test"], + cover: ["coveralls.json"], + "cover.html": ["coveralls.html"] ] end end diff --git a/mix.lock b/mix.lock index 6c91314..3b5cb3d 100644 --- a/mix.lock +++ b/mix.lock @@ -8,6 +8,7 @@ "decimal": {:hex, :decimal, "1.8.1", "a4ef3f5f3428bdbc0d35374029ffcf4ede8533536fa79896dd450168d9acdf3c", [:mix], [], "hexpm", "3cb154b00225ac687f6cbd4acc4b7960027c757a5152b369923ead9ddbca7aec"}, "ecto": {:hex, :ecto, "3.3.2", "002aa428c752a8ee4bb65baa9d1041f0514d7435d2e21d58cb6aa69a1076721d", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "c651e3ea0919241da314f518404b84449c8569525c4d4cf0f3d16f1d5d0a560c"}, "ecto_sql": {:hex, :ecto_sql, "3.3.3", "7d8962d39f16181c1df1bbd0f64aa392bd9ce0b9f8ff5ff21d43dca3d624eee7", [:mix], [{:db_connection, "~> 2.2", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.4 or ~> 3.3.2", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.3.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "3d5b8e14836d930448d79ab6ac325501c3c62caed83c34791d5af77718766795"}, + "excoveralls": {:hex, :excoveralls, "0.12.3", "2142be7cb978a3ae78385487edda6d1aff0e482ffc6123877bb7270a8ffbcfe0", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "568a3e616c264283f5dea5b020783ae40eef3f7ee2163f7a67cbd7b35bcadada"}, "gettext": {:hex, :gettext, "0.17.4", "f13088e1ec10ce01665cf25f5ff779e7df3f2dc71b37084976cf89d1aa124d5c", [:mix], [], "hexpm", "3c75b5ea8288e2ee7ea503ff9e30dfe4d07ad3c054576a6e60040e79a801e14d"}, "hackney": {:hex, :hackney, "1.15.2", "07e33c794f8f8964ee86cebec1a8ed88db5070e52e904b8f12209773c1036085", [:rebar3], [{:certifi, "2.5.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.5", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "e0100f8ef7d1124222c11ad362c857d3df7cb5f4204054f9f0f4a728666591fc"}, "httpoison": {:hex, :httpoison, "1.6.2", "ace7c8d3a361cebccbed19c283c349b3d26991eff73a1eaaa8abae2e3c8089b6", [:mix], [{:hackney, "~> 1.15 and >= 1.15.2", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "aa2c74bd271af34239a3948779612f87df2422c2fdcfdbcec28d9c105f0773fe"}, diff --git a/test/app_api/captures_test.exs b/test/app_api/captures_test.exs index f8a0781..c0af23f 100644 --- a/test/app_api/captures_test.exs +++ b/test/app_api/captures_test.exs @@ -6,17 +6,14 @@ defmodule AppApi.CapturesTest do describe "captures" do alias AppApi.Captures.Capture - @valid_attrs %{completed: true, id_person: 42, text: "some text"} - @update_attrs %{completed: false, id_person: 43, text: "some updated text"} - @invalid_attrs %{completed: nil, id_person: nil, text: nil} + @valid_attrs %{completed: true, id_person: 42, text: "some text", tags: ""} + @update_attrs %{completed: false, id_person: 43, text: "some updated text", tags: "" } + @invalid_attrs %{completed: nil, id_person: nil, text: nil, tags: ""} def capture_fixture(attrs \\ %{}) do - {:ok, capture} = attrs |> Enum.into(@valid_attrs) |> Captures.create_capture() - - capture end test "list_captures/0 returns all captures" do @@ -30,19 +27,19 @@ defmodule AppApi.CapturesTest do end test "create_capture/1 with valid data creates a capture" do - assert {:ok, %Capture{} = capture} = Captures.create_capture(@valid_attrs) + assert %Capture{} = capture = Captures.create_capture(@valid_attrs) assert capture.completed == true assert capture.id_person == 42 assert capture.text == "some text" end test "create_capture/1 with invalid data returns error changeset" do - assert {:error, %Ecto.Changeset{}} = Captures.create_capture(@invalid_attrs) + assert catch_error(Captures.create_capture(@invalid_attrs)) end test "update_capture/2 with valid data updates the capture" do capture = capture_fixture() - assert {:ok, %Capture{} = capture} = Captures.update_capture(capture, @update_attrs) + assert %Capture{} = capture = Captures.update_capture(capture, @update_attrs) assert capture.completed == false assert capture.id_person == 43 assert capture.text == "some updated text" @@ -50,7 +47,7 @@ defmodule AppApi.CapturesTest do test "update_capture/2 with invalid data returns error changeset" do capture = capture_fixture() - assert {:error, %Ecto.Changeset{}} = Captures.update_capture(capture, @invalid_attrs) + assert catch_error(Captures.update_capture(capture, @invalid_attrs)) assert capture == Captures.get_capture!(capture.id) end