tests: move test/helpers to test/test_helpers.
The Rails convention is for test/helpers to be used for testing the view helpers in app/helpers. We were using it to store certain utility methods instead. Move these to test/test_helpers so that test/helpers can be used for its intended purpose.
This commit is contained in:
24
test/test_helpers/download_helper.rb
Normal file
24
test/test_helpers/download_helper.rb
Normal file
@@ -0,0 +1,24 @@
|
||||
module DownloadTestHelper
|
||||
def assert_downloaded(expected_filesize, source)
|
||||
tempfile = Tempfile.new("danbooru-test")
|
||||
download = Downloads::File.new(source, tempfile.path)
|
||||
|
||||
assert_nothing_raised(Downloads::File::Error) do
|
||||
download.download!
|
||||
end
|
||||
|
||||
assert_equal(expected_filesize, tempfile.size, "Tested source URL: #{source}")
|
||||
end
|
||||
|
||||
def assert_rewritten(expected_source, test_source)
|
||||
tempfile = Tempfile.new("danbooru-test")
|
||||
download = Downloads::File.new(test_source, tempfile.path)
|
||||
|
||||
rewritten_source, _, _ = download.before_download(test_source, {})
|
||||
assert_equal(expected_source, rewritten_source, "Tested source URL: #{test_source}")
|
||||
end
|
||||
|
||||
def assert_not_rewritten(source)
|
||||
assert_rewritten(source, source)
|
||||
end
|
||||
end
|
||||
32
test/test_helpers/iqdb_test_helper.rb
Normal file
32
test/test_helpers/iqdb_test_helper.rb
Normal file
@@ -0,0 +1,32 @@
|
||||
module IqdbTestHelper
|
||||
def mock_iqdb_service!
|
||||
mock_sqs_service = Class.new do
|
||||
def initialize
|
||||
@commands = []
|
||||
end
|
||||
|
||||
def commands
|
||||
@commands
|
||||
end
|
||||
|
||||
def send_message(msg)
|
||||
@commands << msg.split(/\n/).first
|
||||
end
|
||||
end
|
||||
|
||||
service = mock_sqs_service.new
|
||||
Post.stubs(:iqdb_sqs_service).returns(service)
|
||||
Post.stubs(:iqdb_enabled?).returns(true)
|
||||
|
||||
Danbooru.config.stubs(:iqdbs_auth_key).returns("hunter2")
|
||||
Danbooru.config.stubs(:iqdbs_server).returns("http://localhost:3004")
|
||||
end
|
||||
|
||||
def mock_iqdb_matches!(post_or_source, matches)
|
||||
source = post_or_source.is_a?(Post) ? post_or_source.complete_preview_file_url : post_or_source
|
||||
url = "http://localhost:3004/similar?key=hunter2&url=#{CGI.escape source}&ref"
|
||||
body = matches.map { |post| { post_id: post.id } }.to_json
|
||||
|
||||
stub_request(:get, url).to_return(body: body)
|
||||
end
|
||||
end
|
||||
30
test/test_helpers/pool_archive_test_helper.rb
Normal file
30
test/test_helpers/pool_archive_test_helper.rb
Normal file
@@ -0,0 +1,30 @@
|
||||
module PoolArchiveTestHelper
|
||||
def mock_pool_archive_service!
|
||||
mock_sqs_service = Class.new do
|
||||
def send_message(msg, *options)
|
||||
_, json = msg.split(/\n/)
|
||||
json = JSON.parse(json)
|
||||
prev = PoolArchive.where(pool_id: json["pool_id"]).order("id desc").first
|
||||
if merge?(prev, json)
|
||||
prev.update_columns(json)
|
||||
else
|
||||
PoolArchive.create(json)
|
||||
end
|
||||
end
|
||||
|
||||
def merge?(prev, json)
|
||||
prev && (prev.updater_id == json["updater_id"]) && (prev.updated_at >= 1.hour.ago)
|
||||
end
|
||||
end
|
||||
|
||||
PoolArchive.stubs(:sqs_service).returns(mock_sqs_service.new)
|
||||
end
|
||||
|
||||
def start_pool_archive_transaction
|
||||
PoolArchive.connection.begin_transaction joinable: false
|
||||
end
|
||||
|
||||
def rollback_pool_archive_transaction
|
||||
PoolArchive.connection.rollback_transaction
|
||||
end
|
||||
end
|
||||
51
test/test_helpers/post_archive_test_helper.rb
Normal file
51
test/test_helpers/post_archive_test_helper.rb
Normal file
@@ -0,0 +1,51 @@
|
||||
module PostArchiveTestHelper
|
||||
def setup
|
||||
super
|
||||
|
||||
mock_post_archive_service!
|
||||
start_post_archive_transaction
|
||||
end
|
||||
|
||||
def teardown
|
||||
super
|
||||
|
||||
rollback_post_archive_transaction
|
||||
end
|
||||
|
||||
def mock_post_archive_service!
|
||||
mock_sqs_service = Class.new do
|
||||
def send_message(msg, *options)
|
||||
_, json = msg.split(/\n/)
|
||||
json = JSON.parse(json)
|
||||
json.delete("created_at")
|
||||
json["version"] = 1 + PostArchive.where(post_id: json["post_id"]).count
|
||||
prev = PostArchive.where(post_id: json["post_id"]).order("id desc").first
|
||||
if prev
|
||||
json["added_tags"] = json["tags"].scan(/\S+/) - prev.tags.scan(/\S+/)
|
||||
json["removed_tags"] = prev.tags.scan(/\S+/) - json["tags"].scan(/\S+/)
|
||||
else
|
||||
json["added_tags"] = json["tags"].scan(/\S+/)
|
||||
end
|
||||
if merge?(prev, json)
|
||||
prev.update_columns(json)
|
||||
else
|
||||
PostArchive.create(json)
|
||||
end
|
||||
end
|
||||
|
||||
def merge?(prev, json)
|
||||
prev && (prev.updater_id == json["updater_id"]) && (prev.updated_at >= 1.hour.ago)
|
||||
end
|
||||
end
|
||||
|
||||
PostArchive.stubs(:sqs_service).returns(mock_sqs_service.new)
|
||||
end
|
||||
|
||||
def start_post_archive_transaction
|
||||
PostArchive.connection.begin_transaction joinable: false
|
||||
end
|
||||
|
||||
def rollback_post_archive_transaction
|
||||
PostArchive.connection.rollback_transaction
|
||||
end
|
||||
end
|
||||
12
test/test_helpers/reportbooru_helper.rb
Normal file
12
test/test_helpers/reportbooru_helper.rb
Normal file
@@ -0,0 +1,12 @@
|
||||
module ReportbooruHelper
|
||||
def mock_popular_search_service!
|
||||
Danbooru.config.stubs(:reportbooru_server).returns("http://localhost:3003")
|
||||
stub_request(:get, "http://localhost:3003/hits/month?date=#{Date.today}").to_return(body: "kantai_collection 1000.0\ntouhou 500.0")
|
||||
stub_request(:get, "http://localhost:3003/hits/day?date=#{Date.today}").to_return(body: "kantai_collection 1000.0\ntouhou 500.0")
|
||||
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")
|
||||
end
|
||||
end
|
||||
23
test/test_helpers/saved_search_test_helper.rb
Normal file
23
test/test_helpers/saved_search_test_helper.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
module SavedSearchTestHelper
|
||||
def mock_saved_search_service!
|
||||
mock_sqs_service = Class.new do
|
||||
def initialize
|
||||
@commands = []
|
||||
end
|
||||
|
||||
def commands
|
||||
@commands
|
||||
end
|
||||
|
||||
def send_message(msg)
|
||||
@commands << msg.split(/\n/).first
|
||||
end
|
||||
end
|
||||
|
||||
service = mock_sqs_service.new
|
||||
SavedSearch.stubs(:sqs_service).returns(service)
|
||||
Danbooru.config.stubs(:aws_sqs_saved_search_url).returns("http://localhost:3002")
|
||||
Danbooru.config.stubs(:listbooru_auth_key).returns("blahblahblah")
|
||||
Danbooru.config.stubs(:listbooru_server).returns("http://localhost:3001")
|
||||
end
|
||||
end
|
||||
23
test/test_helpers/upload_test_helper.rb
Normal file
23
test/test_helpers/upload_test_helper.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
module UploadTestHelper
|
||||
def upload_file(path, content_type, filename)
|
||||
tempfile = Tempfile.new(filename)
|
||||
FileUtils.copy_file(path, tempfile.path)
|
||||
|
||||
(class << tempfile; self; end).class_eval do
|
||||
alias local_path path
|
||||
define_method(:tempfile) {self}
|
||||
define_method(:original_filename) {filename}
|
||||
define_method(:content_type) {content_type}
|
||||
end
|
||||
|
||||
tempfile
|
||||
end
|
||||
|
||||
def upload_jpeg(path)
|
||||
upload_file(path, "image/jpeg", File.basename(path))
|
||||
end
|
||||
|
||||
def upload_zip(path)
|
||||
upload_file(path, "application/zip", File.basename(path))
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user