Skip to content

Introduce property testing #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/weather_loop/snapshot_conversions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ defmodule WeatherLoop.SnapshotConversions do
|> round
end

def cardinal_direction(nil), do: nil
def cardinal_direction(direction_degrees) do
def cardinal_direction(direction_degrees) when is_integer(direction_degrees) do
case direction_degrees do
x when x in 0..11 ->
"N"
Expand Down Expand Up @@ -42,15 +41,21 @@ defmodule WeatherLoop.SnapshotConversions do
"NNW"
x when x in 349..360 ->
"N"
_ ->
nil
end
end
def cardinal_direction(_), do: nil

def wind_detail(nil, nil), do: nil
def wind_detail(wind_direction, wind_speed) do
"#{cardinal_direction(wind_direction)} at #{round(wind_speed)} mph"
end

def convert_epoch(nil), do: nil
def convert_epoch(epoch) when is_float(epoch), do: nil
def convert_epoch(epoch) when epoch < 0, do: nil
def convert_epoch(epoch) when epoch > 2147483647, do: nil
def convert_epoch(epoch) do
{:ok, time_string} =
Calendar.DateTime.Parse.unix!(epoch)
Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ defmodule WeatherLoop.MixProject do
{:hackney, "~> 1.17"},
{:tzdata, "~> 1.1"},
{:calendar, "~> 1.0.0"},
{:tailwind, "~> 0.1", runtime: Mix.env() == :dev}
{:tailwind, "~> 0.1", runtime: Mix.env() == :dev},
{:propcheck, "~> 1.4", only: [:test, :dev]}
]
end

Expand Down
3 changes: 3 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"ibrowse": {:hex, :ibrowse, "4.4.0", "2d923325efe0d2cb09b9c6a047b2835a5eda69d8a47ed6ff8bc03628b764e991", [:rebar3], [], "hexpm", "6a8e5988872086f0506bef68311493551ac5beae7c06ba2a00d5e9f97a60f1c2"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
"jason": {:hex, :jason, "1.3.0", "fa6b82a934feb176263ad2df0dbd91bf633d4a46ebfdffea0c8ae82953714946", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "53fc1f51255390e0ec7e50f9cb41e751c260d065dcba2bf0d08dc51a4002c2ac"},
"libgraph": {:hex, :libgraph, "0.16.0", "3936f3eca6ef826e08880230f806bfea13193e49bf153f93edcf0239d4fd1d07", [:mix], [], "hexpm", "41ca92240e8a4138c30a7e06466acc709b0cbb795c643e9e17174a178982d6bf"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
"mime": {:hex, :mime, "2.0.2", "0b9e1a4c840eafb68d820b0e2158ef5c49385d17fb36855ac6e7e087d4b1dcc5", [:mix], [], "hexpm", "e6a3f76b4c277739e36c2e21a2c640778ba4c3846189d5ab19f97f126df5f9b7"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
Expand All @@ -40,6 +41,8 @@
"plug_cowboy": {:hex, :plug_cowboy, "2.5.2", "62894ccd601cf9597e2c23911ff12798a8a18d237e9739f58a6b04e4988899fe", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "ea6e87f774c8608d60c8d34022a7d073bd7680a0a013f049fc62bf35efea1044"},
"plug_crypto": {:hex, :plug_crypto, "1.2.2", "05654514ac717ff3a1843204b424477d9e60c143406aa94daf2274fdd280794d", [:mix], [], "hexpm", "87631c7ad914a5a445f0a3809f99b079113ae4ed4b867348dd9eec288cecb6db"},
"postgrex": {:hex, :postgrex, "0.16.3", "fac79a81a9a234b11c44235a4494d8565303fa4b9147acf57e48978a074971db", [:mix], [{:connection, "~> 1.1", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "aeaae1d2d1322da4e5fe90d241b0a564ce03a3add09d7270fb85362166194590"},
"propcheck": {:hex, :propcheck, "1.4.1", "c12908dbe6f572032928548089b34ff9d40672d5d70f1562e3a9e9058d226cc9", [:mix], [{:libgraph, "~> 0.13", [hex: :libgraph, repo: "hexpm", optional: false]}, {:proper, "~> 1.4", [hex: :proper, repo: "hexpm", optional: false]}], "hexpm", "e1b088f574785c3c7e864da16f39082d5599b3aaf89086d3f9be6adb54464b19"},
"proper": {:hex, :proper, "1.4.0", "89a44b8c39d28bb9b4be8e4d715d534905b325470f2e0ec5e004d12484a79434", [:rebar3], [], "hexpm", "18285842185bd33efbda97d134a5cb5a0884384db36119fee0e3cfa488568cbb"},
"ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},
"swoosh": {:hex, :swoosh, "1.7.1", "53bb5b8efaf5577bbad1480fb057d8bcb7b16097aa015ab9f369a01592c961ec", [:mix], [{:cowboy, "~> 1.1 or ~> 2.4", [hex: :cowboy, repo: "hexpm", optional: true]}, {:ex_aws, "~> 2.1", [hex: :ex_aws, repo: "hexpm", optional: true]}, {:finch, "~> 0.6", [hex: :finch, repo: "hexpm", optional: true]}, {:gen_smtp, "~> 0.13 or ~> 1.0", [hex: :gen_smtp, repo: "hexpm", optional: true]}, {:hackney, "~> 1.9", [hex: :hackney, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mail, "~> 0.2", [hex: :mail, repo: "hexpm", optional: true]}, {:mime, "~> 1.1 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_cowboy, ">= 1.0.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.2 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "8206d7da5038f0e11292b775b7e588fcd5dc74ac4dd8b661fe72f58207234747"},
Expand Down
61 changes: 61 additions & 0 deletions test/weather_loop/snapshot_conversions_property_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
defmodule WeatherLoop.SnapshotConversionsPropertyTest do
use ExUnit.Case
use PropCheck
alias WeatherLoop.SnapshotConversions

property ".cardinal_direction is string when valid" do
forall type <- range(0, 360) do
(SnapshotConversions.cardinal_direction(type) |> Kernel.is_binary) == true
end
end

property ".cardinal_direction is nil when negative" do
forall type <- neg_integer() do
SnapshotConversions.cardinal_direction(type) == nil
end
end

property ".cardinal_direction is nil when over 360" do
forall type <- range(361, 10000) do
SnapshotConversions.cardinal_direction(type) == nil
end
end

property ".cardinal_direction is nil when float" do
forall type <- float() do
SnapshotConversions.cardinal_direction(type) == nil
end
end

property ".convert_epoch is string when valid" do
forall epoch <- range(100000000, 200000000) do
SnapshotConversions.convert_epoch(epoch) |> Kernel.is_binary == true
end
end

property ".convert_epoch is nil when negative" do
forall epoch <- neg_integer() do
SnapshotConversions.convert_epoch(epoch) == nil
end
end

property ".convert_epoch is nil when float" do
forall epoch <- float() do
SnapshotConversions.convert_epoch(epoch) == nil
end
end

property ".convert_epoch is nil post epoch" do
forall epoch <- range(year_one_post_epoch(), year_ten_post_epoch()) do
SnapshotConversions.convert_epoch(epoch) == nil
end
end

def year_one_post_epoch do
Enum.random(2147483648..2179019647)
end

def year_ten_post_epoch do
Enum.random(2463016447..2494638847)
end
end