waistband 0.4.2 → 0.7.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.
- data/lib/waistband.rb +0 -1
- data/lib/waistband/index.rb +2 -6
- data/lib/waistband/query.rb +12 -169
- data/lib/waistband/version.rb +1 -1
- data/spec/lib/index_spec.rb +1 -5
- data/spec/lib/query_spec.rb +130 -342
- metadata +2 -8
- data/lib/waistband/free_query.rb +0 -34
- data/lib/waistband/model.rb +0 -215
- data/spec/lib/free_query_spec.rb +0 -161
- data/spec/lib/model_spec.rb +0 -217
data/spec/lib/model_spec.rb
DELETED
@@ -1,217 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Waistband::Model do
|
4
|
-
|
5
|
-
class Log < Waistband::Model
|
6
|
-
|
7
|
-
with_index :search
|
8
|
-
columns :log, :user_id, :content
|
9
|
-
stringify :content
|
10
|
-
defaults user_id: 999
|
11
|
-
|
12
|
-
validates :content
|
13
|
-
|
14
|
-
def before_save
|
15
|
-
self.log = true
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
19
|
-
|
20
|
-
describe Log do
|
21
|
-
|
22
|
-
let(:log) { Log.new(user_id: 1, content: "oh yeah!") }
|
23
|
-
|
24
|
-
describe '.first' do
|
25
|
-
|
26
|
-
before { IndexHelper.prepare! }
|
27
|
-
|
28
|
-
it "returns first element by created_at" do
|
29
|
-
logs = 10.times.to_a.map do |i|
|
30
|
-
Timecop.travel((i + 1).minutes.from_now) do
|
31
|
-
Log.create(user_id: 1, content: "something we wanna log!")
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
Log.index.refresh
|
36
|
-
Log.first.id.should eql logs.first.id
|
37
|
-
end
|
38
|
-
|
39
|
-
it "returns a Log object" do
|
40
|
-
Log.create(user_id: 1, content: "something we wanna log!")
|
41
|
-
Log.index.refresh
|
42
|
-
Log.first.should be_a Log
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|
46
|
-
|
47
|
-
describe '.last' do
|
48
|
-
|
49
|
-
before { IndexHelper.prepare! }
|
50
|
-
|
51
|
-
it "returns last element by created_at" do
|
52
|
-
logs = 10.times.to_a.map do |i|
|
53
|
-
Timecop.travel((i + 1).minutes.from_now) do
|
54
|
-
Log.create(user_id: 1, content: "something we wanna log!")
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
Log.index.refresh
|
59
|
-
Log.last.id.should eql logs.last.id
|
60
|
-
end
|
61
|
-
|
62
|
-
it "returns a Log object" do
|
63
|
-
Log.create(user_id: 1, content: "something we wanna log!")
|
64
|
-
Log.index.refresh
|
65
|
-
Log.last.should be_a Log
|
66
|
-
end
|
67
|
-
|
68
|
-
end
|
69
|
-
|
70
|
-
describe '.find' do
|
71
|
-
|
72
|
-
it "loads the log from elastic search" do
|
73
|
-
log.save
|
74
|
-
stored_log = Log.find(log.id)
|
75
|
-
|
76
|
-
stored_log.id.should eql log.id
|
77
|
-
stored_log.user_id.should eql log.user_id
|
78
|
-
stored_log.content.should eql log.content
|
79
|
-
end
|
80
|
-
|
81
|
-
end
|
82
|
-
|
83
|
-
describe '.create' do
|
84
|
-
|
85
|
-
it "saves the log to elastic search" do
|
86
|
-
stored_log = Log.create(log.attributes)
|
87
|
-
|
88
|
-
stored_log.id.should be_present
|
89
|
-
stored_log.id.length.should eql 40
|
90
|
-
|
91
|
-
stored_log.user_id.should eql log.user_id
|
92
|
-
stored_log.content.should eql log.content
|
93
|
-
end
|
94
|
-
|
95
|
-
end
|
96
|
-
|
97
|
-
it "blows up when initializing with invalid column name" do
|
98
|
-
expect { Log.new(bla: 'test') }.to raise_error(ArgumentError, "bla is not a valid column name!")
|
99
|
-
end
|
100
|
-
|
101
|
-
it "permits initializing with `nil` as attributes" do
|
102
|
-
expect { Log.new(nil) }.to_not raise_error
|
103
|
-
end
|
104
|
-
|
105
|
-
it "initializes correctly with valid column names" do
|
106
|
-
log.user_id.should eql 1
|
107
|
-
log.content.should eql "oh yeah!"
|
108
|
-
end
|
109
|
-
|
110
|
-
it "stringifies denoted fields" do
|
111
|
-
log = Log.new(user_id: 1, content: {ok: true})
|
112
|
-
log.save
|
113
|
-
|
114
|
-
Log.index.read(log.id)['content'].should eql "{:ok=>true}"
|
115
|
-
log.content.should eql "{:ok=>true}"
|
116
|
-
|
117
|
-
log = Log.create(user_id: 1, content: {ok: true})
|
118
|
-
|
119
|
-
Log.index.read(log.id)['content'].should eql "{:ok=>true}"
|
120
|
-
log.content.should eql "{:ok=>true}"
|
121
|
-
end
|
122
|
-
|
123
|
-
it "automatically creates relationships" do
|
124
|
-
User ||= double
|
125
|
-
User.should_receive(:find).with(1).once
|
126
|
-
log.user
|
127
|
-
end
|
128
|
-
|
129
|
-
it "permits directly setting the relationship" do
|
130
|
-
User ||= double
|
131
|
-
admin = double(id: 3)
|
132
|
-
|
133
|
-
log.user = admin
|
134
|
-
|
135
|
-
log.user_id.should eql 3
|
136
|
-
|
137
|
-
User.should_receive(:find).with(3).once
|
138
|
-
log.user
|
139
|
-
end
|
140
|
-
|
141
|
-
describe '#save' do
|
142
|
-
|
143
|
-
it "auto generates id when saving" do
|
144
|
-
log.save
|
145
|
-
log.id.should be_present
|
146
|
-
log.id.length.should eql 40
|
147
|
-
end
|
148
|
-
|
149
|
-
it "sets timestamps" do
|
150
|
-
log.save
|
151
|
-
log.created_at.should be_a Fixnum
|
152
|
-
log.updated_at.should be_a Fixnum
|
153
|
-
end
|
154
|
-
|
155
|
-
it "doesn't modify created_at on updates" do
|
156
|
-
log.save
|
157
|
-
created_at = log.created_at
|
158
|
-
updated_at = log.updated_at
|
159
|
-
|
160
|
-
Timecop.travel(5.seconds.from_now) do
|
161
|
-
log.save
|
162
|
-
updated_at.should_not eql log.updated_at
|
163
|
-
created_at.should eql log.created_at
|
164
|
-
end
|
165
|
-
end
|
166
|
-
|
167
|
-
it "doesn't mark id and timestamps if save fails" do
|
168
|
-
log.should_receive(:store!).once.and_return('{"ok":false}')
|
169
|
-
log.save
|
170
|
-
|
171
|
-
log.id.should be_nil
|
172
|
-
log.created_at.should be_nil
|
173
|
-
log.updated_at.should be_nil
|
174
|
-
end
|
175
|
-
|
176
|
-
it "if the id was previously set and the save fails, the id sticks" do
|
177
|
-
log.id = 123
|
178
|
-
log.should_receive(:store!).once.and_return('{"ok":false}')
|
179
|
-
log.save
|
180
|
-
|
181
|
-
log.id.should eql 123
|
182
|
-
end
|
183
|
-
|
184
|
-
it "persists to elastic search" do
|
185
|
-
log.save
|
186
|
-
|
187
|
-
stored = Log.index.read(log.id)
|
188
|
-
|
189
|
-
stored['id'].should eql log.id
|
190
|
-
stored['user_id'].should eql log.user_id
|
191
|
-
stored['content'].should eql log.content
|
192
|
-
end
|
193
|
-
|
194
|
-
it "sets default values" do
|
195
|
-
log = Log.new(content: "some new stuff")
|
196
|
-
log.save.should be_true
|
197
|
-
|
198
|
-
log.user_id.should eql 999
|
199
|
-
end
|
200
|
-
|
201
|
-
it "validates fields" do
|
202
|
-
log = Log.new
|
203
|
-
log.should_not be_valid
|
204
|
-
|
205
|
-
log.errors.should eql ["content cannot be nil"]
|
206
|
-
end
|
207
|
-
|
208
|
-
it "sets the model type" do
|
209
|
-
log.save
|
210
|
-
Log.index.read(log.id)['model_type'].should eql 'log'
|
211
|
-
end
|
212
|
-
|
213
|
-
end
|
214
|
-
|
215
|
-
end
|
216
|
-
|
217
|
-
end
|