-
Notifications
You must be signed in to change notification settings - Fork 109
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 #29 from oltarasenko/24-improve-tests
standardized existing pipeline testing
- Loading branch information
Showing
7 changed files
with
162 additions
and
142 deletions.
There are no files selected for viewing
File renamed without changes.
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,23 @@ | ||
defmodule Pipelines.CSVEncoderTest do | ||
use ExUnit.Case, async: false | ||
|
||
@valid %{first: "some", second: "data"} | ||
setup do | ||
on_exit(fn -> | ||
Application.put_env(:crawly, :item, [:title, :author, :time, :url]) | ||
end) | ||
end | ||
|
||
test "Converts a single-level map to a csv string" do | ||
Application.put_env(:crawly, :item, [:first, :second]) | ||
|
||
pipelines = [Crawly.Pipelines.CSVEncoder] | ||
item = @valid | ||
state = %{} | ||
|
||
{item, _state} = Crawly.Utils.pipe(pipelines, item, state) | ||
|
||
assert is_binary(item) | ||
assert item == ~S("some","data") | ||
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,45 @@ | ||
defmodule Pipelines.DuplicatesFilterTest do | ||
use ExUnit.Case, async: false | ||
|
||
@valid %{data: [%{some: "nested_data"}], id: "my_id"} | ||
setup do | ||
on_exit(fn -> | ||
Application.put_env(:crawly, :item_id, :title) | ||
end) | ||
end | ||
|
||
test "Drops duplicate items with the same item_id value" do | ||
Application.put_env(:crawly, :item_id, :id) | ||
pipelines = [Crawly.Pipelines.DuplicatesFilter] | ||
item = @valid | ||
state = %{} | ||
|
||
{item, state} = Crawly.Utils.pipe(pipelines, item, state) | ||
|
||
# filter state is updated | ||
assert %{"my_id" => true} = state.duplicates_filter | ||
# unchanged | ||
assert item == @valid | ||
|
||
# run again with same item and updated state should drop the item | ||
assert {false, state} = Crawly.Utils.pipe(pipelines, item, state) | ||
end | ||
|
||
test "Inactive when item_id is not set" do | ||
pipelines = [Crawly.Pipelines.DuplicatesFilter] | ||
item = @valid | ||
state = %{} | ||
|
||
{item, state} = Crawly.Utils.pipe(pipelines, item, state) | ||
|
||
# filter state is not updated | ||
assert Map.has_key?(state, :duplicates_filter) == false | ||
|
||
# run with same item and updated state should not drop the item | ||
assert {%{} = item, state} = Crawly.Utils.pipe(pipelines, item, state) | ||
assert Map.has_key?(state, :duplicates_filter) == false | ||
|
||
# unchanged | ||
assert item == @valid | ||
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,18 @@ | ||
defmodule Pipelines.JSONEncoderTest do | ||
use ExUnit.Case, async: false | ||
|
||
@valid %{data: [%{some: "nested_data"}]} | ||
|
||
test "Converts a given map to a json string" do | ||
pipelines = [Crawly.Pipelines.JSONEncoder] | ||
item = @valid | ||
state = %{} | ||
|
||
{item, _state} = Crawly.Utils.pipe(pipelines, item, state) | ||
|
||
assert is_binary(item) | ||
assert item =~ @valid.data |> hd() |> Map.get(:some) | ||
assert item =~ "data" | ||
assert item =~ "some" | ||
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,49 @@ | ||
defmodule Pipelines.ValidateTest do | ||
use ExUnit.Case, async: false | ||
|
||
@valid %{ | ||
title: "title", | ||
author: "data" | ||
} | ||
@invalid_missing %{ | ||
title: "title" | ||
} | ||
@invalid_nil %{ | ||
title: "title", | ||
author: nil | ||
} | ||
|
||
setup do | ||
on_exit(fn -> | ||
Application.put_env(:crawly, :item, [:title, :author, :time, :url]) | ||
end) | ||
end | ||
|
||
test "Returns item unchanged when has required fields" do | ||
Application.put_env(:crawly, :item, [:title, :author]) | ||
pipelines = [Crawly.Pipelines.Validate] | ||
item = @valid | ||
state = %{} | ||
|
||
{item, _state} = Crawly.Utils.pipe(pipelines, item, state) | ||
assert item == @valid | ||
end | ||
|
||
test "Drops items when missing required fields" do | ||
Application.put_env(:crawly, :item, [:title, :author]) | ||
pipelines = [Crawly.Pipelines.Validate] | ||
item = @invalid_missing | ||
state = %{} | ||
|
||
{false, _state} = Crawly.Utils.pipe(pipelines, item, state) | ||
end | ||
|
||
test "Drops items when required fields are equal to nil" do | ||
Application.put_env(:crawly, :item, [:title, :author]) | ||
pipelines = [Crawly.Pipelines.Validate] | ||
item = @invalid_nil | ||
state = %{} | ||
|
||
{false, _state} = Crawly.Utils.pipe(pipelines, item, state) | ||
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