couchrest_model 1.1.0.beta4 → 1.1.0.beta5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,148 @@
1
+ # encoding: utf-8
2
+ require File.expand_path('../../spec_helper', __FILE__)
3
+
4
+ describe CouchRest::Model::Base do
5
+
6
+ before do
7
+ @class = Class.new(CouchRest::Model::Base)
8
+ end
9
+
10
+ describe "instance methods" do
11
+ before :each do
12
+ @obj = @class.new
13
+ end
14
+
15
+ describe "#database" do
16
+ it "should respond to" do
17
+ @obj.should respond_to(:database)
18
+ end
19
+ it "should provided class's database" do
20
+ @obj.class.should_receive :database
21
+ @obj.database
22
+ end
23
+ end
24
+
25
+ describe "#server" do
26
+ it "should respond to method" do
27
+ @obj.should respond_to(:server)
28
+ end
29
+ it "should return class's server" do
30
+ @obj.class.should_receive :server
31
+ @obj.server
32
+ end
33
+ end
34
+ end
35
+
36
+ describe "default configuration" do
37
+
38
+ it "should provide environment" do
39
+ @class.environment.should eql(:development)
40
+ end
41
+ it "should provide connection config file" do
42
+ @class.connection_config_file.should eql(File.join(Dir.pwd, 'config', 'couchdb.yml'))
43
+ end
44
+ it "should provided simple connection details" do
45
+ @class.connection[:prefix].should eql('couchrest')
46
+ end
47
+
48
+ end
49
+
50
+ describe "class methods" do
51
+
52
+ describe ".use_database" do
53
+ it "should respond to" do
54
+ @class.should respond_to(:use_database)
55
+ end
56
+ end
57
+
58
+ describe ".database" do
59
+ it "should respond to" do
60
+ @class.should respond_to(:database)
61
+ end
62
+ it "should provide a database object" do
63
+ @class.database.should be_a(CouchRest::Database)
64
+ end
65
+ it "should provide a database with default name" do
66
+
67
+ end
68
+
69
+ end
70
+
71
+ describe ".server" do
72
+ it "should respond to" do
73
+ @class.should respond_to(:server)
74
+ end
75
+ it "should provide a server object" do
76
+ @class.server.should be_a(CouchRest::Server)
77
+ end
78
+ it "should provide a server with default config" do
79
+ @class.server.uri.should eql("http://localhost:5984")
80
+ end
81
+ it "should allow the configuration to be overwritten" do
82
+ @class.connection = {
83
+ :protocol => "https",
84
+ :host => "127.0.0.1",
85
+ :port => '5985',
86
+ :prefix => 'sample',
87
+ :suffix => 'test',
88
+ :username => 'foo',
89
+ :password => 'bar'
90
+ }
91
+ @class.server.uri.should eql("https://foo:bar@127.0.0.1:5985")
92
+ end
93
+
94
+ end
95
+
96
+ describe ".prepare_database" do
97
+
98
+ it "should respond to" do
99
+ @class.should respond_to(:prepare_database)
100
+ end
101
+
102
+ it "should join the database name correctly" do
103
+ @class.connection[:suffix] = 'db'
104
+ db = @class.prepare_database('test')
105
+ db.name.should eql('couchrest_test_db')
106
+ end
107
+
108
+ it "should ignore nil values in database name" do
109
+ @class.connection[:suffix] = nil
110
+ db = @class.prepare_database('test')
111
+ db.name.should eql('couchrest_test')
112
+ end
113
+ end
114
+
115
+ describe "protected methods" do
116
+
117
+ describe ".connection_configuration" do
118
+ it "should provide main config by default" do
119
+ @class.send(:connection_configuration).should eql(@class.connection)
120
+ end
121
+ it "should load file if available" do
122
+ @class.connection_config_file = File.join(FIXTURE_PATH, 'config', 'couchdb.yml')
123
+ hash = @class.send(:connection_configuration)
124
+ hash[:protocol].should eql('https')
125
+ hash[:host].should eql('sample.cloudant.com')
126
+ hash[:join].should eql('_')
127
+ end
128
+ end
129
+
130
+ describe ".load_connection_config_file" do
131
+ it "should provide an empty hash if config not found" do
132
+ @class.send(:load_connection_config_file).should eql({})
133
+ end
134
+ it "should load file if available" do
135
+ @class.connection_config_file = File.join(FIXTURE_PATH, 'config', 'couchdb.yml')
136
+ hash = @class.send(:load_connection_config_file)
137
+ hash[:development].should_not be_nil
138
+ @class.server.uri.should eql("https://test:user@sample.cloudant.com:443")
139
+ end
140
+
141
+ end
142
+
143
+ end
144
+
145
+ end
146
+
147
+
148
+ end
@@ -17,11 +17,11 @@ describe "Design Documents" do
17
17
  end
