-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from appunite/write-tests
Write test suite
- Loading branch information
Showing
18 changed files
with
367 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"skip_files": [ | ||
"test/support" | ||
"test/support", | ||
"lib/imager_web/instrumenter.ex" | ||
], | ||
|
||
"coverage_options": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[stores.test] | ||
type = "Blackhole" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
defmodule Imager.Config.EnvTransformTest do | ||
use ExUnit.Case, async: true | ||
use ExUnitProperties | ||
|
||
alias Imager.Config.EnvTransform, as: Subject | ||
|
||
defp non_env do | ||
gen all value <- term(), | ||
not match?("$" <> _, value), | ||
do: value | ||
end | ||
|
||
property "values not starting with `$` are passed as is" do | ||
check all key <- atom(:alphanumeric), | ||
value <- non_env() do | ||
assert value == Subject.transform(key, value) | ||
end | ||
end | ||
|
||
test "returns set value when there exists environment variable" do | ||
System.put_env("FOO", "foo") | ||
|
||
on_exit(fn -> System.delete_env("FOO") end) | ||
|
||
assert "foo" == Subject.transform(:foo, "$FOO") | ||
end | ||
|
||
test "returns nil when value is unset" do | ||
assert is_nil(Subject.transform(:foo, "$NON_SET_ENV_VAR")) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
defmodule Imager.Config.PortNormalizationTest do | ||
use ExUnit.Case, async: true | ||
use ExUnitProperties | ||
|
||
alias Imager.Config.PortNormalization, as: Subject | ||
|
||
defp non_positive_integer do | ||
gen all value <- one_of([positive_integer(), constant(0)]), do: -value | ||
end | ||
|
||
defp assert_fail(value) do | ||
assert_raise RuntimeError, | ||
"Expected port to be positive integer, got: #{inspect(value)}", | ||
fn -> | ||
Subject.transform(:port, value) | ||
end | ||
end | ||
|
||
property "port values of positive integer are passed through" do | ||
check all value <- positive_integer() do | ||
assert value == Subject.transform(:port, value) | ||
end | ||
end | ||
|
||
property "port values of strings representing positive integers are passed through" do | ||
check all num <- positive_integer(), | ||
value = Integer.to_string(num) do | ||
assert num == Subject.transform(:port, value) | ||
end | ||
end | ||
|
||
property "raises error for non-positive integers" do | ||
check all value <- non_positive_integer() do | ||
assert_fail(value) | ||
end | ||
end | ||
|
||
property "raises error for strings containing non-positive integers" do | ||
check all num <- non_positive_integer(), | ||
value = Integer.to_string(num) do | ||
assert_fail(value) | ||
end | ||
end | ||
|
||
property "raises error for strings not containing numbers" do | ||
check all number <- string(?0..?9), | ||
alphas <- string(?a..?z, min_length: 1) do | ||
assert_fail(alphas <> number) | ||
assert_fail(number <> alphas) | ||
assert_fail(alphas) | ||
end | ||
end | ||
|
||
property "all non-port fields are passed through" do | ||
check all key <- atom(:alphanumeric), | ||
key != :port, | ||
value <- term() do | ||
assert value == Subject.transform(key, value) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
defmodule Imager.Config.StoreTransformTest do | ||
use ExUnit.Case, async: true | ||
use ExUnitProperties | ||
|
||
alias Imager.Config.StoreTransform, as: Subject | ||
|
||
defp type(types \\ ~w(S3 Local Blackhole)), | ||
do: one_of(Enum.map(types, &constant/1)) | ||
|
||
defp store_config(typ \\ type(), options \\ constant(%{})) do | ||
fixed_map(%{type: typ, options: options}) | ||
end | ||
|
||
property "returns map with stringified atoms" do | ||
check all path <- atom(:alphanumeric), | ||
config <- store_config() do | ||
assert Map.has_key?( | ||
Subject.transform(:stores, %{path => config}), | ||
Atom.to_string(path) | ||
) | ||
end | ||
end | ||
|
||
property "returns map with equal fields `store` and `cache`" do | ||
check all config <- store_config() do | ||
assert %{"foo" => result} = Subject.transform(:stores, %{foo: config}) | ||
assert %{store: data, cache: data} = result | ||
end | ||
end | ||
|
||
property "returns map with different fields `store` and `cache` when both are defined" do | ||
check all [t1, t2] <- uniq_list_of(type(), length: 2), | ||
store <- store_config(constant(t1)), | ||
cache <- store_config(constant(t2)) do | ||
assert %{"foo" => result} = | ||
Subject.transform(:stores, %{foo: %{store: store, cache: cache}}) | ||
|
||
assert %{store: store_tuple, cache: cache_tuple} = result | ||
assert store_tuple != cache_tuple | ||
end | ||
end | ||
|
||
property "returns map with different fields `store` and `cache` when any is set" do | ||
check all [t1, t2] <- uniq_list_of(type(), length: 2), | ||
main <- store_config(constant(t1)), | ||
other <- store_config(constant(t2)), | ||
field <- one_of([constant(:cache), constant(:store)]), | ||
config = Map.put(main, field, other) do | ||
assert %{"foo" => result} = Subject.transform(:stores, %{foo: config}) | ||
assert %{store: store_tuple, cache: cache_tuple} = result | ||
assert store_tuple != cache_tuple | ||
end | ||
end | ||
|
||
property "raises when there is unknown type" do | ||
check all typ <- string(:alphanumeric), | ||
typ not in ~w(S3 Local Blackhole), | ||
config <- store_config(constant(typ)) do | ||
assert_raise RuntimeError, fn -> | ||
Subject.transform(:stores, %{foo: config}) | ||
end | ||
end | ||
end | ||
|
||
property "raises when path contains slash" do | ||
check all a <- string([?a..?z]), | ||
b <- string([?a..?z]), | ||
path = a <> "/" <> b, | ||
config <- store_config() do | ||
assert_raise RuntimeError, "'#{path}' cannot contain '/'", fn -> | ||
Subject.transform(:stores, %{path => config}) | ||
end | ||
end | ||
end | ||
|
||
test "raises when uses reserved name" do | ||
assert_raise RuntimeError, "'health' is reserved name", fn -> | ||
Subject.transform(:stores, %{health: %{type: "Blackhole"}}) | ||
end | ||
end | ||
|
||
property "other values are passed through unchanged" do | ||
check all key <- atom(:alphanumeric), | ||
key != :stores, | ||
value <- term() do | ||
assert value == Subject.transform(key, value) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
defmodule Imager.ConfigTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias Imager.Config, as: Subject | ||
|
||
test "ignores non-existent files" do | ||
assert :ok = Subject.init(path: "non-existent-file.toml") | ||
end | ||
|
||
test "raises on missing path param" do | ||
assert_raise KeyError, fn -> | ||
Subject.init([]) | ||
end | ||
end | ||
|
||
test "raises Toml.Error on invalid TOML" do | ||
assert_raise Toml.Error, fn -> | ||
Subject.init(path: "test/fixtures/incorrect.config.toml") | ||
end | ||
end | ||
|
||
test "sets config after successful run" do | ||
assert :ok = Subject.init(path: "test/fixtures/correct.config.toml") | ||
assert %{"test" => _} = Application.get_env(:imager, :stores) | ||
|
||
Application.delete_env(:imager, :stores) | ||
end | ||
|
||
test "merges config after successful run" do | ||
Application.put_env(:imager, :stores, %{"bar" => nil}) | ||
|
||
assert :ok = Subject.init(path: "test/fixtures/correct.config.toml") | ||
assert %{"test" => _, "bar" => _} = Application.get_env(:imager, :stores) | ||
|
||
Application.delete_env(:imager, :stores) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
defmodule Imager.StatsTest do | ||
use ExUnit.Case, async: true | ||
use ExUnitProperties | ||
|
||
property "tags contain all provided entries" do | ||
check all tags <- list_of(string(:alphanumeric)) do | ||
generated = Imager.Stats.tags(tags) | ||
|
||
for tag <- tags do | ||
assert tag in generated | ||
end | ||
end | ||
end | ||
|
||
property "tags contains default tags" do | ||
check all tags <- list_of(string(:alphanumeric)) do | ||
generated = Imager.Stats.tags(tags) | ||
|
||
assert "app:imager" in generated | ||
|
||
assert Enum.find(generated, fn | ||
"version:" <> _ -> true | ||
_ -> false | ||
end) | ||
end | ||
end | ||
end |
Oops, something went wrong.