Skip to content

Commit 74d48ac

Browse files
committed
wires up test data with valid results
1 parent 971cd76 commit 74d48ac

File tree

7 files changed

+92
-3
lines changed

7 files changed

+92
-3
lines changed

coveralls.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"skip_files": [
3+
"test"
4+
]
5+
}

mix.exs

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ defmodule Geolix.Plug.Mixfile do
1212
deps: deps(),
1313
description: "Geolix Plug",
1414
docs: docs(),
15+
elixirc_paths: elixirc_paths(Mix.env()),
1516
package: package(),
1617
preferred_cli_env: [
1718
coveralls: :test,
@@ -41,6 +42,9 @@ defmodule Geolix.Plug.Mixfile do
4142
]
4243
end
4344

45+
defp elixirc_paths(:test), do: ["lib", "test/helpers"]
46+
defp elixirc_paths(_), do: ["lib"]
47+
4448
defp package do
4549
%{
4650
files: ["CHANGELOG.md", "LICENSE", "mix.exs", "README.md", "lib"],

test/fixtures/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.mmdb

test/geolix_plug_test.exs

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ defmodule Geolix.PlugTest do
55
defmodule Router do
66
use Plug.Router
77

8-
plug Geolix.Plug, where: :city
8+
plug Geolix.Plug, where: :fixture_city
99

1010
plug :match
1111
plug :dispatch
@@ -16,9 +16,12 @@ defmodule Geolix.PlugTest do
1616
@opts Router.init([])
1717

1818
test "returns 200" do
19-
conn = conn(:get, "/") |> Router.call(@opts)
19+
conn =
20+
conn(:get, "/")
21+
|> Map.put(:remote_ip, {2, 125, 160, 216})
22+
|> Router.call(@opts)
2023

2124
assert 200 == conn.status
22-
refute conn.private[:geolix]
25+
assert %Geolix.Result.City{} = conn.private[:geolix]
2326
end
2427
end

test/helpers/fixture_download.ex

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
defmodule Geolix.PlugTestHelpers.FixtureDownload do
2+
@moduledoc false
3+
4+
alias Geolix.PlugTestHelpers.FixtureList
5+
6+
@doc """
7+
Downloads all fixture files.
8+
"""
9+
def run() do
10+
Enum.each(FixtureList.get(), &download/1)
11+
end
12+
13+
defp download({_name, filename, remote}) do
14+
local = local(filename)
15+
16+
if not File.regular?(local) do
17+
Mix.shell().info([:yellow, "Downloading fixture database: #{filename}"])
18+
19+
download_fixture(remote, local)
20+
end
21+
end
22+
23+
defp download_fixture(remote, local) do
24+
{:ok, _} = Application.ensure_all_started(:hackney)
25+
{:ok, _, _, client} = :hackney.get(remote)
26+
{:ok, content} = :hackney.body(client)
27+
28+
File.write!(local, content)
29+
end
30+
31+
defp local(filename) do
32+
[__DIR__, "../fixtures/", filename]
33+
|> Path.join()
34+
|> Path.expand()
35+
end
36+
end

test/helpers/fixture_list.ex

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
defmodule Geolix.PlugTestHelpers.FixtureList do
2+
@moduledoc false
3+
4+
@base_maxmind "https://raw.githubusercontent.com/maxmind/MaxMind-DB/master/test-data"
5+
6+
@doc """
7+
Returns a list of all available/downloaded fixtures.
8+
9+
Each returned entry consists of the following values:
10+
11+
{
12+
:name_as_atom,
13+
"local_filename.mmdb",
14+
"remote_url"
15+
}
16+
"""
17+
@spec get() :: list
18+
def get() do
19+
[
20+
{:fixture_city, "GeoIP2-City-Test.mmdb", "#{@base_maxmind}/GeoIP2-City-Test.mmdb"}
21+
]
22+
end
23+
end

test/test_helper.exs

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
alias Geolix.PlugTestHelpers.FixtureDownload
2+
alias Geolix.PlugTestHelpers.FixtureList
3+
14
{:ok, _} = Application.ensure_all_started(:geolix)
5+
:ok = FixtureDownload.run()
6+
7+
databases =
8+
Enum.map(FixtureList.get(), fn {id, filename, _remote} ->
9+
source =
10+
[__DIR__, "fixtures", filename]
11+
|> Path.join()
12+
|> Path.expand()
13+
14+
%{id: id, adapter: Geolix.Adapter.MMDB2, source: source}
15+
end)
16+
17+
Application.put_env(:geolix, :databases, databases)
18+
Enum.each(databases, &Geolix.load_database/1)
219

320
ExUnit.start()

0 commit comments

Comments
 (0)