riakrest 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,51 +3,35 @@ require File.dirname(__FILE__) + '/../spec_helper.rb'
3
3
  class FooBarBaz # :nodoc:
4
4
  include JiakData
5
5
 
6
- allowed :foo, :bar, :baz
7
- required :foo
8
- readable :foo, :bar
9
- writable :foo, :bar
10
-
11
- def initialize(hsh)
12
- hsh.each {|key,val| send("#{key}=",val)}
13
- end
14
-
15
- def self.jiak_create(jiak)
16
- new(jiak)
17
- end
18
-
19
- def for_jiak
20
- { :foo => @foo,
21
- :bar => @bar
22
- }.reject {|k,v| v.nil?}
23
- end
24
-
25
- def eql?(other)
26
- other.is_a?(FooBarBaz) && other.foo.eql?(@foo) && other.bar.eql?(@bar)
27
- end
6
+ jattr_accessor :foo, :bar
7
+ require :foo
8
+ allow :baz
28
9
  end
29
10
 
30
11
  describe "JiakClient" do
31
12
  before do
32
13
  @base_uri = 'http://127.0.0.1:8002/jiak/'
33
- @client = JiakClient.new @base_uri
14
+ @client = JiakClient.new(@base_uri)
15
+ @params = {:reads => 1, :writes => 2, :durable_writes => 3, :waits => 4}
16
+ @opts = @params.merge({:proxy => 'proxy_uri'})
34
17
  end
35
18
 
36
19
  it "should respond to" do
37
- @client.should respond_to(:set_schema, :schema, :keys)
38
- @client.should respond_to(:uri)
20
+ @client.should respond_to(:set_schema,:schema,:keys)
21
+ @client.should respond_to(:proxy,:proxy=)
22
+ @client.should respond_to(:params,:set_params,:params=)
23
+ @client.should respond_to(:get,:store,:delete,:walk)
39
24
  @client.should respond_to(:==,:eql?)
40
- @client.should respond_to(:get, :store, :delete, :walk)
41
25
  end
42
26
 
43
27
  it "should default to base URI" do
44
- @client.uri.should match @base_uri
28
+ @client.server.should match @base_uri
45
29
  end
46
30
 
47
31
  it "should allow specified base URI" do
48
32
  base_uri = 'http://localhost:1234/tmp/'
49
33
  client = JiakClient.new base_uri
50
- client.uri.should match base_uri
34
+ client.server.should match base_uri
51
35
  end
52
36
 
53
37
  it "should equal another JiakClient with the same URI" do
@@ -56,6 +40,49 @@ describe "JiakClient" do
56
40
  client.should == @client
57
41
  end
58
42
 
43
+ it "should initialize proxy and params" do
44
+ client = JiakClient.new(@base_uri,:proxy => 'proxy_uri',:reads => 1)
45
+ client.proxy.should eql 'proxy_uri'
46
+ client.params[:reads].should == 1
47
+
48
+ client = JiakClient.new(@base_uri,@opts)
49
+ client.proxy.should eql 'proxy_uri'
50
+ client.params.should have_exactly(4).items
51
+ @params.each {|k,v| client.params[k].should == @params[k]}
52
+
53
+ @opts.delete(:proxy)
54
+ @opts.delete(:writes)
55
+ client = JiakClient.new(@base_uri,@opts)
56
+ client.params.should have_exactly(3).items
57
+ client.proxy.should be nil
58
+ client.params[:writes].should be nil
59
+
60
+ @opts[:junk] = 'junk'
61
+ bad_opts = lambda {JiakClient.new(@base_uri,@opts)}
62
+ bad_opts.should raise_error(JiakClientException,/option/)
63
+ end
64
+
65
+ it "should update proxy and params" do
66
+ @client.proxy = 'another_proxy'
67
+ @client.proxy.should eql 'another_proxy'
68
+
69
+ @client.params = @params
70
+ @params.each {|k,v| @client.params[k].should == @params[k]}
71
+
72
+ @client.set_params(:writes => 1, :waits => nil)
73
+ params = @client.params
74
+ params[:reads].should eql 1
75
+ params[:writes].should eql 1
76
+ params[:durable_writes].should eql 3
77
+ params[:waits].should eql nil
78
+
79
+ bad_opts = lambda {@client.set_params(:junk => 'junk')}
80
+ bad_opts.should raise_error(JiakClientException,/option/)
81
+
82
+ @params[:junk] = 'junk'
83
+ bad_opts = lambda {@client.params = @params}
84
+ bad_opts.should raise_error(JiakClientException,/option/)
85
+ end
59
86
  end
