Skip to content

Commit

Permalink
Write passing tests for posts.
Browse files Browse the repository at this point in the history
  • Loading branch information
dodecaphonic committed May 31, 2011
1 parent 307e135 commit 01b629b
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 8 deletions.
72 changes: 72 additions & 0 deletions lib/tumble_out/contentizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,81 @@ def parse(raw_post)
raw_post["slug"],
raw_post["format"])

title, body = case post.type
when "audio"
parse_audio raw_post
when "video"
parse_video raw_post
when "regular"
rt = raw_post.search("regular-title")
rb = raw_post.search("regular-body")

[rt ? rt.text : nil, rb.text]
when "conversation"
parse_conversation raw_post
when "quote"
qt = raw_post.search("quote-text")
qs = raw_post.search("quote-source")

[nil, "#{qt.text}<br/>#{qs}"]
when "photo"
parse_photo raw_post
when "answer"
parse_answer raw_post
end

post.title = title
post.body = body

post
end

def parse_audio(post)
title = post.search("id3-title").text
body = post.search("audio-player").inner_html +
post.search("audio-caption").inner_html

[title, body]
end

def parse_video(post)
body = post.search("video-source").inner_html +
post.search("video-caption").inner_html

[nil, body]
end

def parse_conversation(post)
title = post.search("conversation-title").text
body = "<ul class=\"conversation\">\n"

post.search("conversation line").each { |line|
body += "\t<li>#{line.attr "label"} #{line.text}"
}

body += "</ul>"

[title, body]
end

def parse_answer(post)
question = post.search("question").text
answer = post.search("answer").inner_html

body = "<div class=\"question\">#{question}</div>\n"
body += "<div class=\"answer\">#{answer}</div>"

[nil, body]
end

def parse_photo(post)
src = post.search("photo-url").text
caption = post.search("photo-caption").inner_html
body = "<img src=\"#{src}\"><br/>#{caption}"

[nil, body]
end

def raw_posts(offset=0)
doc = Nokogiri::XML(Net::HTTP.get(URI.parse("http://#{@url}/api/read")))

Expand Down
6 changes: 6 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'minitest/autorun'
require 'mocha'

require File.join(File.dirname(__FILE__), "..",
"lib", "tumble_out")

14 changes: 6 additions & 8 deletions test/unit/test_contentizer.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
require 'minitest/autorun'
require 'mocha'

require File.join(File.dirname(__FILE__), "..", "..", "lib", "tumble_out")
require File.join(File.dirname(__FILE__), "..",
"test_helper")

class TestContentizer < MiniTest::Unit::TestCase
def setup
raw_data = open(
File.join(File.dirname(__FILE__), "..",
"assets", "sample.xml")
)

Net::HTTP.expects(:get).
with(URI.parse("http://sample.tumblr.com/api/read")).returns raw_data
@contentizer = TumbleOut::Contentizer.new("sample.tumblr.com")
end

def test_if_number_of_posts_is_correct
posts = @contentizer.posts

assert_equal 7, posts.size
end

def test_that_post_types_are_of_a_given_count
types = @contentizer.posts.map { |p| p.type }

assert_equal 7, types.size

end

def test_whether_posts_are_of_specific_types
Expand All @@ -34,7 +32,7 @@ def test_whether_posts_are_of_specific_types
post_types = @contentizer.posts.map { |p|
p.type
}.uniq.sort

assert_equal valid_types, post_types
end

Expand Down
31 changes: 31 additions & 0 deletions test/unit/test_post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require File.join(File.dirname(__FILE__), "..",
"test_helper")

class TestPost < MiniTest::Unit::TestCase
def setup
raw_data = open(
File.join(File.dirname(__FILE__), "..",
"assets", "sample.xml")
)

Net::HTTP.expects(:get).
with(URI.parse("http://sample.tumblr.com/api/read")).returns raw_data
contentizer = TumbleOut::Contentizer.new("sample.tumblr.com")
@posts = contentizer.posts
end

def test_if_body_is_set
assert @posts.all? { |p|
!((p.body.nil? || p.body.empty?))
}
end

def test_whether_content_matches_type
convo = @posts.find { |p| p.type == "conversation" }

doc = Nokogiri::HTML(convo.body)
ul = doc.search("ul[class=conversation]")
assert_equal 1, ul.size
assert_equal 4, ul.search("li").size
end
end

0 comments on commit 01b629b

Please sign in to comment.