-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from assaydepot/specs
Added some specs
- Loading branch information
Showing
5 changed files
with
126 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,75 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RRF do | ||
it "has a version number" do | ||
expect(RRF::VERSION).not_to be nil | ||
end | ||
require "spec_helper" | ||
|
||
RSpec.describe RRF::Model do | ||
describe ".fuse" do | ||
let!(:chunk1) { Chunk.create!(body: "hello world") } | ||
let!(:chunk2) { Chunk.create!(body: "hi there") } | ||
let!(:chunk3) { Chunk.create!(body: "hello again") } | ||
|
||
it "fuses results from ActiveRecord and Searchkick" do | ||
# Mocking Searchkick results | ||
search_hits = [ | ||
{ "_id" => chunk1.id }, | ||
{ "_id" => chunk2.id } | ||
] | ||
search_results = instance_double(Searchkick::Relation) | ||
search_results.define_singleton_method(:hits) { search_hits } | ||
allow(search_results).to receive(:is_a?).with(ActiveRecord::Relation).and_return(false) | ||
allow(search_results).to receive(:is_a?).with(Searchkick::Relation).and_return(true) | ||
allow(Chunk).to receive(:search).and_return(search_results) | ||
|
||
ar_result = Chunk.where("body like ?", "%hello%") | ||
es_result = Chunk.search("hello", load: false) | ||
|
||
fused_results = Chunk.fuse(ar_result, es_result, limit: 2) | ||
|
||
expect(fused_results.size).to eq(2) | ||
expect(fused_results).to include(chunk1) | ||
expect(fused_results).to include(chunk3) | ||
end | ||
|
||
it "limits the number of results" do | ||
search_hits = [ | ||
{ "_id" => chunk1.id }, | ||
{ "_id" => chunk2.id } | ||
] | ||
search_results = instance_double(Searchkick::Relation) | ||
search_results.define_singleton_method(:hits) { search_hits } | ||
allow(search_results).to receive(:is_a?).with(ActiveRecord::Relation).and_return(false) | ||
allow(search_results).to receive(:is_a?).with(Searchkick::Relation).and_return(true) | ||
allow(Chunk).to receive(:search).and_return(search_results) | ||
|
||
ar_result = Chunk.where("body like ?", "%hello%") | ||
es_result = Chunk.search("hello", load: false) | ||
|
||
fused_results = Chunk.fuse(ar_result, es_result, limit: 1) | ||
expect(fused_results.size).to eq(1) | ||
end | ||
|
||
it "raises an error for unsupported result set types" do | ||
expect { | ||
Chunk.fuse(["unsupported"], limit: 1) | ||
}.to raise_error(RRF::Error, "Unsupported result set type: Array") | ||
end | ||
|
||
it "correctly calculates and assigns scores" do | ||
search_hits = [ | ||
{ "_id" => chunk1.id }, | ||
{ "_id" => chunk2.id } | ||
] | ||
search_results = instance_double(Searchkick::Relation) | ||
search_results.define_singleton_method(:hits) { search_hits } | ||
allow(search_results).to receive(:is_a?).with(ActiveRecord::Relation).and_return(false) | ||
allow(search_results).to receive(:is_a?).with(Searchkick::Relation).and_return(true) | ||
allow(Chunk).to receive(:search).and_return(search_results) | ||
|
||
ar_result = Chunk.where("body like ?", "%hello%") | ||
es_result = Chunk.search("hello", load: false) | ||
|
||
it "does something useful" do | ||
expect(false).to eq(true) | ||
fused_results = Chunk.fuse(ar_result, es_result, limit: 2) | ||
expect(fused_results.first._rrf_score).to be > 0 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_record" | ||
require "searchkick" | ||
require "faraday" | ||
require "elasticsearch" | ||
require "rrf" | ||
require "rspec" | ||
|
||
# Mock ActiveRecord and Searchkick models | ||
class MockRecord < ActiveRecord::Base | ||
self.abstract_class = true | ||
include RRF::Model | ||
end | ||
|
||
RSpec.configure do |config| | ||
# Enable flags like --only-failures and --next-failure | ||
config.example_status_persistence_file_path = ".rspec_status" | ||
# Enable :focus tag and run focused tests | ||
config.filter_run_when_matching :focus | ||
|
||
config.before(:suite) do | ||
ActiveRecord::Base.establish_connection( | ||
adapter: "sqlite3", | ||
database: ":memory:" | ||
) | ||
|
||
# Disable RSpec exposing methods globally on `Module` and `main` | ||
config.disable_monkey_patching! | ||
ActiveRecord::Schema.define do | ||
create_table :chunks, force: true do |t| | ||
t.string :body | ||
t.timestamps | ||
end | ||
end | ||
|
||
config.expect_with :rspec do |c| | ||
c.syntax = :expect | ||
class Chunk < MockRecord | ||
end | ||
end | ||
end | ||
|
||
config.before(:each) do | ||
Chunk.delete_all | ||
end | ||
end |