feed_parser/test/parser/jsonfeed_test.exs

81 lines
2.4 KiB
Elixir

defmodule FeedParser.Parser.JSONFeedTest do
use ExUnit.Case
alias FeedParser.Parser.JSONFeed
doctest JSONFeed
test "matches json feed" do
data = File.read!("test/fixtures/jsonfeed/feed.json")
assert {true, _} = JSONFeed.accepts(data, "application/json")
end
test "matches v1.1 feed" do
data = File.read!("test/fixtures/jsonfeed/v1_1.json")
assert {true, _} = JSONFeed.accepts(data, "application/feed+json")
end
test "parses json feed" do
data = File.read!("test/fixtures/jsonfeed/feed.json")
{true, parsed_data} = JSONFeed.accepts(data, "application/json")
assert {:ok, %FeedParser.Feed{} = feed} = JSONFeed.parse_feed(parsed_data)
assert feed.title == "My Example Feed"
assert feed.site_url == "https://example.org/"
assert feed.last_updated == nil
assert [%FeedParser.Item{} = item2, %FeedParser.Item{} = item1] = feed.items
assert item2.guid == "2"
assert item2.content == "This is a second item."
assert item2.url == "https://example.org/second-item"
assert item1.guid == "1"
assert item1.content == "<p>Hello, world!</p>"
assert item1.url == "https://example.org/initial-post"
end
test "parses item author" do
assert {:ok, %FeedParser.Feed{items: items}} =
JSONFeed.parse_feed(%{
"title" => "test",
"items" => [
%{
"id" => "1",
"author" => %{
"name" => "foo"
}
}
]
})
assert [%FeedParser.Item{creator: "foo"}] = items
end
test "falls back to feed author" do
assert {:ok, %FeedParser.Feed{items: items}} =
JSONFeed.parse_feed(%{
"title" => "test",
"author" => %{
"name" => "foo"
},
"items" => [
%{
"id" => "1"
}
]
})
assert [%FeedParser.Item{creator: "foo"}] = items
end
test "handles multiple authors" do
assert {:ok, %FeedParser.Feed{items: items}} =
JSONFeed.parse_feed(%{
"title" => "test",
"items" => [
%{
"id" => "1",
"authors" => [%{"name" => "foo"}, %{"name" => "bar"}]
}
]
})
assert [%FeedParser.Item{creator: "foo, bar"}] = items
end
end