Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 112 lines (82 sloc) 4.014 kB
ff00015 @karmi [#366] Cleaned up the Update API support and added an integration tes…
authored
1 require 'test_helper'
2
3 module Tire
4
5 class IndexUpdateDocumentIntegrationTest < Test::Unit::TestCase
6 include Test::Integration
7
8 context "Updating a document" do
9
10 setup do
11 Tire.index 'articles-with-tags' do
12 delete
13 create
14
15 store :type => 'article', :id => 1, :title => 'One', :tags => ['foo'], :views => 0
16 store :type => 'article', :id => 2, :title => 'Two', :tags => ['foo', 'bar'], :views => 10
17 store :type => 'article', :id => 3, :title => 'Three', :tags => ['foobar']
18
19 refresh
20 end
21 end
22
23 teardown { Tire.index('articles-with-tags').delete }
24
25 should "increment a counter" do
26 Tire.index('articles-with-tags') { update( 'article', '1', :script => "ctx._source.views += 1" ) and refresh }
27
28 document = Tire.search('articles-with-tags') { query { string 'title:one' } }.results.first
29 assert_equal 1, document.views, document.inspect
30
31 Tire.index('articles-with-tags') { update( 'article', '2', :script => "ctx._source.views += 1" ) and refresh }
32
33 document = Tire.search('articles-with-tags') { query { string 'title:two' } }.results.first
34 assert_equal 11, document.views, document.inspect
35 end
36
37 should "add a tag to document" do
38 Tire.index('articles-with-tags') do
39 update 'article', '1', :script => "ctx._source.tags += tag",
40 :params => { :tag => 'new' }
41 refresh
42 end
43
44 document = Tire.search('articles-with-tags') { query { string 'title:one' } }.results.first
45 assert_equal ['foo', 'new'], document.tags, document.inspect
46 end
47
48 should "remove a tag from document" do
49 Tire.index('articles-with-tags') do
50 update 'article', '1', :script => "ctx._source.tags = tags",
51 :params => { :tags => [] }
52 refresh
53 end
54
55 document = Tire.index('articles-with-tags').retrieve 'article', '1'
56 assert_equal [], document.tags, document.inspect
57 end
58
59 should "remove the entire document if specific condition is met" do
60 Tire.index('articles-with-tags') do
61 # Remove document when it contains tag 'foobar'
62 update 'article', '3', :script => "ctx._source.tags.contains(tag) ? ctx.op = 'delete' : 'none'",
63 :params => { :tag => 'foobar' }
64 refresh
65 end
66
67 assert_nil Tire.index('articles-with-tags').retrieve 'article', '3'
68 end
69
70 should "pass the operation parameters to the API" do
71 Tire.index('articles-with-tags').update 'article', '2', { :script => "ctx._source.tags += tag",
72 :params => { :tag => 'new' }
73 },
74 {
75 :refresh => true
76 }
77
78 document = Tire.search('articles-with-tags') { query { string 'title:two' } }.results.first
79 assert_equal 3, document.tags.size, document.inspect
80 end
81
82 should "access variables from the outer scope" do
83 $t = self
84
85 class Updater
86 @tags = ['foo', 'bar', 'baz']
87
88 def self.perform_update!
89 $t.assert_not_nil @tags
90
91 Tire.index('articles-with-tags') do |index|
92 $t.assert_not_nil @tags
93
94 index.update 'article', '3', :script => "ctx._source.tags = tags",
95 :params => { :tags => @tags }
96 index.refresh
97 end
98 end
99 end
100
101 Updater.perform_update!
102
103 document = Tire.search('articles-with-tags') { query { string 'title:three' } }.results.first
104 assert_equal 3, document.tags.size, document.inspect
105 end
106
107 end
108
109 end
110
111 end
Something went wrong with that request. Please try again.