Skip to content

Commit

Permalink
Get tests working.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryankopf committed Aug 17, 2023
1 parent 87ea8c6 commit 61c5a9a
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 51 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: ['1.9.3', '2.0.0']
ruby-version: ['3.1.3', '2.0.0']

steps:
- uses: actions/checkout@v2
Expand All @@ -21,6 +21,12 @@ jobs:
with:
ruby-version: ${{ matrix.ruby-version }}

- name: Install Jeweler
run: gem install jeweler

- name: Install libyaml for sdoc
run: sudo apt-get install libyaml-dev

- name: Set up dependencies
run: bundle install

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ coverage
index
*.gem
.bundle
Gemfile.lock
11 changes: 7 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
source "http://rubygems.org"

group :test do
gem "sdoc"
gem "rake"
gem "mocha", "~> 0.9.11"
gem "sqlite3", "~> 1.3.5"
gem "activerecord", "~> 3.2.9"
gem "will_paginate", "~> 3.0.5"
gem "minitest"
gem "mocha"#, "~> 0.9.11"
gem "sqlite3"#, "~> 1.3.5"
gem "activerecord"#, "~> 3.2.9"
gem "will_paginate"#, "~> 4.0"
gem "activesupport"
end
8 changes: 8 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ Rake::TestTask.new('test:integration') do |t|
t.verbose = true
end

desc 'Run temp tests.'
Rake::TestTask.new('test:temp') do |t|
t.libs << 'test'
t.libs << 'lib'
t.pattern = 'test/integration/will_paginate_search_test.rb'
t.verbose = true
end

desc 'Run performance tests.'
Rake::TestTask.new('test:performance') do |t|
t.libs << 'test'
Expand Down
2 changes: 2 additions & 0 deletions lib/acts_as_indexed.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'active_record'

path = File.expand_path(File.dirname(__FILE__))
$:.unshift path unless $:.include?(path)
require 'acts_as_indexed/class_methods'
require 'acts_as_indexed/instance_methods'
require 'acts_as_indexed/singleton_methods'
Expand Down
71 changes: 54 additions & 17 deletions lib/acts_as_indexed/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ def acts_as_indexed(options = {})
named_scope :with_query, lambda { |query| { :conditions => ["#{table_name}.#{primary_key} IN (?)", search_index(query, {}, {:ids_only => true}) ] } }
end

cattr_accessor :aai_config, :aai_fields
unless respond_to?(:aai_fields) && respond_to?(:aai_config)
cattr_accessor :aai_config, :aai_fields
end
#cattr_accessor :aai_config, :aai_fields

self.aai_fields = options.delete(:fields)
raise(ArgumentError, 'no fields specified') if self.aai_fields.nil? || self.aai_fields.empty?
Expand Down Expand Up @@ -134,25 +137,59 @@ def search_index(query, find_options={}, options={})

return part_query if options[:ids_only]

with_scope :find => find_options do
# Doing the find like this eliminates the possibility of errors occuring
# on either missing records (out-of-sync) or an empty results array.
records = find(:all, :conditions => [ "#{table_name}.#{primary_key} IN (?)", part_query])

if find_options.include?(:order)
records # Just return the records without ranking them.
# New 2023 way to find the records.
conditions = ["#{table_name}.#{primary_key} IN (?)", part_query]
query_options = find_options.slice(:limit, :offset, :order)
records = where(conditions)

else
# Results come back in random order from SQL, so order again.
ranked_records = ActiveSupport::OrderedHash.new
records.each do |r|
ranked_records[r] = @query_cache[query][r.id]
end
if find_options[:conditions]
records = records.where(find_options[:conditions])
end
if find_options[:joins]
records = records.joins(find_options[:joins])
end
if find_options[:includes]
records = records.includes(find_options[:includes])
end
if find_options[:limit]
records = records.limit(find_options[:limit])
end
if find_options[:offset]
records = records.offset(find_options[:offset])
end
if find_options[:order]
records = records.order(find_options[:order])
return records # Just return the records without ranking them.
end

sort(ranked_records.to_a).map{ |r| r.first }
end
# Results come back in random order from SQL, so order again.
ranked_records = ActiveSupport::OrderedHash.new
records.each do |r|
ranked_records[r] = @query_cache[query][r.id]
end

sort(ranked_records.to_a).map{ |r| r.first }

