Skip to content

Commit

Permalink
Merge pull request #16 from dwyl/testing
Browse files Browse the repository at this point in the history
Testing
  • Loading branch information
nelsonic authored Mar 12, 2019
2 parents 719d291 + e595017 commit dc8ef80
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 10 deletions.
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions coveralls.json
Original file line number Diff line number Diff line change
@@ -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/*"
]
}
5 changes: 4 additions & 1 deletion lib/store_finder/create_stores.ex
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/store_finder_web/controllers/page_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ defmodule StoreFinderWeb.PageController do
false ->
conn
|> put_flash(:error, "invalid postcode")
|> render("index.html")
|> redirect(to: "/")

decoded_response ->
lat = decoded_response["result"]["latitude"]
long = decoded_response["result"]["longitude"]
{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

Expand Down
4 changes: 0 additions & 4 deletions lib/store_finder_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<h1>Nearby Stores</h1>
<%= for {postcode, _lat, _long} <- @stores do %>
<p><%= postcode %></p>
<% end %>
11 changes: 9 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
23 changes: 23 additions & 0 deletions test/store_finder/create_stores_test.exs
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions test/store_finder/haversine_test.exs
Original file line number Diff line number Diff line change
@@ -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
21 changes: 20 additions & 1 deletion test/store_finder_web/controllers/page_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit dc8ef80

Please sign in to comment.