relaxdb 0.3.5
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/LICENSE +20 -0
- data/README.textile +200 -0
- data/Rakefile +63 -0
- data/docs/spec_results.html +1059 -0
- data/lib/more/atomic_bulk_save_support.rb +18 -0
- data/lib/more/grapher.rb +48 -0
- data/lib/relaxdb.rb +50 -0
- data/lib/relaxdb/all_delegator.rb +44 -0
- data/lib/relaxdb/belongs_to_proxy.rb +29 -0
- data/lib/relaxdb/design_doc.rb +57 -0
- data/lib/relaxdb/document.rb +600 -0
- data/lib/relaxdb/extlib.rb +24 -0
- data/lib/relaxdb/has_many_proxy.rb +101 -0
- data/lib/relaxdb/has_one_proxy.rb +42 -0
- data/lib/relaxdb/migration.rb +40 -0
- data/lib/relaxdb/migration_version.rb +21 -0
- data/lib/relaxdb/net_http_server.rb +61 -0
- data/lib/relaxdb/paginate_params.rb +53 -0
- data/lib/relaxdb/paginator.rb +88 -0
- data/lib/relaxdb/query.rb +76 -0
- data/lib/relaxdb/references_many_proxy.rb +97 -0
- data/lib/relaxdb/relaxdb.rb +250 -0
- data/lib/relaxdb/server.rb +109 -0
- data/lib/relaxdb/taf2_curb_server.rb +63 -0
- data/lib/relaxdb/uuid_generator.rb +21 -0
- data/lib/relaxdb/validators.rb +11 -0
- data/lib/relaxdb/view_object.rb +34 -0
- data/lib/relaxdb/view_result.rb +18 -0
- data/lib/relaxdb/view_uploader.rb +49 -0
- data/lib/relaxdb/views.rb +114 -0
- data/readme.rb +80 -0
- data/spec/belongs_to_spec.rb +124 -0
- data/spec/callbacks_spec.rb +80 -0
- data/spec/derived_properties_spec.rb +112 -0
- data/spec/design_doc_spec.rb +34 -0
- data/spec/doc_inheritable_spec.rb +100 -0
- data/spec/document_spec.rb +545 -0
- data/spec/has_many_spec.rb +202 -0
- data/spec/has_one_spec.rb +123 -0
- data/spec/migration_spec.rb +97 -0
- data/spec/migration_version_spec.rb +28 -0
- data/spec/paginate_params_spec.rb +15 -0
- data/spec/paginate_spec.rb +360 -0
- data/spec/query_spec.rb +90 -0
- data/spec/references_many_spec.rb +173 -0
- data/spec/relaxdb_spec.rb +364 -0
- data/spec/server_spec.rb +32 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +65 -0
- data/spec/spec_models.rb +199 -0
- data/spec/view_by_spec.rb +76 -0
- data/spec/view_object_spec.rb +47 -0
- data/spec/view_spec.rb +23 -0
- metadata +137 -0
data/spec/server_spec.rb
ADDED
@@ -0,0 +1,32 @@
|
|
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, :design_doc => "spec_doc"
|
8
|
+
@server = RelaxDB::Server.new("localhost", 5984)
|
9
|
+
end
|
10
|
+
|
11
|
+
before(:each) do
|
12
|
+
RelaxDB.delete_db "relaxdb_spec" rescue "ok"
|
13
|
+
RelaxDB.use_db "relaxdb_spec"
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "GET" do
|
17
|
+
|
18
|
+
it "should raise a HTTP_404 for a non existant doc" do
|
19
|
+
lambda do
|
20
|
+
@server.get "/relaxdb_spec/foo"
|
21
|
+
end.should raise_error(RelaxDB::HTTP_404)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should raise a RuntimeError for non specific errors" do
|
25
|
+
lambda do
|
26
|
+
@server.get "/relaxdb_spec/_design/spec_doc/_view?fail=true"
|
27
|
+
end.should raise_error(RuntimeError)
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,65 @@
|
|
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'
|
11
|
+
|
12
|
+
class RdbFormatter; def call(sv, time, progname, msg); puts msg; end; end
|
13
|
+
|
14
|
+
def setup_test_db
|
15
|
+
# RelaxDB.configure :host => "localhost", :port => 5984, :design_doc => "spec_doc", :logger => Logger.new(STDOUT)
|
16
|
+
# RelaxDB.logger.formatter = RdbFormatter.new
|
17
|
+
|
18
|
+
RelaxDB.configure :host => "localhost", :port => 5984, :design_doc => "spec_doc"
|
19
|
+
|
20
|
+
RelaxDB.delete_db "relaxdb_spec" rescue "ok"
|
21
|
+
RelaxDB.use_db "relaxdb_spec"
|
22
|
+
begin
|
23
|
+
RelaxDB.replicate_db "relaxdb_spec_base", "relaxdb_spec"
|
24
|
+
RelaxDB.enable_view_creation
|
25
|
+
rescue => e
|
26
|
+
puts "\n===== Run rake create_base_db before the first spec run ====="
|
27
|
+
puts
|
28
|
+
exit!
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def create_test_db params = {}
|
33
|
+
defaults = {:host => "localhost", :port => 5984, :design_doc => "spec_doc"}
|
34
|
+
RelaxDB.configure defaults.merge(params)
|
35
|
+
|
36
|
+
RelaxDB.delete_db "relaxdb_spec" rescue "ok"
|
37
|
+
RelaxDB.use_db "relaxdb_spec"
|
38
|
+
RelaxDB.enable_view_creation
|
39
|
+
end
|
40
|
+
|
41
|
+
def create_base_db
|
42
|
+
RelaxDB.configure :host => "localhost", :port => 5984, :design_doc => "spec_doc"
|
43
|
+
RelaxDB.delete_db "relaxdb_spec_base" rescue "ok"
|
44
|
+
RelaxDB.use_db "relaxdb_spec_base"
|
45
|
+
RelaxDB.enable_view_creation
|
46
|
+
require File.dirname(__FILE__) + '/spec_models.rb'
|
47
|
+
puts "Created relaxdb_spec_base"
|
48
|
+
end
|
49
|
+
|
50
|
+
def roll_clock_forward(distance)
|
51
|
+
Time.meta_class.instance_eval do
|
52
|
+
define_method(:future_now) do
|
53
|
+
standard_now + distance
|
54
|
+
end
|
55
|
+
alias_method :standard_now, :now
|
56
|
+
alias_method :now, :future_now
|
57
|
+
begin
|
58
|
+
yield
|
59
|
+
rescue => e
|
60
|
+
raise e
|
61
|
+
ensure
|
62
|
+
alias_method :now, :standard_now
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/spec/spec_models.rb
ADDED
@@ -0,0 +1,199 @@
|
|
1
|
+
class Atom < RelaxDB::Document
|
2
|
+
end
|
3
|
+
|
4
|
+
class Initiative < RelaxDB::Document
|
5
|
+
property :x
|
6
|
+
attr_reader :foo
|
7
|
+
def initialize(data)
|
8
|
+
data[:_id] = data[:x]
|
9
|
+
super data
|
10
|
+
@foo = :bar
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Primitives < RelaxDB::Document
|
15
|
+
|
16
|
+
property :str
|
17
|
+
property :num
|
18
|
+
property :true_bool
|
19
|
+
property :false_bool
|
20
|
+
property :created_at
|
21
|
+
property :updated_at
|
22
|
+
property :empty
|
23
|
+
|
24
|
+
view_by :num
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
class PrimitivesChild < Primitives
|
29
|
+
end
|
30
|
+
|
31
|
+
class BespokeReader < RelaxDB::Document
|
32
|
+
property :val
|
33
|
+
def val; @val + 5; end
|
34
|
+
end
|
35
|
+
|
36
|
+
class BespokeWriter < RelaxDB::Document
|
37
|
+
property :val
|
38
|
+
property :tt
|
39
|
+
def val=(v); @val = v - 10; end
|
40
|
+
def tt=(t); @tt = t.is_a?(String) ? Time.parse(t) : t; end
|
41
|
+
end
|
42
|
+
|
43
|
+
class Invite < RelaxDB::Document
|
44
|
+
|
45
|
+
property :message
|
46
|
+
|
47
|
+
belongs_to :sender
|
48
|
+
belongs_to :recipient
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
class Item < RelaxDB::Document
|
53
|
+
|
54
|
+
property :name
|
55
|
+
belongs_to :user
|
56
|
+
|
57
|
+
view_by :user_id
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
class User < RelaxDB::Document
|
62
|
+
|
63
|
+
property :name, :default => "u"
|
64
|
+
property :age
|
65
|
+
|
66
|
+
has_many :items, :class => "Item"
|
67
|
+
|
68
|
+
has_many :invites_received, :class => "Invite", :known_as => :recipient
|
69
|
+
has_many :invites_sent, :class => "Invite", :known_as => :sender
|
70
|
+
|
71
|
+
view_by :name, :age
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
class Post < RelaxDB::Document
|
76
|
+
|
77
|
+
property :subject
|
78
|
+
property :content
|
79
|
+
property :created_at
|
80
|
+
property :viewed_at
|
81
|
+
|
82
|
+
view_by :content
|
83
|
+
view_by :subject
|
84
|
+
view_by :viewed_at
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
class Rating < RelaxDB::Document
|
89
|
+
|
90
|
+
property :stars, :default => 5
|
91
|
+
belongs_to :photo
|
92
|
+
|
93
|
+
view_by :stars
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
class Photo < RelaxDB::Document
|
98
|
+
|
99
|
+
property :name
|
100
|
+
|
101
|
+
has_one :rating
|
102
|
+
|
103
|
+
references_many :tags, :class => "Tag", :known_as => :photos
|
104
|
+
|
105
|
+
has_many :taggings, :class => "Tagging"
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
class Tag < RelaxDB::Document
|
110
|
+
|
111
|
+
property :name
|
112
|
+
references_many :photos, :class => "Photo", :known_as => :tags
|
113
|
+
|
114
|
+
has_many :taggings, :class => "Tagging"
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
class Tagging < RelaxDB::Document
|
119
|
+
|
120
|
+
belongs_to :photo
|
121
|
+
belongs_to :tag
|
122
|
+
property :relevance
|
123
|
+
|
124
|
+
end
|
125
|
+
|
126
|
+
class MultiWordClass < RelaxDB::Document
|
127
|
+
has_one :multi_word_child
|
128
|
+
has_many :multi_word_children, :class => "MultiWordChild"
|
129
|
+
end
|
130
|
+
|
131
|
+
class MultiWordChild < RelaxDB::Document
|
132
|
+
belongs_to :multi_word_class
|
133
|
+
end
|
134
|
+
|
135
|
+
class TwitterUser < RelaxDB::Document
|
136
|
+
|
137
|
+
property :name
|
138
|
+
references_many :followers, :class => "User", :known_as => :leaders
|
139
|
+
references_many :leaders, :class => "User", :known_as => :followers
|
140
|
+
|
141
|
+
end
|
142
|
+
|
143
|
+
class Dysfunctional < RelaxDB::Document
|
144
|
+
has_one :failure
|
145
|
+
has_many :failures, :class => "Failure"
|
146
|
+
end
|
147
|
+
|
148
|
+
class Failure < RelaxDB::Document
|
149
|
+
property :pathological, :validator => lambda { false }
|
150
|
+
belongs_to :dysfunctional
|
151
|
+
end
|
152
|
+
|
153
|
+
class Letter < RelaxDB::Document
|
154
|
+
property :letter
|
155
|
+
property :number
|
156
|
+
view_by :letter, :number
|
157
|
+
view_by :number
|
158
|
+
end
|
159
|
+
|
160
|
+
class Ancestor < RelaxDB::Document
|
161
|
+
|
162
|
+
property :x
|
163
|
+
property :y, :default => true,
|
164
|
+
:validator => lambda { |y| y },
|
165
|
+
:validation_msg => "Uh oh"
|
166
|
+
|
167
|
+
references :user
|
168
|
+
property :user_name,
|
169
|
+
:derived => [:user, lambda { |p, o| o.user.name } ]
|
170
|
+
|
171
|
+
view_by :x
|
172
|
+
end
|
173
|
+
|
174
|
+
class Descendant < Ancestor
|
175
|
+
end
|
176
|
+
|
177
|
+
class SubDescendant < Descendant
|
178
|
+
end
|
179
|
+
|
180
|
+
class RichDescendant < Descendant
|
181
|
+
property :foo
|
182
|
+
|
183
|
+
references :ukulele
|
184
|
+
property :ukulele_name,
|
185
|
+
:derived => [:ukulele, lambda { |p, o| o.ukulele.name } ]
|
186
|
+
end
|
187
|
+
|
188
|
+
module Inh
|
189
|
+
|
190
|
+
class X < RelaxDB::Document; end
|
191
|
+
|
192
|
+
class Y < X; end
|
193
|
+
class Y1 < Y; end
|
194
|
+
|
195
|
+
class Z < X; end
|
196
|
+
class Z1 < Z; end
|
197
|
+
class Z2 < Z; end
|
198
|
+
|
199
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
require File.dirname(__FILE__) + '/spec_models.rb'
|
3
|
+
|
4
|
+
describe "view_by" do
|
5
|
+
|
6
|
+
before(:all) do
|
7
|
+
RelaxDB.configure :host => "localhost", :port => 5984, :design_doc => "spec_doc"
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "view_by" do
|
11
|
+
|
12
|
+
before(:each) do
|
13
|
+
RelaxDB.delete_db "relaxdb_spec" rescue "ok"
|
14
|
+
RelaxDB.use_db "relaxdb_spec"
|
15
|
+
RelaxDB.enable_view_creation
|
16
|
+
|
17
|
+
class ViewByFoo < RelaxDB::Document
|
18
|
+
property :foo
|
19
|
+
view_by :foo, :descending => true
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should create corresponding views" do
|
25
|
+
dd = RelaxDB::DesignDocument.get "spec_doc"
|
26
|
+
dd.data["views"]["ViewByFoo_by_foo"].should be
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should create a by_ att list method" do
|
30
|
+
ViewByFoo.new(:foo => :bar).save!
|
31
|
+
res = ViewByFoo.by_foo
|
32
|
+
res.first.foo.should == "bar"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should create a paginate_by_ att list method" do
|
36
|
+
ViewByFoo.new(:foo => :bar).save!
|
37
|
+
res = ViewByFoo.paginate_by_foo :page_params => {}, :startkey => {}, :endkey => nil
|
38
|
+
res.first.foo.should == "bar"
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should apply query defaults to by_" do
|
42
|
+
ViewByFoo.new(:foo => "a").save!
|
43
|
+
ViewByFoo.new(:foo => "b").save!
|
44
|
+
|
45
|
+
ViewByFoo.by_foo.map{ |o| o.foo }.should == ["b", "a"]
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should allow a single arg to be passed to by_" do
|
49
|
+
vbf = ViewByFoo.new(:foo => "a").save!
|
50
|
+
ViewByFoo.by_foo("a").should == vbf
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should apply query defaults to paginate_by_" do
|
54
|
+
ViewByFoo.new(:foo => "a").save!
|
55
|
+
ViewByFoo.new(:foo => "b").save!
|
56
|
+
|
57
|
+
res = ViewByFoo.paginate_by_foo :page_params => {}, :startkey => {}, :endkey => nil
|
58
|
+
res.map{ |o| o.foo }.should == ["b", "a"]
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should allow query defaults to be overridden for paginate_by_" do
|
62
|
+
ViewByFoo.new(:foo => :bar).save!
|
63
|
+
res = ViewByFoo.paginate_by_foo :page_params => {}, :startkey => nil, :endkey => {}, :descending => false
|
64
|
+
res.first.foo.should == "bar"
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should allow query defaults to be overridden for by_" do
|
68
|
+
ViewByFoo.new(:foo => "a").save!
|
69
|
+
ViewByFoo.new(:foo => "b").save!
|
70
|
+
|
71
|
+
ViewByFoo.by_foo(:descending => false).map{ |o| o.foo }.should == ["a", "b"]
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
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
|
data/spec/view_spec.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
require File.dirname(__FILE__) + '/spec_models.rb'
|
3
|
+
|
4
|
+
describe RelaxDB::View do
|
5
|
+
|
6
|
+
describe "exists" do
|
7
|
+
|
8
|
+
before(:each) do
|
9
|
+
create_test_db
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should return nil if a view doesnt exist" do
|
13
|
+
RelaxDB::ViewCreator.all([Atom]).should_not be_exists
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should return the view if it exits" do
|
17
|
+
RelaxDB::ViewCreator.all([Atom]).save
|
18
|
+
RelaxDB::ViewCreator.all([Atom]).should be_exists
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|