Add feed parsing tests

This commit is contained in:
Shadowfacts 2019-09-01 15:49:30 -04:00
parent 39fa3f80a6
commit 284f840613
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
10 changed files with 220 additions and 5 deletions

View File

@ -12,7 +12,7 @@ defmodule FeedParser.Parser.Atom do
def accepts(data, content_type) do def accepts(data, content_type) do
case content_type do case content_type do
"application/atom+xml" -> "application/atom+xml" ->
true {true, XML.parse(data)}
_ when content_type in ["text/xml", "application/xml"] -> _ when content_type in ["text/xml", "application/xml"] ->
doc = XML.parse(data) doc = XML.parse(data)

View File

@ -1,8 +1,4 @@
defmodule FeedParserTest do defmodule FeedParserTest do
use ExUnit.Case use ExUnit.Case
doctest FeedParser doctest FeedParser
test "greets the world" do
assert FeedParser.hello() == :world
end
end end

20
test/fixtures/atom/feed.xml vendored Normal file
View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Example Feed</title>
<link href="http://example.org/"/>
<updated>2003-12-13T18:30:02Z</updated>
<author>
<name>John Doe</name>
</author>
<id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
<entry>
<title>Atom-Powered Robots Run Amok</title>
<link href="http://example.org/2003/12/13/atom03"/>
<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
<updated>2003-12-13T18:30:02Z</updated>
<summary>Some text.</summary>
</entry>
</feed>

18
test/fixtures/jsonfeed/feed.json vendored Normal file
View File

@ -0,0 +1,18 @@
{
"version": "https://jsonfeed.org/version/1",
"title": "My Example Feed",
"home_page_url": "https://example.org/",
"feed_url": "https://example.org/feed.json",
"items": [
{
"id": "2",
"content_text": "This is a second item.",
"url": "https://example.org/second-item"
},
{
"id": "1",
"content_html": "<p>Hello, world!</p>",
"url": "https://example.org/initial-post"
}
]
}

22
test/fixtures/rss2/feed.xml vendored Normal file
View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"><script/>
<channel>
<title>Liftoff News</title>
<link>http://liftoff.msfc.nasa.gov/</link>
<description>Liftoff to Space Exploration.</description>
<language>en-us</language>
<pubDate>Tue, 10 Jun 2003 04:00:00 GMT</pubDate>
<lastBuildDate>Tue, 10 Jun 2003 09:41:01 GMT</lastBuildDate>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<generator>Weblog Editor 2.0</generator>
<managingEditor>editor@example.com</managingEditor>
<webMaster>webmaster@example.com</webMaster>
<item>
<title>Star City</title>
<link>http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp</link>
<description>How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's &lt;a href="http://howe.iki.rssi.ru/GCTC/gctc_e.htm"&gt;Star City&lt;/a&gt;.</description>
<pubDate>Tue, 03 Jun 2003 09:39:21 UTC</pubDate>
<guid>http://liftoff.msfc.nasa.gov/2003/06/03.html#item573</guid>
</item>
</channel>
</rss>

58
test/fixtures/rssinjson/feed.json vendored Normal file
View File

@ -0,0 +1,58 @@
{
"rss": {
"version": "2.0",
"xmlns:source": "http://source.scripting.com/",
"channel": {
"title": "Scripting News",
"link": "http://scripting.com/",
"description": "Scripting News, the weblog started in 1994 that bootstrapped the blogging revolution. 🚀",
"pubDate": "Sat, 31 Aug 2019 18:04:35 GMT",
"lastBuildDate": "Sat, 31 Aug 2019 22:49:49 GMT",
"language": "en-us",
"copyright": "&copy; 1994-2019 <a href=\"http://davewiner.com/\">Dave Winer</a>.",
"generator": "oldSchool v0.5.29",
"docs": "https://github.com/scripting/Scripting-News/blob/master/rss-in-json/README.md",
"source:localTime": "Sat, August 31, 2019 6:49 PM EDT",
"cloud": {
"domain": "rpc.rsscloud.io",
"port": 5337,
"path": "/pleaseNotify",
"registerProcedure": "",
"protocol": "http-post"
},
"source:account": [
{
"service": "twitter",
"#value": "davewiner"
},
{
"service": "facebook",
"#value": "dave.winer.12"
},
{
"service": "github",
"#value": "scripting"
},
{
"service": "linkedin",
"#value": "scripting"
}
],
"item": [
{
"link": "http://scripting.com/2019/08/31.html#a151142",
"description": "I've been reading a lot about the <a href=\"https://www.media.mit.edu/\">MIT Media Lab</a> lately. I read that founder of the lab, <a href=\"https://en.wikipedia.org/wiki/Nicholas_Negroponte\">Nicholas Negroponte</a>, made a deal with <a href=\"https://en.wikipedia.org/wiki/Louis_Rossetto\">Louis</a> and <a href=\"https://en.wikipedia.org/wiki/Jane_Metcalfe\">Jane</a>, the founders of Wired, he bought 10 percent of the magazine for $75K and got a column on the inside back cover, which was prime real estate. This got me thinking. The Media Lab and Wired were made for each other. I loved Wired, but I also was aware that they went for gee-whiz tech over substantial tech more than I wished they would. The Media Lab produced lots of buzzworthy demos that never turned into usable tech. What I really wanted? A Wired/Media Lab that only covered open source tech. It was the style of Wired that attracted me, at the time all tech pubs were made for geeks who were suspicious of anything that looked too slick. I want beautiful artistic simplicity, the kind of look that Wired was so good at, applied to practical almost mundane tech. The developers who love all that stuff are the people I want to work with. Stuff that works and that can change the world right now, looking good, not popcorn-like gee-whiz tech that has limited depth or substance. ",
"pubDate": "Sat, 31 Aug 2019 15:11:42 UTC",
"guid": "http://scripting.com/2019/08/31.html#a151142",
"source:outline": {
"text": "I've been reading a lot about the <a href=\"https://www.media.mit.edu/\">MIT Media Lab</a> lately. I read that founder of the lab, <a href=\"https://en.wikipedia.org/wiki/Nicholas_Negroponte\">Nicholas Negroponte</a>, made a deal with <a href=\"https://en.wikipedia.org/wiki/Louis_Rossetto\">Louis</a> and <a href=\"https://en.wikipedia.org/wiki/Jane_Metcalfe\">Jane</a>, the founders of Wired, he bought 10 percent of the magazine for $75K and got a column on the inside back cover, which was prime real estate. This got me thinking. The Media Lab and Wired were made for each other. I loved Wired, but I also was aware that they went for gee-whiz tech over substantial tech more than I wished they would. The Media Lab produced lots of buzzworthy demos that never turned into usable tech. What I really wanted? A Wired/Media Lab that only covered open source tech. It was the style of Wired that attracted me, at the time all tech pubs were made for geeks who were suspicious of anything that looked too slick. I want beautiful artistic simplicity, the kind of look that Wired was so good at, applied to practical almost mundane tech. The developers who love all that stuff are the people I want to work with. Stuff that works and that can change the world right now, looking good, not popcorn-like gee-whiz tech that has limited depth or substance. ",
"created": "Sat, 31 Aug 2019 15:11:42 UTC",
"type": "outline",
"image": "http://scripting.com/images/2019/08/31/thisIsNextYear.png",
"permalink": "http://scripting.com/2019/08/31.html#a151142"
}
}
]
}
}
}

