Foundation: fix some video posts not being extracted

Also adjusts SourceTestHelper to not autogenerate contexts, so that
tests can be launched individually.
This commit is contained in:
nonamethanks
2022-04-21 17:50:10 +02:00
parent edb67b1b81
commit e6cb255a7a
6 changed files with 224 additions and 197 deletions

View File

@@ -4,51 +4,42 @@ module SourceTestHelper
# * If download_size is nil, it tests that the file is downloaded correctly, otherwise it also checks the filesize.
# * If deleted is true, it skips the downloading check, but it still tries everything else and makes sure nothing breaks.
# * Any passed kwargs parameter is tested against the strategy.
def strategy_should_work(url, referer: nil, download_size: nil, deleted: false, **methods_to_test)
context "a strategy for #{url}#{", referer: #{referer}" if referer.present?}".chomp do
strategy = Source::Extractor.find(url, referer)
should "not raise anything" do
assert_nothing_raised { strategy.to_h }
end
def strategy_should_work(url, arguments)
# XXX: can't use **kwargs because of a bug with shoulda-context
referer, download_size, deleted = [:referer, :download_size, :deleted].map { |arg| arguments.delete(arg) }
should "make sure that image_urls is an array of valid elements" do
assert((strategy.image_urls.instance_of? Array))
assert_not(strategy.image_urls.include?(nil))
end
strategy = Source::Extractor.find(url, referer)
should_download_successfully(strategy, download_size) unless deleted
# {profile_url: nil}[:profile_url].present? -> false
# Doing it this way instead we can check profile_url even if it's passed as a nil.
if methods_to_test.include? :profile_url
profile_url = methods_to_test.delete(:profile_url)
should_handle_artists_correctly(strategy, profile_url)
end
tags = methods_to_test.delete(:tags)
should_validate_tags(strategy, tags)
# check any method that is passed as kwargs, in order to hardcode as few thingss as possible
methods_to_test.each do |method_name, expected_value|
should "make sure that '#{method_name}' matches" do
if expected_value.instance_of? Regexp
assert_match(expected_value, strategy.try(method_name))
elsif expected_value.nil?
assert_nil(strategy.try(method_name))
else
assert_equal(expected_value, strategy.try(method_name))
end
end
end
should "not raise anything" do
assert_nothing_raised { strategy.to_h }
end
should "make sure that image_urls is an array of valid elements" do
assert((strategy.image_urls.instance_of? Array))
assert_not(strategy.image_urls.include?(nil))
end
should_download_successfully(strategy, download_size) unless deleted
# {profile_url: nil}[:profile_url].present? -> false
# Doing it this way instead we can check profile_url even if it's passed as a nil.
if arguments.include? :profile_url
profile_url = arguments.delete(:profile_url)
should_handle_artists_correctly(strategy, profile_url)
end
tags = arguments.delete(:tags)
should_validate_tags(strategy, tags)
should_match_source_data(strategy, arguments)
end
def should_download_successfully(strategy, download_size = nil)
should "download successfully" do
file = strategy.download_file!(strategy.image_urls.first)
if download_size.present?
assert_equal(expected_filesize, file.size)
assert_equal(download_size, file.size)
else
assert_not_nil(file.size)
end
@@ -90,4 +81,21 @@ module SourceTestHelper
end
end
def should_match_source_data(strategy, methods_to_test)
# check any method that is passed as kwargs, in order to hardcode as few things as possible
# XXX can't use **kwargs because of a bug with shoulda-context, so we're using a hash temporarily
methods_to_test.each do |method_name, expected_value|
should "make sure that '#{method_name}' matches" do
if expected_value.instance_of? Regexp
assert_match(expected_value, strategy.try(method_name))
elsif expected_value.instance_of? Array
assert_equal(expected_value.sort, strategy.try(method_name).sort)
elsif expected_value.nil?
assert_nil(strategy.try(method_name))
else
assert_equal(expected_value, strategy.try(method_name))
end
end
end
end
end