ceritium-relaxdb 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/LICENSE +20 -0
  2. data/README.textile +185 -0
  3. data/Rakefile +58 -0
  4. data/docs/spec_results.html +604 -0
  5. data/lib/more/atomic_bulk_save_support.rb +18 -0
  6. data/lib/more/grapher.rb +48 -0
  7. data/lib/relaxdb.rb +40 -0
  8. data/lib/relaxdb/all_delegator.rb +68 -0
  9. data/lib/relaxdb/belongs_to_proxy.rb +29 -0
  10. data/lib/relaxdb/design_doc.rb +55 -0
  11. data/lib/relaxdb/document.rb +531 -0
  12. data/lib/relaxdb/extlib.rb +15 -0
  13. data/lib/relaxdb/has_many_proxy.rb +104 -0
  14. data/lib/relaxdb/has_one_proxy.rb +45 -0
  15. data/lib/relaxdb/paginate_params.rb +54 -0
  16. data/lib/relaxdb/paginator.rb +78 -0
  17. data/lib/relaxdb/query.rb +75 -0
  18. data/lib/relaxdb/references_many_proxy.rb +101 -0
  19. data/lib/relaxdb/relaxdb.rb +208 -0
  20. data/lib/relaxdb/server.rb +156 -0
  21. data/lib/relaxdb/sorted_by_view.rb +65 -0
  22. data/lib/relaxdb/uuid_generator.rb +21 -0
  23. data/lib/relaxdb/validators.rb +11 -0
  24. data/lib/relaxdb/view_object.rb +34 -0
  25. data/lib/relaxdb/view_result.rb +18 -0
  26. data/lib/relaxdb/view_uploader.rb +47 -0
  27. data/lib/relaxdb/views.rb +54 -0
  28. data/spec/belongs_to_spec.rb +129 -0
  29. data/spec/callbacks_spec.rb +80 -0
  30. data/spec/derived_properties_spec.rb +117 -0
  31. data/spec/design_doc_spec.rb +34 -0
  32. data/spec/document_spec.rb +556 -0
  33. data/spec/has_many_spec.rb +176 -0
  34. data/spec/has_one_spec.rb +128 -0
  35. data/spec/query_spec.rb +80 -0
  36. data/spec/references_many_spec.rb +178 -0
  37. data/spec/relaxdb_spec.rb +226 -0
  38. data/spec/spec.opts +1 -0
  39. data/spec/spec_helper.rb +10 -0
  40. data/spec/spec_models.rb +151 -0
  41. data/spec/view_object_spec.rb +47 -0
  42. metadata +123 -0
