Skip to content

Commit 4733eab

Browse files
authored
Merge pull request #694 from banzay/tests-color-cloudinary
Add unit test for CodeCorps.Helpers.CloudinaryUrl and RandomIconColor
2 parents 939b6c0 + c9c342c commit 4733eab

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
defmodule Cloudex.CloudinaryApi.Test do
2+
@moduledoc """
3+
Testing stub for `Cloudex`,
4+
5+
Each function should have the same signature as `Cloudex`.
6+
"""
7+
8+
defmodule Url do
9+
def for(public_id, options) do
10+
{public_id, options}
11+
end
12+
end
13+
end
+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
defmodule CodeCorps.Helpers.CloudinaryUrl do
22

3+
@cloudex Application.get_env(:cloudex, :cloudinary_api)
4+
35
def for(nil, _options, version, default_color, type) do
46
"#{Application.get_env(:code_corps, :asset_host)}/icons/#{type}_default_#{version}_#{default_color}.png"
57
end
68
def for(public_id, options, _version, _default_color, _type) do
7-
Cloudex.Url.for(public_id, options)
9+
@cloudex.Url.for(public_id, options)
810
end
911
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
defmodule CodeCorps.Helpers.CloudinaryUrlTest do
2+
use ExUnit.Case, async: true
3+
4+
test "calls Cloudex.Url.for with correct arguments" do
5+
expected_args = {:test_public_id, %{test_option: nil}}
6+
args = CodeCorps.Helpers.CloudinaryUrl.for(:test_public_id, %{test_option: nil}, nil, nil, nil)
7+
assert expected_args == args
8+
end
9+
10+
test "returns correct url if called without public_id" do
11+
expected_url = "#{Application.get_env(:code_corps, :asset_host)}/icons/type1_default_version1_color1.png"
12+
url = CodeCorps.Helpers.CloudinaryUrl.for(nil, %{}, "version1", "color1", "type1")
13+
assert expected_url == url
14+
end
15+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
defmodule CodeCorps.RandomIconColor.RandomIconColorTest do
2+
use ExUnit.Case, async: true
3+
import CodeCorps.Helpers.RandomIconColor
4+
import Ecto.Changeset
5+
6+
test "inserts color into changeset" do
7+
changeset = generate_icon_color(cast({%{}, %{}}, %{}, []), :color_key)
8+
assert get_field(changeset, :color_key) == "blue"
9+
end
10+
11+
test "ignores invalid changeset" do
12+
changeset = {%{}, %{color_key: :required}}
13+
|> cast(%{}, [])
14+
|> validate_required(:color_key)
15+
assert generate_icon_color(changeset, :color_key) == changeset
16+
end
17+
end

0 commit comments

Comments
 (0)