Skip to content

Commit

Permalink
Replace import_and_refresh helper with reindex factory trait
Browse files Browse the repository at this point in the history
  • Loading branch information
colby-swandale committed Oct 9, 2024
1 parent d0c6755 commit 5d85d79
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 94 deletions.
12 changes: 5 additions & 7 deletions test/functional/api/v1/searches_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ def self.should_respond_to(format)
setup do
@match = create(:rubygem, name: "match")
@other = create(:rubygem, name: "other")
create(:version, rubygem: @match)
create(:version, rubygem: @other)
import_and_refresh
create(:version, :reindex, rubygem: @match)
create(:version, :reindex, rubygem: @other)
end

should_respond_to(:json) do |body|
Expand Down Expand Up @@ -79,10 +78,9 @@ def self.should_respond_to(format)
@match1 = create(:rubygem, name: "match1")
@match2 = create(:rubygem, name: "match2")
@other = create(:rubygem, name: "other")
create(:version, rubygem: @match1)
create(:version, rubygem: @match2)
create(:version, rubygem: @other)
import_and_refresh
create(:version, :reindex, rubygem: @match1)
create(:version, :reindex, rubygem: @match2)
create(:version, :reindex, rubygem: @other)
end

context "with elasticsearch up" do
Expand Down
30 changes: 13 additions & 17 deletions test/functional/searches_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class SearchesControllerTest < ActionController::TestCase

context "on GET to show with search parameters for a rubygem without versions" do
setup do
@sinatra = create(:rubygem, name: "sinatra")
import_and_refresh
@sinatra = create(:rubygem, :reindex, name: "sinatra")

assert_nil @sinatra.most_recent_version
assert_predicate @sinatra.reload.versions.count, :zero?
Expand All @@ -35,10 +34,10 @@ class SearchesControllerTest < ActionController::TestCase
@sinatra = create(:rubygem, name: "sinatra")
@sinatra_redux = create(:rubygem, name: "sinatra-redux")
@brando = create(:rubygem, name: "brando")
create(:version, rubygem: @sinatra)
create(:version, rubygem: @sinatra_redux)
create(:version, rubygem: @brando)
import_and_refresh
create(:version, :reindex, rubygem: @sinatra)
create(:version, :reindex, rubygem: @sinatra_redux)
create(:version, :reindex, rubygem: @brando)

get :show, params: { query: "sinatra" }
end

Expand All @@ -61,10 +60,9 @@ class SearchesControllerTest < ActionController::TestCase
@sinatra = create(:rubygem, name: "sinatra")
@sinatra_redux = create(:rubygem, name: "sinatra-redux")
@brando = create(:rubygem, name: "brando")
create(:version, rubygem: @sinatra)
create(:version, rubygem: @sinatra_redux)
create(:version, rubygem: @brando)
import_and_refresh
create(:version, :reindex, rubygem: @sinatra)
create(:version, :reindex, rubygem: @sinatra_redux)
create(:version, :reindex, rubygem: @brando)
get :show, params: { query: "sinatra" }
end

Expand Down Expand Up @@ -103,10 +101,9 @@ class SearchesControllerTest < ActionController::TestCase
@sinatra = create(:rubygem, name: "sinatra")
@sinatra_redux = create(:rubygem, name: "sinatra-redux")
@brando = create(:rubygem, name: "brando")
create(:version, rubygem: @sinatra)
create(:version, rubygem: @sinatra_redux)
create(:version, rubygem: @brando)
import_and_refresh
create(:version, :reindex, rubygem: @sinatra)
create(:version, :reindex, rubygem: @sinatra_redux)
create(:version, :reindex, rubygem: @brando)
get :show, params: { query: "sinatre" }
end

Expand All @@ -132,9 +129,8 @@ class SearchesControllerTest < ActionController::TestCase
setup do
@sinatra = create(:rubygem, name: "sinatra")
@sinatra_redux = create(:rubygem, name: "sinatra-redux")
create(:version, rubygem: @sinatra)
create(:version, :yanked, rubygem: @sinatra_redux)
import_and_refresh
create(:version, :reindex, rubygem: @sinatra)
create(:version, :yanked, :reindex, rubygem: @sinatra_redux)
get :show, params: { query: @sinatra_redux.name.to_s, yanked: true }
end

