active-fedora 5.3.1 → 5.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -36,8 +36,9 @@ describe ActiveFedora::SolrService do
36
36
  @foo_content = @foo_object.datastreams['descMetadata'].content
37
37
  end
38
38
  after(:all) do
39
- # @test_object.delete
40
- # @foo_object.delete
39
+ @test_object.delete
40
+ @foo_object.delete
41
+ Object.send(:remove_const, :FooObject)
41
42
  end
42
43
  it "should return an array of objects that are of the class stored in active_fedora_model_s" do
43
44
  query = "id\:#{ActiveFedora::SolrService.escape_uri_for_query(@test_object.pid)} OR id\:#{ActiveFedora::SolrService.escape_uri_for_query(@foo_object.pid)}"
@@ -19,7 +19,7 @@ describe ActiveFedora::Datastream do
19
19
  end
20
20
 
21
21
  it "should be inspectable" do
22
- @test_datastream.inspect.should match /#<ActiveFedora::Datastream:-?\d+ @pid=\"__DO_NOT_USE__\" @dsid=\"abcd\" @controlGroup=\"M\" changed=\"true\" @mimeType=\"\" >/
22
+ @test_datastream.inspect.should match /#<ActiveFedora::Datastream @pid=\"__DO_NOT_USE__\" @dsid=\"abcd\" @controlGroup=\"M\" changed=\"true\" @mimeType=\"\" >/
23
23
  end
24
24
 
25
25
  describe '#validate_content_present' do
@@ -7,7 +7,7 @@ describe ActiveFedora::Datastreams do
7
7
  before do
8
8
  class FooHistory < ActiveFedora::Base
9
9
  has_metadata :name => 'dsid'
10
- has_metadata :name => 'complex_ds', :versionable => true, :autocreate => true, :type => 'Z', :label => 'My Label', :control_group => 'Z'
10
+ has_metadata 'complex_ds', :versionable => true, :autocreate => true, :type => 'Z', :label => 'My Label', :control_group => 'Z'
11
11
  end
12
12
  end
13
13
 
@@ -28,11 +28,13 @@ describe ActiveFedora::Datastreams do
28
28
  before do
29
29
  class FooHistory < ActiveFedora::Base
30
30
  has_file_datastream :name => 'dsid'
31
+ has_file_datastream 'another'
31
32
  end
32
33
  end
33
34
 
34
35
  it "should have reasonable defaults" do
35
36
  FooHistory.ds_specs['dsid'].should include(:type => ActiveFedora::Datastream, :label => 'File Datastream', :control_group => 'M')
37
+ FooHistory.ds_specs['another'].should include(:type => ActiveFedora::Datastream, :label => 'File Datastream', :control_group => 'M')
36
38
  end
37
39
  end
38
40
 
@@ -4,10 +4,7 @@ describe ActiveFedora::Model do
4
4
 
5
5
  before(:all) do
6
6
  module SpecModel
7
- class Basic
8
- include ActiveFedora::Model
9
- def initialize (args = {})
10
- end
7
+ class Basic < ActiveFedora::Base
11
8
  end
12
9
  end
13
10
  @model_query = "has_model_s:#{solr_uri("info:fedora/afmodel:SpecModel_Basic")}"
@@ -31,204 +28,6 @@ describe ActiveFedora::Model do
31
28
  end
32
29
  end
33
30
 