60
87
 
61
88
  describe "JiakClient URI handling" do
@@ -94,14 +121,14 @@ describe "JiakClient processing" do
94
121
  end
95
122
 
96
123
  it "should update an existing bucket schema" do
97
- FooBarBazBuz = JiakDataHash.create(:foo,:bar,:baz,:buz)
124
+ FooBarBazBuz = JiakDataFields.create(:foo,:bar,:baz,:buz)
98
125
  @client.set_schema(JiakBucket.new(@bucket_name,FooBarBazBuz))
99
126
 
100
127
  resp_schema = @client.schema(@bucket)
101
- resp_schema.allowed_fields.should include 'buz'
128
+ resp_schema.allowed_fields.should include :buz
102
129
  resp_schema.required_fields.should be_empty
103
- resp_schema.read_mask.should include 'baz'
104
- resp_schema.write_mask.should include 'baz'
130
+ resp_schema.read_mask.should include :baz
131
+ resp_schema.write_mask.should include :baz
105
132
 
106
133
  end
107
134
 
@@ -241,16 +268,18 @@ describe "JiakClient processing" do
241
268
 
242
269
  end
243
270
 
244
- Parent = JiakDataHash.create(:name)
245
- Child = JiakDataHash.create(:name)
271
+ class PersonData
272
+ include JiakData
273
+ jattr_accessor :name
274
+ end
246
275
 
247
276
  describe "JiakClient links" do
248
277
  before do
249
278
  @base_uri = 'http://127.0.0.1:8002/jiak/'
250
279
  @client = JiakClient.new @base_uri
251
280
 
252
- @p_bucket = JiakBucket.new('parent',Parent)
253
- @c_bucket = JiakBucket.new('child',Child)
281
+ @p_bucket = JiakBucket.new('parent',PersonData)
282
+ @c_bucket = JiakBucket.new('child',PersonData)
254
283
  @client.set_schema(@p_bucket)
255
284
  @client.set_schema(@c_bucket)
256
285
  end
@@ -276,7 +305,7 @@ describe "JiakClient links" do
276
305
  # - create p and c (when necessary)
277
306
  # - add links for p -> c and c -> p
278
307
  parent_children.each do |p_name,c_names|
279
- p_data = Parent.new(:name => p_name)
308
+ p_data = PersonData.new(:name => p_name)
280
309
  p_obj = JiakObject.new(:bucket => @p_bucket,
281
310
  :key => p_name,
282
311
  :data => p_data)
@@ -286,7 +315,7 @@ describe "JiakClient links" do
286
315
  begin
287
316
  child = @client.get(@c_bucket,c_name)
288
317
  rescue JiakResourceNotFound
289
- c_data = Child.new(:name => c_name)
318
+ c_data = PersonData.new(:name => c_name)
290
319
  c_obj = JiakObject.new(:bucket => @c_bucket,
291
320
  :key => c_name,
292
321
  :data => c_data)
@@ -330,7 +359,7 @@ describe "JiakClient links" do
330
359
  # p's should include links to their c's
331
360
  c_link = QueryLink.new(@c_bucket,'child',nil)
332
361
  parent_children.each do |p_name,children|
333
- @client.walk(@p_bucket,p_name,c_link,Child).each do |c_obj|
362
+ @client.walk(@p_bucket,p_name,c_link,PersonData).each do |c_obj|
334
363
  children.should include c_obj.data.name