Expand Down
7 changes: 0 additions & 7 deletions test/helpers/es_helper.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
module SearchKickHelper
def import_and_refresh
Rubygem.searchkick_reindex

# wait for indexing to finish
Searchkick.client.cluster.health wait_for_status: "yellow"
end

def es_downloads(id)
response = get_response(id)
response["_source"]["downloads"]
Expand Down
9 changes: 3 additions & 6 deletions test/integration/page_params_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,23 @@ class PageParamsTest < SystemTest
end

test "api search with page smaller than 1" do
create(:rubygem, name: "some", number: "1.0.0")
import_and_refresh
create(:rubygem, :reindex, name: "some", number: "1.0.0")
visit api_v1_search_path(page: "0", query: "some", format: :json)

assert redirect_to(api_v1_search_path(page: "1", query: "some", format: :json))
refute_empty JSON.parse(page.body)
end

test "api search with page is not a numer" do
create(:rubygem, name: "some", number: "1.0.0")
import_and_refresh
create(:rubygem, :reindex, name: "some", number: "1.0.0")
visit api_v1_search_path(page: "foo", query: "some", format: :json)

assert redirect_to(api_v1_search_path(page: "1", query: "some", format: :json))
refute_empty JSON.parse(page.body)
end

test "api search with page that can't be converted to a number" do
create(:rubygem, name: "some", number: "1.0.0")
import_and_refresh
create(:rubygem, :reindex, name: "some", number: "1.0.0")
visit api_v1_search_path(page: { "$acunetix" => "1" }, query: "some", format: :json)

assert redirect_to(api_v1_search_path(page: "1", query: "some", format: :json))
Expand Down
25 changes: 10 additions & 15 deletions test/integration/search_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ class SearchTest < SystemTest
include SearchKickHelper

test "searching for a gem" do
create(:rubygem, name: "LDAP", number: "1.0.0")
create(:rubygem, name: "LDAP-PLUS", number: "1.0.0")
import_and_refresh
create(:rubygem, :reindex, name: "LDAP", number: "1.0.0")
create(:rubygem, :reindex, name: "LDAP-PLUS", number: "1.0.0")

visit search_path

Expand All @@ -20,8 +19,7 @@ class SearchTest < SystemTest

test "searching for a yanked gem" do
rubygem = create(:rubygem, name: "LDAP")
create(:version, rubygem: rubygem, indexed: false)
import_and_refresh
create(:version, :reindex, rubygem: rubygem, indexed: false)

visit search_path

Expand All @@ -39,9 +37,8 @@ class SearchTest < SystemTest

test "searching for a gem with yanked versions" do
rubygem = create(:rubygem, name: "LDAP")
create(:version, rubygem: rubygem, number: "1.1.1", indexed: true)
create(:version, rubygem: rubygem, number: "2.2.2", indexed: false)
import_and_refresh
create(:version, :reindex, rubygem: rubygem, number: "1.1.1", indexed: true)
create(:version, :reindex, rubygem: rubygem, number: "2.2.2", indexed: false)

visit search_path

Expand All @@ -60,9 +57,8 @@ class SearchTest < SystemTest

test "params has non white listed keys" do
Kaminari.configure { |c| c.default_per_page = 1 }
create(:rubygem, name: "ruby-ruby", number: "1.0.0")
create(:rubygem, name: "ruby-gems", number: "1.0.0")
import_and_refresh
create(:rubygem, :reindex, name: "ruby-ruby", number: "1.0.0")
create(:rubygem, :reindex, name: "ruby-gems", number: "1.0.0")

visit "/search?query=ruby&original_script_name=javascript:alert(1)//&script_name=javascript:alert(1)//"

Expand All @@ -77,10 +73,9 @@ class SearchTest < SystemTest
orignal_val = Gemcutter::SEARCH_MAX_PAGES
Gemcutter::SEARCH_MAX_PAGES = 2

create(:rubygem, name: "ruby-ruby", number: "1.0.0")
create(:rubygem, name: "ruby-gems", number: "1.0.0")
create(:rubygem, name: "ruby-thing", number: "1.0.0")
import_and_refresh
create(:rubygem, :reindex, name: "ruby-ruby", number: "1.0.0")
create(:rubygem, :reindex, name: "ruby-gems", number: "1.0.0")
create(:rubygem, :reindex, name: "ruby-thing", number: "1.0.0")

