mongoid-giza 0.5.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,8 @@ require "spec_helper"
5
5
  describe Mongoid::Giza::Index::Attribute do
6
6
  describe "name" do
7
7
  it "should be mandatory" do
8
- expect { Mongoid::Giza::Index::Attribute.new }.to raise_error(ArgumentError)
8
+ expect { Mongoid::Giza::Index::Attribute.new }
9
+ .to raise_error(ArgumentError)
9
10
  end
10
11
 
11
12
  it "should be set on creation" do
@@ -43,7 +44,8 @@ describe Mongoid::Giza::Index::Attribute do
43
44
 
44
45
  describe "type" do
45
46
  it "should be mandatory" do
46
- expect { Mongoid::Giza::Index::Attribute.new("attribute") }.to raise_error(ArgumentError)
47
+ expect { Mongoid::Giza::Index::Attribute.new("attribute") }
48
+ .to raise_error(ArgumentError)
47
49
  end
48
50
 
49
51
  it "should be set on creation" do
@@ -53,12 +55,47 @@ describe Mongoid::Giza::Index::Attribute do
53
55
  end
54
56
 
55
57
  it "should be a valid type" do
56
- expect { Mongoid::Giza::Index::Attribute.new("attribute", :type) }.to raise_error(TypeError)
58
+ expect { Mongoid::Giza::Index::Attribute.new("attribute", :type) }
59
+ .to raise_error(TypeError)
57
60
  end
58
61
  end
59
62
 
60
63
  it "should accept a block" do
61
- attribute = Mongoid::Giza::Index::Attribute.new("attribute", :uint) { }
64
+ attribute = Mongoid::Giza::Index::Attribute.new("attribute", :uint) {}
62
65
  expect(attribute.block).to be_a(Proc)
63
66
  end
67
+
68
+ describe "options" do
69
+ describe "default" do
70
+ it "should accept default option" do
71
+ attribute = Mongoid::Giza::Index::Attribute.new("attribute",
72
+ :uint, default: 1)
73
+ expect(attribute.default).to be 1
74
+ end
75
+
76
+ it "should be nil if not set" do
77
+ attribute = Mongoid::Giza::Index::Attribute.new("attribute", :uint)
78
+ expect(attribute.default).to be nil
79
+ end
80
+ end
81
+
82
+ describe "bits" do
83
+ it "should accept bits option" do
84
+ attribute = Mongoid::Giza::Index::Attribute.new("attribute",
85
+ :uint, bits: 16)
86
+ expect(attribute.bits).to be 16
87
+ end
88
+
89
+ it "should ignore if it's not an int attribute" do
90
+ attribute = Mongoid::Giza::Index::Attribute.new("attribute",
91
+ :bool, bits: 16)
92
+ expect(attribute.bits).to be nil
93
+ end
94
+
95
+ it "should be nil if not set" do
96
+ attribute = Mongoid::Giza::Index::Attribute.new("attribute", :uint)
97
+ expect(attribute.bits).to be nil
98
+ end
99
+ end
100
+ end
64
101
  end
@@ -47,7 +47,7 @@ describe Mongoid::Giza::Index::Field do
47
47
  end
48
48
 
49
49
  it "should accept a block" do
50
- field = Mongoid::Giza::Index::Field.new("field") { }
50
+ field = Mongoid::Giza::Index::Field.new("field") {}
51
51
  expect(field.block).to be_a(Proc)
52
52
  end
53
53
  end
@@ -44,7 +44,7 @@ describe Mongoid::Giza::Index do
44
44
  end
45
45
 
46
46
  it "should create a new Field" do
47
- expect(Mongoid::Giza::Index::Field).to receive(:new).with(name, false)
47
+ expect(Mongoid::Giza::Index::Field).to receive(:new).with(name, nil)
48
48
  index.field(name)
49
49
  end
50
50
 
@@ -63,14 +63,17 @@ describe Mongoid::Giza::Index do
63
63
  describe "attribute" do
64
64
  let(:name) { "attribute" }
65
65
 
66
- let(:type) { :uint }
66
+ let(:type) { :int }
67
+
68
+ let(:options) { {default: 1} }
67
69
 
68
70
  it "should require a name" do
69
71
  expect { index.attribute }.to raise_error(ArgumentError)
70
72
  end
71
73
 
72
74
  it "should accept a type" do
73
- expect(Mongoid::Giza::Index::Attribute).to receive(:new).with(name, type)
75
+ expect(Mongoid::Giza::Index::Attribute).to receive(:new)
76
+ .with(name, type, {})
74
77
  index.attribute(name, type)
75
78
  end
76
79
 
