couchrest 0.12.2
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 +176 -0
- data/README.md +69 -0
- data/Rakefile +103 -0
- data/THANKS.md +18 -0
- data/bin/couchdir +20 -0
- data/examples/model/example.rb +138 -0
- data/examples/word_count/markov +38 -0
- data/examples/word_count/views/books/chunked-map.js +3 -0
- data/examples/word_count/views/books/united-map.js +1 -0
- data/examples/word_count/views/markov/chain-map.js +6 -0
- data/examples/word_count/views/markov/chain-reduce.js +7 -0
- data/examples/word_count/views/word_count/count-map.js +6 -0
- data/examples/word_count/views/word_count/count-reduce.js +3 -0
- data/examples/word_count/word_count.rb +67 -0
- data/examples/word_count/word_count_query.rb +39 -0
- data/lib/couchrest/commands/generate.rb +71 -0
- data/lib/couchrest/commands/push.rb +103 -0
- data/lib/couchrest/core/database.rb +239 -0
- data/lib/couchrest/core/design.rb +89 -0
- data/lib/couchrest/core/document.rb +63 -0
- data/lib/couchrest/core/model.rb +607 -0
- data/lib/couchrest/core/server.rb +51 -0
- data/lib/couchrest/core/view.rb +4 -0
- data/lib/couchrest/helper/pager.rb +103 -0
- data/lib/couchrest/helper/streamer.rb +44 -0
- data/lib/couchrest/monkeypatches.rb +24 -0
- data/lib/couchrest.rb +140 -0
- data/spec/couchrest/core/couchrest_spec.rb +201 -0
- data/spec/couchrest/core/database_spec.rb +630 -0
- data/spec/couchrest/core/design_spec.rb +131 -0
- data/spec/couchrest/core/document_spec.rb +130 -0
- data/spec/couchrest/core/model_spec.rb +855 -0
- data/spec/couchrest/helpers/pager_spec.rb +122 -0
- data/spec/couchrest/helpers/streamer_spec.rb +23 -0
- data/spec/fixtures/attachments/README +3 -0
- data/spec/fixtures/attachments/couchdb.png +0 -0
- data/spec/fixtures/attachments/test.html +11 -0
- data/spec/fixtures/views/lib.js +3 -0
- data/spec/fixtures/views/test_view/lib.js +3 -0
- data/spec/fixtures/views/test_view/only-map.js +4 -0
- data/spec/fixtures/views/test_view/test-map.js +3 -0
- data/spec/fixtures/views/test_view/test-reduce.js +3 -0
- data/spec/spec.opts +6 -0
- data/spec/spec_helper.rb +20 -0
- data/utils/remap.rb +27 -0
- data/utils/subset.rb +30 -0
- metadata +156 -0
@@ -0,0 +1,131 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../spec_helper'
|
2
|
+
|
3
|
+
describe CouchRest::Design do
|
4
|
+
|
5
|
+
describe "defining a view" do
|
6
|
+
it "should add a view to the design doc" do
|
7
|
+
@des = CouchRest::Design.new
|
8
|
+
method = @des.view_by :name
|
9
|
+
method.should == "by_name"
|
10
|
+
@des["views"]["by_name"].should_not be_nil
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "with an unsaved view" do
|
15
|
+
before(:each) do
|
16
|
+
@des = CouchRest::Design.new
|
17
|
+
method = @des.view_by :name
|
18
|
+
end
|
19
|
+
it "should accept a name" do
|
20
|
+
@des.name = "mytest"
|
21
|
+
@des.name.should == "mytest"
|
22
|
+
end
|
23
|
+
it "should not save on view definition" do
|
24
|
+
@des.rev.should be_nil
|
25
|
+
end
|
26
|
+
it "should freak out on view access" do
|
27
|
+
lambda{@des.view :by_name}.should raise_error
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "saving" do
|
32
|
+
before(:each) do
|
33
|
+
@des = CouchRest::Design.new
|
34
|
+
method = @des.view_by :name
|
35
|
+
@des.database = reset_test_db!
|
36
|
+
end
|
37
|
+
it "should fail without a name" do
|
38
|
+
lambda{@des.save}.should raise_error(ArgumentError)
|
39
|
+
end
|
40
|
+
it "should work with a name" do
|
41
|
+
@des.name = "myview"
|
42
|
+
@des.save
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "when it's saved" do
|
47
|
+
before(:each) do
|
48
|
+
@db = reset_test_db!
|
49
|
+
@db.bulk_save([{"name" => "x"},{"name" => "y"}])
|
50
|
+
@des = CouchRest::Design.new
|
51
|
+
@des.database = @db
|
52
|
+
method = @des.view_by :name
|
53
|
+
end
|
54
|
+
it "should by queryable when it's saved" do
|
55
|
+
@des.name = "mydesign"
|
56
|
+
@des.save
|
57
|
+
res = @des.view :by_name
|
58
|
+
res["rows"][0]["key"].should == "x"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "from a saved document" do
|
63
|
+
before(:each) do
|
64
|
+
@db = reset_test_db!
|
65
|
+
@db.save({
|
66
|
+
"_id" => "_design/test",
|
67
|
+
"views" => {
|
68
|
+
"by_name" => {
|
69
|
+
"map" => "function(doc){if (doc.name) emit(doc.name, null)}"
|
70
|
+
}
|
71
|
+
}
|
72
|
+
})
|
73
|
+
@db.bulk_save([{"name" => "a"},{"name" => "b"}])
|
74
|
+
@des = @db.get "_design/test"
|
75
|
+
end
|
76
|
+
it "should be a Design" do
|
77
|
+
@des.should be_an_instance_of(CouchRest::Design)
|
78
|
+
end
|
79
|
+
it "should have a modifiable name" do
|
80
|
+
@des.name.should == "test"
|
81
|
+
@des.name = "supertest"
|
82
|
+
@des.id.should == "_design/supertest"
|
83
|
+
end
|
84
|
+
it "should by queryable" do
|
85
|
+
res = @des.view :by_name
|
86
|
+
res["rows"][0]["key"].should == "a"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe "a view with default options" do
|
91
|
+
before(:all) do
|
92
|
+
@db = reset_test_db!
|
93
|
+
@des = CouchRest::Design.new
|
94
|
+
@des.name = "test"
|
95
|
+
method = @des.view_by :name, :descending => true
|
96
|
+
@des.database = @db
|
97
|
+
@des.save
|
98
|
+
@db.bulk_save([{"name" => "a"},{"name" => "z"}])
|
99
|
+
end
|
100
|
+
it "should save them" do
|
101
|
+
@d2 = @db.get(@des.id)
|
102
|
+
@d2["views"]["by_name"]["couchrest-defaults"].should == {"descending"=>true}
|
103
|
+
end
|
104
|
+
it "should use them" do
|
105
|
+
res = @des.view :by_name
|
106
|
+
res["rows"].first["key"].should == "z"
|
107
|
+
end
|
108
|
+
it "should override them" do
|
109
|
+
res = @des.view :by_name, :descending => false
|
110
|
+
res["rows"].first["key"].should == "a"
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
describe "a view with multiple keys" do
|
115
|
+
before(:all) do
|
116
|
+
@db = reset_test_db!
|
117
|
+
@des = CouchRest::Design.new
|
118
|
+
@des.name = "test"
|
119
|
+
method = @des.view_by :name, :age
|
120
|
+
@des.database = @db
|
121
|
+
@des.save
|
122
|
+
@db.bulk_save([{"name" => "a", "age" => 2},
|
123
|
+
{"name" => "a", "age" => 4},{"name" => "z", "age" => 9}])
|
124
|
+
end
|
125
|
+
it "should work" do
|
126
|
+
res = @des.view :by_name_and_age
|
127
|
+
res["rows"].first["key"].should == ["a",2]
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
@@ -0,0 +1,130 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../spec_helper'
|
2
|
+
|
3
|
+
describe CouchRest::Document, "[]=" do
|
4
|
+
before(:each) do
|
5
|
+
@doc = CouchRest::Document.new
|
6
|
+
end
|
7
|
+
it "should work" do
|
8
|
+
@doc["enamel"].should == nil
|
9
|
+
@doc["enamel"] = "Strong"
|
10
|
+
@doc["enamel"].should == "Strong"
|
11
|
+
end
|
12
|
+
it "[]= should convert to string" do
|
13
|
+
@doc["enamel"].should == nil
|
14
|
+
@doc[:enamel] = "Strong"
|
15
|
+
@doc["enamel"].should == "Strong"
|
16
|
+
end
|
17
|
+
it "should read as a string" do
|
18
|
+
@doc[:enamel] = "Strong"
|
19
|
+
@doc[:enamel].should == "Strong"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe CouchRest::Document, "new" do
|
24
|
+
before(:each) do
|
25
|
+
@doc = CouchRest::Document.new("key" => [1,2,3], :more => "values")
|
26
|
+
end
|
27
|
+
it "should create itself from a Hash" do
|
28
|
+
@doc["key"].should == [1,2,3]
|
29
|
+
@doc["more"].should == "values"
|
30
|
+
end
|
31
|
+
it "should not have rev and id" do
|
32
|
+
@doc.rev.should be_nil
|
33
|
+
@doc.id.should be_nil
|
34
|
+
end
|
35
|
+
it "should freak out when saving without a database" do
|
36
|
+
lambda{@doc.save}.should raise_error(ArgumentError)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# move to database spec
|
41
|
+
describe CouchRest::Document, "saving using a database" do
|
42
|
+
before(:all) do
|
43
|
+
@doc = CouchRest::Document.new("key" => [1,2,3], :more => "values")
|
44
|
+
@db = reset_test_db!
|
45
|
+
@resp = @db.save(@doc)
|
46
|
+
end
|
47
|
+
it "should apply the database" do
|
48
|
+
@doc.database.should == @db
|
49
|
+
end
|
50
|
+
it "should get id and rev" do
|
51
|
+
@doc.id.should == @resp["id"]
|
52
|
+
@doc.rev.should == @resp["rev"]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe CouchRest::Document, "bulk saving" do
|
57
|
+
before :all do
|
58
|
+
@db = reset_test_db!
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should use the document bulk save cache" do
|
62
|
+
doc = CouchRest::Document.new({"_id" => "bulkdoc", "val" => 3})
|
63
|
+
doc.database = @db
|
64
|
+
doc.save(true)
|
65
|
+
lambda { doc.database.get(doc["_id"]) }.should raise_error(RestClient::ResourceNotFound)
|
66
|
+
doc.database.bulk_save
|
67
|
+
doc.database.get(doc["_id"])["val"].should == doc["val"]
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "getting from a database" do
|
72
|
+
before(:all) do
|
73
|
+
@db = reset_test_db!
|
74
|
+
@resp = @db.save({
|
75
|
+
"key" => "value"
|
76
|
+
})
|
77
|
+
@doc = @db.get @resp['id']
|
78
|
+
end
|
79
|
+
it "should return a document" do
|
80
|
+
@doc.should be_an_instance_of(CouchRest::Document)
|
81
|
+
end
|
82
|
+
it "should have a database" do
|
83
|
+
@doc.database.should == @db
|
84
|
+
end
|
85
|
+
it "should be saveable and resavable" do
|
86
|
+
@doc["more"] = "keys"
|
87
|
+
@doc.save
|
88
|
+
@db.get(@resp['id'])["more"].should == "keys"
|
89
|
+
@doc["more"] = "these keys"
|
90
|
+
@doc.save
|
91
|
+
@db.get(@resp['id'])["more"].should == "these keys"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe "destroying a document from a db" do
|
96
|
+
before(:all) do
|
97
|
+
@db = reset_test_db!
|
98
|
+
@resp = @db.save({
|
99
|
+
"key" => "value"
|
100
|
+
})
|
101
|
+
@doc = @db.get @resp['id']
|
102
|
+
end
|
103
|
+
it "should make it disappear" do
|
104
|
+
@doc.destroy
|
105
|
+
lambda{@db.get @resp['id']}.should raise_error
|
106
|
+
end
|
107
|
+
it "should error when there's no db" do
|
108
|
+
@doc = CouchRest::Document.new("key" => [1,2,3], :more => "values")
|
109
|
+
lambda{@doc.destroy}.should raise_error(ArgumentError)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
describe "destroying a document from a db using bulk save" do
|
115
|
+
before(:all) do
|
116
|
+
@db = reset_test_db!
|
117
|
+
@resp = @db.save({
|
118
|
+
"key" => "value"
|
119
|
+
})
|
120
|
+
@doc = @db.get @resp['id']
|
121
|
+
end
|
122
|
+
it "should defer actual deletion" do
|
123
|
+
@doc.destroy(true)
|
124
|
+
@doc['_id'].should == nil
|
125
|
+
@doc['_rev'].should == nil
|
126
|
+
lambda{@db.get @resp['id']}.should_not raise_error
|
127
|
+
@db.bulk_save
|
128
|
+
lambda{@db.get @resp['id']}.should raise_error
|
129
|
+
end
|
130
|
+
end
|