visit "/search?query=ruby"

Expand Down
8 changes: 5 additions & 3 deletions test/jobs/fastly_log_processor_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class FastlyLogProcessorJobTest < ActiveJob::TestCase
@processor = FastlyLogProcessor.new("test-bucket", "fastly-fake.log")
@job = FastlyLogProcessorJob.new(bucket: "test-bucket", key: "fastly-fake.log")
create(:gem_download)
import_and_refresh
end

teardown do
Expand Down Expand Up @@ -58,8 +57,6 @@ class FastlyLogProcessorJobTest < ActiveJob::TestCase
create(:version, rubygem: json, number: "1.8.3", platform: "java")
create(:version, rubygem: json, number: "1.8.3")
create(:version, rubygem: json, number: "1.8.2")

import_and_refresh
end

context "#perform" do
Expand All @@ -69,6 +66,8 @@ class FastlyLogProcessorJobTest < ActiveJob::TestCase
assert_equal 0, GemDownload.count_for_rubygem(json.id)
3.times { @job.perform_now }

json.reindex(refresh: true)

assert_equal 7, es_downloads(json.id)
assert_equal 7, GemDownload.count_for_rubygem(json.id)
end
Expand All @@ -85,6 +84,7 @@ class FastlyLogProcessorJobTest < ActiveJob::TestCase
end

json = Rubygem.find_by_name("json")
json.reindex(refresh: true)

assert_equal 7, GemDownload.count_for_rubygem(json.id)
assert_equal 7, es_downloads(json.id)
Expand All @@ -93,6 +93,7 @@ class FastlyLogProcessorJobTest < ActiveJob::TestCase

should "not run if already processed" do
json = Rubygem.find_by_name("json")
json.reindex(refresh: true)

assert_equal 0, json.downloads
assert_equal 0, es_downloads(json.id)
Expand All @@ -119,6 +120,7 @@ class FastlyLogProcessorJobTest < ActiveJob::TestCase

@job.perform_now
json = Rubygem.find_by_name("json")
json.reindex(refresh: true)

assert_equal 0, json.downloads
assert_equal 0, es_downloads(json.id)
Expand Down
42 changes: 19 additions & 23 deletions test/models/concerns/rubygem_searchable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class RubygemSearchableTest < ActiveSupport::TestCase
run_gem = create(:rubygem, name: "example_gem_run_dep")
@dev_dep = create(:dependency, :development, rubygem: dev_gem, version: version)
@run_dep = create(:dependency, :runtime, rubygem: run_gem, version: version)

@rubygem.reindex(refresh: true)
end

should "return a hash" do
Expand Down Expand Up @@ -107,8 +109,7 @@ class RubygemSearchableTest < ActiveSupport::TestCase
context "when the number of downloads exceeds a 32 bit integer" do
setup do
@rubygem = create(:rubygem, name: "large_downloads_example_gem", downloads: 10_000_000_000) # 10 Billion downloads
@version = create(:version, number: "1.0.0", rubygem: @rubygem)
import_and_refresh
@version = create(:version, :reindex, number: "1.0.0", rubygem: @rubygem)
end

should "allow the number of downloads to be stored as a 64 bit integer" do
Expand All @@ -121,10 +122,9 @@ class RubygemSearchableTest < ActiveSupport::TestCase

context "rubygems analyzer" do
setup do
create(:rubygem, name: "example-gem", number: "0.0.1")
create(:rubygem, name: "example_1", number: "0.0.1")
create(:rubygem, name: "example.rb", number: "0.0.1")

create(:rubygem, :reindex, name: "example-gem", number: "0.0.1")
create(:rubygem, :reindex, name: "example_1", number: "0.0.1")
create(:rubygem, :reindex, name: "example.rb", number: "0.0.1")
end

should "find all gems with matching tokens" do
Expand All @@ -141,9 +141,8 @@ class RubygemSearchableTest < ActiveSupport::TestCase
setup do
example_1 = create(:rubygem, name: "example_1")
example_2 = create(:rubygem, name: "example_2")
create(:version, rubygem: example_1, indexed: false)
create(:version, rubygem: example_2)