@@ -85,21 +88,23 @@ describe Mongoid::Giza::Index do
85
88
  end
86
89
  fields
87
90
  end
88
- expect(Mongoid::Giza::Index::Attribute).to receive(:new).with(name, Mongoid::Giza::Index::TYPES_MAP[type])
91
+ expect(Mongoid::Giza::Index::Attribute).to receive(:new)
92
+ .with(name, Mongoid::Giza::Index::TYPES_MAP[type], {})
89
93
  index.attribute(name)
90
94
  end
91
95
 
92
- it "should default to the first type when the field is not found" do
96
+ it "should default to string when the field is not found" do
93
97
  allow(klass).to receive(:fields) do
94
98
  fields = double("fields")
95
99
  allow(fields).to receive(:[]).with(name) { nil }
96
100
  fields
97
101
  end
98
- expect(Mongoid::Giza::Index::Attribute).to receive(:new).with(name, Mongoid::Giza::Index::TYPES_MAP.values.first)
102
+ expect(Mongoid::Giza::Index::Attribute).to receive(:new)
103
+ .with(name, :string, {})
99
104
  index.attribute(name)
100
105
  end
101
106
 
102
- it "should default to the first type when the type is not mapped" do
107
+ it "should default to string when the type is not mapped" do
103
108
  allow(klass).to receive(:fields) do
104
109
  fields = double("fields")
105
110
  allow(fields).to receive(:[]).with(name) do
@@ -109,9 +114,16 @@ describe Mongoid::Giza::Index do
109
114
  end
110
115
  fields
111
116
  end
112
- expect(Mongoid::Giza::Index::Attribute).to receive(:new).with(name, Mongoid::Giza::Index::TYPES_MAP.values.first)
117
+ expect(Mongoid::Giza::Index::Attribute).to receive(:new)
118
+ .with(name, :string, {})
113
119
  index.attribute(name)
114
120
  end
121
+
122
+ it "should accept options" do
123
+ expect(Mongoid::Giza::Index::Attribute).to receive(:new)
124
+ .with(name, type, options)
125
+ index.attribute(name, type, options)
126
+ end
115
127
  end
116
128
 
117
129
  describe "name" do
@@ -137,7 +149,9 @@ describe Mongoid::Giza::Index do
137
149
  let(:buffer) { double("buffer") }
138
150
 
139
151
  it "should create a new XMLPipe2 object" do
140
- expect(Mongoid::Giza::XMLPipe2).to receive(:new).with(index, buffer) { xmlpipe2.as_null_object }
152
+ expect(Mongoid::Giza::XMLPipe2).to receive(:new).with(index, buffer) do
153
+ xmlpipe2.as_null_object
154
+ end
141
155
  index.xmlpipe2(buffer)
142
156
  end
143
157
 
@@ -11,7 +11,7 @@ describe Mongoid::Giza::Indexer do
11
11
 
12
12
  describe "index!" do
13
13
  it "should execute the sphinx indexer" do
14
- expect(@controller).to receive(:index).with(no_args())
14
+ expect(@controller).to receive(:index).with(no_args)
15
15
  @indexer.index!
16
16
  end
17
17
 
@@ -67,15 +67,18 @@ describe Mongoid::Giza::Indexer do
67
67
 
68
68
  describe "giza_classes" do
69
69
  before(:all) do
70
+ # :nodoc:
70
71
  class One
71
72
  include Mongoid::Document
72
73
  include Mongoid::Giza
73
74
  end
74
75
 
76
+ # :nodoc:
75
77
  class Two
76
78
  include Mongoid::Document
77
79
  end
78
80
 
81
+ # :nodoc:
79
82
  class Three
80
83
  include Mongoid::Document
81
84
  include Mongoid::Giza
@@ -1,8 +1,8 @@
1
1
  require "spec_helper"
2
2
 
3
- describe Mongoid::Giza::GizaID do
3
+ describe Mongoid::Giza::ID do
4
4
  describe "instance" do
5
- let(:giza_id) { Mongoid::Giza::GizaID.new(id: :Person) }
5
+ let(:giza_id) { Mongoid::Giza::ID.new(id: :Person) }
6
6
 
7
7
  it "should be valid" do
8
8
  expect(giza_id).to be_valid
@@ -17,14 +17,14 @@ describe Mongoid::Giza::GizaID do
17
17
  end
18
18
  end
19
19
 
20
- describe "next_id" do
20
+ describe "next" do
21
21
  before do
22
- Mongoid::Giza::GizaID.create(id: :Person)
22
+ Mongoid::Giza::ID.create(id: :Person)
23
23
  end
24
24
 