34
- describe '#find' do
35
- describe "without :cast" do
36
- describe ":all" do
37
- describe "called on a concrete class" do
38
- it "should query solr for all objects with :has_model_s of self.class" do
39
- SpecModel::Basic.should_receive(:find_one).with("changeme:30", nil).and_return("Fake Object1")
40
- SpecModel::Basic.should_receive(:find_one).with("changeme:22", nil).and_return("Fake Object2")
41
- mock_docs = [{"id" => "changeme:30"}, {"id" => "changeme:22"}]
42
- mock_docs.should_receive(:has_next?).and_return(false)
43
- ActiveFedora::SolrService.instance.conn.should_receive(:paginate).with(1, 1000, 'select', :params=>{:q=>@model_query, :qt => 'standard', :sort => ['system_create_dt asc'], :fl=> 'id', }).and_return('response'=>{'docs'=>mock_docs})
44
- SpecModel::Basic.find(:all).should == ["Fake Object1", "Fake Object2"]
45
- end
46
- end
47
- describe "called without a specific class" do
48
- it "should specify a q parameter" do
49
- ActiveFedora::Base.should_receive(:find_one).with("changeme:30", nil).and_return("Fake Object1")
50
- ActiveFedora::Base.should_receive(:find_one).with("changeme:22", nil).and_return("Fake Object2")
51
- mock_docs = [{"id" => "changeme:30"},{"id" => "changeme:22"}]
52
- mock_docs.should_receive(:has_next?).and_return(false)
53
- ActiveFedora::SolrService.instance.conn.should_receive(:paginate).with(1, 1000, 'select', :params=>{:q=>'*:*', :qt => 'standard', :sort => ['system_create_dt asc'], :fl=> 'id', }).and_return('response'=>{'docs'=>mock_docs})
54
- ActiveFedora::Base.find(:all).should == ["Fake Object1", "Fake Object2"]
55
- end
56
- end
57
- end
58
- describe "and a pid is specified" do
59
- it "should use SpecModel::Basic.allocate.init_with to instantiate an object" do
60
- SpecModel::Basic.any_instance.should_receive(:init_with).and_return(SpecModel::Basic.new)
61
- ActiveFedora::DigitalObject.should_receive(:find).and_return(stub("inner obj", :'new?'=>false))
62
- SpecModel::Basic.find("_PID_").should be_a SpecModel::Basic
63
- end
64
- it "should raise an exception if it is not found" do
65
- Rubydora::Repository.any_instance.should_receive(:object).and_raise(RestClient::ResourceNotFound)
66
- SpecModel::Basic.should_receive(:connection_for_pid).with("_PID_")
67
- lambda {SpecModel::Basic.find("_PID_")}.should raise_error ActiveFedora::ObjectNotFoundError
68
- end
69
- end
70
- end
71
- describe "with :cast" do
72
- it "should use SpecModel::Basic.allocate.init_with to instantiate an object" do
73
- SpecModel::Basic.any_instance.should_receive(:init_with).and_return(mock("Model", :adapt_to_cmodel=>SpecModel::Basic.new ))
74
- ActiveFedora::DigitalObject.should_receive(:find).and_return(stub("inner obj", :'new?'=>false))
75
- SpecModel::Basic.find("_PID_", :cast=>true)
76
- end
77
- end
78
-
79
- describe "with conditions" do
80
- it "should filter by the provided fields" do
81
- SpecModel::Basic.should_receive(:find_one).with("changeme:30", nil).and_return("Fake Object1")
82
- SpecModel::Basic.should_receive(:find_one).with("changeme:22", nil).and_return("Fake Object2")
83
-
84
- mock_docs = [{"id" => "changeme:30"},{"id" => "changeme:22"}]
85
- mock_docs.should_receive(:has_next?).and_return(false)
86
- ActiveFedora::SolrService.instance.conn.should_receive(:paginate).with() { |page, rows, method, hash|
87
- page == 1 &&
88
- rows == 1000 &&
89
- method == 'select' &&
90
- hash[:params] &&
91
- hash[:params][:sort] == ['system_create_dt asc'] &&
92
- hash[:params][:fl] == 'id' &&
93
- hash[:params][:q].split(" AND ").include?(@model_query) &&
94
- hash[:params][:q].split(" AND ").include?("foo:\"bar\"") &&
95
- hash[:params][:q].split(" AND ").include?("baz:\"quix\"") &&
96
- hash[:params][:q].split(" AND ").include?("baz:\"quack\"")
97
- }.and_return('response'=>{'docs'=>mock_docs})
98
- SpecModel::Basic.find({:foo=>'bar', :baz=>['quix','quack']}).should == ["Fake Object1", "Fake Object2"]
99
- end
100
- end
101
- end
102
-
103
- describe '#all' do
104
- it "should pass everything through to .find" do
105
- SpecModel::Basic.should_receive(:find).with(:all, {})
106
- SpecModel::Basic.all
107
- end
108
- end
109
-
110
- describe '#find_each' do
111
- it "should query solr for all objects with :active_fedora_model_s of self.class" do
112
- mock_docs = [{"id" => "changeme:30"},{"id" => "changeme:22"}]
113
- mock_docs.should_receive(:has_next?).and_return(false)
114
- ActiveFedora::SolrService.instance.conn.should_receive(:paginate).with(1, 1000, 'select', :params=>{:q=>@model_query, :qt => 'standard', :sort => ['system_create_dt asc'], :fl=> 'id', }).and_return('response'=>{'docs'=>mock_docs})
115
-
116
- SpecModel::Basic.should_receive(:find_one).with("changeme:30", nil).and_return(SpecModel::Basic.new(:pid=>'changeme:30'))
117
- SpecModel::Basic.should_receive(:find_one).with("changeme:22", nil).and_return(SpecModel::Basic.new(:pid=>'changeme:22'))
118
- yielded = mock("yielded method")
119
- yielded.should_receive(:run).with { |obj| obj.class == SpecModel::Basic}.twice
120
- SpecModel::Basic.find_each(){|obj| yielded.run(obj) }
121
- end
122
- describe "with conditions" do
123
- it "should filter by the provided fields" do
124
- SpecModel::Basic.should_receive(:find_one).with("changeme:30", nil).and_return(SpecModel::Basic.new(:pid=>'changeme:30'))
125
- SpecModel::Basic.should_receive(:find_one).with("changeme:22", nil).and_return(SpecModel::Basic.new(:pid=>'changeme:22'))
126
-
127
- mock_docs = [{"id" => "changeme:30"},{"id" => "changeme:22"}]
128
- mock_docs.should_receive(:has_next?).and_return(false)
129
- ActiveFedora::SolrService.instance.conn.should_receive(:paginate).with() { |page, rows, method, hash|
130
- page == 1 &&
131
- rows == 1000 &&
132
- method == 'select' &&
133
- hash[:params] &&
134
- hash[:params][:sort] == ['system_create_dt asc'] &&
135
- hash[:params][:fl] == 'id' &&
136
- hash[:params][:q].split(" AND ").include?(@model_query) &&
137
- hash[:params][:q].split(" AND ").include?("foo:\"bar\"") &&
138
- hash[:params][:q].split(" AND ").include?("baz:\"quix\"") &&
139
- hash[:params][:q].split(" AND ").include?("baz:\"quack\"")
140
- }.and_return('response'=>{'docs'=>mock_docs})
141
- yielded = mock("yielded method")
142
- yielded.should_receive(:run).with { |obj| obj.class == SpecModel::Basic}.twice
143
- SpecModel::Basic.find_each({:foo=>'bar', :baz=>['quix','quack']}){|obj| yielded.run(obj) }
144
- end
145
- end
146
- end
147
-
148
- describe '#find_in_batches' do
149
- describe "with conditions hash" do
150
- it "should filter by the provided fields" do
151
- mock_docs = mock('docs')
152
- mock_docs.should_receive(:has_next?).and_return(false)
153
- ActiveFedora::SolrService.instance.conn.should_receive(:paginate).with() { |page, rows, method, hash|
154
- page == 1 &&
155
- rows == 1002 &&
156
- method == 'select' &&
157
- hash[:params] &&
158
- hash[:params][:sort] == ['system_create_dt asc'] &&
159
- hash[:params][:fl] == 'id' &&
160
- hash[:params][:q].split(" AND ").include?(@model_query) &&
161
- hash[:params][:q].split(" AND ").include?("foo:\"bar\"") &&
162
- hash[:params][:q].split(" AND ").include?("baz:\"quix\"") &&
163
- hash[:params][:q].split(" AND ").include?("baz:\"quack\"")
164
- }.and_return('response'=>{'docs'=>mock_docs})
165
- yielded = mock("yielded method")
166
- yielded.should_receive(:run).with(mock_docs)
167
- SpecModel::Basic.find_in_batches({:foo=>'bar', :baz=>['quix','quack']}, {:batch_size=>1002, :fl=>'id'}){|group| yielded.run group }.should
168
- end
169
- end
170
- end
171
-
172
- describe '#count' do
173
-
174
- it "should return a count" do
175
- mock_result = {'response'=>{'numFound'=>7}}
176
- ActiveFedora::SolrService.should_receive(:query).with(@model_query, :rows=>0, :raw=>true).and_return(mock_result)
177
- SpecModel::Basic.count.should == 7
178
- end
179
- it "should allow conditions" do
180
- mock_result = {'response'=>{'numFound'=>7}}
181
- ActiveFedora::SolrService.should_receive(:query).with("#{@model_query} AND foo:bar", :rows=>0, :raw=>true).and_return(mock_result)
182
- SpecModel::Basic.count(:conditions=>'foo:bar').should == 7
183
- end
184
-
185
- it "should count without a class specified" do
186
- mock_result = {'response'=>{'numFound'=>7}}
187
- ActiveFedora::SolrService.should_receive(:query).with("foo:bar", :rows=>0, :raw=>true).and_return(mock_result)
188
- ActiveFedora::Base.count(:conditions=>'foo:bar').should == 7
189
- end
190
- end
191
-
192
- describe '#find_with_conditions' do
193
- it "should make a query to solr and return the results" do
194
- mock_result = stub('Result')
195
- ActiveFedora::SolrService.should_receive(:query).with() { |args|
196
- q = args.first if args.is_a? Array
197
- q ||= args
198
- q.split(" AND ").include?(@model_query) &&
199
- q.split(" AND ").include?("foo:\"bar\"") &&
200
- q.split(" AND ").include?("baz:\"quix\"") &&
201
- q.split(" AND ").include?("baz:\"quack\"")
202
- }.and_return(mock_result)
203
- SpecModel::Basic.find_with_conditions(:foo=>'bar', :baz=>['quix','quack']).should == mock_result
204
- end
205
-
206
- it "should escape quotes" do
207
- mock_result = stub('Result')
208
- ActiveFedora::SolrService.should_receive(:query).with() { |args|
209
- q = args.first if args.is_a? Array
210
- q ||= args
211
- q.split(" AND ").include?(@model_query) &&
212
- q.split(" AND ").include?(@model_query) &&
213
- q.split(" AND ").include?('foo:"9\\" Nails"') &&
214
- q.split(" AND ").include?('baz:"7\\" version"') &&
215
- q.split(" AND ").include?('baz:"quack"')
216
- }.and_return(mock_result)
217
- SpecModel::Basic.find_with_conditions(:foo=>'9" Nails', :baz=>['7" version','quack']).should == mock_result
218
- end
219
-
220
- it "shouldn't use the class if it's called on AF:Base " do
221
- mock_result = stub('Result')
222
- ActiveFedora::SolrService.should_receive(:query).with('baz:"quack"', {:sort => ['system_create_dt asc']}).and_return(mock_result)
223
- ActiveFedora::Base.find_with_conditions(:baz=>'quack').should == mock_result
224
- end
225
- it "should use the query string if it's provided" do
226
- mock_result = stub('Result')
227
- ActiveFedora::SolrService.should_receive(:query).with('chunky:monkey', {:sort => ['system_create_dt asc']}).and_return(mock_result)
228
- ActiveFedora::Base.find_with_conditions('chunky:monkey').should == mock_result
229
- end
230
- end
231
-
232
31
  describe "URI translation" do