create(:version, :reindex, rubygem: example_1, indexed: false)
create(:version, :reindex, rubygem: example_2)
end

should "filter yanked gems from the result" do
Expand All @@ -160,10 +159,9 @@ class RubygemSearchableTest < ActiveSupport::TestCase
example_gem1 = create(:rubygem, name: "keyword", downloads: 1)
example_gem2 = create(:rubygem, name: "example_gem2", downloads: 1)
example_gem3 = create(:rubygem, name: "example_gem3", downloads: 1)
create(:version, rubygem: example_gem1, description: "some", summary: "some")
create(:version, rubygem: example_gem2, description: "keyword", summary: "some")
create(:version, rubygem: example_gem3, summary: "keyword", description: "some")

create(:version, :reindex, rubygem: example_gem1, description: "some", summary: "some")
create(:version, :reindex, rubygem: example_gem2, description: "keyword", summary: "some")
create(:version, :reindex, rubygem: example_gem3, summary: "keyword", description: "some")
end

should "look for keyword in name, summary and description and order them in same priority order" do
Expand All @@ -178,9 +176,8 @@ class RubygemSearchableTest < ActiveSupport::TestCase
setup do
(10..30).step(10) do |downloads|
rubygem = create(:rubygem, name: "gem_#{downloads}", downloads: downloads)
create(:version, rubygem: rubygem)
create(:version, :reindex, rubygem: rubygem)
end

end

should "boost score of result by downloads count" do
Expand Down Expand Up @@ -219,8 +216,7 @@ class RubygemSearchableTest < ActiveSupport::TestCase
example1 = create(:rubygem, name: "keyword")
example2 = create(:rubygem, name: "keywordo")
example3 = create(:rubygem, name: "keywo")
[example1, example2, example3].each { |gem| create(:version, rubygem: gem) }

[example1, example2, example3].each { |gem| create(:version, :reindex, rubygem: gem) }
end

should "suggest names of possible gems" do
Expand All @@ -242,9 +238,8 @@ class RubygemSearchableTest < ActiveSupport::TestCase
setup do
rubygem1 = create(:rubygem, name: "example", downloads: 101)
rubygem2 = create(:rubygem, name: "web-rubygem", downloads: 99)
create(:version, rubygem: rubygem1, summary: "special word with web-rubygem")
create(:version, rubygem: rubygem2, description: "example special word")

create(:version, :reindex, rubygem: rubygem1, summary: "special word with web-rubygem")
create(:version, :reindex, rubygem: rubygem2, description: "example special word")
end

should "filter gems on downloads" do
Expand Down Expand Up @@ -299,6 +294,9 @@ class RubygemSearchableTest < ActiveSupport::TestCase
rubygem1.update_column("updated_at", 2.days.ago)
rubygem2.update_column("updated_at", 10.days.ago)

rubygem1.reindex(refresh: true)
rubygem2.reindex(refresh: true)

_, @response = ElasticSearcher.new("example").search
end

Expand Down Expand Up @@ -357,7 +355,6 @@ class RubygemSearchableTest < ActiveSupport::TestCase
rubygem = create(:rubygem, name: gem_name, downloads: 10)
create(:version, rubygem: rubygem)
end

end

should "not affect results" do
Expand All @@ -372,9 +369,8 @@ class RubygemSearchableTest < ActiveSupport::TestCase
context "query matches gem name prefix" do
setup do
%w[term-ansicolor term-an].each do |gem_name|
create(:rubygem, name: gem_name, number: "0.0.1", downloads: 10)
create(:rubygem, :reindex, name: gem_name, number: "0.0.1", downloads: 10)
end

end

should "return results" do
Expand Down
1 change: 0 additions & 1 deletion test/models/deletion_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class DeletionTest < ActiveSupport::TestCase
@gem_file.rewind
@version = Version.last
@spec_rz = RubygemFs.instance.get("quick/Marshal.4.8/#{@version.full_name}.gemspec.rz")
import_and_refresh
end

teardown do
Expand Down
Loading

0 comments on commit 5d85d79

Please sign in to comment.