18
18
 
19
19
  it "should calculate a consistent checksum for model" do
20
- WithTemplateAndUniqueID.design_doc.checksum!.should eql('ff6fa2eaf774397391942d51428c1fe2')
20
+ WithTemplateAndUniqueID.design_doc.checksum!.should eql('caa2b4c27abb82b4e37421de76d96ffc')
21
21
  end
22
22
 
23
23
  it "should calculate checksum for complex model" do
24
- Article.design_doc.checksum!.should eql('fb65c06a76b6141529e31e894ad00b1a')
24
+ Article.design_doc.checksum!.should eql('70dff8caea143bf40fad09adf0701104')
25
25
  end
26
26
 
27
27
  it "should cache the generated checksum value" do
@@ -74,7 +74,7 @@ describe "Design View" do
74
74
  it "should auto generate mapping from name" do
75
75
  lambda { @klass.create(DesignViewModel, 'by_title') }.should_not raise_error
76
76
  str = @design_doc['views']['by_title']['map']
77
- str.should include("((doc['model'] == 'DesignViewModel') && (doc['title'] != null))")
77
+ str.should include("((doc['#{DesignViewModel.model_type_key}'] == 'DesignViewModel') && (doc['title'] != null))")
78
78
  str.should include("emit(doc['title'], 1);")
79
79
  str = @design_doc['views']['by_title']['reduce']
80
80
  str.should include("return sum(values);")
@@ -87,6 +87,20 @@ describe "Design" do
87
87
 
88
88
  end
89
89
 
90
+ describe "#filter" do
91
+
92
+ before :each do
93
+ @object = @klass.new(DesignModel)
94
+ end
95
+
96
+ it "should add the provided function to the design doc" do
97
+ @object.filter(:important, "function(doc, req) { return doc.priority == 'high'; }")
98
+ DesignModel.design_doc['filters'].should_not be_empty
99
+ DesignModel.design_doc['filters']['important'].should_not be_blank
100
+ end
101
+
102
+ end
103
+
90
104
  describe "#create_view_method" do
91
105
  before :each do
92
106
  @object = @klass.new(DesignModel)
@@ -56,7 +56,11 @@ describe "Model properties" do
56
56
  end
57
57
 
58
58
  it "should raise error if property name coincides with model type key" do
59
- lambda { Person.property(Person.model_type_key) }.should raise_error(/already used/)
59
+ lambda { Cat.property(Cat.model_type_key) }.should raise_error(/already used/)
60
+ end
61
+
62
+ it "should not raise error if property name coincides with model type key on non-model" do
63
+ lambda { Person.property(Article.model_type_key) }.should_not raise_error
60
64
  end
61
65
 
62
66
  it "should be auto timestamped" do
@@ -3,8 +3,9 @@ require File.expand_path("../../spec_helper", __FILE__)
3
3
  require File.join(FIXTURE_PATH, 'more', 'cat')
4
4
 
5
5
  class DummyProxyable < CouchRest::Model::Base
6
- def proxy_database
7
- 'db' # Do not use this!
6
+ proxy_database_method :db
7
+ def db
8
+ 'db'
8
9
  end
9
10
  end
10
11
 
@@ -13,13 +14,39 @@ end
13
14
 
14
15
  describe "Proxyable" do
15
16
 
16
- describe "class methods" do
17
+ describe "#proxy_database" do
18
+
19
+ before do
20
+ @class = Class.new(CouchRest::Model::Base)
21
+ @class.class_eval do
22
+ def slug; 'proxy'; end
23
+ end
24
+ @obj = @class.new
25
+ end
26
+
27
+ it "should respond to method" do
28
+ @obj.should respond_to(:proxy_database)
29
+ end
30
+
31
+ it "should provide proxy database from method" do
32
+ @class.stub!(:proxy_database_method).twice.and_return(:slug)
33
+ @obj.proxy_database.should be_a(CouchRest::Database)
34
+ @obj.proxy_database.name.should eql('couchrest_proxy')
35
+ end
17
36
 