335
364
  end
336
365
  end
@@ -338,14 +367,14 @@ describe "JiakClient links" do
338
367
  # c's should include links to their p's
339
368
  p_link = QueryLink.new(@p_bucket,'parent',nil)
340
369
  child_parents.each do |c_name,parents|
341
- @client.walk(@c_bucket,c_name,p_link,Parent).each do |p_obj|
370
+ @client.walk(@c_bucket,c_name,p_link,PersonData).each do |p_obj|
342
371
  parents.should include p_obj.data.name
343
372
  end
344
373
  end
345
374
 
346
375
  # c siblings requires a second step
347
376
  c1s,c2s,c3s,c4s,c5s,c6s,c7s = child_parents.keys.map do |c|
348
- siblings = @client.walk(@c_bucket,c,[p_link,c_link],Child)
377
+ siblings = @client.walk(@c_bucket,c,[p_link,c_link],PersonData)
349
378
  me = @client.get(@c_bucket,c)
350
379
  siblings.reject! {|s| s.eql?(me)}
351
380
  siblings
@@ -12,7 +12,7 @@ describe "JiakLink" do
12
12
  @jiak_link.should respond_to(:bucket,:key,:tag)
13
13
  @jiak_link.should respond_to(:bucket=,:key=,:tag=)
14
14
 
15
- @jiak_link.should respond_to(:for_jiak)
15
+ @jiak_link.should respond_to(:to_jiak)
16
16
 
17
17
  @jiak_link.should respond_to(:eql?,:==)
18
18
  end
@@ -60,7 +60,7 @@ describe "JiakLink" do
60
60
  end
61
61
 
62
62
  it "should convert for Jiak" do
63
- @jiak_link.for_jiak.should eql [@bucket,@key,@tag]
63
+ @jiak_link.to_jiak.should eql [@bucket,@key,@tag]
64
64
  end
65
65
 
66
66
  it "should compare to another JiakLink via eql?" do
@@ -1,20 +1,20 @@
1
1
  require File.dirname(__FILE__) + '/../spec_helper.rb'
2
2
 
3
- DataObject = JiakDataHash.create(:f1,:f2,:f3)
3
+ UserData = JiakDataFields.create :f1, :f2, :f3
4
4
 
5
5
  describe "JiakObject" do
6
6
  before do
7
7
  @data =
8
- DataObject.new(:f1 => 'f1',:f2 => ['a','b'], :f3 => {:f3_1 => 'f3_1'})
8
+ UserData.new(:f1 => 'f1',:f2 => ['a','b'], :f3 => {:f3_1 => 'f3_1'})
9
9
  @bucket_name = 'bucket'
10
- @bucket = JiakBucket.new(@bucket_name,DataObject)
10
+ @bucket = JiakBucket.new(@bucket_name,UserData)
11
11
  @key = 'hey'
12
12
  @l1 = JiakLink.new('l1b','l1k','l1t')
13
13
  @l2 = JiakLink.new('l2b','l2k','l2t')
14
14
  @links = [@l1,@l2]
15
15
 
16
- object = @data.for_jiak
17
- links = @links.map {|link| link.for_jiak}
16
+ object = @data.to_jiak
17
+ links = @links.map {|link| link.to_jiak}
18
18
 
