Refactor Reportbooru API clients.

* Combine MissedSearchService, PostViewCountService, and
  PopularSearchService into single ReportbooruService class.
* Use Danbooru::Http for these services instead of HTTParty.
This commit is contained in:
evazion
2020-06-14 00:24:15 -05:00
parent 1846133cd6
commit a4df18e650
17 changed files with 102 additions and 143 deletions

View File

@@ -28,8 +28,17 @@ module Explore
end
end
context "#viewed" do
should "render" do
mock_post_view_rankings(Date.today, [[@post.id, 100]])
get viewed_explore_posts_path
assert_response :success
end
end
context "#searches" do
should "render" do
mock_post_search_rankings(Date.today, [["1girl", 100], ["original", 50]])
get searches_explore_posts_path
assert_response :success
end
@@ -37,6 +46,7 @@ module Explore
context "#missed_searches" do
should "render" do
mock_missed_search_rankings([["1girl", 100], ["original", 50]])
get missed_searches_explore_posts_path
assert_response :success
end

View File

@@ -3,13 +3,15 @@ require "test_helper"
class PostsControllerTest < ActionDispatch::IntegrationTest
context "The posts controller" do
setup do
PopularSearchService.stubs(:enabled?).returns(false)
@user = travel_to(1.month.ago) {create(:user)}
@post = as(@user) { create(:post, tag_string: "aaaa") }
end
context "index action" do
setup do
mock_post_search_rankings(Date.today, [["1girl", 100], ["original", 50]])
end
should "render" do
get posts_path
assert_response :success

View File

@@ -16,6 +16,7 @@ class StaticControllerTest < ActionDispatch::IntegrationTest
context "sitemap action" do
should "work" do
create_list(:post, 3)
mock_post_search_rankings(Time.zone.yesterday, [["1girl", 100.0], ["2girls", 50.0]])
get sitemap_path, as: :xml
assert_response :success
end

View File

@@ -43,8 +43,6 @@ class ActiveSupport::TestCase
setup do
Socket.stubs(:gethostname).returns("www.example.com")
mock_popular_search_service!
mock_missed_search_service!
WebMock.allow_net_connect!
storage_manager = StorageManager::Local.new(base_dir: Dir.mktmpdir("uploads-test-storage-"))

View File

@@ -1,12 +1,24 @@
module ReportbooruHelper
def mock_popular_search_service!
Danbooru.config.stubs(:reportbooru_server).returns("http://localhost:3003")
stub_request(:get, "http://localhost:3003/post_searches/month?date=#{Date.today}").to_return(body: "kantai_collection 1000.0\ntouhou 500.0")
stub_request(:get, "http://localhost:3003/post_searches/day?date=#{Date.today}").to_return(body: "kantai_collection 1000.0\ntouhou 500.0")
def mock_request(url, method: :get, status: 200, body: nil, http: Danbooru::Http.any_instance)
response = HTTP::Response.new(status: status, body: body, version: "1.1")
http.stubs(method).with(url).returns(response)
end
def mock_missed_search_service!
Danbooru.config.stubs(:reportbooru_server).returns("http://localhost:3003")
stub_request(:get, "http://localhost:3003/missed_searches").to_return(body: "kantai_collection 1000.0\ntouhou 500.0")
def mock_post_search_rankings(date = Date.today, rankings)
Danbooru.config.stubs(:reportbooru_server).returns("http://localhost:1234")
url = "http://localhost:1234/post_searches/rank?date=#{date}"
mock_request(url, body: rankings.to_json)
end
def mock_missed_search_rankings(date = Date.today, rankings)
Danbooru.config.stubs(:reportbooru_server).returns("http://localhost:1234")
url = "http://localhost:1234/missed_searches"
mock_request(url, body: rankings.to_json)
end
def mock_post_view_rankings(date = Date.today, rankings)
Danbooru.config.stubs(:reportbooru_server).returns("http://localhost:1234")
url = "http://localhost:1234/post_views/rank?date=#{date}"
mock_request(url, body: rankings.to_json)
end
end

View File

@@ -1,8 +1,8 @@
require 'test_helper'
class PostViewCountServiceTest < ActiveSupport::TestCase
class ReportbooruServiceTest < ActiveSupport::TestCase
def setup
@service = PostViewCountService.new(reportbooru_server: "http://localhost:1234")
@service = ReportbooruService.new(reportbooru_server: "http://localhost:1234")
@post = create(:post)
@date = "2000-01-01"
end