# Old way, deprecated and broken.
# with_scope :find => find_options do
# # Doing the find like this eliminates the possibility of errors occuring
# # on either missing records (out-of-sync) or an empty results array.
# records = find(:all, :conditions => [ "#{table_name}.#{primary_key} IN (?)", part_query])
#
# if find_options.include?(:order)
# records # Just return the records without ranking them.
#
# else
# # Results come back in random order from SQL, so order again.
# ranked_records = ActiveSupport::OrderedHash.new
# records.each do |r|
# ranked_records[r] = @query_cache[query][r.id]
# end
#
# sort(ranked_records.to_a).map{ |r| r.first }
# end
# end

end

# Builds an index from scratch for the current model class.
Expand All @@ -162,7 +199,7 @@ def build_index
return if aai_config.index_file.directory?

index = new_index
find_in_batches({ :batch_size => 500 }) do |records|
find_in_batches(batch_size: 500) do |records|
index.add_records(records)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/acts_as_indexed/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Configuration

# Sets the location for the index. Specify as an array. The default, for
# example, would be set as [Rails.root,'tmp','index].
attr_accessor :index_file
#attr_accessor :index_file

# Tuning value for the index partitioning. Larger values result in quicker
# searches, but slower indexing. Default is 3.
Expand All @@ -18,7 +18,7 @@ class Configuration
# Proc that allows you to turn on or off index for a record.
# Useful if you don't want an object to be placed in the index, such as a
# draft post.
attr_accessor :if_proc
attr_writer :if_proc

# Enable or disable case sensitivity.
# Set to true to enable.
Expand Down
4 changes: 2 additions & 2 deletions lib/will_paginate_search.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module ActsAsIndexed

module WillPaginate
module WillPaginate

module Search
module Search

def paginate_search(query, options)
page = options.fetch(:page) { raise ArgumentError, ":page parameter required" }
Expand Down
12 changes: 6 additions & 6 deletions test/integration/acts_as_indexed_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_search_returns_post_ids
def test_updates_index
p = Post.create(:title => 'A special title', :body => 'foo bar bla bla bla')
assert find_with_index_ids('title').include?(p.id)
p.update_attributes(:title => 'No longer special')
p.update(:title => 'No longer special')
assert !find_with_index_ids('title').include?(p.id)
end

Expand Down Expand Up @@ -185,7 +185,7 @@ def test_should_error_when_ids_only_combined_with_finder_options
# all records with that same atom from the index.
def test_update_record_bug
p = Post.find(6)
assert p.update_attributes(:body => p.body + ' crane')
assert p.update(:body => p.body + ' crane')
assert_equal 2, find_with_index_ids('crane').size
assert_equal 2, find_with_index_ids('ship').size
end
Expand Down Expand Up @@ -216,7 +216,7 @@ def test_update_if_update

assert_equal 1, Post.find_with_index('crane', {}, { :no_query_cache => true, :ids_only => true}).size
p = Post.find(6)
assert p.update_attributes(:visible => true)
assert p.update(:visible => true)
assert_equal 1, Post.find_with_index('crane', {}, { :no_query_cache => true, :ids_only => true}).size
end

Expand All @@ -227,7 +227,7 @@ def test_update_if_remove

assert_equal 1, Post.find_with_index('crane', {}, { :no_query_cache => true, :ids_only => true}).size
p = Post.find(6)
assert p.update_attributes(:visible => false)
assert p.update(:visible => false)
assert_equal 0, Post.find_with_index('crane',{},{ :no_query_cache => true, :ids_only => true}).size
end

Expand All @@ -238,7 +238,7 @@ def test_update_if_add

assert_equal 1, Post.find_with_index('crane', {}, { :no_query_cache => true, :ids_only => true}).size
p = Post.find(5)
assert p.update_attributes(:visible => true)
assert p.update(:visible => true)
assert_equal 2, Post.find_with_index('crane',{},{ :no_query_cache => true, :ids_only => true}).size
end

Expand All @@ -249,7 +249,7 @@ def test_update_if_not_in

assert_equal [6], Post.find_with_index('crane', {}, { :no_query_cache => true, :ids_only => true})

posts(:wikipedia_article_5).update_attributes(:title => 'A new title')
posts(:wikipedia_article_5).update(:title => 'A new title')
assert_equal [6], Post.find_with_index('crane',{},{ :no_query_cache => true, :ids_only => true})
end

Expand Down
3 changes: 1 addition & 2 deletions test/integration/will_paginate_search_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'integration_test_helper.rb'
require 'integration_test_helper'

