Skip to content

Commit

Permalink
Import StreamData Formatter Rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Fontaine committed Aug 16, 2019
1 parent 472eaae commit 012d24b
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 100 deletions.
1 change: 1 addition & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Used by "mix format"
[
import_deps: [:stream_data],
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
10 changes: 7 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ defmodule Unleash.MixProject do
{:ex_doc, "~> 0.21", only: :dev, runtime: false},
{:eliver, "~> 2.0", only: :dev, runtime: false},
{:junit_formatter, "~> 3.0", only: :test},
{:stream_data, "~> 0.4.3", only: :test},
{:stream_data, "~> 0.4.3", only: [:test, :dev]},
{:excoveralls, "~> 0.8", only: :test},
{:murmur, "~> 1.0"},
{:tesla, "~> 1.2"},
{:hackney, "~> 1.14"},
{:jason, "~> 1.1"}
{:jason, "~> 1.1"},
{:plug, "~> 1.8", optional: true},
{:phoenix_gon, "~> 0.4.0", optional: true}
]
end

Expand All @@ -72,7 +74,9 @@ defmodule Unleash.MixProject do
extras: ["README.md"],
deps: [
tesla: "https://hexdocs.pm/tesla",
murmur: "https://hexdocs.pm/murmur/readme.html"
murmur: "https://hexdocs.pm/murmur",
plug: "https://hexdocs.pm/plug",
phoenix_gon: "https://hexdocs.pm/phoenix_gon"
],
groups_for_modules: [
Strategies: ~r"Strateg(y|ies)\."
Expand Down
5 changes: 5 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
"murmur": {:hex, :murmur, "1.0.1", "a6e6bced2dd0d666090a9cf3e73699f3b9176bbcf32d35b0f022f137667608e3", [:mix], [], "hexpm"},
"nimble_parsec": {:hex, :nimble_parsec, "0.5.1", "c90796ecee0289dbb5ad16d3ad06f957b0cd1199769641c961cfe0b97db190e0", [:mix], [], "hexpm"},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm"},
"phoenix_gon": {:hex, :phoenix_gon, "0.4.0", "ff67988ccd412f1cc04f095c8f0553915a4c8d2b50959db773b9da9f0cf23cb3", [:mix], [{:phoenix_html, "~> 2.7", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}, {:poison, "~> 3.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"},
"phoenix_html": {:hex, :phoenix_html, "2.13.3", "850e292ff6e204257f5f9c4c54a8cb1f6fbc16ed53d360c2b780a3d0ba333867", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
"plug": {:hex, :plug, "1.8.3", "12d5f9796dc72e8ac9614e94bda5e51c4c028d0d428e9297650d09e15a684478", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm"},
"plug_crypto": {:hex, :plug_crypto, "1.0.0", "18e49317d3fa343f24620ed22795ec29d4a5e602d52d1513ccea0b07d8ea7d4d", [:mix], [], "hexpm"},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"},
"poolboy": {:hex, :poolboy, "1.5.2", "392b007a1693a64540cead79830443abf5762f5d30cf50bc95cb2c1aaafa006b", [:rebar3], [], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.4", "f0eafff810d2041e93f915ef59899c923f4568f4585904d010387ed74988e77b", [:make, :mix, :rebar3], [], "hexpm"},
"stream_data": {:hex, :stream_data, "0.4.3", "62aafd870caff0849a5057a7ec270fad0eb86889f4d433b937d996de99e3db25", [:mix], [], "hexpm"},
Expand Down
20 changes: 8 additions & 12 deletions test/unleash/strategy/active_for_users_with_id_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,20 @@ defmodule Unleash.Strategy.ActiveForUsersWithIdTest do

describe "enabled?" do
property "returns true if the user is in the list" do
check all(
{user_ids, user} <-
map(nonempty(list_of(positive_integer())), fn list ->
{Enum.join(list, ","), Enum.random(list)}
end)
) do
check all {user_ids, user} <-
map(nonempty(list_of(positive_integer())), fn list ->
{Enum.join(list, ","), Enum.random(list)}
end) do
assert {true, _} =
ActiveForUsersWithId.enabled?(%{"userIds" => user_ids}, %{user_id: user})
end
end

property "returns false if the user is not in the list" do
check all(
{user_ids, user} <-
map(nonempty(list_of(positive_integer())), fn list ->
{Enum.join(list, ","), Enum.max(list) + 1}
end)
) do
check all {user_ids, user} <-
map(nonempty(list_of(positive_integer())), fn list ->
{Enum.join(list, ","), Enum.max(list) + 1}
end) do
assert {false, _} =
ActiveForUsersWithId.enabled?(%{"userIds" => user_ids}, %{user_id: user})
end
Expand Down
18 changes: 7 additions & 11 deletions test/unleash/strategy/application_hostname_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,18 @@ defmodule Unleash.Strategy.ApplicationHostnameTest do

describe "enabled?" do
property "returns true if the hostname is in the list" do
check all(
{:ok, hostname} <- constant(:inet.gethostname()),
hosts <- nonempty(list_of(binary())),
hostnames <- constant(Enum.join([hostname | hosts], ","))
) do
check all {:ok, hostname} <- constant(:inet.gethostname()),
hosts <- nonempty(list_of(binary())),
hostnames <- constant(Enum.join([hostname | hosts], ",")) do
assert {true, _} = ApplicationHostname.enabled?(%{"hostnames" => hostnames}, %{})
end
end

property "returns false if the hostname is in the list" do
check all(
hostname <-
map(constant(:inet.gethostname()), fn {:ok, host} -> List.to_string(host) end),
hostnames <- nonempty(list_of(binary())),
hostname not in hostnames
) do
check all hostname <-
map(constant(:inet.gethostname()), fn {:ok, host} -> List.to_string(host) end),
hostnames <- nonempty(list_of(binary())),
hostname not in hostnames do
assert {false, _} = ApplicationHostname.enabled?(%{"hostnames" => hostnames}, %{})
end
end
Expand Down
26 changes: 10 additions & 16 deletions test/unleash/strategy/gradual_rollout_session_id_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ defmodule Unleash.Strategy.GradualRolloutSessionIdTest do

describe "enabled?" do
property "returns false if the session is empty" do
check all(
group_id <- binary(),
session <- constant(""),
percentage <- integer(1..100)
) do
check all group_id <- binary(),
session <- constant(""),
percentage <- integer(1..100) do
refute GradualRolloutSessionId.enabled?(
%{"percentage" => percentage, "groupId" => group_id},
%{session_id: session}
Expand All @@ -20,12 +18,10 @@ defmodule Unleash.Strategy.GradualRolloutSessionIdTest do
end

property "checks if the session and group are within the normalized percentage" do
check all(
group_id <- binary(),
session <- binary(min_length: 1),
percentage <- integer(1..100),
{:ok, result} = {:ok, percentage >= Utils.normalize(session, group_id)}
) do
check all group_id <- binary(),
session <- binary(min_length: 1),
percentage <- integer(1..100),
{:ok, result} = {:ok, percentage >= Utils.normalize(session, group_id)} do
assert {^result, _} =
GradualRolloutSessionId.enabled?(
%{"percentage" => percentage, "groupId" => group_id},
Expand All @@ -35,11 +31,9 @@ defmodule Unleash.Strategy.GradualRolloutSessionIdTest do
end

property "returns false if the percentage is 0" do
check all(
group_id <- binary(),
session <- binary(min_length: 1),
percentage <- constant(0)
) do
check all group_id <- binary(),
session <- binary(min_length: 1),
percentage <- constant(0) do
assert {false, _} =
GradualRolloutSessionId.enabled?(
%{"percentage" => percentage, "groupId" => group_id},
Expand Down
26 changes: 10 additions & 16 deletions test/unleash/strategy/gradual_rollout_user_id_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ defmodule Unleash.Strategy.GradualRolloutUserIdTest do

describe "enabled?" do
property "returns false if the user is empty" do
check all(
group_id <- binary(),
user <- constant(""),
percentage <- integer(1..100)
) do
check all group_id <- binary(),
user <- constant(""),
percentage <- integer(1..100) do
refute GradualRolloutUserId.enabled?(
%{"percentage" => percentage, "groupId" => group_id},
%{user_id: user}
Expand All @@ -20,12 +18,10 @@ defmodule Unleash.Strategy.GradualRolloutUserIdTest do
end

property "checks if the user and group are within the normalized percentage" do
check all(
group_id <- binary(),
user <- binary(min_length: 1),
percentage <- integer(1..100),
{:ok, result} = {:ok, percentage >= Utils.normalize(user, group_id)}
) do
check all group_id <- binary(),
user <- binary(min_length: 1),
percentage <- integer(1..100),
{:ok, result} = {:ok, percentage >= Utils.normalize(user, group_id)} do
assert {^result, _} =
GradualRolloutUserId.enabled?(
%{"percentage" => percentage, "groupId" => group_id},
Expand All @@ -35,11 +31,9 @@ defmodule Unleash.Strategy.GradualRolloutUserIdTest do
end

property "returns false if the percentage is 0" do
check all(
group_id <- binary(),
user <- binary(min_length: 1),
percentage <- constant(0)
) do
check all group_id <- binary(),
user <- binary(min_length: 1),
percentage <- constant(0) do
assert {false, _} =
GradualRolloutUserId.enabled?(
%{"percentage" => percentage, "groupId" => group_id},
Expand Down
30 changes: 13 additions & 17 deletions test/unleash/strategy/remote_address_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,24 @@ defmodule Unleash.Strategy.RemoteAddressTest do

describe "enabled?" do
property "returns true if an IP is in the list" do
check all(
list <-
map(nonempty(list_of(string(:alphanumeric, min_length: 1))), fn list ->
Enum.map(list, &String.replace(&1, ",", ""))
end),
address <- member_of(list),
ips = Enum.join(list, ",")
) do
check all list <-
map(nonempty(list_of(string(:alphanumeric, min_length: 1))), fn list ->
Enum.map(list, &String.replace(&1, ",", ""))
end),
address <- member_of(list),
ips = Enum.join(list, ",") do
assert {true, _} = RemoteAddress.enabled?(%{"ips" => ips}, %{remote_address: address})
end
end

property "returns false if an IP is not in the list" do
check all(
list <-
map(nonempty(list_of(string(:alphanumeric, min_length: 1))), fn list ->
Enum.map(list, &String.replace(&1, ",", ""))
end),
address <- string(:alphanumeric, min_length: 1),
address not in list,
ips = Enum.join(list, ",")
) do
check all list <-
map(nonempty(list_of(string(:alphanumeric, min_length: 1))), fn list ->
Enum.map(list, &String.replace(&1, ",", ""))
end),
address <- string(:alphanumeric, min_length: 1),
address not in list,
ips = Enum.join(list, ",") do
assert {false, _} = RemoteAddress.enabled?(%{"ips" => ips}, %{remote_address: address})
end
end
Expand Down
42 changes: 17 additions & 25 deletions test/unleash/strategy/util_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,27 @@ defmodule Unleash.Strategy.UtilsTest do

describe "in_list?" do
property "returns true if the member is in the list" do
check all(
list <- list_of(string(:alphanumeric), min_length: 1),
member <- member_of(list),
string_list = Enum.join(list, ",")
) do
check all list <- list_of(string(:alphanumeric), min_length: 1),
member <- member_of(list),
string_list = Enum.join(list, ",") do
assert Utils.in_list?(string_list, member)
end
end

property "returns false if the member is not in the list" do
check all(
list <- list_of(string(:alphanumeric), min_length: 1),
member <- string(:alphanumeric),
member not in list,
string_list = Enum.join(list, ",")
) do
check all list <- list_of(string(:alphanumeric), min_length: 1),
member <- string(:alphanumeric),
member not in list,
string_list = Enum.join(list, ",") do
refute Utils.in_list?(string_list, member)
end
end

property "runs the transformation function on members in the list" do
check all(
list <- list_of(string(:alphanumeric), min_length: 1),
member <- member_of(list),
member = String.upcase(member),
string_list = Enum.join(list, ",")
) do
check all list <- list_of(string(:alphanumeric), min_length: 1),
member <- member_of(list),
member = String.upcase(member),
string_list = Enum.join(list, ",") do
assert Utils.in_list?(string_list, member, &String.upcase/1)
end
end
Expand All @@ -51,29 +45,27 @@ defmodule Unleash.Strategy.UtilsTest do

describe "parse_int" do
property "returns integers as-is" do
check all(x <- integer()) do
check all x <- integer() do
assert ^x = Utils.parse_int(x)
end
end

property "parses integers with strings" do
check all({i, x} <- map(positive_integer(), fn j -> {j, Integer.to_string(j)} end)) do
check all {i, x} <- map(positive_integer(), fn j -> {j, Integer.to_string(j)} end) do
assert ^i = Utils.parse_int(x)
end
end

property "returns 0 if given negative numbers" do
check all(
i <- positive_integer(),
i = i * -1,
x = Integer.to_string(i)
) do
check all i <- positive_integer(),
i = i * -1,
x = Integer.to_string(i) do
assert 0 = Utils.parse_int(x)
end
end

property "returns 0 if given non-numbers" do
check all(x <- string([?a..?z, ?A..?Z])) do
check all x <- string([?a..?z, ?A..?Z]) do
assert 0 = Utils.parse_int(x)
end
end
Expand Down

0 comments on commit 012d24b

Please sign in to comment.