tanker 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,183 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
2
+ require File.expand_path(File.join(File.dirname(__FILE__), 'integration_spec_conf'))
3
+
4
+ require 'active_record'
5
+ require 'sqlite3'
6
+ require 'logger'
7
+
8
+ FileUtils.rm( 'data.sqlite3' ) rescue nil
9
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
10
+ ActiveRecord::Base.establish_connection(
11
+ 'adapter' => 'sqlite3',
12
+ 'database' => 'data.sqlite3',
13
+ 'pool' => 5,
14
+ 'timeout' => 5000
15
+ )
16
+
17
+ ActiveRecord::Schema.define do
18
+ create_table :products do |t|
19
+ t.string :name
20
+ t.string :href
21
+ t.string :tags
22
+ end
23
+ end
24
+
25
+ class Product < ActiveRecord::Base
26
+ include Tanker
27
+
28
+ tankit 'tanker_integration_tests' do
29
+ indexes :name
30
+ indexes :href
31
+ end
32
+ end
33
+
34
+ #class Product
35
+ # include Tanker
36
+
37
+ # tankit 'tanker_integration_tests' do
38
+ # indexes :name
39
+ # indexes :href
40
+ # indexes :tags
41
+ # end
42
+
43
+ # attr_accessor :name, :href, :tags
44
+
45
+ # def initialize(options = {})
46
+ # @name = options[:name]
47
+ # @href = options[:href]
48
+ # @tags = options[:tags]
49
+ # end
50
+
51
+ # def id
52
+ # @id ||= self.class.throwaway_id
53
+ # end
54
+ #
55
+ # def id=(val)
56
+ # @id = val
57
+ # end
58
+ #
59
+ # class << self
60
+ # def create(options)
61
+ # self.new(options)
62
+ # end
63
+ #
64
+ # def throwaway_id
65
+ # @throwaway_id = (@throwaway_id ? @throwaway_id + 1 : 0)
66
+ # end
67
+ #
68
+ # def all
69
+ # ObjectSpace.each_object(self)
70
+ # end
71
+ #
72
+ # def find(ids)
73
+ # all.select{|instance| ids.include?(instance.id.to_s) }
74
+ # end
75
+ # end
76
+ #end
77
+
78
+ describe 'Tanker integration tests with IndexTank' do
79
+
80
+ before(:all) do
81
+ Tanker::Utilities.clear_index('tanker_integration_tests')
82
+
83
+ @catapult = Product.create(:name => 'Acme catapult')
84
+ @tnt = Product.create(:name => 'Acme TNT')
85
+ @cat = Product.create(:name => 'Acme cat')
86
+
87
+ Product.tanker_reindex
88
+ end
89
+
90
+ context 'An imaginary store' do
91
+ describe 'basic searching' do
92
+ it 'should find all Acme products' do
93
+ @results = Product.search_tank('Acme')
94
+ (@results - [@catapult, @tnt, @cat]).should be_empty
95
+ @results[0].id.should_not be_nil
96
+ end
97
+
98
+ it 'should find all catapults' do
99
+ @results = Product.search_tank('catapult')
100
+ (@results - [@catapult]).should be_empty
101
+ end
102
+
103
+ it 'should find all things cat' do
104
+ @results = Product.search_tank('cat')
105
+ (@results - [@catapult, @cat]).should be_empty
106
+ end
107
+ end
108
+
109
+ describe 'filtering dogs' do
110
+
111
+ before(:all) do
112
+ @doggie_1 = Product.create(:name => 'doggie 1', :tags => ['puppuy', 'pug'] )
113
+ @doggie_2 = Product.create(:name => 'doggie 2', :tags => ['pug'] )
114
+ @doggie_3 = Product.create(:name => 'doggie 3', :tags => ['puppuy', 'yoirkie'] )
115
+ Product.tanker_reindex
116
+ end
117
+
118
+ after(:all) do
119
+ @doggie_1.delete_tank_indexes
120
+ @doggie_2.delete_tank_indexes
121
+ @doggie_3.delete_tank_indexes
122
+ end
123
+
124
+ it 'should filter by puppy tags' do
125
+ @results = Product.search_tank('doggie', :conditions => {:tags => 'puppy'})
126
+ (@results - [@doggie_1, @doggie_3]).should be_empty
127
+ end
128
+
129
+ it 'should not search for doggie_3' do
130
+ @results = Product.search_tank('doggie', :conditions => {:tags => 'puppy', '-name' => 'doggie_3'})
131
+ (@results - [@doggie_1]).should be_empty
132
+
133
+ @results = Product.search_tank('doggie', :conditions => {:tags => 'puppy', 'NOT name' => 'doggie_3'})
134
+ (@results - [@doggie_1]).should be_empty
135
+
136
+ @results = Product.search_tank('doggie NOT doggie_3', :conditions => {:tags => 'puppy'} )
137
+ (@results - [@doggie_1]).should be_empty
138
+ end
139
+ end
140
+
141
+ describe 'snippets and fetching data' do
142
+ before(:all) do
143
+ @prod_1 = Product.create(:name => 'something small')
144
+ @very_long_sting = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
145
+ @prod_2 = Product.create(:name => @very_long_sting, :href => 'http://google.com' )
146
+ @prod_3 = Product.create(:name => 'product with tags', :tags => ['oneword', 'two words'])
147
+ Product.tanker_reindex
148
+ end
149
+
150
+ after(:all) do
151
+ @prod_1.delete_tank_indexes
152
+ @prod_2.delete_tank_indexes
153
+ @prod_3.delete_tank_indexes
154
+ end
155
+
156
+ it 'should fetch attribute requested from Index Tank and create an intstance of the Model without calling the database' do
157
+ @results = Product.search_tank('something', :fetch => [:name])
158
+ @results.count.should == 1
159
+
160
+ @new_prod_instance = @results[0]
161
+ @new_prod_instance.name.should == 'something small'
162
+ end
163
+
164
+ it 'should get a snippet for an attribute requested from Index Tank and create an intstance of the Model without calling the database and with a _snippet attribute reader' do
165
+ @results = Product.search_tank('product', :snippets => [:name])
166
+ @results.count.should == 1
167
+
168
+ @new_prod_instance = @results[0]
169
+ @new_prod_instance.name_snippet.should =~ /<b>product<\/b>/
170
+ end
171
+
172
+ it 'should create a new instance of a model and fetch attributes that where requested and get snippets for the attributes required as snippets' do
173
+ @results = Product.search_tank('quis exercitation', :snippets => [:name], :fetch => [:href])
174
+ @results.count.should == 1
175
+
176
+ @new_prod_instance = @results[0]
177
+ @new_prod_instance.name_snippet.should =~ /<b>quis<\/b>/
178
+ @new_prod_instance.href.should == 'http://google.com'
179
+ end
180
+ end
181
+ end
182
+ end
183
+
@@ -0,0 +1 @@
1
+ Tanker.configuration = {:url => 'http://xxxxx@xxxxx.api.indextank.com'}
data/spec/spec_helper.rb CHANGED
@@ -12,10 +12,6 @@ end
12
12
 
13
13
  Tanker.configuration = {:url => 'http://api.indextank.com'}
14
14
 
15
- class Dummy
16
-
17
- end
18
-
19
15
  $frozen_moment = Time.now
20
16
 
21
17
  class Person
@@ -45,7 +41,11 @@ class Person
45
41
  end
46
42
 
47
43
  def id
48
- 1
44
+ @id ||= 1
45
+ end
46
+
47
+ def id=(val)
48
+ @id = val
49
49
  end
50
50
  end
51
51
 
@@ -77,4 +77,14 @@ class Cat
77
77
 
78
78
  end
79
79
 
80
+ module Foo
81
+ class Bar
82
+ include Tanker
80
83
 
84
+ tankit 'dummy index' do
85
+ indexes :bar do
86
+ "bar"
87
+ end
88
+ end
89
+ end
90
+ end