25
25
  it "should return the next id for the given class" do
26
- expect(Mongoid::Giza::GizaID.next_id(:Person)).to eql(1)
27
- expect(Mongoid::Giza::GizaID.next_id(:Person)).to eql(2)
26
+ expect(Mongoid::Giza::ID.next(:Person)).to eql(1)
27
+ expect(Mongoid::Giza::ID.next(:Person)).to eql(2)
28
28
  end
29
29
  end
30
30
  end
@@ -1,22 +1,19 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Mongoid::Giza::Search do
4
- let(:client) do
5
- client = double("client")
6
- allow(Riddle::Client).to receive(:new).with("localhost", 9132) { client }
7
- client
8
- end
4
+ let(:client) { double("client") }
9
5
 
10
6
  let(:search) { Mongoid::Giza::Search.new("localhost", 9132) }
11
7
 
12
- let(:filters) do
13
- filters = double("filters")
14
- allow(client).to receive(:filters) { filters }
15
- filters
16
- end
8
+ let(:filters) { double("filters") }
17
9
 
18
10
  let(:filter) { double("filter") }
19
11
 
12
+ before do
13
+ allow(Riddle::Client).to receive(:new).with("localhost", 9132) { client }
14
+ allow(client).to receive(:filters) { filters }
15
+ end
16
+
20
17
  describe "initialize" do
21
18
  it "should create a new client with the given host and port" do
22
19
  expect(Riddle::Client).to receive(:new).with("localhost", 9132)
@@ -24,28 +21,24 @@ describe Mongoid::Giza::Search do
24
21
  end
25
22
 
26
23
  it "should accept a index list" do
27
- indexes = Mongoid::Giza::Search.new("localhost", 9132, [:index1, :index2]).indexes
24
+ indexes = Mongoid::Giza::Search.new("localhost", 9132, [:index1, :index2])
25
+ .indexes
28
26
  expect(indexes).to eql([:index1, :index2])
29
27
  end
30
28
  end
31
29
 
32
30
  describe "fulltext" do
33
- it "should append a query on the specified indexes" do
34
- expect(client).to receive(:append_query).with("query", "index1 index2")
35
- allow(search).to receive(:indexes) { [:index1, :index2] }
36
- search.fulltext("query")
37
- end
38
-
39
- it "should search all indexes by default" do
40
- expect(client).to receive(:append_query).with("query", "*")
41
- allow(search).to receive(:indexes) { [] }
31
+ it "should define the query string" do
42
32
  search.fulltext("query")
33
+ expect(search.query_string).to eql("query")
43
34
  end
44
35
  end
45
36
 
46
37
  describe "with" do
47
38
  it "should add a filter to the search" do
48
- expect(Riddle::Client::Filter).to receive(:new).with("attr", 1, false) { filter }
39
+ expect(Riddle::Client::Filter).to receive(:new).with("attr", 1, false) do
40
+ filter
41
+ end
49
42
  expect(filters).to receive(:<<).with(filter)
50
43
  client
51
44
  search.with(:attr, 1)
@@ -54,7 +47,9 @@ describe Mongoid::Giza::Search do
54
47
 
55
48
  describe "without" do
56
49
  it "should add a filter to the search" do
57
- expect(Riddle::Client::Filter).to receive(:new).with("attr", 1, true) { filter }
50
+ expect(Riddle::Client::Filter).to receive(:new).with("attr", 1, true) do
51
+ filter
52
+ end
58
53
  expect(filters).to receive(:<<).with(filter)
59
54
  client
60
55
  search.without(:attr, 1)
@@ -69,30 +64,57 @@ describe Mongoid::Giza::Search do
69
64
  end
70
65
 
71
66
  describe "run" do
72
- it "should run the query" do
73
- expect(client).to receive(:run) { [1, 2] }
67
+ let(:result) { double("result") }
68
+
69
+ before do
70
+ search.fulltext("query")
71
+ end
72
+
73
+ it "should execute the query" do
74
+ allow(search).to receive(:indexes) { [:index1, :index2] }
75
+ expect(client).to receive(:query).with("query", "index1 index2")
74
76
  search.run
75
77
  end
76
78
 
77
- it "should return the result array" do
78
- expect(client).to receive(:run) { [1, 2] }
79
- expect(search.run).to eql([1, 2])
79
+ it "should search all indexes by default" do
80
+ expect(client).to receive(:query).with("query", "*")
81
+ search.run
82
+ end
83
+
84
+ it "should return the result of the query" do
85
+ allow(client).to receive(:query).with("query", "*") { result }
86
+ expect(search.run).to be result
80
87
  end
81
88
  end