class WillPaginateSearchTest < ActiveSupport::TestCase
fixtures :posts
Expand All @@ -10,7 +10,6 @@ def teardown
end

def test_paginate_search

assert_equal [5, 2, 1, 3, 6, 4], paginate_search(1, 10)
assert_equal [5, 2, 1], paginate_search(1, 3)
assert_equal [3, 6, 4], paginate_search(2, 3)
Expand Down
11 changes: 6 additions & 5 deletions test/integration_test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require 'test/unit'
require 'minitest/autorun'
require 'fileutils'
require 'rubygems'

require 'bundler/setup'
require 'active_record'
Expand Down Expand Up @@ -29,19 +28,21 @@ def self.env

ActiveRecord::Base.logger = Logger.new(test_path.join('test.log').to_s)
ActiveRecord::Base.configurations = YAML::load(IO.read(test_path.join('config', 'database.yml').to_s))
ActiveRecord::Base.establish_connection(ENV['DB'] || 'sqlite3')
ActiveRecord::Base.establish_connection(ENV['DB'] || :sqlite3)

# Load Schema
load(test_path.join('db', 'schema.rb').to_s)

# Load model.
$LOAD_PATH.unshift(test_path.join('fixtures').to_s)
require 'post'

class ActiveSupport::TestCase #:nodoc:
include ActiveRecord::TestFixtures
self.fixture_path = Pathname.new(File.expand_path('../', __FILE__)).join('fixtures').to_s
self.use_transactional_fixtures = true
self.use_instantiated_fixtures = false
self.use_transactional_tests = true
self.use_instantiated_fixtures = false
#fixtures :all

def destroy_index
FileUtils.rm_rf(index_loc)
Expand Down
6 changes: 3 additions & 3 deletions test/unit/configuration_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'unit_test_helper.rb'
require 'unit_test_helper'
include ActsAsIndexed

class ConfigurationTest < ActiveSupport::TestCase
Expand Down Expand Up @@ -35,7 +35,7 @@ def test_index_file_depth_should_be_writeable
end

def test_index_file_depth_should_raise_on_lower_than_1_value
assert_nothing_raised(ArgumentError) { config.index_file_depth = 1 }
assert_nothing_raised { config.index_file_depth = 1 }

e = assert_raise(ArgumentError) { config.index_file_depth = 0 }
assert_equal 'index_file_depth cannot be less than one (1)', e.message
Expand All @@ -49,7 +49,7 @@ def test_min_word_size_should_be_writeable
end

def test_min_word_size_should_raise_on_lower_than_1_value
assert_nothing_raised(ArgumentError) { config.min_word_size = 1 }
assert_nothing_raised { config.min_word_size = 1 }

e = assert_raise(ArgumentError) { config.min_word_size = 0 }
assert_equal 'min_word_size cannot be less than one (1)', e.message
Expand Down
4 changes: 2 additions & 2 deletions test/unit/search_atom_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'unit_test_helper.rb'
include ActsAsIndexed

class SearchAtomTest < ActiveSupport::TestCase
class SearchAtomTest < ActiveSupport::TestCase

def test_should_create_a_new_instance
assert SearchAtom.new
Expand Down Expand Up @@ -48,7 +48,7 @@ def test_positions_should_return_positions
end

def test_positions_should_return_nil
assert_equal nil, build_search_atom.positions(456)
assert_nil build_search_atom.positions(456)
end

def test_remove_record
Expand Down
16 changes: 9 additions & 7 deletions test/unit_test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
require 'test/unit'
require 'minitest/autorun'
require 'fileutils'
require 'rubygems'

require 'bundler/setup'
require 'mocha'
require 'mocha/integration/test_unit'
# require 'bundler/setup'
# require 'mocha'
# require 'mocha/integration/test_unit'
require 'mocha/minitest'
require 'active_support/test_case'

# Mock out the required environment variables.
# Do this before requiring AAI.
Expand All @@ -17,5 +18,6 @@ def self.env
end
end

test_path = Pathname.new(File.expand_path('../', __FILE__))
require test_path.parent.join('lib', 'acts_as_indexed').to_s
# test_path = Pathname.new(File.expand_path('../', __FILE__))
# require test_path.parent.join('lib', 'acts_as_indexed').to_s
require_relative '../lib/acts_as_indexed'

0 comments on commit 61c5a9a

Please sign in to comment.