This repository has been archived by the owner on Dec 12, 2021. It is now read-only.
/
query_spec.rb
86 lines (72 loc) · 3.89 KB
/
query_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
require "spec_helper"
describe Xapit::Server::Query do
before(:each) do
load_xapit_database
end
it "fetches results matching a simple search term" do
Xapit.database.add_document(:attributes => {:greeting => {:value => "hello world", :text => {}}}, :id => 123, :class => "Greeting")
query = Xapit::Server::Query.new([{:search => "hello"}])
query.records.should eq([{:class => "Greeting", :id => "123", :relevance => 100}])
query = Xapit::Server::Query.new([{:search => "matchnothing"}])
query.records.should eq([])
end
it "fetches facets when told to include them" do
Xapit.database.add_document(:attributes => {:priority => {:value => "3", :facet => {}}}, :id => 123, :class => "Greeting")
query = Xapit::Server::Query.new([{:include_facets => [:priority]}])
query.facets.should eq(:priority => [{:value => "3", :count => 1}])
end
it "fetches results matching a given facet" do
Xapit.database.add_document(:attributes => {:priority => {:value => "3", :field => {}, :facet => {}}}, :id => 123, :class => "Greeting")
query = Xapit::Server::Query.new([{:with_facets => [Xapit.facet_identifier(:priority, "3")]}])
query.records.should eq([{:class => "Greeting", :id => "123", :relevance => 100}])
query = Xapit::Server::Query.new([{:with_facets => [Xapit.facet_identifier(:priority, "4")]}])
query.records.should eq([])
end
it "fetches results containing applied facets" do
Xapit.database.add_document(:attributes => {:priority => {:value => "3", :facet => {}}}, :id => 123, :class => "Greeting")
query = Xapit::Server::Query.new([{:with_facets => [Xapit.facet_identifier(:priority, "3")]}])
query.applied_facet_options.should eq([{:id => Xapit.facet_identifier(:priority, "3"), :name => "priority", :value => "3"}])
end
it "fetches results based on time in string" do
Xapit.database.add_document(:attributes => {:priority => {:value => 3.days.ago, :field => {}}}, :id => 123, :class => "Greeting")
query = Xapit::Server::Query.new([{:where => {:priority => {from: 5.days.ago.to_json, to: 1.day.ago.to_json}}}])
query.records.first[:id].should eq("123")
end
describe "with priorities" do
before(:each) do
(1..5).each do |number|
Xapit.database.add_document(:attributes => {:priority => {:value => number, :field => {}}}, :id => number, :class => "Greeting")
end
end
it "fetches results matching a simple where clause" do
query = Xapit::Server::Query.new([{:where => {:priority => 3}}])
query.records.map { |r| r[:id] }.should eq(%w[3])
end
it "fetches results matching a multi where clause" do
query = Xapit::Server::Query.new([{:where => {:priority => [3, 4]}}])
query.records.map { |r| r[:id] }.should eq(%w[3 4])
end
it "fetches results matching :from and :to" do
query = Xapit::Server::Query.new([{:where => {:priority => {:from => 2, :to => 4}}}])
query.records.map { |r| r[:id] }.should eq(%w[2 3 4])
end
it "fetches results matching less than or greater than" do
query = Xapit::Server::Query.new([{:where => {:priority => {:gte => 3}}}])
query.records.map { |r| r[:id] }.should eq(%w[3 4 5])
query = Xapit::Server::Query.new([{:where => {:priority => {:lte => 3}}}])
query.records.map { |r| r[:id] }.should eq(%w[1 2 3])
end
it "fetches results matching exact terms" do
query = Xapit::Server::Query.new([{:all_terms => ["Xpriority-1"]}])
query.records.map { |r| r[:id] }.should eq(%w[1])
end
it "fetches results matching exact terms" do
query = Xapit::Server::Query.new([{:any_terms => ["Xpriority-1", "Xpriority-2"]}])
query.records.map { |r| r[:id] }.should eq(%w[1 2])
end
it "fetches results not matching exact terms" do
query = Xapit::Server::Query.new([{:not_terms => ["Xpriority-1", "Xpriority-2"]}])
query.records.map { |r| r[:id] }.should eq(%w[3 4 5])
end
end
end