@@ -0,0 +1,226 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+ require File.dirname(__FILE__) + '/spec_models.rb'
3
+
4
+ describe RelaxDB do
5
+
6
+ before(:all) do
7
+ RelaxDB.configure(:host => "localhost", :port => 5984)
8
+ end
9
+
10
+ before(:each) do
11
+ RelaxDB.delete_db "relaxdb_spec_db" rescue "ok"
12
+ RelaxDB.use_db "relaxdb_spec_db"
13
+ end
14
+
15
+ describe ".create_object" do
16
+
17
+ it "should return an instance of a known object if passed a hash with a class key" do
18
+ data = { "class" => "Item" }
19
+ obj = RelaxDB.create_object(data)
20
+ obj.should be_instance_of(Item)
21
+ end
22
+
23
+ it "should return an instance of a dynamically created object if no class key is provided" do
24
+ data = { "name" => "tesla coil", "strength" => 5000 }
25
+ obj = RelaxDB.create_object(data)
26
+ obj.name.should == "tesla coil"
27
+ obj.strength.should == 5000
28
+ end
29
+
30
+ end
31
+
32
+ # bulk_save and bulk_save! should match Document#save and Document#save! semantics
33
+ describe ".bulk_save" do
34
+
35
+ it "should be invokable multiple times" do
36
+ t1, t2 = Tag.new, Tag.new
37
+ RelaxDB.bulk_save(t1, t2)
38
+ RelaxDB.bulk_save(t1, t2)
39
+ end
40
+
41
+ it "should return the objects it was passed" do
42
+ t1, t2 = Tag.new, Tag.new
43
+ ta, tb = RelaxDB.bulk_save(t1, t2)
44
+ ta.should == t1
45
+ tb.should == t2
46
+ end
47
+
48
+ it "should succeed when passed no args" do
49
+ RelaxDB.bulk_save
50
+ end
51
+
52
+ it "should return false on failure" do
53
+ c = Class.new(RelaxDB::Document) do
54
+ property :foo, :validator => lambda { false }
55
+ end
56
+ x = c.new
57
+ RelaxDB.bulk_save(x).should be_false
58
+ end
59
+
60
+ it "should not attempt to save if a pre-save stage fails" do
61
+ c = Class.new(RelaxDB::Document) do
62
+ property :foo, :validator => lambda { false }
63
+ end
64
+ x = c.new
65
+ RelaxDB.bulk_save(x)
66
+ x.should be_new_document
67
+ end
68
+
69
+ it "should invoke the after-save stage after a successful save" do
70
+ c = Class.new(RelaxDB::Document) do
71
+ attr_accessor :foo
72
+ after_save lambda { |c| c.foo = :bar }
73
+ end
74
+ x = c.new
75
+ RelaxDB.bulk_save(x).first.foo.should == :bar
76
+ end
77
+
78
+ end
79
+
80
+ describe ".bulk_save!" do
81
+
82
+ it "should raise an exception if a obj fails validation" do
83
+ c = Class.new(RelaxDB::Document) do
84
+ property :foo, :validator => lambda { false }
85
+ end
86
+ lambda { RelaxDB.bulk_save!(c.new) }.should raise_error(RelaxDB::ValidationFailure)
87
+ end
88
+
89
+ it "should raise an exception if a document update conflict occurs on save" do
90
+ Atom.new(:_id => "a1").save!
91
+ lambda { RelaxDB.bulk_save! Atom.new(:_id => "a1") }.should raise_error(RelaxDB::UpdateConflict)
92
+ end
93
+
94
+ end
95
+
96
+ describe ".replicate_db" do
97
+
98
+ it "should replicate the named database" do
99
+ orig = "relaxdb_spec_db"
100
+ replica = "relaxdb_spec_db_replica"
101
+ RelaxDB.delete_db replica rescue "ok"
102
+ Atom.new.save # implicitly saved to orig
103
+ RelaxDB.replicate_db orig, replica
104
+ RelaxDB.use_db replica
105
+ Atom.all.size.should == 1
106
+ end
107
+
108
+ end
109
+
110
+ describe ".load" do
111
+
112
+ it "should load a single document" do
113
+ a = Atom.new.save
114
+ ar = RelaxDB.load a._id
115
+ ar.should == a
116
+ end
117
+
118
+ it "should load an arbitrary number of documents" do
119
+ a1, a2 = Atom.new.save, Atom.new.save
120
+ ar1, ar2 = RelaxDB.load [a1._id, a2._id]
121
+ ar1.should == a1
122
+ ar2.should == a2
123
+ end
124
+
125
+ it "should return nil when given a id for a non existant doc" do
126
+ RelaxDB.load("nothere").should be_nil
127
+ end
128
+
129
+ it "should return an array with correctly placed nils when given a list containing non existant doc ids" do
130
+ a1, a2 = Atom.new.save, Atom.new.save
131
+ res = RelaxDB.load [nil, a1._id, nil, a2._id, nil]
132
+ res.should == [nil, a1, nil, a2, nil]
133
+ end
134
+
135
+ end
136
+
137
+ describe ".load!" do
138
+
139
+ it "should load a single document" do
140
+ a = Atom.new.save
141
+ ar = RelaxDB.load! a._id
142
+ ar.should == a
143
+ end
144
+
145
+ it "should load multiple documents" do
146
+ a1, a2 = Atom.new.save, Atom.new.save
147
+ ar1, ar2 = RelaxDB.load! [a1._id, a2._id]
148
+ ar1.should == a1
149
+ ar2.should == a2
150
+ end
151
+
152
+ it "should throw an exception if given a single id for a non-existant doc" do
153
+ lambda do
154
+ RelaxDB.load! "nothere"
155
+ end.should raise_error(RelaxDB::NotFound)
156
+ end
157
+
158
+ it "should throw an exception if any of a list of doc ids is for a non-existant doc" do
159
+ a = Atom.new.save
160
+ lambda do
161
+ RelaxDB.load! [nil, a._id]
162
+ end.should raise_error(RelaxDB::NotFound)
163
+ end
164
+
165
+ end
166
+
167
+ describe ".view" do
168
+
169
+ map_func = %Q<
170
+ function (doc) {
171
+ emit(doc._id, doc);
172
+ }
173
+ >
174
+
175
+ it "should request a view and return a hash" do
176
+ RelaxDB::DesignDocument.get("viewtest").add_view("simple", "map", map_func).save
177
+ data = RelaxDB.view("viewtest", "simple")
178
+ data.should be_instance_of(Hash)
179
+ end
180
+
181
+ it "may accept a block" do
182
+ RelaxDB::DesignDocument.get("viewtest").add_view("simple", "map", map_func).save
183
+ RelaxDB.db.put("x", {}.to_json)
184
+ RelaxDB.db.put("y", {}.to_json)
185
+ data = RelaxDB.view("viewtest", "simple") { |q| q.key("x") }
186
+ data["rows"].size.should == 1
187
+ end
188
+
189
+ it "should be queryable with a multi key post" do
190
+ 5.times { |i| Primitives.new(:num => i).save }
191
+ # Create the view
192
+ Primitives.all.sorted_by(:num)
193
+ resp = RelaxDB.view("Primitives", "all_sorted_by_num") do |q|
194
+ q.keys([0, 4])
195
+ q.reduce(false).group(true) # group invocation should hopefully be temporary
196
+ end
197
+ RelaxDB.instantiate(resp).map{ |p| p.num }.should == [0, 4]
198
+ end
199
+
200
+ end
201
+
202
+ describe ".merge" do
203
+
204
+ it "should merge rows sharing a common merge key into a single ViewObject" do
205
+ rows = [
206
+ {"value" => {"sculptor_id" => 1, "sculpture_name" => "strandbeesten"} },
207
+ {"value" => {"sculptor_id" => 1, "sculptor_name" => "hans"} },
208
+ {"value" => {"sculptor_id" => 2, "sculpture_name" => "parading dogs"} },
209
+ {"value" => {"sculptor_id" => 2, "sculptor_name" => "holmes"} }
210
+ ]
211
+ data = {"rows" => rows}
212
+ result = RelaxDB.merge(data, "sculptor_id")
213
+ result = result.sort { |a, b| a.sculptor_name <=> b.sculptor_name }
214
+
215
+ result[0].sculptor_name.should == "hans"
216
+ result[0].sculpture_name.should == "strandbeesten"
217
+ result[1].sculptor_name.should == "holmes"
218
+ result[1].sculpture_name.should == "parading dogs"
219
+ end
220
+
221
+ end
222
+
223
+ # if caching is added
224
+ # it "should offer an example where behaviour is different with caching enabled and caching disabled"
225
+
226
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,10 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+
9
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
10
+ require 'relaxdb'
@@ -0,0 +1,151 @@
1
+ #
2
+ # RSpec loads this file multiple times, thus breaking invocations like Document.has_one_rels
3
+ # The following clause ensures the tests pass, but perhaps Document should raise a warning
4
+ # if it's loaded more than once...
5
+ #
6
+ unless @spec_models_loaded
7
+
8
+ class Atom < RelaxDB::Document
9
+ end
10
+
11
+ class Initiative < RelaxDB::Document
12
+ property :x
13
+ attr_reader :foo
14
+ def initialize(data)
15
+ data[:_id] = data[:x]
16
+ super data
17
+ @foo = :bar
18
+ end
19
+ end
20
+
21
+ class Primitives < RelaxDB::Document
22
+
23
+ property :str
24
+ property :num
25
+ property :true_bool
26
+ property :false_bool
27
+ property :created_at
28
+ property :empty
29
+
30
+ end
31
+
32
+ class BespokeReader < RelaxDB::Document
33
+ property :val
34
+ def val; @val + 5; end
35
+ end
36
+
37
+ class BespokeWriter < RelaxDB::Document
38
+ property :val
39
+ def val=(v); @val = v - 10; end
40
+ end
41
+
42
+ class Letter < RelaxDB::Document
43
+
44
+ property :letter
45
+ property :number
46
+
47
+ end
48
+
49
+ class Invite < RelaxDB::Document
50
+
51
+ property :message
52
+
53
+ belongs_to :sender
54
+ belongs_to :recipient
55
+
56
+ end
57
+
58
+ class Item < RelaxDB::Document
59
+
60
+ property :name
61
+ belongs_to :user
62
+
63
+ end
64
+
65
+ class User < RelaxDB::Document
66
+
67
+ property :name, :default => "u"
68
+ property :age
69
+
70
+ has_many :items, :class => "Item"
71
+
72
+ has_many :invites_received, :class => "Invite", :known_as => :recipient
73
+ has_many :invites_sent, :class => "Invite", :known_as => :sender
74
+
75
+ end
76
+
77
+ class Post < RelaxDB::Document
78
+
79
+ property :subject
80
+ property :content
81
+ property :created_at
82
+ property :viewed_at
83
+
84
+ end
85
+
86
+ class Rating < RelaxDB::Document
87
+
88
+ property :stars, :default => 5
89
+ belongs_to :photo
90
+
91
+ end
92
+
93
+ class Photo < RelaxDB::Document
94
+
95
+ property :name
96
+
97
+ has_one :rating
98
+
99
+ references_many :tags, :class => "Tag", :known_as => :photos
100
+
101
+ has_many :taggings, :class => "Tagging"
102
+
103
+ end
104
+
105
+ class Tag < RelaxDB::Document
106
+
107
+ property :name
108
+ references_many :photos, :class => "Photo", :known_as => :tags
109
+
110
+ has_many :taggings, :class => "Tagging"
111
+
112
+ end
113
+
114
+ class Tagging < RelaxDB::Document
115
+
116
+ belongs_to :photo
117
+ belongs_to :tag
118
+ property :relevance
119
+
120
+ end
121
+
122
+ class MultiWordClass < RelaxDB::Document
123
+ has_one :multi_word_child
124
+ has_many :multi_word_children, :class => "MultiWordChild"
125
+ end
126
+
127
+ class MultiWordChild < RelaxDB::Document
128
+ belongs_to :multi_word_class
129
+ end
130
+
131
+ class TwitterUser < RelaxDB::Document
132
+
133
+ property :name
134
+ references_many :followers, :class => "User", :known_as => :leaders
135
+ references_many :leaders, :class => "User", :known_as => :followers
136
+
137
+ end
138
+
139
+ class Dysfunctional < RelaxDB::Document
140
+ has_one :failure
141
+ has_many :failures, :class => "Failure"
142
+ end
143
+
144
+ class Failure < RelaxDB::Document
145
+ property :pathological, :validator => lambda { false }
146
+ belongs_to :dysfunctional
147
+ end
148
+
149
+ @spec_models_loaded = true
150
+
151
+ end
@@ -0,0 +1,47 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+ require File.dirname(__FILE__) + '/spec_models.rb'
3
+
4
+ describe RelaxDB::ViewObject do
5
+
6
+ describe ".new" do
7
+
8
+ it "should provide readers for the object passed in the hash" do
9
+ data = { :name => "chaise", :variety => "longue" }
10
+ obj = RelaxDB::ViewObject.new(data)
11
+ obj.name.should == "chaise"
12
+ obj.variety.should == "longue"
13
+ end
14
+
15
+ it "should try to convert objects ending in _at to a time" do
16
+ now = Time.now
17
+ data = { :ends_at => now.to_s }
18
+ obj = RelaxDB::ViewObject.new(data)
19
+ obj.ends_at.should be_close(now, 1)
20
+ end
21
+
22
+ end
23
+
24
+ describe ".create" do
25
+
26
+ it "should return an array of view objects when passed an array" do
27
+ data = [ {:half_life => 2}, {:half_life => 16} ]
28
+ obj = RelaxDB::ViewObject.create(data)
29
+ obj.size.should == 2
30
+ obj[0].half_life.should == 2
31
+ obj[1].half_life.should == 16
32
+ end
33
+
34
+ it "should return a view object when passed a hash" do
35
+ data = {:half_life => 32}
36
+ obj = RelaxDB::ViewObject.create(data)
37
+ obj.half_life.should == 32
38
+ end
39
+
40
+ it "should return a simple value when passed a primitive" do
41
+ obj = RelaxDB::ViewObject.create(10)
42
+ obj.should == 10
43
+ end
44
+
45
+ end
46
+
47
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ceritium-relaxdb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.8
5
+ platform: ruby
6
+ authors:
7
+ - Paul Carey
8
+ autorequire: relaxdb
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-02 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: extlib
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.9.4
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: uuid
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ description:
46
+ email: paul.p.carey@gmail.com
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files: []
52
+
53
+ files:
54
+ - LICENSE
55
+ - README.textile
56
+ - Rakefile
57
+ - docs/spec_results.html
58
+ - lib/relaxdb
59
+ - lib/relaxdb/all_delegator.rb
60
+ - lib/relaxdb/belongs_to_proxy.rb
61
+ - lib/relaxdb/design_doc.rb
62
+ - lib/relaxdb/document.rb
63
+ - lib/relaxdb/extlib.rb
64
+ - lib/relaxdb/has_many_proxy.rb
65
+ - lib/relaxdb/has_one_proxy.rb
66
+ - lib/relaxdb/paginate_params.rb
67
+ - lib/relaxdb/paginator.rb
68
+ - lib/relaxdb/query.rb
69
+ - lib/relaxdb/references_many_proxy.rb
70
+ - lib/relaxdb/relaxdb.rb
71
+ - lib/relaxdb/server.rb
72
+ - lib/relaxdb/sorted_by_view.rb
73
+ - lib/relaxdb/uuid_generator.rb
74
+ - lib/relaxdb/validators.rb
75
+ - lib/relaxdb/view_object.rb
76
+ - lib/relaxdb/view_result.rb
77
+ - lib/relaxdb/view_uploader.rb
78
+ - lib/relaxdb/views.rb
79
+ - lib/more/grapher.rb
80
+ - lib/relaxdb.rb
81
+ - lib/more/atomic_bulk_save_support.rb
82
+ - spec/belongs_to_spec.rb
83
+ - spec/callbacks_spec.rb
84
+ - spec/design_doc_spec.rb
85
+ - spec/derived_properties_spec.rb
86
+ - spec/document_spec.rb
87
+ - spec/has_many_spec.rb
88
+ - spec/has_one_spec.rb
89
+ - spec/query_spec.rb
90
+ - spec/references_many_spec.rb
91
+ - spec/relaxdb_spec.rb
92
+ - spec/spec.opts
93
+ - spec/spec_helper.rb
94
+ - spec/spec_models.rb
95
+ - spec/view_object_spec.rb
96
+ has_rdoc: false
97
+ homepage: http://github.com/paulcarey/relaxdb/
98
+ post_install_message:
99
+ rdoc_options: []
100
+
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: "0"
108
+ version:
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: "0"
114
+ version:
115
+ requirements: []
116
+
117
+ rubyforge_project:
118
+ rubygems_version: 1.2.0
119
+ signing_key:
120
+ specification_version: 2
121
+ summary: RelaxDB provides a simple interface to CouchDB
122
+ test_files: []
123
+