Skip to content

Commit

Permalink
Added type restriction for Tire::Model::Persistence all and `firs…
Browse files Browse the repository at this point in the history
…t` finders

Closes karmi#489
  • Loading branch information
fonzo14 authored and karmi committed Oct 23, 2012
1 parent 6f8f760 commit 4e63b3d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/tire/model/persistence/finders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ def find *args

def all
# TODO: Options like `sort`; Possibly `filters`
s = Tire::Search::Search.new(index.name, :wrapper => self).query { all }
s = Tire::Search::Search.new(index.name, :type => document_type, :wrapper => self).query { all }
s.version(true).results
end

def first
# TODO: Options like `sort`; Possibly `filters`
s = Tire::Search::Search.new(index.name, :wrapper => self).query { all }.size(1)
s = Tire::Search::Search.new(index.name, :type => document_type, :wrapper => self).query { all }.size(1)
s.version(true).results.first
end

Expand Down
27 changes: 27 additions & 0 deletions test/integration/persistent_model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,33 @@ def teardown

end

context "with multiple types within single index" do

setup do
# Create documents of two types within single index
1.upto(3) { |number| PersistentArticleInIndex.create :title => "TestInIndex#{number}", :tags => ['in_index'] }
1.upto(3) { |number| PersistentArticle.create :title => "Test#{number}", :tags => [] }
PersistentArticle.index.refresh
end

should "returns all documents with proper type" do
results = PersistentArticle.all

assert_equal 3, results.size
assert results.all? { |r| r.tags == [] }, "Incorrect results:" + results.to_a.inspect

results = PersistentArticleInIndex.all

assert_equal 3, results.size
assert_equal ['in_index'], results.first.tags
end

should "returns first document with proper type" do
assert_equal [], PersistentArticle.first.tags
assert_equal ['in_index'], PersistentArticleInIndex.first.tags
end
end

end

end
Expand Down
16 changes: 16 additions & 0 deletions test/models/persistent_article_in_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Example class with ElasticSearch persistence in index `persistent_articles`
#
# The `index` is `persistent_articles`
#

class PersistentArticleInIndex

include Tire::Model::Persistence

property :title
property :published_on
property :tags

index_name "persistent_articles"

end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
require File.dirname(__FILE__) + '/models/active_record_models'
require File.dirname(__FILE__) + '/models/article'
require File.dirname(__FILE__) + '/models/persistent_article'
require File.dirname(__FILE__) + '/models/persistent_article_in_index'
require File.dirname(__FILE__) + '/models/persistent_article_in_namespace'
require File.dirname(__FILE__) + '/models/persistent_article_with_casting'
require File.dirname(__FILE__) + '/models/persistent_article_with_defaults'
Expand Down
17 changes: 13 additions & 4 deletions test/unit/model_persistence_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,26 @@ class << a
assert_equal ids.size, documents.count
end

should "find all documents" do
Configuration.client.stubs(:get).returns(mock_response(@find_all.to_json))
should "find all documents with correct type" do
Configuration.client.expects(:get).
with do |url,payload|
assert_equal "#{Configuration.url}/persistent_articles/persistent_article/_search?wrapper=PersistentArticle", url
end.
times(3).
returns(mock_response(@find_all.to_json))
documents = PersistentArticle.all

assert_equal 3, documents.count
assert_equal 'First', documents.first.attributes['title']
assert_equal PersistentArticle.find(:all).map { |e| e.id }, PersistentArticle.all.map { |e| e.id }
end

should "find first document" do
Configuration.client.expects(:get).returns(mock_response(@find_first.to_json))
should "find first document with correct type" do
Configuration.client.expects(:get).
with do |url,payload|
assert_equal "#{Configuration.url}/persistent_articles/persistent_article/_search?size=1&wrapper=PersistentArticle", url
end.
returns(mock_response(@find_first.to_json))
document = PersistentArticle.first

assert_equal 'First', document.attributes['title']
Expand Down

0 comments on commit 4e63b3d

Please sign in to comment.