diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..f7127a1 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +language: elixir +elixir: + - 1.8 +env: + - MIX_ENV=test +script: + - mix coveralls.json +after_success: + - bash <(curl -s https://codecov.io/bash) \ No newline at end of file diff --git a/coveralls.json b/coveralls.json new file mode 100644 index 0000000..29c5789 --- /dev/null +++ b/coveralls.json @@ -0,0 +1,12 @@ +{ + "coverage_options": { + "minimum_coverage": 95 + }, + "skip_files": [ + "lib/store_finder/application.ex", + "lib/store_finder/uk_postcodes.ex", + "lib/store_finder_web.ex", + "lib/store_finder_web/views/error_helpers", + "test/*" + ] +} \ No newline at end of file diff --git a/lib/store_finder/create_stores.ex b/lib/store_finder/create_stores.ex index 14ea04c..366d814 100644 --- a/lib/store_finder/create_stores.ex +++ b/lib/store_finder/create_stores.ex @@ -1,8 +1,10 @@ defmodule StoreFinder.CreateStores do + def get_random_postcodes(n)when not is_integer(n), do: "Need to pass integer" + def get_random_postcodes(n) when n < 1, do: "arg needs to be at least 1" + def get_random_postcodes(n) when n >= 200, do: "arg needs to be less than 200" def get_random_postcodes(n), do: get_random_postcodes(n, []) defp get_random_postcodes(0, acc), do: acc - defp get_random_postcodes(n, _acc) when n > 200, do: "Too many postcodes." defp get_random_postcodes(n, acc) do {:ok, res} = HTTPoison.get("https://api.postcodes.io/random/postcodes") @@ -19,6 +21,7 @@ defmodule StoreFinder.CreateStores do get_random_postcodes(n - 1, [{postcode, lat, long} | acc]) end + # A "here's one I made earlier" type list # so people do not need to call get_random_postcodes themselves # will also be useful for testing diff --git a/lib/store_finder_web/controllers/page_controller.ex b/lib/store_finder_web/controllers/page_controller.ex index 5309e7f..c1eb53f 100644 --- a/lib/store_finder_web/controllers/page_controller.ex +++ b/lib/store_finder_web/controllers/page_controller.ex @@ -11,7 +11,7 @@ defmodule StoreFinderWeb.PageController do false -> conn |> put_flash(:error, "invalid postcode") - |> render("index.html") + |> redirect(to: "/") decoded_response -> lat = decoded_response["result"]["latitude"] @@ -19,7 +19,7 @@ defmodule StoreFinderWeb.PageController do {within, _} = Integer.parse(within) nearby_stores = Haversine.find_nearest_stores({lat, long}, within) - render(conn, "store.html", stores: nearby_stores) + render(conn, "nearby_stores.html", stores: nearby_stores) end end diff --git a/lib/store_finder_web/router.ex b/lib/store_finder_web/router.ex index cfc3248..8ad0969 100644 --- a/lib/store_finder_web/router.ex +++ b/lib/store_finder_web/router.ex @@ -9,10 +9,6 @@ defmodule StoreFinderWeb.Router do plug :put_secure_browser_headers end - pipeline :api do - plug :accepts, ["json"] - end - scope "/", StoreFinderWeb do pipe_through :browser diff --git a/lib/store_finder_web/templates/page/store.html.eex b/lib/store_finder_web/templates/page/nearby_stores.html.eex similarity index 78% rename from lib/store_finder_web/templates/page/store.html.eex rename to lib/store_finder_web/templates/page/nearby_stores.html.eex index 4f9446d..e936158 100644 --- a/lib/store_finder_web/templates/page/store.html.eex +++ b/lib/store_finder_web/templates/page/nearby_stores.html.eex @@ -1,3 +1,4 @@ +

Nearby Stores

<%= for {postcode, _lat, _long} <- @stores do %>

<%= postcode %>

<% end %> \ No newline at end of file diff --git a/mix.exs b/mix.exs index 6fe096d..7e5548a 100644 --- a/mix.exs +++ b/mix.exs @@ -10,7 +10,13 @@ defmodule StoreFinder.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.json": :test + ] ] end @@ -43,7 +49,8 @@ defmodule StoreFinder.MixProject do {:gettext, "~> 0.11"}, {:jason, "~> 1.0"}, {:plug_cowboy, "~> 2.0"}, - {:httpoison, "~> 1.4"} + {:httpoison, "~> 1.4"}, + {:excoveralls, "~> 0.10", only: :test} ] end diff --git a/mix.lock b/mix.lock index 3facd66..a53d1da 100644 --- a/mix.lock +++ b/mix.lock @@ -7,6 +7,7 @@ "decimal": {:hex, :decimal, "1.7.0", "30d6b52c88541f9a66637359ddf85016df9eb266170d53105f02e4a67e00c5aa", [:mix], [], "hexpm"}, "ecto": {:hex, :ecto, "3.0.7", "44dda84ac6b17bbbdeb8ac5dfef08b7da253b37a453c34ab1a98de7f7e5fec7f", [:mix], [{:decimal, "~> 1.6", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: true]}], "hexpm"}, "ecto_sql": {:hex, :ecto_sql, "3.0.5", "7e44172b4f7aca4469f38d7f6a3da394dbf43a1bcf0ca975e958cb957becd74e", [:mix], [{:db_connection, "~> 2.0", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.0.6", [hex: :ecto, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.9.1", [hex: :mariaex, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.14.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.3.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm"}, + "excoveralls": {:hex, :excoveralls, "0.10.6", "e2b9718c9d8e3ef90bc22278c3f76c850a9f9116faf4ebe9678063310742edc2", [:mix], [{:hackney, "~> 1.13", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"}, "file_system": {:hex, :file_system, "0.2.6", "fd4dc3af89b9ab1dc8ccbcc214a0e60c41f34be251d9307920748a14bf41f1d3", [:mix], [], "hexpm"}, "gettext": {:hex, :gettext, "0.16.1", "e2130b25eebcbe02bb343b119a07ae2c7e28bd4b146c4a154da2ffb2b3507af2", [:mix], [], "hexpm"}, "hackney": {:hex, :hackney, "1.15.1", "9f8f471c844b8ce395f7b6d8398139e26ddca9ebc171a8b91342ee15a19963f4", [: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.4", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"}, diff --git a/test/store_finder/create_stores_test.exs b/test/store_finder/create_stores_test.exs new file mode 100644 index 0000000..175a685 --- /dev/null +++ b/test/store_finder/create_stores_test.exs @@ -0,0 +1,23 @@ +defmodule StoreFinder.CreateStoresTest do + use StoreFinderWeb.ConnCase + alias StoreFinder.CreateStores + + describe "tests get_random_postcodes" do + test "returns error messages when given incorrect value" do + too_few = CreateStores.get_random_postcodes(-1) + too_many = CreateStores.get_random_postcodes(200) + string = CreateStores.get_random_postcodes("hello") + + assert too_few == "arg needs to be at least 1" + assert too_many == "arg needs to be less than 200" + assert string == "Need to pass integer" + end + + test "returns a list of tuples when given int over 0" do + stores = CreateStores.get_random_postcodes(1) + + assert is_list(stores) + assert is_tuple(Enum.at(stores, 0)) + end + end +end diff --git a/test/store_finder/haversine_test.exs b/test/store_finder/haversine_test.exs new file mode 100644 index 0000000..6eaa2f3 --- /dev/null +++ b/test/store_finder/haversine_test.exs @@ -0,0 +1,19 @@ +defmodule StoreFinder.HaversineTest do + use StoreFinderWeb.ConnCase + alias StoreFinder.Haversine + + test "ets table exists when application starts" do + stores = :ets.match_object(:store_cache, :_) + assert is_list(stores) + refute Enum.empty?(stores) + end + + test "find_nearest_stores returns nearby stores" do + ll = {51.563729, -0.107694} + + # becase I am using dummy data I know how many venues are in a 1km and 10km + # radius of the given lat-long + assert Haversine.find_nearest_stores(ll, 1) == [] + assert Haversine.find_nearest_stores(ll, 10) |> length() == 5 + end +end \ No newline at end of file diff --git a/test/store_finder_web/controllers/page_controller_test.exs b/test/store_finder_web/controllers/page_controller_test.exs index 2f42ba6..a383c97 100644 --- a/test/store_finder_web/controllers/page_controller_test.exs +++ b/test/store_finder_web/controllers/page_controller_test.exs @@ -3,6 +3,25 @@ defmodule StoreFinderWeb.PageControllerTest do test "GET /", %{conn: conn} do conn = get(conn, "/") - assert html_response(conn, 200) =~ "Welcome to Phoenix!" + assert html_response(conn, 200) =~ "Search" + end + + describe "testing nearby-stores route" do + test "postcode with invalid format redirects to /", %{conn: conn} do + conn = post(conn, "/nearby-stores", postcode: "bad-postcode", within: "1") + assert redirected_to(conn, 302) == "/" + assert get_flash(conn, :error) == "invalid postcode" + end + + test "invalid postcode but with valid format redirects to /", %{conn: conn} do + conn = post(conn, "/nearby-stores", postcode: "z11zz", within: "1") + assert redirected_to(conn, 302) == "/" + assert get_flash(conn, :error) == "invalid postcode" + end + + test "Valid postcodes takes user to resutls page", %{conn: conn} do + conn = post(conn, "/nearby-stores", postcode: "n43hf", within: "1") + assert html_response(conn, 200) =~ "Nearby Stores" + end end end