Skip to content

Commit

Permalink
update and refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kociamber committed Jul 10, 2024
1 parent 91b8c51 commit 0c96148
Show file tree
Hide file tree
Showing 6 changed files with 348 additions and 420 deletions.
154 changes: 65 additions & 89 deletions test/get_current_weather_test.exs
Original file line number Diff line number Diff line change
@@ -1,185 +1,161 @@
defmodule GetCurrentWeatherTest do
use ExUnit.Case

test ": can get weather data with get_current_weather/1 by city name" do
# given
city = %{city: "Bengaluru"}
# when
result = ExOwm.get_current_weather(city)
# then
# check whether a list of maps is returned
assert is_list(result)
setup do
# Introduce a delay of 1 second between each test due to free API key restriction of 60 calls/minute
:timer.sleep(1000)
:ok
end

test ":get_current_weather/1 with a city name" do
result = ExOwm.get_current_weather(%{city: "Bengaluru"})
assert result != []

{:ok, map} = List.first(result)

assert is_map(map)
# check whether map has specific keys to confirm that request was successful
assert Map.get(map, "name") == "Bengaluru"
end

test ": can get weather data with get_current_weather/1 by city name list" do
# given
city = %{city: "Lucerne"}
# when
result = ExOwm.get_current_weather([city])
# then
# check whether a list of maps is returned
test ":get_current_weather/1 with a list of cities" do
result = ExOwm.get_current_weather([%{city: "Lucerne"}])

assert is_list(result)
assert result != []

{:ok, map} = List.first(result)

assert is_map(map)
# check whether map has specific keys to confirm that request was successful
assert Map.get(map, "name") == "Lucerne"
end

test ": can get weather data with get_current_weather/1 by city name and country code" do
# given
city = %{city: "Munich", countr_code: "de"}
# when
result = ExOwm.get_current_weather([city])
# then
# check whether a list of maps is returned
test "get_current_weather/1 with a city name and a country code" do
result = ExOwm.get_current_weather([%{city: "Munich", countr_code: "de"}])

assert is_list(result)
assert result != []

{:ok, map} = List.first(result)

assert is_map(map)
# check whether map has specific keys to confirm that request was successful
assert Map.get(map, "name") == "Munich"
end

test ": can get weather data with get_current_weather/1 by city id" do
# given
city = %{id: 2_759_794}
# when
result = ExOwm.get_current_weather([city])
# then
# check whether a list of maps is returned
test ":get_current_weather/1 with a city id" do
result = ExOwm.get_current_weather([%{id: 2_759_794}])

assert is_list(result)
assert result != []

{:ok, map} = List.first(result)
assert is_map(map)
# check whether map has specific keys to confirm that request was successful
assert Map.get(map, "name") == "Amsterdam" or "Gemeente Amsterdam"
end

test ": can get weather data with get_current_weather/1 by latitude and longitude" do
# given
city = %{lat: 52.374031, lon: 4.88969}
# when
result = ExOwm.get_current_weather([city])
# then
# check whether a list of maps is returned
test "get_current_weather/1 with a latitude and a longitude" do
result = ExOwm.get_current_weather([%{lat: 52.374031, lon: 4.88969}])

assert is_list(result)
assert result != []

{:ok, map} = List.first(result)

assert is_map(map)
# check whether map has specific keys to confirm that request was successful
assert Map.get(map, "name") == "Amsterdam" or "Gemeente Amsterdam"
end

test ": can get weather data with get_current_weather/1 by zip and country code" do
# given
city = %{zip: "94040", country_code: "us"}
# when
result = ExOwm.get_current_weather([city])
# then
# check whether a list of maps is returned
test "get_current_weather/1 with a zip and a country code" do
result = ExOwm.get_current_weather([%{zip: "94040", country_code: "us"}])

assert is_list(result)
assert result != []

{:ok, map} = List.first(result)

assert is_map(map)
# check whether map has specific keys to confirm that request was successful
assert Map.get(map, "name") == "Mountain View"
end

test ": can get weather data with get_current_weather/1 by city name with options" do
# given
test "get_current_weather/1 with a city name and options" do
city = %{city: "Warsaw"}
options = [units: :metric, lang: :pl]
# when

result = ExOwm.get_current_weather([city], options)
# then
# check whether a list of maps is returned

assert is_list(result)
assert result != []

{:ok, map} = List.first(result)

assert is_map(map)
# check whether map has specific keys to confirm that request was successful
assert Map.get(map, "name") == "Warszawa"
end

test ": can get weather data with get_current_weather/1 by city name and country code with options" do
# given
test "get_current_weather/1 with a city name and a country code and options" do
city = %{city: "Warsaw", countr_code: "pl"}
options = [units: :metric, lang: :pl]
# when

result = ExOwm.get_current_weather([city], options)
# then
# check whether a list of maps is returned

assert is_list(result)
assert result != []

{:ok, map} = List.first(result)

assert is_map(map)
# check whether map has specific keys to confirm that request was successful
assert Map.get(map, "name") == "Warszawa"
end

test ": can get weather data with get_current_weather/1 by city id with options" do
# given
test "get_current_weather/1 with a city id and options" do
city = %{id: 2_759_794}
options = [units: :metric, lang: :pl]
# when

result = ExOwm.get_current_weather([city], options)
# then
# check whether a list of maps is returned

assert is_list(result)
assert result != []

{:ok, map} = List.first(result)

assert is_map(map)
# check whether map has specific keys to confirm that request was successful
assert Map.get(map, "name") == "Amsterdam" or "Gemeente Amsterdam"
end

test ": can get weather data with get_current_weather/1 by latitude and longitude with options" do
# given
test "get_current_weather/1 with a latitude and a longitude and options" do
city = %{lat: 52.374031, lon: 4.88969}
options = [units: :metric, lang: :pl]
# when

result = ExOwm.get_current_weather([city], options)
# then
# check whether a list of maps is returned

assert is_list(result)
assert result != []

{:ok, map} = List.first(result)

assert is_map(map)
# check whether map has specific keys to confirm that request was successful
assert Map.get(map, "name") == "Amsterdam" or "Gemeente Amsterdam"
end

test ": can get weather data with get_current_weather/1 by zip and country code with options" do
# given
test "get_current_weather/1 with a zip code, a country code and options" do
city = %{zip: "94040", country_code: "us"}
options = [units: :metric, lang: :pl]
# when

result = ExOwm.get_current_weather([city], options)
# then
# check whether a list of maps is returned

assert is_list(result)
assert result != []

{:ok, map} = List.first(result)

assert is_map(map)
# check whether map has specific keys to confirm that request was successful
assert Map.get(map, "name") == "Mountain View"
end

test ": Parses errors correctly" do
# given
city = %{city: "unknown city, yes, really unknown"}
# when
result = ExOwm.get_current_weather(city)
# then
# check whether a list of maps is returned
assert is_list(result)
assert result != []
{:error, :not_found, %{"cod" => "404", "message" => "city not found"}} = List.first(result)
test "get_current_weather/1 with non-existing city" do
result = ExOwm.get_current_weather(%{city: "Bonkersville"})

assert result == [
{:error, :not_found, {:ok, %{"cod" => "404", "message" => "city not found"}}}
]
end
end
Loading

0 comments on commit 0c96148

Please sign in to comment.