82
89
 
83
90
  describe "riddle methods mapping" do
84
- it "should respond to method from riddle" do
85
- allow(client).to receive(:respond_to?).with("offset=") { true }
86
- expect(search.respond_to?(:offset)).to eql(true)
91
+ context "with no argument" do
92
+ it "should respond to method from riddle" do
93
+ allow(client).to receive(:respond_to?).with(:offset) { true }
94
+ expect(client).to receive(:"offset").with(no_args)
95
+ search.offset
96
+ end
97
+ end
98
+
99
+ context "with one argument" do
100
+ it "should respond to method from riddle" do
101
+ allow(client).to receive(:respond_to?).with("offset=") { true }
102
+ expect(client).to receive(:"offset=").with(1)
103
+ search.offset(1)
104
+ end
87
105
  end
88
106
 
89
- it "should call the equivalent method from riddle" do
90
- allow(client).to receive(:respond_to?).with("offset=") { true }
91
- expect(client).to receive(:offset=).with(1)
92
- search.offset(1)
107
+ context "with multiple arguments" do
108
+ it "should respond to method from riddle" do
109
+ allow(client).to receive(:respond_to?).with(:offset) { true }
110
+ expect(client).to receive(:"offset").with(1, 2)
111
+ search.offset(1, 2)
112
+ end
93
113
  end
94
114
 
95
- it "should raise an error when the equivalent riddle's method does not exists" do
115
+ it "should raise an error when the equivalent riddle's method does not " \
116
+ "exists" do
117
+ allow(client).to receive(:respond_to?).with(:idontexist) { false }
96
118
  allow(client).to receive(:respond_to?).with("idontexist=") { false }
97
119
  expect { search.idontexist }.to raise_error(NoMethodError)
98
120
  end
@@ -15,20 +15,38 @@ describe Mongoid::Giza::XMLPipe2 do
15
15
  allow(@index).to receive(:fields) { [@field] }
16
16
  allow(@index).to receive(:attributes) { [@attribute] }
17
17
  allow(@field).to receive(:name) { :name }
18
+ allow(@field).to receive(:attribute)
18
19
  allow(@attribute).to receive(:name) { :age }
19
- allow(@attribute).to receive(:type) { :uint }
20
+ allow(@attribute).to receive(:type) { :int }
21
+ allow(@attribute).to receive(:default)
22
+ allow(@attribute).to receive(:bits)
20
23
  end
21
24
 
22
25
  it "should generate the schema of the docset" do
23
- allow(@field).to receive(:attribute) { false }
24
26
  xmlpipe2.generate_schema
25
- expect(@buffer).to eql('<sphinx:schema><sphinx:field name="name"/><sphinx:attr name="age" type="uint"/></sphinx:schema>')
27
+ expect(@buffer).to eql('<sphinx:schema><sphinx:field name="name"/>' \
28
+ '<sphinx:attr name="age" type="int"/></sphinx:schema>')
26
29
  end
27
30
 
28
31
  it "should generate a field attribute" do
29
32
  allow(@field).to receive(:attribute) { true }
30
33
  xmlpipe2.generate_schema
31
- expect(@buffer).to eql('<sphinx:schema><sphinx:field name="name" attr="string"/><sphinx:attr name="age" type="uint"/></sphinx:schema>')
34
+ expect(@buffer).to eql('<sphinx:schema><sphinx:field name="name" '\
35
+ 'attr="string"/><sphinx:attr name="age" type="int"/></sphinx:schema>')
36
+ end
37
+
38
+ it "should include default attribute if present" do
39
+ allow(@attribute).to receive(:default) { 1 }
40
+ xmlpipe2.generate_schema
41
+ expect(@buffer).to eql('<sphinx:schema><sphinx:field name="name"/>' \
42
+ '<sphinx:attr name="age" type="int" default="1"/></sphinx:schema>')
43
+ end
44
+
45
+ it "should include bits attribute if present" do
46
+ allow(@attribute).to receive(:bits) { 16 }
47
+ xmlpipe2.generate_schema
48
+ expect(@buffer).to eql('<sphinx:schema><sphinx:field name="name"/>' \
49
+ '<sphinx:attr name="age" type="int" bits="16"/></sphinx:schema>')
32
50
  end
33
51
  end
34
52
 
@@ -40,19 +58,19 @@ describe Mongoid::Giza::XMLPipe2 do
40
58
  allow(@index).to receive(:fields) { [field] }
41
59
  allow(@index).to receive(:attributes) { [attribute] }
42
60
  allow(@index).to receive(:criteria) { [person] }
