basilik 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,301 @@
1
+ require 'spec_helper'
2
+
3
+ describe Basilik::Load do
4
+ before :all do
5
+ @url = ENV['fb_url']
6
+ @fb = Basilik::Load.new( @url )
7
+ end
8
+
9
+ describe '#inc' do
10
+ before :all do
11
+ @fb.remove
12
+ @fb.set(
13
+ {
14
+ a: {
15
+ a_1: 1,
16
+ },
17
+ b: {
18
+ b_1: 1.5
19
+ },
20
+ c: {
21
+ c_1: "fred"
22
+ }
23
+ }
24
+ )
25
+ end
26
+
27
+ it "increases an integer value correctly" do
28
+ @fb.child( :a ).inc( :a_1 )
29
+ @fb.child( 'a/a_1' ).read.should == 2
30
+ end
31
+
32
+ it "increases an float value correctly" do
33
+ @fb.child( :b ).inc( :b_1 )
34
+ @fb.child( 'b/b_1' ).read.should == 2.5
35
+ end
36
+
37
+ it "set the value to zero is the data does not exist" do
38
+ @fb.child( :b ).inc( :b_2 )
39
+ @fb.child( 'b/b_2' ).read.should == 0
40
+ end
41
+
42
+ it "fails if the field is non numeric" do
43
+ lambda { @fb.child( :c ).inc( :c_1 ) }.should raise_error( Basilik::Load::NonNumericFieldError )
44
+ end
45
+ end
46
+
47
+ describe '#dec' do
48
+ before :all do
49
+ @fb.remove
50
+ @fb.set(
51
+ {
52
+ a: {
53
+ a_1: 2,
54
+ },
55
+ b: {
56
+ b_1: 1.5
57
+ },
58
+ c: {
59
+ c_1: "fred"
60
+ }
61
+ }
62
+ )
63
+ end
64
+
65
+ it "decreases an integer value correctly" do
66
+ @fb.child( :a ).dec( :a_1 )
67
+ @fb.child( 'a/a_1' ).read.should == 1
68
+ end
69
+
70
+ it "decreases an float value correctly" do
71
+ @fb.child( :b ).dec( :b_1 )
72
+ @fb.child( 'b/b_1' ).read.should == 0.5
73
+ end
74
+
75
+ it "set the value to zero is the data does not exist" do
76
+ @fb.child( :b ).dec( :b_2 )
77
+ @fb.child( 'b/b_2' ).read.should == 0
78
+ end
79
+
80
+ it "fails if the field is non numeric" do
81
+ lambda { @fb.child( :c ).dec( :c_1 ) }.should raise_error( Basilik::Load::NonNumericFieldError )
82
+ end
83
+ end
84
+
85
+ describe '#set_rules' do
86
+ before :each do
87
+ @auth_fb = Basilik::Load.new( @url, ENV['fb_auth_token'] )
88
+ end
89
+
90
+ after :each do
91
+ @auth_fb.set_rules( { '.read' => true, '.write' => true } )
92
+ end
93
+
94
+ it "fetch security rules correctly" do
95
+ @auth_fb.set( tmp: { a: 0, b: 1 } )
96
+ @auth_fb.set_rules(
97
+ { '.read' => true, '.write' => false,
98
+ "tmp" => { '.read' => true, '.write' => false }
99
+ })
100
+ res= @auth_fb.get_rules
101
+ res.rules.tmp['.read'].should be_true
102
+ res.rules.tmp['.write'].should be_false
103
+
104
+ res = @fb.child(:tmp).read
105
+ res.should == { a: 0, b: 1 }
106
+
107
+ lambda { @fb.set( tmp: { d: 0 } ) }.should raise_error Basilik::Load::PermissionDeniedError
108
+ end
109
+ end
110
+
111
+ describe "#read" do
112
+ before :all do
113
+ @fb.remove
114
+ @fb.set( {
115
+ test_1: {
116
+ test_1_1: {
117
+ key_1: 'Hello',
118
+ key_2: 'World'
119
+ },
120
+ test_1_2: {
121
+ key_1: 10,
122
+ key_2: [10,20,30],
123
+ key_3: {
124
+ test_1_2_1: {
125
+ key_1: 50
126
+ }
127
+ }
128
+ }
129
+ },
130
+ test_2: 10,
131
+ test_3: :Blee,
132
+ test_4: %w[hello world],
133
+ test_5: [{a:1,b:2}, {c:3,d:4}]
134
+ }
135
+ )
136
+ end
137
+
138
+ it "reads a simple number correctly" do
139
+ @fb.child( :test_2 ).read.to_i.should == 10
140
+ end
141
+
142
+ it "reads a string correctly" do
143
+ @fb.child( :test_3 ).read.should == "Blee"
144
+ end
145
+
146
+ it "reads a simple array correctly" do
147
+ @fb.child( :test_4 ).read.should == %w[hello world]
148
+ end
149
+
150
+ it "reads an array of hash correctly" do
151
+ @fb.child( 'test_5/0' ).read.a.should == 1
152
+ end
153
+
154
+ it "reads a deeply nested hash correctly" do
155
+ @fb.child( 'test_1/test_1_2/key_3').read.test_1_2_1.key_1.should == 50
156
+ end
157
+ end
158
+
159
+ describe '#push' do
160
+ before :each do
161
+ @fb.remove
162
+ end
163
+
164
+ it "makes up an empty ref if no data" do
165
+ resp = @fb.push()
166
+ resp.name.should_not be_nil
167
+ end
168
+
169
+ it "builds a list with values" do
170
+ resp = @fb.push( {a:1,b:2} )
171
+ resp.name.should_not be_nil
172
+ @fb.child( resp.name ).read.a.should == 1
173
+ @fb.child( resp.name ).read.b.should == 2
174
+ end
175
+ end
176
+
177
+ describe 'priority' do
178
+ before :each do
179
+ @fb.remove
180
+ end
181
+
182
+ it "sets priority with complex data correctly" do
183
+ @fb.set( c:{d:1} )
184
+ ref = @fb.child( :c )
185
+ ref.set_priority( 2.0 )
186
+ ref.get_priority.to_i.should == 2.0
187
+ end
188
+
189
+ it "unsets priority correctly" do
190
+ @fb.set( c:{d:1} )
191
+ ref = @fb.child( :c )
192
+ ref.set_priority( 2.0 )
193
+ ref.get_priority.to_i.should == 2.0
194
+ ref.set_priority( nil )
195
+ ref.get_priority.should == nil
196
+ end
197
+
198
+ it "sets priority for lists correctly" do
199
+ a_ref = @fb.push( {b:1, a:2} )
200
+ b_ref = @fb.push( {e:1, d:2} )
201
+ a_ref.set_priority( 20 )
202
+ b_ref.set_priority( 10 )
203
+ a_ref.get_priority.to_i.should == 20
204
+ b_ref.get_priority.to_i.should == 10
205
+ res = a_ref.parent.read
206
+ keys = [b_ref.name, a_ref.name]
207
+ index = 0
208
+ res.each_pair do |k,v|
209
+ k.should == keys[index]
210
+ index += 1
211
+ end
212
+ end
213
+
214
+ it "sets priority for lists correctly" do
215
+ a_ref = @fb.push
216
+ a1_ref = a_ref.push( a:1 )
217
+ a2_ref = a_ref.push( b:1 )
218
+ b_ref = @fb.push
219
+ b1_ref = b_ref.push( c:1 )
220
+ b2_ref = b_ref.push( d:1 )
221
+
222
+ a_ref.set_priority( 20 )
223
+ a1_ref.set_priority( 50 )
224
+ a2_ref.set_priority( 30 )
225
+ b_ref.set_priority( 10 )
226
+ b1_ref.set_priority( 50 )
227
+ b2_ref.set_priority( 30 )
228
+
229
+ a1_ref.get_priority.to_i.should == 50
230
+ a2_ref.get_priority.to_i.should == 30
231
+ b1_ref.get_priority.to_i.should == 50
232
+ b2_ref.get_priority.to_i.should == 30
233
+
234
+ res = a_ref.parent.read
235
+ keys = [b_ref.name, a_ref.name]
236
+ index = 0
237
+ res.each_pair do |k,v|
238
+ k.should == keys[index]
239
+ index += 1
240
+ end
241
+ end
242
+
243
+ end
244
+
245
+ it "reads a simple number correctly" do
246
+ @fb.update( {test_1: 20 } )
247
+ @fb.child( :test_1 ).read.to_i.should == 20
248
+ end
249
+
250
+ describe '#remove' do
251
+ before :each do
252
+ @fb.remove
253
+ end
254
+
255
+ it "removes data correctly" do
256
+ @fb.set( {fred: 10} )
257
+ @fb.child( :fred ).remove
258
+ lambda { @fb.child( :fred ).read }.should raise_error( /No data/ )
259
+ end
260
+ end
261
+
262
+ describe '#update' do
263
+ before :all do
264
+ @fb.remove
265
+ @fb.set( a:{b:1,c:2} )
266
+ @a_ref = @fb.child( :a )
267
+ end
268
+
269
+ describe 'oneshot' do
270
+ it "updates an integer correctly" do
271
+ @a_ref.update( c:5 )
272
+ @a_ref.child( :c ).read.should == 5
273
+ end
274
+
275
+ it "updates a float correctly" do
276
+ @a_ref.update( c:5.0 )
277
+ @a_ref.child( :c ).read.should == 5.0
278
+ end
279
+
280
+ it "updates a boolean correctly" do
281
+ @a_ref.update( c:true )
282
+ @a_ref.child( :c ).read.should be_true
283
+ end
284
+
285
+ it "updates a boolean correctly" do
286
+ @a_ref.update( c:true )
287
+ @a_ref.child( :c ).read.should be_true
288
+ end
289
+
290
+ it "updates an object correctly" do
291
+ @a_ref.update( c:{fred:'Hello', blee:'World'} )
292
+ @a_ref.child( :c ).read.should == {fred:'Hello', blee:'World'}
293
+ end
294
+
295
+ it "updates an array correctly" do
296
+ @a_ref.update( c:%w[Hello World] )
297
+ @a_ref.child( :c ).read.should == %w[Hello World]
298
+ end
299
+ end
300
+ end
301
+ end
@@ -0,0 +1,174 @@
1
+ require 'spec_helper'
2
+ require 'yaml'
3
+
4
+ describe Basilik::Snapshot do
5
+
6
+ describe '#to_map' do
7
+ it "sorts a simple hierarchy correctly" do
8
+ snap = Basilik::Snapshot.new( {a:{b:1} } )
9
+ snap.to_map.a.should == {b:1}
10
+ end
11
+
12
+ it "sorts a priority hierarchy correctly" do
13
+ snap = Basilik::Snapshot.new({
14
+ a: {
15
+ '.priority' => 1000,
16
+ a_1: {
17
+ '.priority' => 20,
18
+ a_1_1: 1
19
+ },
20
+ a_2: {
21
+ '.priority' => 10,
22
+ a_2_1: 1
23
+ }
24
+ },
25
+ b: {
26
+ '.priority' => 0,
27
+ b_1: {
28
+ '.priority' => 50,
29
+ b_1_1: 1
30
+ },
31
+ b_2: {
32
+ '.priority' => 10,
33
+ b_2_1: 1
34
+ }
35
+ }
36
+ })
37
+ map = snap.to_map
38
+ map.keys.should == %w(b a)
39
+ map.b.keys.should == %w(b_2 b_1)
40
+ map.a.keys.should == %w(a_2 a_1)
41
+ end
42
+
43
+ it "sorts a mixed priority hierarchy correctly" do
44
+ snap = Basilik::Snapshot.new({
45
+ a: {
46
+ a_1: {
47
+ '.priority' => 20,
48
+ a_1_1: 1
49
+ },
50
+ a_2: {
51
+ '.priority' => 10,
52
+ a_2_1: 1
53
+ }
54
+ },
55
+ b: {
56
+ b_1: {
57
+ '.priority' => "10",
58
+ b_1_1: 1
59
+ },
60
+ b_2: {
61
+ '.priority' => "10",
62
+ b_2_1: 1
63
+ }
64
+ }
65
+ })
66
+ map = snap.to_map
67
+ map.keys.should == %w(a b)
68
+ map.b.keys.should == %w(b_1 b_2)
69
+ map.a.keys.should == %w(a_2 a_1)
70
+ end
71
+
72
+ it "sorts a firebase hierarchy correctly" do
73
+ map = Map( JSON.parse( '{
74
+ "-IrIRswPWIABbNj1q045" : {
75
+ ".priority" : 10.0,
76
+ "-IrIRt0-K0Ae0VkYGzPz" : {
77
+ "c" : 1,
78
+ ".priority" : 50.0
79
+ },
80
+ "-IrIRt4bNTVPk7bILOqR" : {
81
+ ".priority" : 30.0,
82
+ "d" : 1
83
+ }
84
+ },
85
+ "-IrIRsiaW32Hp5xUOyMZ" : {
86
+ ".priority" : 20.0,
87
+ "-IrIRsnD1QRcgMU4lgH1" : {
88
+ ".priority" : 50.0,
89
+ "a" : 1
90
+ },
91
+ "-IrIRsrpWvE7ykm4tRBT" : {
92
+ ".priority" : 30.0,
93
+ "b" : 1
94
+ }
95
+ }
96
+ }'))
97
+ snap = Basilik::Snapshot.new( map )
98
+ map = snap.to_map
99
+ map.keys.should == %w(-IrIRswPWIABbNj1q045 -IrIRsiaW32Hp5xUOyMZ)
100
+ map['-IrIRsiaW32Hp5xUOyMZ'].keys.should == %w(-IrIRsrpWvE7ykm4tRBT -IrIRsnD1QRcgMU4lgH1)
101
+ end
102
+
103
+ it "sorts a complex hierarchy correctly" do
104
+ map = Map( JSON.parse( '{
105
+ "-IrIRswPWIABbNj1q045" : {
106
+ ".priority" : 10.0,
107
+ "-IrIRt0-K0Ae0VkYGzPz" : {
108
+ "c" : 1,
109
+ ".priority" : 50.0
110
+ },
111
+ "-IrIRt4bNTVPk7bILOqR" : {
112
+ ".priority" : 30.0,
113
+ "d" : 1
114
+ }
115
+ },
116
+ "-IrIRsiaW32Hp5xUOyMZ" : {
117
+ "-IrIRsnD1QRcgMU4lgH1" : {
118
+ ".priority" : 50.0,
119
+ "a" : 1
120
+ },
121
+ "-IrIRsrpWvE7ykm4tRBT" : {
122
+ ".priority" : 30.0,
123
+ "b" : 1
124
+ }
125
+ }
126
+ }'))
127
+ snap = Basilik::Snapshot.new( map )
128
+ map = snap.to_map
129
+ map.keys.should == %w(-IrIRsiaW32Hp5xUOyMZ -IrIRswPWIABbNj1q045)
130
+ map['-IrIRsiaW32Hp5xUOyMZ'].keys.should == %w(-IrIRsrpWvE7ykm4tRBT -IrIRsnD1QRcgMU4lgH1)
131
+ end
132
+
133
+ it "sorts a complex hierarchy correctly" do
134
+ map = Map( JSON.parse( '{
135
+ "-IrIRswPWIABbNj1q045" : {
136
+ ".priority" : "10.0",
137
+ "-IrIRt0-K0Ae0VkYGzPz" : {
138
+ "c" : 1,
139
+ ".priority" : 50.0
140
+ },
141
+ "-IrIRt4bNTVPk7bILOqR" : {
142
+ ".priority" : 30.0,
143
+ "d" : 1
144
+ }
145
+ },
146
+ "-IrIRsiaW32Hp5xUOyMZ" : {
147
+ ".priority" : 10.0,
148
+ "-IrIRsnD1QRcgMU4lgH1" : {
149
+ ".priority" : 50.0,
150
+ "a" : 1
151
+ },
152
+ "-IrIRsrpWvE7ykm4tRBT" : {
153
+ ".priority" : 30.0,
154
+ "b" : 1
155
+ }
156
+ },
157
+ "-IrIRsiaW32Hp5xUOyMA" : {
158
+ "-IrIRsnD1QRcgMU4lgH1" : {
159
+ ".priority" : 50.0,
160
+ "a" : 1
161
+ },
162
+ "-IrIRsrpWvE7ykm4tRBT" : {
163
+ ".priority" : 30.0,
164
+ "b" : 1
165
+ }
166
+ }
167
+ }'))
168
+ snap = Basilik::Snapshot.new( map )
169
+ map = snap.to_map
170
+ map.keys.should == %w(-IrIRsiaW32Hp5xUOyMA -IrIRsiaW32Hp5xUOyMZ -IrIRswPWIABbNj1q045)
171
+ map['-IrIRsiaW32Hp5xUOyMZ'].keys.should == %w(-IrIRsrpWvE7ykm4tRBT -IrIRsnD1QRcgMU4lgH1)
172
+ end
173
+ end
174
+ end
@@ -0,0 +1,10 @@
1
+ require 'basilik'
2
+ require 'simplecov'
3
+
4
+ if ENV['COV']
5
+ SimpleCov.start do
6
+ end
7
+ end
8
+
9
+ RSpec.configure do |config|
10
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: basilik
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Fernand Galiana
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ prerelease: false
15
+ version_requirements: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 6.3.0
20
+ name: map
21
+ requirement: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ~>
24
+ - !ruby/object:Gem::Version
25
+ version: 6.3.0
26
+ type: :runtime
27
+ - !ruby/object:Gem::Dependency
28
+ prerelease: false
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.6.2
34
+ name: typhoeus
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ~>
38
+ - !ruby/object:Gem::Version
39
+ version: 0.6.2
40
+ type: :runtime
41
+ description: Ruby implementation of Firebase framework
42
+ email:
43
+ - fernand.galiana@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .autotest
49
+ - .gitignore
50
+ - Gemfile
51
+ - Gemfile.lock
52
+ - README.md
53
+ - Rakefile
54
+ - basilik.gemspec
55
+ - examples/a.rb
56
+ - examples/b.rb
57
+ - examples/c.rb
58
+ - examples/d.rb
59
+ - examples/e.rb
60
+ - examples/f.rb
61
+ - examples/g.rb
62
+ - examples/h.rb
63
+ - examples/i.rb
64
+ - examples/j.rb
65
+ - lib/basilik.rb
66
+ - lib/basilik/action.rb
67
+ - lib/basilik/core.rb
68
+ - lib/basilik/core_ext/map.rb
69
+ - lib/basilik/core_ext/string.rb
70
+ - lib/basilik/faults.rb
71
+ - lib/basilik/load.rb
72
+ - lib/basilik/ref.rb
73
+ - lib/basilik/snapshot.rb
74
+ - lib/basilik/version.rb
75
+ - spec/basilik/.firebase_spec.rb.swp
76
+ - spec/basilik/core_ext/map_spec.rb
77
+ - spec/basilik/core_ext/string_spec.rb
78
+ - spec/basilik/ref_spec.rb
79
+ - spec/basilik/root_spec.rb
80
+ - spec/basilik/snapshot_spec.rb
81
+ - spec/spec_helper.rb
82
+ homepage: https://github.com/derailed/basilik
83
+ licenses: []
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project: firebase
101
+ rubygems_version: 2.0.3
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Ruby implementation of Firebase framework
105
+ test_files: []