18
- before(:each) do
19
- @class = DummyProxyable.clone
37
+ it "should raise an error if called and no proxy_database_method set" do
38
+ lambda { @obj.proxy_database }.should raise_error(StandardError, /Please set/)
20
39
  end
21
40
 
41
+ end
42
+
43
+ describe "class methods" do
44
+
45
+
22
46
  describe ".proxy_owner_method" do
47
+ before(:each) do
48
+ @class = DummyProxyable.clone
49
+ end
23
50
  it "should provide proxy_owner_method accessors" do
24
51
  @class.should respond_to(:proxy_owner_method)
25
52
  @class.should respond_to(:proxy_owner_method=)
@@ -30,7 +57,20 @@ describe "Proxyable" do
30
57
  end
31
58
  end
32
59
 
60
+ describe ".proxy_database_method" do
61
+ before do
62
+ @class = Class.new(CouchRest::Model::Base)
63
+ end
64
+ it "should be possible to set the proxy database method" do
65
+ @class.proxy_database_method :db
66
+ @class.proxy_database_method.should eql(:db)
67
+ end
68
+ end
69
+
33
70
  describe ".proxy_for" do
71
+ before(:each) do
72
+ @class = DummyProxyable.clone
73
+ end
34
74
 
35
75
  it "should be provided" do
36
76
  @class.should respond_to(:proxy_for)
@@ -48,7 +88,6 @@ describe "Proxyable" do
48
88
  @obj = DummyProxyable.new
49
89
  CouchRest::Model::Proxyable::ModelProxy.should_receive(:new).with(Cat, @obj, 'dummy_proxyable', 'db').and_return(true)
50
90
  @obj.should_receive('proxy_database').and_return('db')
51
- @obj.should_receive(:respond_to?).with('proxy_database').and_return(true)
52
91
  @obj.cats
53
92
  end
54
93
 
@@ -60,26 +99,16 @@ describe "Proxyable" do
60
99
  @obj = DummyProxyable.new
61
100
  CouchRest::Model::Proxyable::ModelProxy.should_receive(:new).with(::Document, @obj, 'dummy_proxyable', 'db').and_return(true)
62
101
  @obj.should_receive('proxy_database').and_return('db')
63
- @obj.should_receive(:respond_to?).with('proxy_database').and_return(true)
64
102
  @obj.documents
65
103
  end
66
-
67
- it "should raise an error if the database method is missing" do
68
- @class.proxy_for(:cats)
69
- @obj = @class.new
70
- @obj.should_receive(:respond_to?).with('proxy_database').and_return(false)
71
- lambda { @obj.cats }.should raise_error(StandardError, "Missing #proxy_database method for proxy")
72
- end
73
-
74
- it "should raise an error if custom database method missing" do
75
- @class.proxy_for(:proxy_kittens, :database_method => "foobardom")
76
- @obj = @class.new
77
- lambda { @obj.proxy_kittens }.should raise_error(StandardError, "Missing #foobardom method for proxy")
78
- end
79
104
  end
80
105
  end
81
106
 
82
107
  describe ".proxied_by" do
108
+ before do
109
+ @class = Class.new(CouchRest::Model::Base)
110
+ end
111
+
83
112
  it "should be provided" do
84
113
  @class.should respond_to(:proxied_by)
85
114
  end
@@ -107,6 +136,11 @@ describe "Proxyable" do
107
136
  @class.proxied_by(:department)
108
137
  lambda { @class.proxied_by(:company) }.should raise_error
109
138
  end
139
+
140
+ it "should overwrite the database method to provide an error" do
141
+ @class.proxied_by(:company)
142
+ lambda { @class.database }.should raise_error(StandardError, /database must be accessed via/)
143
+ end
110
144
  end
111
145
  end
112
146
 
@@ -316,7 +350,7 @@ describe "Proxyable" do
316
350
 
317
351
  it "should allow creation of new entries" do
318
352
  inv = @company.proxyable_invoices.new(:client => "Lorena", :total => 35)
319
- inv.database.should_not be_nil
353
+ # inv.database.should_not be_nil
320
354
  inv.save.should be_true