19
19
  core = {
20
20
  :bucket => @bucket_name, :key => @key,
@@ -30,7 +30,7 @@ describe "JiakObject" do
30
30
  end
31
31
 
32
32
  it "should respond to" do
33
- JiakObject.should respond_to(:new,:from_jiak)
33
+ JiakObject.should respond_to(:new,:jiak_create)
34
34
 
35
35
  @object.should respond_to(:bucket,:bucket=)
36
36
  @object.should respond_to(:key,:key=)
@@ -73,7 +73,7 @@ describe "JiakObject" do
73
73
  it "should initialize from JSON" do
74
74
  [@core_json,@full_json].each do |json|
75
75
  jiak = JSON.parse(json)
76
- jobj = JiakObject.from_jiak(jiak,@bucket.data_class)
76
+ jobj = JiakObject.jiak_create(jiak,@bucket.data_class)
77
77
  jobj.bucket.should eql @object.bucket
78
78
  jobj.key.should eql @object.key
79
79
  jobj.data.f1.should eql @data.f1
@@ -88,7 +88,7 @@ describe "JiakObject" do
88
88
  end
89
89
 
90
90
  jiak = JSON.parse(@full_json)
91
- jobj = JiakObject.from_jiak(jiak,@bucket.data_class)
91
+ jobj = JiakObject.jiak_create(jiak,@bucket.data_class)
92
92
  jobj.riak[:vclock].should eql 'vclock'
93
93
  jobj.riak[:vtag].should eql 'vtag'
94
94
  jobj.riak[:lastmod].should eql 'last mod'
@@ -124,7 +124,7 @@ describe "JiakObject" do
124
124
  end
125
125
 
126
126
  it "should convert to JSON" do
127
- json = @object.to_jiak
127
+ json = @object.to_jiak.to_json
128
128
  json.should eql @core_json
129
129
 
130
130
  parsed = JSON.parse(json)
@@ -134,13 +134,13 @@ describe "JiakObject" do
134
134
  parsed['object']['f2'].should eql @data.f2
135
135
  parsed['object']['f3']['f3_1'].should eql @data.f3[:f3_1]
136
136
  @links.each_with_index do |link,ndx|
137
- parsed['links'][ndx].should eql link.for_jiak
137
+ parsed['links'][ndx].should eql link.to_jiak
138
138
  end
139
139
  end
140
140
 
141
141
  it "should allow attribute updates" do
142
142
  bucket_name = 'new bucket'
143
- bucket = JiakBucket.new(bucket_name,DataObject)
143
+ bucket = JiakBucket.new(bucket_name,UserData)
144
144
  @object.bucket = bucket
145
145
  @object.bucket.should eql bucket
146
146
 
@@ -148,7 +148,7 @@ describe "JiakObject" do
148
148
  @object.key = key
149
149
  @object.key.should eql key
150
150
 
151
- object = DataObject.new(:f1 => 'new f1',:f2 => [], :f3 => 6)
151
+ object = UserData.new(:f1 => 'new f1',:f2 => [], :f3 => 6)
152
152
  @object.data.should_not eql object
153
153
  @object.data = object
154
154
  @object.data.should eql object
@@ -16,12 +16,13 @@ describe "JiakSchema" do
16
16
  end
17
17
 
18
18
  it "should respond to" do
19
- JiakSchema.should respond_to(:from_jiak)
19
+ JiakSchema.should respond_to(:jiak_create)
20
20
 
21
21
  @jiak_schema.should respond_to(:allowed_fields,:allowed_fields=)
22
22
  @jiak_schema.should respond_to(:required_fields,:required_fields=)
23
23
  @jiak_schema.should respond_to(:read_mask,:read_mask=)
24
24
  @jiak_schema.should respond_to(:write_mask,:write_mask=)
25
+ @jiak_schema.should respond_to(:allow,:require,:readable,:writable)
25
26
 
26
27
  @jiak_schema.should respond_to(:to_jiak)
27
28
  @jiak_schema.should respond_to(:==,:eql?)
@@ -83,8 +84,17 @@ describe "JiakSchema" do
83
84
  jiak_schema.write_mask.should eql @write_mask
84
85
  end
85
86
 
87
+ it "should create an empty schema" do
88
+ [JiakSchema.new, JiakSchema.new {}].each do |schema|
89
+ schema.allowed_fields.should be_empty
90
+ schema.required_fields.should be_empty
91
+ schema.read_mask.should be_empty
92
+ schema.write_mask.should be_empty
93
+ end
94
+ end
95
+
86
96
  it "should create from json" do
87
- schema = JiakSchema.from_jiak(JSON.parse(@hash.to_json))
97
+ schema = JiakSchema.jiak_create(JSON.parse(@hash.to_json))
88
98
  @allowed_fields.same_fields?(schema.allowed_fields).should be true
89
99
  @required_fields.same_fields?(schema.required_fields).should be true
90
100
  @write_mask.same_fields?(schema.write_mask).should be true
@@ -121,27 +131,96 @@ describe "JiakSchema" do
121
131
  bad = lambda {JiakSchema.new(hash)}
122
132
  bad.should raise_error(JiakSchemaException,/write_mask.*strings/)
123
133
 
134
+ end
135
+
136
+ it "should ignore duplicate fields" do
124
137
  hash = @hash.clone
125
138
  hash[:allowed_fields] << @allowed_fields[0].to_s
126
- bad = lambda {JiakSchema.new(hash)}
127
- bad.should raise_error(JiakSchemaException,/unique/)
139
+ jiak_schema = JiakSchema.new(hash)
140
+ jiak_schema.should eql @jiak_schema
128
141
 
129
- hash = @hash.clone
130
- hash[:allowed_fields] << @allowed_fields[0].to_sym
131
- bad = lambda {JiakSchema.new(hash)}
132
- bad.should raise_error(JiakSchemaException,/unique/)
142
+ jiak_schema.allowed_fields = hash[:allowed_fields]
143
+ jiak_schema.allowed_fields.should eql @jiak_schema.allowed_fields
144
+
145
+ hash[:read_mask] << @read_mask[0]
146
+ jiak_schema.read_mask = hash[:read_mask]
147
+ jiak_schema.read_mask.should eql @hash[:read_mask]
148
+
149
+ hash[:write_mask] << @write_mask[0]
150
+ jiak_schema.write_mask = hash[:write_mask]
151
+ jiak_schema.write_mask.should eql @hash[:write_mask]
152
+
153
+ jiak_schema.readwrite = hash[:read_mask]
154
+ jiak_schema.read_mask.should eql @hash[:read_mask]
155
+ jiak_schema.write_mask.should eql @hash[:read_mask]
156
+ end
157
+
158
+ it "should update individual arrays" do
159
+ arr1 = [:f1]
160
+ arr12 = [:f1,:f2]
161
+ arr13 = [:f1,:f3]
162
+ arr14 = [:f1,:f4]
163
+ arr123 = [:f1,:f2,:f3]
164
+ arr1234 = [:f1,:f2,:f3,:f4]
165
+
166
+ jiak_schema = JiakSchema.new(arr1)
167
+ jiak_schema.allow :f2
168
+ jiak_schema.allowed_fields.should eql arr12
169
+ jiak_schema.read_mask.should eql arr1
170
+ jiak_schema.write_mask.should eql arr1
171
+ jiak_schema.required_fields.should be_empty
172
+ jiak_schema.readable :f3
173
+ jiak_schema.read_mask.should eql arr13
174
+ jiak_schema.write_mask.should eql arr1
175
+ jiak_schema.allowed_fields.should eql arr123
176
+ jiak_schema.required_fields.should be_empty
177
+ jiak_schema.writable :f4
178
+ jiak_schema.write_mask.should eql arr14
179
+ jiak_schema.read_mask.should eql arr13
180
+ jiak_schema.allowed_fields.should eql arr1234
181
+ jiak_schema.required_fields.should be_empty
182
+
183
+ jiak_schema = JiakSchema.new(arr1)
184
+ jiak_schema.readwrite :f2
185
+ jiak_schema.read_mask.should eql arr12
186
+ jiak_schema.write_mask.should eql arr12
187
+ jiak_schema.allowed_fields.should eql arr12
188
+ jiak_schema.required_fields.should be_empty
189
+
190
+ jiak_schema.require :f3
191
+ jiak_schema.required_fields.should eql [:f3]
192
+ jiak_schema.allowed_fields.should eql arr123
193
+ jiak_schema.read_mask.should eql arr123
194
+ jiak_schema.write_mask.should eql arr123
133
195
  end
134
196
 
135
- it "should update with validation" do
197
+ it "should return added fields when updating individual arrays" do
198
+ arr12 = [:f1,:f2]
199
+ arr23 = [:f2,:f3]
200
+ jiak_schema = JiakSchema.new
201
+ added = jiak_schema.allow arr12
202
+ added.should eql arr12
203
+ added = jiak_schema.allow arr23
204
+ added.should eql [:f3]
205
+
206
+ # other add methods call same internal method. previous test checks this is
207
+ # true so don't need more tests here
208
+ end
209
+
210
+ it "should set arrays with validation" do
136
211
  arr = [:f1,'f2']
137
212
  @jiak_schema.allowed_fields = arr
138
- @jiak_schema.allowed_fields.should eql arr
213
+ @jiak_schema.allowed_fields.should_not eql arr
214
+ @jiak_schema.allowed_fields.same_fields?(arr).should eql true
139
215
  @jiak_schema.required_fields = arr
140
- @jiak_schema.required_fields.should eql arr
216
+ @jiak_schema.required_fields.should_not eql arr
217
+ @jiak_schema.required_fields.same_fields?(arr).should eql true
141
218
  @jiak_schema.read_mask = arr
142
- @jiak_schema.read_mask.should eql arr
219
+ @jiak_schema.read_mask.should_not eql arr
220
+ @jiak_schema.read_mask.same_fields?(arr).should eql true
143
221
  @jiak_schema.write_mask = arr
144
- @jiak_schema.write_mask.should eql arr
222
+ @jiak_schema.write_mask.should_not eql arr
223
+ @jiak_schema.write_mask.same_fields?(arr).should eql true
145
224
 
146
225
  bad = lambda {@jiak_schema.allowed_fields = nil}
147
226
  bad.should raise_error(JiakSchemaException,/allowed_fields.*array/)
@@ -151,14 +230,39 @@ describe "JiakSchema" do
151
230
 
152
231
  bad = lambda {@jiak_schema.read_mask = [:f,1]}
153
232
  bad.should raise_error(JiakSchemaException,/read_mask.*symbol/)
154
-
155
- arr << :f1
156
- bad = lambda {@jiak_schema.write_mask = arr}
157
- bad.should raise_error(JiakSchemaException,/unique/)
233
+ end
234
+
235
+ it "should ignore duplicates on setting of arrays" do
236
+ arr = [:f1,:f2,:f1,:f3]
237
+ arr123 = [:f1,:f2,:f3]
238
+
239
+ @jiak_schema.allowed_fields = arr
240
+ @jiak_schema.allowed_fields.should eql arr123
241
+
242
+ @jiak_schema.required_fields = arr
243
+ @jiak_schema.required_fields.should eql arr123
244
+
245
+ @jiak_schema.read_mask = arr
246
+ @jiak_schema.read_mask.should eql arr123
247
+
248
+ @jiak_schema.write_mask = arr
249
+ @jiak_schema.write_mask.should eql arr123
250
+
251
+ @jiak_schema.allow :f1
252
+ @jiak_schema.allowed_fields.should eql arr123
253
+
254
+ @jiak_schema.readable :f2
255
+ @jiak_schema.read_mask.should eql arr123
256
+
257
+ @jiak_schema.writable :f3
258
+ @jiak_schema.read_mask.should eql arr123
259
+
260
+ @jiak_schema.require :f2
261
+ @jiak_schema.required_fields.should eql arr123
158
262
  end
159
263
 
160
264
  it "should convert to json" do
161
- @jiak_schema.to_jiak.should eql ({:schema => @hash}.to_json)
265
+ @jiak_schema.to_jiak.should eql ({:schema => @hash})
162
266
  end
163
267
 
164
268
  it "should equal an equal JiakSchema" do