43
- allow(person).to receive(:giza_id) { 1 }
61
+ allow(person).to receive(:_giza_id) { 1 }
44
62
  allow(xmlpipe2).to receive(:generate_doc_tags).with([field], person) do
45
63
  @buffer << "<name>Person One</name>"
46
64
  end
47
- allow(xmlpipe2).to receive(:generate_doc_tags).with([attribute], person) do
48
- @buffer << "<age>25</age>"
49
- end
65
+ allow(xmlpipe2).to receive(:generate_doc_tags)
66
+ .with([attribute], person) { @buffer << "<age>25</age>" }
50
67
  end
51
68
 
52
69
  context "static fields and attributes" do
53
70
  it "should generate the document entries" do
54
71
  xmlpipe2.generate_docset
55
- expect(@buffer).to eql('<sphinx:document id="1"><name>Person One</name><age>25</age></sphinx:document>')
72
+ expect(@buffer).to eql('<sphinx:document id="1"><name>Person One' \
73
+ "</name><age>25</age></sphinx:document>")
56
74
  end
57
75
  end
58
76
  end
@@ -65,7 +83,7 @@ describe Mongoid::Giza::XMLPipe2 do
65
83
  @person = {name: "Person One", bio: "About me"}
66
84
  allow(name).to receive(:name) { :name }
67
85
  allow(name).to receive(:block) do
68
- Proc.new { |document| document[:name].upcase }
86
+ proc { |document| document[:name].upcase }
69
87
  end
70
88
  allow(bio).to receive(:name) { :bio }
71
89
  allow(bio).to receive(:block) { nil }
@@ -73,26 +91,51 @@ describe Mongoid::Giza::XMLPipe2 do
73
91
 
74
92
  it "should generate all tags for the given fields or attributes" do
75
93
  xmlpipe2.generate_doc_tags(@fields, @person)
76
- expect(@buffer).to eql('<name>PERSON ONE</name><bio>About me</bio>')
94
+ expect(@buffer).to eql("<name>PERSON ONE</name><bio>About me</bio>")
77
95
  end
78
96
  end
79
97
 
80
98
  describe "generate!" do
81
99
  it "should generate a xml file of the index" do
82
100
  result = '<?xml version="1.0" encoding="utf-8"?>'
83
- result << '<sphinx:docset><sphinx:schema>'
84
- result << '<sphinx:field name="name"/><sphinx:attr name="age" type="uint"/>'
85
- result << '</sphinx:schema>'
86
- result << '<sphinx:document id="1"><name>Person One</name><age>25</age></sphinx:document>'
87
- result << '</sphinx:docset>'
101
+ result << "<sphinx:docset><sphinx:schema>"
102
+ result <<
103
+ '<sphinx:field name="name"/><sphinx:attr name="age" type="int"/>'
104
+ result << "</sphinx:schema>"
105
+ result << '<sphinx:document id="1"><name>Person One</name><age>25</age>' \
106
+ "</sphinx:document>"
107
+ result << "</sphinx:docset>"
88
108
  expect(xmlpipe2).to receive(:generate_schema) do
89
- @buffer << '<sphinx:schema><sphinx:field name="name"/><sphinx:attr name="age" type="uint"/></sphinx:schema>'
109
+ @buffer << '<sphinx:schema><sphinx:field name="name"/><sphinx:attr ' \
110
+ 'name="age" type="int"/></sphinx:schema>'
90
111
  end
91
112
  expect(xmlpipe2).to receive(:generate_docset) do
92
- @buffer << '<sphinx:document id="1"><name>Person One</name><age>25</age></sphinx:document>'
113
+ @buffer << '<sphinx:document id="1"><name>Person One</name>' \
114
+ "<age>25</age></sphinx:document>"
93
115
  end
94
116
  xmlpipe2.generate!
95
117
  expect(@buffer).to eql(result)
96
118
  end
97
119
  end
120
+
121
+ describe "process_value" do
122
+ let(:time_attr) { Mongoid::Giza::Index::Attribute.new(:at, :timestamp) }
123
+
124
+ let(:attribute) { Mongoid::Giza::Index::Attribute.new(:age, :uint) }
125
+
126
+ let(:object) { {at: @now, age: 50} }
127
+
128
+ before do
129
+ @now = Time.new(1)
130
+ end
131
+
132
+ it "should convert timestamp attributes to unix time" do
133
+ value = xmlpipe2.process_value(time_attr, object)
134
+ expect(Time.at(value)).to eq(@now)
135
+ end
136
+
137
+ it "should return other values as is" do
138
+ expect(xmlpipe2.process_value(attribute, object)).to be 50
139
+ end
140
+ end
98
141
  end