70 lines
2.1 KiB
Elixir
70 lines
2.1 KiB
Elixir
defmodule Microwaveprop.Buildings.BulkFetchTest do
|
|
use ExUnit.Case, async: false
|
|
|
|
import ExUnit.CaptureLog
|
|
|
|
alias Microwaveprop.Buildings.BulkFetch
|
|
|
|
doctest BulkFetch
|
|
|
|
setup do
|
|
tmp_dir =
|
|
Path.join(System.tmp_dir!(), "bulk_fetch_test_#{System.unique_integer([:positive])}")
|
|
|
|
File.mkdir_p!(tmp_dir)
|
|
|
|
prev_cache_dir = Application.get_env(:microwaveprop, :buildings_cache_dir)
|
|
prev_http_get = Application.get_env(:microwaveprop, :ms_footprints_http_get)
|
|
|
|
Application.put_env(:microwaveprop, :buildings_cache_dir, tmp_dir)
|
|
|
|
Application.put_env(:microwaveprop, :ms_footprints_http_get, fn _url, _opts ->
|
|
{:ok, %{status: 200, body: "Location\tQuadkey\tUrl\tSize\n"}}
|
|
end)
|
|
|
|
Microwaveprop.Cache.invalidate(:ms_footprints_index)
|
|
|
|
on_exit(fn ->
|
|
File.rm_rf!(tmp_dir)
|
|
|
|
if prev_cache_dir,
|
|
do: Application.put_env(:microwaveprop, :buildings_cache_dir, prev_cache_dir),
|
|
else: Application.delete_env(:microwaveprop, :buildings_cache_dir)
|
|
|
|
if prev_http_get,
|
|
do: Application.put_env(:microwaveprop, :ms_footprints_http_get, prev_http_get),
|
|
else: Application.delete_env(:microwaveprop, :ms_footprints_http_get)
|
|
end)
|
|
|
|
%{tmp_dir: tmp_dir}
|
|
end
|
|
|
|
describe "run/4" do
|
|
test "returns the result struct with zero counters when index is empty" do
|
|
# Stub the dataset index fetch to return empty TSV -> no quadkeys
|
|
# in index -> no downloads attempted.
|
|
Application.put_env(:microwaveprop, :ms_footprints_http_get, fn _url, _opts ->
|
|
{:ok, %{status: 200, body: "Location\tQuadkey\tUrl\tSize\n"}}
|
|
end)
|
|
|
|
# Need to invalidate the dataset_index cache so our stub takes effect.
|
|
Microwaveprop.Cache.invalidate(:ms_footprints_index)
|
|
|
|
capture_log(fn ->
|
|
result = BulkFetch.run(32.8, -97.0, 1, concurrency: 1)
|
|
send(self(), result)
|
|
end)
|
|
|
|
assert_received %{
|
|
downloaded: 0,
|
|
cached: 0,
|
|
failed: 0,
|
|
missing: missing,
|
|
cache_dir: cache_dir
|
|
}
|
|
|
|
assert is_integer(missing)
|
|
assert byte_size(cache_dir) > 0
|
|
end
|
|
end
|
|
end
|