Skip to content

First class testing for other ORMs #415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
matrix:
ruby-version: ["3.2", "3.3", "3.4"]
rails-version: [6.1, 7.0]
mongodb-version: [8.0]
env:
RAILS_VERSION: ${{ matrix.rails-version }}
name: integration-tests (Rails ${{ matrix.rails-version }} with Ruby ${{ matrix.ruby-version }})
Expand All @@ -34,6 +35,10 @@ jobs:
bundler-cache: true
- name: Meilisearch (latest) setup with Docker
run: docker run -d -p 7700:7700 getmeili/meilisearch:latest meilisearch --master-key=masterKey --no-analytics
- name: Start MongoDB
uses: supercharge/[email protected]
with:
mongodb-version: ${{ matrix.mongodb-version }}
- name: Run tests
run: bundle exec rspec
- name: Upload coverage reports to Codecov
Expand Down
24 changes: 12 additions & 12 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2025-03-24 19:18:19 UTC using RuboCop version 1.27.0.
# on 2025-03-28 20:11:18 UTC using RuboCop version 1.27.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
# Offense count: 2
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: TreatCommentsAsGroupSeparators, ConsiderPunctuation, Include.
# Include: **/*.gemfile, **/Gemfile, **/gems.rb
Expand Down Expand Up @@ -66,10 +66,10 @@ Lint/UnusedMethodArgument:
Exclude:
- 'lib/meilisearch-rails.rb'

# Offense count: 15
# Offense count: 17
# Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 105
Max: 108

# Offense count: 1
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
Expand All @@ -82,10 +82,10 @@ Metrics/BlockLength:
Metrics/ClassLength:
Max: 171

# Offense count: 11
# Offense count: 12
# Configuration parameters: IgnoredMethods.
Metrics/CyclomaticComplexity:
Max: 28
Max: 29

# Offense count: 23
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
Expand All @@ -95,12 +95,12 @@ Metrics/MethodLength:
# Offense count: 1
# Configuration parameters: CountComments, CountAsOne.
Metrics/ModuleLength:
Max: 437
Max: 439

# Offense count: 9
# Offense count: 10
# Configuration parameters: IgnoredMethods.
Metrics/PerceivedComplexity:
Max: 35
Max: 36

# Offense count: 1
Naming/AccessorMethodName:
Expand Down Expand Up @@ -139,7 +139,7 @@ RSpec/ContextWording:
- 'spec/options_spec.rb'
- 'spec/system/tech_shop_spec.rb'

# Offense count: 72
# Offense count: 74
# Configuration parameters: CountAsOne.
RSpec/ExampleLength:
Max: 16
Expand Down Expand Up @@ -167,7 +167,7 @@ RSpec/MultipleDescribes:
Exclude:
- 'spec/search_spec.rb'

# Offense count: 3
# Offense count: 5
RSpec/NestedGroups:
Max: 4

Expand Down Expand Up @@ -241,7 +241,7 @@ Style/StringLiterals:
Exclude:
- 'spec/ms_clean_up_job_spec.rb'

# Offense count: 20
# Offense count: 22
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ end
group :test do
rails_version = ENV['RAILS_VERSION'] || '7.1'
sequel_version = ENV['SEQUEL_VERSION'] ? "~> #{ENV['SEQUEL_VERSION']}" : '>= 4.0'
mongoid_version = ENV['MONGOID_VERSION'] || '9.0'

gem 'active_model_serializers'
gem 'rails', "~> #{rails_version}"
gem 'sequel', sequel_version
gem 'mongoid', "~> #{mongoid_version}"

# remove when deprecate rails 6
if Gem::Version.new(rails_version) >= Gem::Version.new('7.0')
Expand Down
6 changes: 4 additions & 2 deletions lib/meilisearch-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ def meilisearch(options = {}, &block)
end
end
elsif respond_to?(:after_destroy)
after_destroy_commit { |searchable| searchable.ms_enqueue_remove_from_index!(ms_synchronous?) }
after_commit(on: :destroy) { |searchable| searchable.ms_enqueue_remove_from_index!(ms_synchronous?) }
end
end
end
Expand Down Expand Up @@ -696,6 +696,8 @@ def ms_search(query, params = {})
# respond with a valid database column. The blocks below prevent that from happening.
has_virtual_column_as_pk = if defined?(::Sequel::Model) && self < Sequel::Model
meilisearch_options[:type].columns.map(&:to_s).exclude?(condition_key.to_s)
elsif Utilities.mongo_model?(self)
fields.keys.exclude?(condition_key.to_s)
else
meilisearch_options[:type].columns.map(&:name).map(&:to_s).exclude?(condition_key.to_s)
end
Expand Down Expand Up @@ -784,7 +786,7 @@ def ms_must_reindex?(document)

def ms_primary_key_method(options = nil)
options ||= meilisearch_options
options[:primary_key] || options[:id] || :id
options[:primary_key] || options[:id] || (Utilities.mongo_model?(self) ? :_id : :id)
end

protected
Expand Down
11 changes: 11 additions & 0 deletions spec/integration/mongoid/record_is_created_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'support/mongo_models/citizen'

describe 'MongoDB record is created' do
it 'is added to meilisearch' do
john_wick = Citizen.create(name: 'John Wick', age: 40)

AsyncHelper.await_last_task

expect(Citizen.search('John')).to eq([john_wick])
end
end
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
require 'sqlite3' unless defined?(JRUBY_VERSION)
require 'logger'
require 'sequel'
require 'mongoid'
require 'active_model_serializers'
require 'byebug'