233
32
  before :all do
234
33
  module SpecModel
@@ -0,0 +1,208 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveFedora::Base do
4
+
5
+ before(:all) do
6
+ module SpecModel
7
+ class Basic < ActiveFedora::Base
8
+ end
9
+ end
10
+ @model_query = "has_model_s:#{solr_uri("info:fedora/afmodel:SpecModel_Basic")}"
11
+ end
12
+
13
+ after(:all) do
14
+ Object.send(:remove_const, :SpecModel)
15
+ end
16
+
17
+ describe '#find' do
18
+ describe "without :cast" do
19
+ describe ":all" do
20
+ describe "called on a concrete class" do
21
+ it "should query solr for all objects with :has_model_s of self.class" do
22
+ SpecModel::Basic.should_receive(:find_one).with("changeme:30", nil).and_return("Fake Object1")
23
+ SpecModel::Basic.should_receive(:find_one).with("changeme:22", nil).and_return("Fake Object2")
24
+ mock_docs = [{"id" => "changeme:30"}, {"id" => "changeme:22"}]
25
+ mock_docs.should_receive(:has_next?).and_return(false)
26
+ ActiveFedora::SolrService.instance.conn.should_receive(:paginate).with(1, 1000, 'select', :params=>{:q=>@model_query, :qt => 'standard', :sort => ['system_create_dt asc'], :fl=> 'id', }).and_return('response'=>{'docs'=>mock_docs})
27
+ SpecModel::Basic.find(:all).should == ["Fake Object1", "Fake Object2"]
28
+ end
29
+ end
30
+ describe "called without a specific class" do
31
+ it "should specify a q parameter" do
32
+ ActiveFedora::Base.should_receive(:find_one).with("changeme:30", nil).and_return("Fake Object1")
33
+ ActiveFedora::Base.should_receive(:find_one).with("changeme:22", nil).and_return("Fake Object2")
34
+ mock_docs = [{"id" => "changeme:30"},{"id" => "changeme:22"}]
35
+ mock_docs.should_receive(:has_next?).and_return(false)
36
+ ActiveFedora::SolrService.instance.conn.should_receive(:paginate).with(1, 1000, 'select', :params=>{:q=>'*:*', :qt => 'standard', :sort => ['system_create_dt asc'], :fl=> 'id', }).and_return('response'=>{'docs'=>mock_docs})
37
+ ActiveFedora::Base.find(:all).should == ["Fake Object1", "Fake Object2"]
38
+ end
39
+ end
40
+ end
41
+ describe "and a pid is specified" do
42
+ it "should use SpecModel::Basic.allocate.init_with to instantiate an object" do
43
+ SpecModel::Basic.any_instance.should_receive(:init_with).and_return(SpecModel::Basic.new)
44
+ ActiveFedora::DigitalObject.should_receive(:find).and_return(stub("inner obj", :'new?'=>false))
45
+ SpecModel::Basic.find("_PID_").should be_a SpecModel::Basic
46
+ end
47
+ it "should raise an exception if it is not found" do
48
+ Rubydora::Repository.any_instance.should_receive(:object).and_raise(RestClient::ResourceNotFound)
49
+ SpecModel::Basic.should_receive(:connection_for_pid).with("_PID_")
50
+ lambda {SpecModel::Basic.find("_PID_")}.should raise_error ActiveFedora::ObjectNotFoundError
51
+ end
52
+ end
53
+ end
54
+ describe "with :cast" do
55
+ it "should use SpecModel::Basic.allocate.init_with to instantiate an object" do
56
+ SpecModel::Basic.any_instance.should_receive(:init_with).and_return(mock("Model", :adapt_to_cmodel=>SpecModel::Basic.new ))
57
+ ActiveFedora::DigitalObject.should_receive(:find).and_return(stub("inner obj", :'new?'=>false))
58
+ SpecModel::Basic.find("_PID_", :cast=>true)
59
+ end
60
+ end
61
+
62
+ describe "with conditions" do
63
+ it "should filter by the provided fields" do
64
+ SpecModel::Basic.should_receive(:find_one).with("changeme:30", nil).and_return("Fake Object1")
65
+ SpecModel::Basic.should_receive(:find_one).with("changeme:22", nil).and_return("Fake Object2")
66
+
67
+ mock_docs = [{"id" => "changeme:30"},{"id" => "changeme:22"}]
68
+ mock_docs.should_receive(:has_next?).and_return(false)
69
+ ActiveFedora::SolrService.instance.conn.should_receive(:paginate).with() { |page, rows, method, hash|
70
+ page == 1 &&
71
+ rows == 1000 &&
72
+ method == 'select' &&
73
+ hash[:params] &&
74
+ hash[:params][:sort] == ['system_create_dt asc'] &&
75
+ hash[:params][:fl] == 'id' &&
76
+ hash[:params][:q].split(" AND ").include?(@model_query) &&
77
+ hash[:params][:q].split(" AND ").include?("foo:\"bar\"") &&
78
+ hash[:params][:q].split(" AND ").include?("baz:\"quix\"") &&
79
+ hash[:params][:q].split(" AND ").include?("baz:\"quack\"")
80
+ }.and_return('response'=>{'docs'=>mock_docs})
81
+ SpecModel::Basic.find({:foo=>'bar', :baz=>['quix','quack']}).should == ["Fake Object1", "Fake Object2"]
82
+ end
83
+ end
84
+ end
85
+
86
+
87
+ describe '#find_each' do
88
+ it "should query solr for all objects with :active_fedora_model_s of self.class" do
89
+ mock_docs = [{"id" => "changeme:30"},{"id" => "changeme:22"}]
90
+ mock_docs.should_receive(:has_next?).and_return(false)
91
+ ActiveFedora::SolrService.instance.conn.should_receive(:paginate).with(1, 1000, 'select', :params=>{:q=>@model_query, :qt => 'standard', :sort => ['system_create_dt asc'], :fl=> 'id', }).and_return('response'=>{'docs'=>mock_docs})
92
+
93
+ SpecModel::Basic.should_receive(:find_one).with("changeme:30", nil).and_return(SpecModel::Basic.new(:pid=>'changeme:30'))
94
+ SpecModel::Basic.should_receive(:find_one).with("changeme:22", nil).and_return(SpecModel::Basic.new(:pid=>'changeme:22'))
95
+ yielded = mock("yielded method")
96
+ yielded.should_receive(:run).with { |obj| obj.class == SpecModel::Basic}.twice
97
+ SpecModel::Basic.find_each(){|obj| yielded.run(obj) }
98
+ end
99
+ describe "with conditions" do
100
+ it "should filter by the provided fields" do
101
+ SpecModel::Basic.should_receive(:find_one).with("changeme:30", nil).and_return(SpecModel::Basic.new(:pid=>'changeme:30'))
102
+ SpecModel::Basic.should_receive(:find_one).with("changeme:22", nil).and_return(SpecModel::Basic.new(:pid=>'changeme:22'))
103
+
104
+ mock_docs = [{"id" => "changeme:30"},{"id" => "changeme:22"}]
105
+ mock_docs.should_receive(:has_next?).and_return(false)
106
+ ActiveFedora::SolrService.instance.conn.should_receive(:paginate).with() { |page, rows, method, hash|
107
+ page == 1 &&
108
+ rows == 1000 &&
109
+ method == 'select' &&
110
+ hash[:params] &&
111
+ hash[:params][:sort] == ['system_create_dt asc'] &&
112
+ hash[:params][:fl] == 'id' &&
113
+ hash[:params][:q].split(" AND ").include?(@model_query) &&
114
+ hash[:params][:q].split(" AND ").include?("foo:\"bar\"") &&
115
+ hash[:params][:q].split(" AND ").include?("baz:\"quix\"") &&
116
+ hash[:params][:q].split(" AND ").include?("baz:\"quack\"")
117
+ }.and_return('response'=>{'docs'=>mock_docs})
118
+ yielded = mock("yielded method")
119
+ yielded.should_receive(:run).with { |obj| obj.class == SpecModel::Basic}.twice
120
+ SpecModel::Basic.find_each({:foo=>'bar', :baz=>['quix','quack']}){|obj| yielded.run(obj) }
121
+ end
122
+ end
123
+ end
124
+
125
+ describe '#find_in_batches' do
126
+ describe "with conditions hash" do
127
+ it "should filter by the provided fields" do
128
+ mock_docs = mock('docs')
129
+ mock_docs.should_receive(:has_next?).and_return(false)
130
+ ActiveFedora::SolrService.instance.conn.should_receive(:paginate).with() { |page, rows, method, hash|
131
+ page == 1 &&
132
+ rows == 1002 &&
133
+ method == 'select' &&
134
+ hash[:params] &&
135
+ hash[:params][:sort] == ['system_create_dt asc'] &&
136
+ hash[:params][:fl] == 'id' &&
137
+ hash[:params][:q].split(" AND ").include?(@model_query) &&
138
+ hash[:params][:q].split(" AND ").include?("foo:\"bar\"") &&
139
+ hash[:params][:q].split(" AND ").include?("baz:\"quix\"") &&
140
+ hash[:params][:q].split(" AND ").include?("baz:\"quack\"")
141
+ }.and_return('response'=>{'docs'=>mock_docs})
142
+ yielded = mock("yielded method")
143
+ yielded.should_receive(:run).with(mock_docs)
144
+ SpecModel::Basic.find_in_batches({:foo=>'bar', :baz=>['quix','quack']}, {:batch_size=>1002, :fl=>'id'}){|group| yielded.run group }.should
145
+ end
146
+ end
147
+ end
148
+
149
+ describe '#count' do
150
+
151
+ it "should return a count" do
152
+ mock_result = {'response'=>{'numFound'=>7}}
153
+ ActiveFedora::SolrService.should_receive(:query).with(@model_query, :rows=>0, :raw=>true).and_return(mock_result)
154
+ SpecModel::Basic.count.should == 7
155
+ end
156
+ it "should allow conditions" do
157
+ mock_result = {'response'=>{'numFound'=>7}}
158
+ ActiveFedora::SolrService.should_receive(:query).with("#{@model_query} AND foo:bar", :rows=>0, :raw=>true).and_return(mock_result)
159
+ SpecModel::Basic.count(:conditions=>'foo:bar').should == 7
160
+ end
161
+
162
+ it "should count without a class specified" do
163
+ mock_result = {'response'=>{'numFound'=>7}}
164
+ ActiveFedora::SolrService.should_receive(:query).with("foo:bar", :rows=>0, :raw=>true).and_return(mock_result)
165
+ ActiveFedora::Base.count(:conditions=>'foo:bar').should == 7
166
+ end
167
+ end
168
+
169
+ describe '#find_with_conditions' do
170
+ it "should make a query to solr and return the results" do
171
+ mock_result = stub('Result')
172
+ ActiveFedora::SolrService.should_receive(:query).with() { |args|
173
+ q = args.first if args.is_a? Array
174
+ q ||= args
175
+ q.split(" AND ").include?(@model_query) &&
176
+ q.split(" AND ").include?("foo:\"bar\"") &&
177
+ q.split(" AND ").include?("baz:\"quix\"") &&
178
+ q.split(" AND ").include?("baz:\"quack\"")
179
+ }.and_return(mock_result)
180
+ SpecModel::Basic.find_with_conditions(:foo=>'bar', :baz=>['quix','quack']).should == mock_result
181
+ end
182
+
183
+ it "should escape quotes" do
184
+ mock_result = stub('Result')
185
+ ActiveFedora::SolrService.should_receive(:query).with() { |args|
186
+ q = args.first if args.is_a? Array
187
+ q ||= args
188
+ q.split(" AND ").include?(@model_query) &&
189
+ q.split(" AND ").include?(@model_query) &&
190
+ q.split(" AND ").include?('foo:"9\\" Nails"') &&
191
+ q.split(" AND ").include?('baz:"7\\" version"') &&
192
+ q.split(" AND ").include?('baz:"quack"')
193
+ }.and_return(mock_result)
194
+ SpecModel::Basic.find_with_conditions(:foo=>'9" Nails', :baz=>['7" version','quack']).should == mock_result
195
+ end
196
+
197
+ it "shouldn't use the class if it's called on AF:Base " do
198
+ mock_result = stub('Result')
199
+ ActiveFedora::SolrService.should_receive(:query).with('baz:"quack"', {:sort => ['system_create_dt asc']}).and_return(mock_result)
200
+ ActiveFedora::Base.find_with_conditions(:baz=>'quack').should == mock_result
201
+ end
202
+ it "should use the query string if it's provided" do
203
+ mock_result = stub('Result')
204
+ ActiveFedora::SolrService.should_receive(:query).with('chunky:monkey', {:sort => ['system_create_dt asc']}).and_return(mock_result)
205
+ ActiveFedora::Base.find_with_conditions('chunky:monkey').should == mock_result
206
+ end
207
+ end
208
+ end