couchmodel 0.1.0.beta2

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.
Files changed (45) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +156 -0
  3. data/Rakefile +20 -0
  4. data/lib/core_extension/array.rb +14 -0
  5. data/lib/core_extension/string.rb +12 -0
  6. data/lib/couch_model/active_model.rb +86 -0
  7. data/lib/couch_model/base/accessor.rb +39 -0
  8. data/lib/couch_model/base/association.rb +63 -0
  9. data/lib/couch_model/base/finder.rb +28 -0
  10. data/lib/couch_model/base/setup.rb +88 -0
  11. data/lib/couch_model/base.rb +117 -0
  12. data/lib/couch_model/collection.rb +84 -0
  13. data/lib/couch_model/configuration.rb +68 -0
  14. data/lib/couch_model/database.rb +64 -0
  15. data/lib/couch_model/design.rb +92 -0
  16. data/lib/couch_model/server.rb +44 -0
  17. data/lib/couch_model/transport.rb +68 -0
  18. data/lib/couch_model/view.rb +52 -0
  19. data/lib/couch_model.rb +15 -0
  20. data/spec/fake_transport.yml +202 -0
  21. data/spec/fake_transport_helper.rb +27 -0
  22. data/spec/integration/basic_spec.rb +125 -0
  23. data/spec/integration/design/membership.design +5 -0
  24. data/spec/integration/design/user.design +2 -0
  25. data/spec/lib/core_extension/array_spec.rb +24 -0
  26. data/spec/lib/core_extension/string_spec.rb +22 -0
  27. data/spec/lib/couch_model/active_model_spec.rb +228 -0
  28. data/spec/lib/couch_model/base_spec.rb +169 -0
  29. data/spec/lib/couch_model/collection_spec.rb +100 -0
  30. data/spec/lib/couch_model/configuration_spec.rb +117 -0
  31. data/spec/lib/couch_model/core/accessor_spec.rb +59 -0
  32. data/spec/lib/couch_model/core/association_spec.rb +114 -0
  33. data/spec/lib/couch_model/core/finder_spec.rb +24 -0
  34. data/spec/lib/couch_model/core/setup_spec.rb +88 -0
  35. data/spec/lib/couch_model/database_spec.rb +165 -0
  36. data/spec/lib/couch_model/design/association_test_model_one.design +5 -0
  37. data/spec/lib/couch_model/design/base_test_model.design +10 -0
  38. data/spec/lib/couch_model/design/setup_test_model.design +10 -0
  39. data/spec/lib/couch_model/design_spec.rb +144 -0
  40. data/spec/lib/couch_model/server_spec.rb +64 -0
  41. data/spec/lib/couch_model/transport_spec.rb +44 -0
  42. data/spec/lib/couch_model/view_spec.rb +166 -0
  43. data/spec/lib/couch_model_spec.rb +3 -0
  44. data/spec/spec_helper.rb +27 -0
  45. metadata +128 -0
