Skip to content

Commit

Permalink
Tweet tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
benubois committed Jan 15, 2018
1 parent 8af87b5 commit ff1e519
Show file tree
Hide file tree
Showing 8 changed files with 391 additions and 9 deletions.
8 changes: 5 additions & 3 deletions app/jobs/save_pages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ def perform(entry_id)

key = FeedbinUtils.page_cache_key(url)
begin
saved_pages[url] = Rails.cache.fetch(key) do
page = Rails.cache.fetch(key)
if !page
Librato.increment 'readability.first_parse'
MercuryParser.parse(url)
page = MercuryParser.parse(url)
Rails.cache.write(key, page)
end
rescue
saved_pages[url] = page.to_h
end

entry.data["saved_pages"] = saved_pages
Expand Down
12 changes: 8 additions & 4 deletions app/models/mercury_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ def domain
result['domain']
end

def to_h
{
result: result,
url: url
}
end

private

def result
Expand All @@ -54,10 +61,7 @@ def result
end

def marshal_dump
{
result: result,
url: url
}
to_h
end

def marshal_load(data)
Expand Down
11 changes: 11 additions & 0 deletions test/controllers/entries_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ class EntriesControllerTest < ActionController::TestCase
assert_response :success
end

test "should get show with tweet" do
entry = create_tweet_entry(@user.feeds.first)
url = "https://mercury.postlight.com/parser?url=https://9to5mac.com/2018/01/12/final-cut-pro-x-how-to-improve-slow-motion-in-your-projects-video/"
stub_request_file('parsed_page.json', url, headers: {"Content-Type" => "application/json; charset=utf-8"})
SavePages.new().perform(entry.id)

login_as @user
get :show, params: {id: entry}, xhr: true
assert_response :success
end

test "should get content" do
login_as @user
content = Faker::Lorem.paragraph
Expand Down
27 changes: 27 additions & 0 deletions test/jobs/save_pages_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'test_helper'

class SavePagesTest < ActiveSupport::TestCase

setup do
user = users(:ben)
@entry = create_tweet_entry(user.feeds.first)
end

test "should build" do
article_url = "https://9to5mac.com/2018/01/12/final-cut-pro-x-how-to-improve-slow-motion-in-your-projects-video/"
url = "https://mercury.postlight.com/parser?url=#{article_url}"
stub_request_file('parsed_page.json', url, headers: {"Content-Type" => "application/json; charset=utf-8"})

SavePages.new().perform(@entry.id)

saved_pages = @entry.reload.data["saved_pages"]
assert saved_pages.has_key?(article_url), "Entry should have saved page"
page = saved_pages[article_url]["result"]

%w{title author url date_published content domain}.each do |key|
assert page.has_key?(key), "page is missing #{key}"
end

end

end
7 changes: 7 additions & 0 deletions test/support/factory_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,11 @@ def mark_unread(user)
UnreadEntry.create_from_owners(user, entry)
end
end

def create_tweet_entry(feed)
entry = create_entry(feed)
entry.data["tweet"] = load_tweet
entry.save!
entry
end
end

0 comments on commit ff1e519

Please sign in to comment.