25
test/parser/atom_test.exs Normal file
View File

@ -0,0 +1,25 @@
defmodule FeedParser.Parser.AtomTest do
use ExUnit.Case
alias FeedParser.Parser.Atom
doctest Atom
test "matches atom feed" do
data = File.read!("test/fixtures/atom/feed.xml")
assert {true, _} = Atom.accepts(data, "application/atom+xml")
assert {true, _} = Atom.accepts(data, "application/xml")
end
test "parses atom feed" do
data = File.read!("test/fixtures/atom/feed.xml")
{true, parsed_data} = Atom.accepts(data, "application/atom+xml")
assert {:ok, %FeedParser.Feed{} = feed} = Atom.parse_feed(parsed_data)
assert feed.site_url == "http://example.org/"
assert feed.title == "Example Feed"
assert [%FeedParser.Item{} = item] = feed.items
assert item.guid == "urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a"
assert item.title == "Atom-Powered Robots Run Amok"
assert item.url == "http://example.org/2003/12/13/atom03"
assert item.date == ~U[2003-12-13 18:30:02Z]
assert item.content == "Some text."
end
end

View File

@ -0,0 +1,26 @@
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 "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/"
IO.inspect(feed.items)
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
end

28
test/parser/rss2_test.exs Normal file
View File

@ -0,0 +1,28 @@
defmodule FeedParser.Parser.RSS2Test do
use ExUnit.Case
alias FeedParser.Parser.RSS2
doctest RSS2
test "matches rss2 feed" do
data = File.read!("test/fixtures/rss2/feed.xml")
assert {true, _} = RSS2.accepts(data, "application/rss+xml")
assert {true, _} = RSS2.accepts(data, "application/xml")
end
test "parses rss2 feed" do
data = File.read!("test/fixtures/rss2/feed.xml")
{true, parsed_data} = RSS2.accepts(data, "application/rss+xml")
assert {:ok, %FeedParser.Feed{} = feed} = RSS2.parse_feed(parsed_data)
assert feed.title == "Liftoff News"
assert feed.site_url == "http://liftoff.msfc.nasa.gov/"
assert [%FeedParser.Item{} = item] = feed.items
assert item.title == "Star City"
assert item.url == "http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp"
assert item.content ==
"How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's <a href=\"http://howe.iki.rssi.ru/GCTC/gctc_e.htm\">Star City</a>."
assert item.date == ~U[2003-06-03 09:39:21Z]
assert item.guid == "http://liftoff.msfc.nasa.gov/2003/06/03.html#item573"
end
end

View File

@ -0,0 +1,22 @@
defmodule FeedParser.Parser.RSSInJSONTest do
use ExUnit.Case
alias FeedParser.Parser.RSSInJSON
doctest RSSInJSON
test "matches rss-in-json feed" do
data = File.read!("test/fixtures/rssinjson/feed.json")
assert {true, _} = RSSInJSON.accepts(data, "application/json")
end
test "parses rss-in-json feed" do
data = File.read!("test/fixtures/rssinjson/feed.json")
{true, parsed_data} = RSSInJSON.accepts(data, "application/json")
assert {:ok, %FeedParser.Feed{} = feed} = RSSInJSON.parse_feed(parsed_data)
assert feed.title == "Scripting News"
assert feed.site_url == "http://scripting.com/"
assert [%FeedParser.Item{} = item] = feed.items
assert item.url == "http://scripting.com/2019/08/31.html#a151142"
assert item.guid == "http://scripting.com/2019/08/31.html#a151142"
assert item.date == ~U[2019-08-31 15:11:42Z]
end
end