321
355
  @company.proxyable_invoices.count.should eql(1)
322
356
  @company.proxyable_invoices.first.client.should eql("Lorena")
@@ -7,11 +7,15 @@ require File.join(FIXTURE_PATH, 'more', 'course')
7
7
  describe "Model views" do
8
8
 
9
9
  class Unattached < CouchRest::Model::Base
10
- # Note: no use_database here
11
10
  property :title
12
11
  property :questions
13
12
  property :professor
14
13
  view_by :title
14
+
15
+ # Force the database to always be nil
16
+ def self.database
17
+ nil
18
+ end
15
19
  end
16
20
 
17
21
 
@@ -195,7 +199,7 @@ describe "Model views" do
195
199
  end
196
200
  end
197
201
 
198
- describe "a model class not tied to a database" do
202
+ describe "a model class with database provided manually" do
199
203
  before(:all) do
200
204
  reset_test_db!
201
205
  @db = DB
@@ -0,0 +1,10 @@
1
+
2
+ development:
3
+ protocol: 'https'
4
+ host: sample.cloudant.com
5
+ port: 443
6
+ prefix: project
7
+ suffix: text
8
+ username: test
9
+ password: user
10
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: couchrest_model
3
3
  version: !ruby/object:Gem::Version
4
- hash: -1848230064
4
+ hash: -1848230063
5
5
  prerelease: true
6
6
  segments:
7
7
  - 1
8
8
  - 1
9
9
  - 0
10
- - beta4
11
- version: 1.1.0.beta4
10
+ - beta5
11
+ version: 1.1.0.beta5
12
12
  platform: ruby
13
13
  authors:
14
14
  - J. Chris Anderson
@@ -20,7 +20,7 @@ autorequire:
20
20
  bindir: bin
21
21
  cert_chain: []
22
22
 
23
- date: 2011-01-16 00:00:00 +01:00
23
+ date: 2011-04-29 00:00:00 +02:00
24
24
  default_executable:
25
25
  dependencies:
26
26
  - !ruby/object:Gem::Dependency
@@ -63,12 +63,12 @@ dependencies:
63
63
  requirements:
64
64
  - - ~>
65
65
  - !ruby/object:Gem::Version
66
- hash: 13
66
+ hash: 7
67
67
  segments:
68
68
  - 3
69
69
  - 0
70
- - 5
71
- version: 3.0.5
70
+ - 0
71
+ version: 3.0.0
72
72
  type: :runtime
73
73
  version_requirements: *id003
74
74
  - !ruby/object:Gem::Dependency
@@ -156,7 +156,7 @@ files:
156
156
  - VERSION
157
157
  - benchmarks/dirty.rb
158
158
  - couchrest_model.gemspec
159
- - history.txt
159
+ - history.md
160
160
  - init.rb
161
161
  - lib/couchrest/model.rb
162
162
  - lib/couchrest/model/associations.rb
@@ -169,6 +169,7 @@ files:
169
169
  - lib/couchrest/model/class_proxy.rb
170
170
  - lib/couchrest/model/collection.rb
171
171
  - lib/couchrest/model/configuration.rb
172
+ - lib/couchrest/model/connection.rb
172
173
  - lib/couchrest/model/core_extensions/hash.rb
173
174
  - lib/couchrest/model/core_extensions/time_parsing.rb
174
175
  - lib/couchrest/model/design_doc.rb
@@ -204,6 +205,7 @@ files:
204
205
  - spec/couchrest/class_proxy_spec.rb
205
206
  - spec/couchrest/collection_spec.rb
206
207
  - spec/couchrest/configuration_spec.rb
208
+ - spec/couchrest/connection_spec.rb
207
209
  - spec/couchrest/core_extensions/time_parsing.rb
208
210
  - spec/couchrest/design_doc_spec.rb
209
211
  - spec/couchrest/designs/view_spec.rb
@@ -222,6 +224,7 @@ files:
222
224
  - spec/fixtures/attachments/couchdb.png
223
225
  - spec/fixtures/attachments/test.html
224
226
  - spec/fixtures/base.rb
227
+ - spec/fixtures/config/couchdb.yml
225
228
  - spec/fixtures/more/article.rb
226
229
  - spec/fixtures/more/card.rb
227
230
  - spec/fixtures/more/cat.rb