Expand All @@ -43,6 +44,7 @@
NEW_RAILS = Gem.loaded_specs['rails'].version >= Gem::Version.new('6.0')

Dir["#{File.dirname(__FILE__)}/support/*.rb"].each { |file| require file }
Dir["#{File.dirname(__FILE__)}/support/test_models/*.rb"].each { |file| require file }

RSpec.configure do |c|
c.mock_with :rspec
Expand All @@ -67,5 +69,7 @@
safe_index_list.each do |index|
Meilisearch::Rails.client.delete_index(index)
end

Mongoid.default_client.database.drop
end
end
13 changes: 13 additions & 0 deletions spec/support/3_model_specification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Models
class ModelSpecification
Field = Data.define(:name, :type)

attr_reader :name, :fields, :body

def initialize(klass_name, fields:, &block)
@name = klass_name
@fields = fields.map { |name, type| Field.new(name, type) }
@body = block
end
end
end
45 changes: 45 additions & 0 deletions spec/support/4_active_record_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# This file is responsible for one-time set up of ActiveRecord
# 1 - Set ActiveRecord connection & options
# 2 - Delete existing database
# 3 - Expose "TestModels::ActiveRecord.initialize_model" to allow dynamically creating models
require 'active_record'

ActiveRecord::Base.logger = Logger.new($stdout)
ActiveRecord::Base.logger.level = Logger::WARN
ActiveRecord::Base.establish_connection(
'adapter' => defined?(JRUBY_VERSION) ? 'jdbcsqlite3' : 'sqlite3',
'database' => 'data.sqlite3',
'pool' => 5,
'timeout' => 5000
)

ActiveRecord::Base.raise_in_transactional_callbacks = true if ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks)

FileUtils.rm_f('data.sqlite3')

module Models
module ActiveRecord
def self.schema
@schema ||= ::ActiveRecord::Schema.new.tap { |schema| schema.verbose = false }
end

def self.initialize_model(specification)
klass = Class.new(::ActiveRecord::Base) do
define_singleton_method(:model_name) do
name = "Models::ActiveRecord::#{specification.name}"
ActiveModel::Name.new(self, nil, name)
end
end

const_set(specification.name, klass)

klass.class_eval(&specification.body)

schema.create_table klass.table_name do |t|
specification.fields.each do |field|
t.send(field.type, field.name)
end
end
end
end
end
30 changes: 0 additions & 30 deletions spec/support/active_record_schema.rb

This file was deleted.

28 changes: 10 additions & 18 deletions spec/support/models/animals.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
require 'support/active_record_schema'

ar_schema.create_table :cats do |t|
t.string :name
end

ar_schema.create_table :dogs do |t|
t.string :name
end

class Cat < ActiveRecord::Base
cats_specification = Models::ModelSpecification.new(
'Cat',
fields: [%i[name string]]
) do
include Meilisearch::Rails

meilisearch index_uid: safe_index_uid('animals'), synchronous: true, primary_key: :ms_id
Expand All @@ -20,7 +15,10 @@ def ms_id
end
end

class Dog < ActiveRecord::Base
dogs_specification = Models::ModelSpecification.new(
'Dog',
fields: [%i[name string]]
) do
include Meilisearch::Rails

meilisearch index_uid: safe_index_uid('animals'), synchronous: true, primary_key: :ms_id
Expand All @@ -32,11 +30,5 @@ def ms_id
end
end

module TestUtil
def self.reset_animals!
Cat.clear_index!(true)
Cat.delete_all
Dog.clear_index!(true)
Dog.delete_all
end
end
Models::ActiveRecord.initialize_model(cats_specification)
Models::ActiveRecord.initialize_model(dogs_specification)
62 changes: 44 additions & 18 deletions spec/support/models/book.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
require 'support/active_record_schema'

ar_schema.create_table :books do |t|
t.string :name
t.string :author
t.boolean :premium
t.boolean :released
t.string :genre
end

class Book < ActiveRecord::Base
book_specification = Models::ModelSpecification.new(
'Book',
fields: [
%i[name string],
%i[author string],
%i[premium boolean],
%i[released boolean],
%i[genre string]
]
) do
include Meilisearch::Rails

meilisearch synchronous: true, index_uid: safe_index_uid('SecuredBook'), sanitize: true do
Expand All @@ -33,10 +32,37 @@ def public?
end
end

module TestUtil
def self.reset_books!
Book.clear_index!(true)
Book.index(safe_index_uid('BookAuthor')).delete_all_documents
Book.index(safe_index_uid('Book')).delete_all_documents
end
end
Models::ActiveRecord.initialize_model(book_specification)

# class Book < ActiveRecord::Base
# include Meilisearch::Rails
#
# meilisearch synchronous: true, index_uid: safe_index_uid('SecuredBook'), sanitize: true do
# searchable_attributes [:name]
# typo_tolerance min_word_size_for_typos: { one_typo: 5, twoTypos: 8 }
# filterable_attributes %i[genre author]
# faceting max_values_per_facet: 3
#
# add_index safe_index_uid('BookAuthor') do
# searchable_attributes [:author]
# end
#
# add_index safe_index_uid('Book'), if: :public? do
# searchable_attributes [:name]
# end
# end
#
# private
#
# def public?
# released && !premium
# end
# end

# module TestUtil
# def self.reset_books!
# Book.clear_index!(true)
# Book.index(safe_index_uid('BookAuthor')).delete_all_documents
# Book.index(safe_index_uid('Book')).delete_all_documents
# end
# end
Loading
Loading