@@ -0,0 +1,144 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
2
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "lib", "couch_model", "design"))
3
+
4
+ CouchModel::Configuration.design_directory = File.join File.dirname(__FILE__), "design"
5
+
6
+ describe CouchModel::Design do
7
+
8
+ before :each do
9
+ @model_class = Object
10
+ @model_class.stub!(:to_s).and_return("BaseTestModel")
11
+ @database = CouchModel::Database.new :name => "test"
12
+ @design = CouchModel::Design.new @database, @model_class, :language => "another_language"
13
+ end
14
+
15
+ describe "initialize" do
16
+
17
+ it "should set the database" do
18
+ @design.database.should == @database
19
+ end
20
+
21
+ it "should set the model class" do
22
+ @design.model_class.should == @model_class
23
+ end
24
+
25
+ it "should set the attributes" do
26
+ @design.language.should == "another_language"
27
+ end
28
+
29
+ end
30
+
31
+ describe "filename" do
32
+
33
+ it "should return the name of the expected file" do
34
+ @design.filename.should == File.join(CouchModel::Configuration.design_directory, "base_test_model.design")
35
+ end
36
+
37
+ end
38
+
39
+ describe "load_file" do
40
+
41
+ def do_load
42
+ @design.load_file
43
+ end
44
+
45
+ context "file does exists" do
46
+
47
+ before :each do
48
+ File.stub!(:exists?).and_return(true)
49
+ end
50
+
51
+ it "should set the attributes" do
52
+ do_load
53
+ @design.id.should == "test_design"
54
+ @design.language.should == "javascript"
55
+ @design.views.should_not be_nil
56
+ end
57
+
58
+ it "should return true" do
59
+ do_load.should be_true
60
+ end
61
+
62
+ end
63
+
64
+ context "file doesn't exists" do
65
+
66
+ before :each do
67
+ File.stub!(:exists?).and_return(false)
68
+ end
69
+
70
+ it "should return false" do
71
+ do_load.should be_false
72
+ end
73
+
74
+ end
75
+
76
+ end
77
+
78
+ describe "views=" do
79
+
80
+ before :each do
81
+ @view_hash = { "view_1" => { :map => "map function", :reduce => "reduce function" } }
82
+ end
83
+
84
+ it "should convert the given array of hashes into an array of views" do
85
+ @design.views = @view_hash
86
+ @design.views.first.should be_instance_of(CouchModel::View)
87
+ @design.views.first.name.should == "view_1"
88
+ @design.views.first.map.should == "map function"
89
+ @design.views.first.reduce.should == "reduce function"
90
+ end
91
+
92
+ end
93
+
94
+ describe "generate_view" do
95
+
96
+ def do_generate
97
+ @design.generate_view "test", :keys => [ :test ]
98
+ end
99
+
100
+ it "should return the view" do
101
+ do_generate.should be_instance_of(CouchModel::View)
102
+ end
103
+
104
+ it "should add a generated view" do
105
+ lambda do
106
+ do_generate
107
+ end.should change(@design.views, :size).by(1)
108
+ end
109
+
110
+ end
111
+
112
+ describe "to_hash" do
113
+
114
+ it "should return a hash with all the design data" do
115
+ @design.to_hash.should == {
116
+ "_id" => "_design/test_design",
117
+ "language" => "another_language",
118
+ "views" => { "test_view" => {"map" => "function(document) { };", "reduce" => "function(key, values, rereduce) { };" } }
119
+ }
120
+ end
121
+
122
+ end
123
+
124
+ describe "exists?" do
125
+
126
+ it "should return true is the design document exists" do
127
+ @design.exists?.should be_true
128
+ end
129
+
130
+ end
131
+
132
+ describe "push" do
133
+
134
+ def do_push
135
+ @design.push
136
+ end
137
+
138
+ it "should push the design" do
139
+ do_push.should be_true
140
+ end
141
+
142
+ end
143
+
144
+ end
@@ -0,0 +1,64 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
2
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "lib", "couch_model", "server"))
3
+
4
+ describe CouchModel::Server do
5
+
6
+ before :each do
7
+ @server = CouchModel::Server.new
8
+ end
9
+
10
+ describe "==" do
11
+
12
+ it "should be true when comparing two equal servers" do
13
+ other = CouchModel::Server.new
14
+ @server.should == other
15
+ end
16
+
17
+ it "should be false when comparing two different servers" do
18
+ other = CouchModel::Server.new :host => "other"
19
+ @server.should_not == other
20
+ end
21
+
22
+ end
23
+
24
+ describe "informations" do
25
+
26
+ it "should return server informations" do
27
+ informations = @server.informations
28
+ informations.should == {
29
+ "couchdb" => "Welcome",
30
+ "version" => "0.10.0"
31
+ }
32
+ end
33
+
34
+ end
35
+
36
+ describe "statistics" do
37
+
38
+ it "should return server statistics" do
39
+ statistics = @server.statistics
40
+ statistics.should have_key("httpd_status_codes")
41
+ statistics.should have_key("httpd_request_methods")
42
+ end
43
+
44
+ end
45
+
46
+ describe "database_names" do
47
+
48
+ it "should return the names of all databases" do
49
+ database_names = @server.database_names
50
+ database_names.should == [ "development", "test" ]
51
+ end
52
+
53
+ end
54
+
55
+ describe "uuids" do
56
+
57
+ it "should return a given number of uuids" do
58
+ uuids = @server.uuids 3
59
+ uuids.size.should == 3
60
+ end
61
+
62
+ end
63
+
64
+ end
@@ -0,0 +1,44 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
2
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "lib", "couch_model", "transport"))
3
+
4
+ describe CouchModel::Transport do
5
+
6
+ describe "request" do
7
+
8
+ use_real_transport!
9
+
10
+ before :each do
11
+ @http_method = :get
12
+ @url = "http://localhost:5984/"
13
+ @options = { }
14
+
15
+ @request = Net::HTTP::Get.new "/"
16
+ @response = Object.new
17
+ @response.stub!(:code).and_return("200")
18
+ @response.stub!(:body).and_return("{\"couchdb\":\"Welcome\",\"version\":\"0.10.0\"}")
19
+ Net::HTTP.stub!(:start).and_return(@response)
20
+ end
21
+
22
+ def do_request(options = { })
23
+ CouchModel::Transport.request @http_method, @url, @options.merge(options)
24
+ end
25
+
26
+ it "should initialize the correct request object" do
27
+ Net::HTTP::Get.should_receive(:new).with("/").and_return(@request)
28
+ do_request
29
+ end
30
+
31
+ it "should perform the request" do
32
+ Net::HTTP.should_receive(:start).and_return(@response)
33
+ do_request
34
+ end
35
+
36
+ it "should raise UnexpectedStatusCodeError if an unexpected status id returned" do
37
+ lambda do
38
+ do_request :expected_status_code => 201
39
+ end.should raise_error(CouchModel::Transport::UnexpectedStatusCodeError)
40
+ end
41
+
42
+ end
43
+
44
+ end
@@ -0,0 +1,166 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
2
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "lib", "couch_model", "database"))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "lib", "couch_model", "design"))
4
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "lib", "couch_model", "view"))
5
+
6
+ CouchModel::Configuration.design_directory = File.join File.dirname(__FILE__), "design"
7
+
8
+ describe CouchModel::View do
9
+
10
+ before :each do
11
+ @model_class = Object
12
+ @model_class.stub!(:to_s).and_return("TestModel")
13
+ @database = CouchModel::Database.new :name => "test"
14
+ @design = CouchModel::Design.new @database, @model_class
15
+ @view = CouchModel::View.new @design,
16
+ :name => "view name",
17
+ :map => "map function",
18
+ :reduce => "reduce function"
19
+ end
20
+
21
+ describe "initialize" do
22
+
23
+ before :each do
24
+ @options = { :name => "view name" }
25
+ end
26
+
27
+ def do_initialize(options = { })
28
+ @view = CouchModel::View.new @design, @options.merge(options)
29
+ end
30
+
31
+ it "should set the name" do
32
+ do_initialize
33
+ @view.name.should == "view name"
34
+ end
35
+
36
+ context "using function generator" do
37
+
38
+ before :each do
39
+ @options.merge! :keys => [ "test key" ]
40
+ end
41
+
42
+ it "should generate the map function" do
43
+ do_initialize
44
+ @view.map.should ==
45
+ """function(document) {
46
+ if (document['#{CouchModel::Configuration::CLASS_KEY}'] == 'TestModel' && document['test key']) {
47
+ emit(document['test key'], null);
48
+ }
49
+ }
50
+ """
51
+ end
52
+
53
+ it "should set the reduce function to nil" do
54
+ do_initialize
55
+ @view.reduce.should be_nil
56
+ end
57
+
58
+ end
59
+
60
+ context "using explizit functions" do
61
+
62
+ before :each do
63
+ @options.merge! :map => "map function",
64
+ :reduce => "reduce function"
65
+ end
66
+
67
+ it "should set the map function" do
68
+ do_initialize
69
+ @view.map.should == "map function"
70
+ end
71
+
72
+ it "should set the reduce function" do
73
+ do_initialize
74
+ @view.reduce.should == "reduce function"
75
+ end
76
+
77
+ end
78
+
79
+ end
80
+
81
+ describe "collection" do
82
+
83
+ it "should return a collection" do
84
+ @view.collection.should be_instance_of(CouchModel::Collection)
85
+ end
86
+
87
+ it "should initialize the collection with the view url" do
88
+ @view.collection.url.should == @view.url
89
+ end
90
+
91
+ it "should pass the options to the collection" do
92
+ @view.collection(:test => "test").options.should == { :test => "test" }
93
+ end
94
+
95
+ end
96
+
97
+ describe "to_hash" do
98
+
99
+ it "should return a hash with all view data" do
100
+ @view.to_hash.should == {
101
+ "view name" => {
102
+ "map" => "map function",
103
+ "reduce" => "reduce function"
104
+ }
105
+ }
106
+ end
107
+
108
+ end
109
+
110
+ describe "generate_functions" do
111
+
112
+ before :each do
113
+ @options = { }
114
+ end
115
+
116
+ def do_generate
117
+ @view.generate_functions @options
118
+ end
119
+
120
+ describe "without any keys given" do
121
+
122
+ it "should set the map function" do
123
+ do_generate
124
+ @view.map.should ==
125
+ """function(document) {
126
+ if (document['#{CouchModel::Configuration::CLASS_KEY}'] == 'TestModel') {
127
+ emit(document['_id'], null);
128
+ }
129
+ }
130
+ """
131
+ end
132
+
133
+ it "should set the reduce function to nil" do
134
+ do_generate
135
+ @view.reduce.should be_nil
136
+ end
137
+
138
+ end
139
+
140
+ describe "with keys given" do
141
+
142
+ before :each do
143
+ @options.merge! :keys => [ :foo, :bar ]
144
+ end
145
+
146
+ it "should set the map function" do
147
+ do_generate
148
+ @view.map.should ==
149
+ """function(document) {
150
+ if (document['#{CouchModel::Configuration::CLASS_KEY}'] == 'TestModel' && document['foo'] && document['bar']) {
151
+ emit([ document['foo'], document['bar'] ], null);
152
+ }
153
+ }
154
+ """
155
+ end
156
+
157
+ it "should set the reduce function to nil" do
158
+ do_generate
159
+ @view.reduce.should be_nil
160
+ end
161
+
162
+ end
163
+
164
+ end
165
+
166
+ end
@@ -0,0 +1,3 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
2
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "lib", "couch_model"))
3
+
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+ gem 'rspec'
3
+ require 'spec'
4
+
5
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "lib", "couch_model", "configuration"))
6
+ require File.join(File.dirname(__FILE__), "fake_transport_helper")
7
+
8
+ CouchModel::Configuration.fake_transport = true
9
+ Spec::Runner.configure do |configuration|
10
+ configuration.before :each do
11
+ CouchModel::Transport.fake! if CouchModel::Configuration.fake_transport
12
+ end
13
+ end
14
+
15
+ def use_real_transport!
16
+ class_eval do
17
+
18
+ before :all do
19
+ CouchModel::Configuration.fake_transport = false
20
+ end
21
+
22
+ after :all do
23
+ CouchModel::Configuration.fake_transport = true
24
+ end
25
+
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: couchmodel
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: true
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ - beta2
10
+ version: 0.1.0.beta2
11
+ platform: ruby
12
+ authors:
13
+ - "Philipp Br\xC3\xBCll"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-03-02 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: CouchModel provides an interface to easly handle CouchDB documents. It also comes with a ActiveModel implementation to integrate into an Rails 3 application.
23
+ email: b.phifty@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.rdoc
30
+ files:
31
+ - README.rdoc
32
+ - LICENSE
33
+ - Rakefile
34
+ - lib/couch_model/configuration.rb
35
+ - lib/couch_model/base/setup.rb
36
+ - lib/couch_model/base/accessor.rb
37
+ - lib/couch_model/base/finder.rb
38
+ - lib/couch_model/base/association.rb
39
+ - lib/couch_model/active_model.rb
40
+ - lib/couch_model/view.rb
41
+ - lib/couch_model/collection.rb
42
+ - lib/couch_model/server.rb
43
+ - lib/couch_model/design.rb
44
+ - lib/couch_model/transport.rb
45
+ - lib/couch_model/base.rb
46
+ - lib/couch_model/database.rb
47
+ - lib/couch_model.rb
48
+ - lib/core_extension/string.rb
49
+ - lib/core_extension/array.rb
50
+ - spec/spec_helper.rb
51
+ - spec/lib/couch_model/view_spec.rb
52
+ - spec/lib/couch_model/server_spec.rb
53
+ - spec/lib/couch_model/core/association_spec.rb
54
+ - spec/lib/couch_model/core/finder_spec.rb
55
+ - spec/lib/couch_model/core/accessor_spec.rb
56
+ - spec/lib/couch_model/core/setup_spec.rb
57
+ - spec/lib/couch_model/design_spec.rb
58
+ - spec/lib/couch_model/transport_spec.rb
59
+ - spec/lib/couch_model/active_model_spec.rb
60
+ - spec/lib/couch_model/collection_spec.rb
61
+ - spec/lib/couch_model/design/association_test_model_one.design
62
+ - spec/lib/couch_model/design/base_test_model.design
63
+ - spec/lib/couch_model/design/setup_test_model.design
64
+ - spec/lib/couch_model/configuration_spec.rb
65
+ - spec/lib/couch_model/database_spec.rb
66
+ - spec/lib/couch_model/base_spec.rb
67
+ - spec/lib/couch_model_spec.rb
68
+ - spec/lib/core_extension/string_spec.rb
69
+ - spec/lib/core_extension/array_spec.rb
70
+ - spec/fake_transport_helper.rb
71
+ - spec/integration/design/membership.design
72
+ - spec/integration/design/user.design
73
+ - spec/integration/basic_spec.rb
74
+ - spec/fake_transport.yml
75
+ has_rdoc: true
76
+ homepage: http://github.com/phifty/couchmodel
77
+ licenses: []
78
+
79
+ post_install_message:
80
+ rdoc_options:
81
+ - --line-numbers
82
+ - --main
83
+ - README.rdoc
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ segments:
91
+ - 1
92
+ - 9
93
+ - 1
94
+ version: 1.9.1
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">"
98
+ - !ruby/object:Gem::Version
99
+ segments:
100
+ - 1
101
+ - 3
102
+ - 1
103
+ version: 1.3.1
104
+ requirements: []
105
+
106
+ rubyforge_project:
107
+ rubygems_version: 1.3.6
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: CouchModel provides an interface to easly handle CouchDB documents.
111
+ test_files:
112
+ - spec/lib/couch_model/view_spec.rb
113
+ - spec/lib/couch_model/server_spec.rb
114
+ - spec/lib/couch_model/core/association_spec.rb
115
+ - spec/lib/couch_model/core/finder_spec.rb
116
+ - spec/lib/couch_model/core/accessor_spec.rb
117
+ - spec/lib/couch_model/core/setup_spec.rb
118
+ - spec/lib/couch_model/design_spec.rb
119
+ - spec/lib/couch_model/transport_spec.rb
120
+ - spec/lib/couch_model/active_model_spec.rb
121
+ - spec/lib/couch_model/collection_spec.rb
122
+ - spec/lib/couch_model/configuration_spec.rb
123
+ - spec/lib/couch_model/database_spec.rb
124
+ - spec/lib/couch_model/base_spec.rb
125
+ - spec/lib/couch_model_spec.rb
126
+ - spec/lib/core_extension/string_spec.rb
127
+ - spec/lib/core_extension/array_spec.rb
128
+ - spec/integration/basic_spec.rb