couchmodel 0.1.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
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,117 @@
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", "configuration"))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "lib", "couch_model", "database"))
4
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "lib", "couch_model", "design"))
5
+
6
+ describe CouchModel::Configuration do
7
+
8
+ describe "design_directory=" do
9
+
10
+ it "should set the design directory" do
11
+ CouchModel::Configuration.design_directory = "test"
12
+ CouchModel::Configuration.design_directory.should == "test"
13
+ end
14
+
15
+ end
16
+
17
+ describe "register_database" do
18
+
19
+ before :each do
20
+ CouchModel::Configuration.databases.clear
21
+ @database = CouchModel::Database.new :name => "test"
22
+ @other = CouchModel::Database.new :name => "test"
23
+ end
24
+
25
+ def do_register
26
+ CouchModel::Configuration.register_database @database
27
+ end
28
+
29
+ it "should add the database" do
30
+ do_register
31
+ CouchModel::Configuration.databases.should include(@database)
32
+ end
33
+
34
+ it "should return a database with equal parameters if such is added before" do
35
+ CouchModel::Configuration.register_database @other
36
+ do_register.object_id.should == @other.object_id
37
+ end
38
+
39
+ end
40
+
41
+ describe "setup_databases" do
42
+
43
+ before :each do
44
+ CouchModel::Configuration.class_variable_set :@@databases, [ ]
45
+ end
46
+
47
+ def do_setup
48
+ CouchModel::Configuration.setup_databases
49
+ end
50
+
51
+ describe "for an existing database" do
52
+
53
+ before :each do
54
+ @database = CouchModel::Database.new :name => "test"
55
+ CouchModel::Configuration.register_database @database
56
+ end
57
+
58
+ it "should not create the database" do
59
+ @database.should_not_receive(:create!)
60
+ do_setup
61
+ end
62
+
63
+ end
64
+
65
+ describe "for a not-existing database" do
66
+
67
+ before :each do
68
+ @database = CouchModel::Database.new :name => "new_database"
69
+ CouchModel::Configuration.register_database @database
70
+ end
71
+
72
+ it "should create the database" do
73
+ @database.should_receive(:create!)
74
+ do_setup
75
+ end
76
+
77
+ end
78
+
79
+ end
80
+
81
+ describe "register_design" do
82
+
83
+ before :each do
84
+ CouchModel::Configuration.designs.clear
85
+ @database = CouchModel::Database.new :name => "test"
86
+ @design = CouchModel::Design.new @database, :id => "test_design"
87
+ end
88
+
89
+ it "should add the design" do
90
+ CouchModel::Configuration.register_design @design
91
+ CouchModel::Configuration.designs.should include(@design)
92
+ end
93
+
94
+ end
95
+
96
+ describe "setup_designs" do
97
+
98
+ before :each do
99
+ CouchModel::Configuration.class_variable_set :@@designs, [ ]
100
+
101
+ @database = CouchModel::Database.new :name => "test"
102
+ @design = CouchModel::Design.new @database, :id => "test_design"
103
+ CouchModel::Configuration.register_design @design
104
+ end
105
+
106
+ def do_setup
107
+ CouchModel::Configuration.setup_designs
108
+ end
109
+
110
+ it "should push the design" do
111
+ @design.should_receive(:push)
112
+ do_setup
113
+ end
114
+
115
+ end
116
+
117
+ end
@@ -0,0 +1,59 @@
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", "base"))
3
+
4
+ class AccessorTestModel < CouchModel::Base
5
+
6
+ setup_database :url => "http://localhost:5984/test"
7
+
8
+ end
9
+
10
+ describe AccessorTestModel do
11
+
12
+ before :each do
13
+ @model = AccessorTestModel.new :id => "test_model_1"
14
+ end
15
+
16
+ describe "key_reader" do
17
+
18
+ before :each do
19
+ AccessorTestModel.key_reader :test
20
+ @model = AccessorTestModel.new
21
+ end
22
+
23
+ it "should define a reader method" do
24
+ @model.should respond_to(:test)
25
+ end
26
+
27
+ end
28
+
29
+ describe "key_writer" do
30
+
31
+ before :each do
32
+ AccessorTestModel.key_writer :test
33
+ @model = AccessorTestModel.new
34
+ end
35
+
36
+ it "should define a writer method" do
37
+ @model.should respond_to(:test=)
38
+ end
39
+
40
+ end
41
+
42
+ describe "key_accessor" do
43
+
44
+ before :each do
45
+ AccessorTestModel.key_accessor :test
46
+ @model = AccessorTestModel.new
47
+ end
48
+
49
+ it "should define a reader method" do
50
+ @model.should respond_to(:test)
51
+ end
52
+
53
+ it "should define a writer method" do
54
+ @model.should respond_to(:test=)
55
+ end
56
+
57
+ end
58
+
59
+ end
@@ -0,0 +1,114 @@
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", "base"))
3
+
4
+ CouchModel::Configuration.design_directory = File.join File.dirname(__FILE__), "..", "design"
5
+
6
+ class AssociationTestModelOne < CouchModel::Base
7
+
8
+ setup_database :url => "http://localhost:5984/test"
9
+
10
+ key_accessor :name
11
+
12
+ belongs_to :related, :class_name => "AssociationTestModelTwo"
13
+
14
+ end
15
+
16
+ class AssociationTestModelTwo < CouchModel::Base
17
+
18
+ setup_database :url => "http://localhost:5984/test"
19
+
20
+ key_accessor :name
21
+
22
+ has_many :related,
23
+ :class_name => "AssociationTestModelOne",
24
+ :view_name => :by_related_id_and_name,
25
+ :query => lambda { |name| { :startkey => [ self.id, (name || nil) ], :endkey => [ self.id, (name || { }) ] } }
26
+
27
+ end
28
+
29
+ describe AssociationTestModelOne do
30
+
31
+ before :each do
32
+ @model = AssociationTestModelOne.find "test_model_1"
33
+ end
34
+
35
+ describe "belongs_to" do
36
+
37
+ it "should define the :related_id accessor methods" do
38
+ @model.should respond_to(:related_id)
39
+ @model.should respond_to(:related_id=)
40
+ end
41
+
42
+ it "should define the :related method" do
43
+ @model.should respond_to(:related)
44
+ end
45
+
46
+ end
47
+
48
+ describe "related" do
49
+
50
+ it "should return a model" do
51
+ @model.related.should be_instance_of(AssociationTestModelTwo)
52
+ end
53
+
54
+ end
55
+
56
+ describe "related=" do
57
+
58
+ before :each do
59
+ @other = AssociationTestModelTwo.find "test_model_2"
60
+ end
61
+
62
+ it "should set the relation to nil" do
63
+ @model.related_id = "test"
64
+ @model.related = nil
65
+ @model.related_id.should be_nil
66
+ end
67
+
68
+ it "should set the relation" do
69
+ @model.related = @other
70
+ @model.related_id.should == "test_model_2"
71
+ end
72
+
73
+ it "should raise an ArgumentError on wrong class" do
74
+ lambda do
75
+ @model.related = "test"
76
+ end.should raise_error(ArgumentError)
77
+ end
78
+
79
+ end
80
+
81
+ end
82
+
83
+ describe AssociationTestModelTwo do
84
+
85
+ before :each do
86
+ @model = AssociationTestModelTwo.find "test_model_2"
87
+ end
88
+
89
+ describe "has_many" do
90
+
91
+ it "should define the :related method" do
92
+ @model.should respond_to(:related)
93
+ end
94
+
95
+ end
96
+
97
+ describe "related" do
98
+
99
+ before :each do
100
+ @other = AssociationTestModelOne.find "test_model_1"
101
+ end
102
+
103
+ it "should return a collection" do
104
+ @model.related.should be_instance_of(CouchModel::Collection)
105
+ end
106
+
107
+ it "should include the test model one" do
108
+ @model.related.should include(@other)
109
+ @model.related(@other.name).should include(@other)
110
+ end
111
+
112
+ end
113
+
114
+ end
@@ -0,0 +1,24 @@
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", "base"))
3
+
4
+ class FinderTestModel < CouchModel::Base
5
+
6
+ setup_database :url => "http://localhost:5984/test"
7
+
8
+ key_accessor :name
9
+
10
+ end
11
+
12
+ describe FinderTestModel do
13
+
14
+ describe "find" do
15
+
16
+ it "should find the model" do
17
+ model = FinderTestModel.find "test_model_1"
18
+ model.should be_instance_of(FinderTestModel)
19
+ model.name.should == "phil"
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,88 @@
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", "base"))
3
+
4
+ CouchModel::Configuration.design_directory = File.join File.dirname(__FILE__), "..", "design"
5
+
6
+ class SetupTestModel < CouchModel::Base
7
+
8
+ setup_database :url => "http://localhost:5984/test"
9
+
10
+ end
11
+
12
+ describe SetupTestModel do
13
+
14
+ describe "setup_database" do
15
+
16
+ before :each do
17
+ @design = SetupTestModel.design
18
+ @design.stub!(:push)
19
+ CouchModel::Design.stub!(:new).and_return(@design)
20
+
21
+ @options = { :url => "http://localhost:5984/test" }
22
+ end
23
+
24
+ def do_setup
25
+ SetupTestModel.setup_database @options
26
+ end
27
+
28
+ it "should initialize the database" do
29
+ do_setup
30
+ SetupTestModel.database.url.should == @options[:url]
31
+ end
32
+
33
+ it "should register the database" do
34
+ do_setup
35
+ CouchModel::Configuration.databases.should include(SetupTestModel.database)
36
+ end
37
+
38
+ it "should register just one database if two databases has been set up" do
39
+ database = SetupTestModel.database
40
+ do_setup
41
+ database.object_id.should == SetupTestModel.database.object_id
42
+ end
43
+
44
+ it "should setup the database on initialization if requested" do
45
+ @options[:setup_on_initialization] = true
46
+ database = SetupTestModel.database
47
+ database.should_receive(:setup!).with(@options)
48
+ CouchModel::Database.stub!(:new).and_return(database)
49
+ do_setup
50
+ end
51
+
52
+ it "should initialize the design" do
53
+ do_setup
54
+ SetupTestModel.design.should be_instance_of(CouchModel::Design)
55
+ end
56
+
57
+ it "should register the design" do
58
+ do_setup
59
+ CouchModel::Configuration.designs.should include(SetupTestModel.design)
60
+ end
61
+
62
+ it "should generate the class view" do
63
+ do_setup
64
+ SetupTestModel.design.views.first.name.should == CouchModel::Configuration::CLASS_VIEW_NAME
65
+ end
66
+
67
+ it "should define the view methods" do
68
+ do_setup
69
+ SetupTestModel.should respond_to(:test_view)
70
+ SetupTestModel.test_view.should be_instance_of(CouchModel::Collection)
71
+ end
72
+
73
+ it "should push the design on initialization if requested" do
74
+ @options[:setup_on_initialization] = true
75
+ @design.should_receive(:push)
76
+ do_setup
77
+ end
78
+
79
+ it "should raise an ArgumentError on missing url option" do
80
+ @options[:url] = nil
81
+ lambda do
82
+ do_setup
83
+ end.should raise_error(ArgumentError)
84
+ end
85
+
86
+ end
87
+
88
+ end
@@ -0,0 +1,165 @@
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
+
4
+ describe CouchModel::Database do
5
+
6
+ before :each do
7
+ @database = CouchModel::Database.new :name => "test"
8
+ end
9
+
10
+ describe "==" do
11
+
12
+ before :each do
13
+ @other = CouchModel::Database.new :name => "test"
14
+ end
15
+
16
+ it "should be true when comparing two equal databases" do
17
+ @database.should == @other
18
+ end
19
+
20
+ it "should be false when comparing two different databases" do
21
+ @other = CouchModel::Database.new :name => "other"
22
+ @database.should_not == @other
23
+ end
24
+
25
+ it "should be false when comparing two databases with on different servers" do
26
+ @other = CouchModel::Database.new :name => "test", :server => CouchModel::Server.new(:host => "other")
27
+ @database.should_not == @other
28
+ end
29
+
30
+ end
31
+
32
+ describe "===" do
33
+
34
+ before :each do
35
+ @other = CouchModel::Database.new :name => "test"
36
+ end
37
+
38
+ it "should be true when comparing a database object with itself" do
39
+ @database.should === @database
40
+ end
41
+
42
+ it "should be false when comparing a database object with another database object" do
43
+ @database.should_not === @other
44
+ end
45
+
46
+ end
47
+
48
+ describe "create!" do
49
+
50
+ before :each do
51
+ @response = { :code => "201" }
52
+ CouchModel::Transport.stub!(:request).and_return(@response)
53
+ end
54
+
55
+ it "should create the database" do
56
+ CouchModel::Transport.should_receive(:request).with(:put, /test$/, anything).and_return(@response)
57
+ @database.create!
58
+ end
59
+
60
+ end
61
+
62
+ describe "delete!" do
63
+
64
+ before :each do
65
+ @response = { :code => "200" }
66
+ CouchModel::Transport.stub!(:request).and_return(@response)
67
+ end
68
+
69
+ it "should delete the database" do
70
+ CouchModel::Transport.should_receive(:request).with(:delete, /test$/, anything).and_return(@response)
71
+ @database.delete!
72
+ end
73
+
74
+ end
75
+
76
+ describe "setup!" do
77
+
78
+ before :each do
79
+ @database.stub!(:create!)
80
+ @database.stub!(:delete!)
81
+ @database.stub!(:exists?).and_return(true)
82
+ @options = { }
83
+ end
84
+
85
+ def do_setup
86
+ @database.setup! @options
87
+ end
88
+
89
+ describe "with delete_if_exists set to true" do
90
+
91
+ before :each do
92
+ @options.merge! :delete_if_exists => true
93
+ end
94
+
95
+ it "should delete an existing database" do
96
+ @database.should_receive(:delete!)
97
+ do_setup
98
+ end
99
+
100
+ it "should not delete a not-existing database" do
101
+ @database.stub!(:exists?).and_return(false)
102
+ @database.should_not_receive(:delete!)
103
+ do_setup
104
+ end
105
+
106
+ it "should create the database" do
107
+ @database.should_receive(:create!)
108
+ do_setup
109
+ end
110
+
111
+ end
112
+
113
+ describe "with delete_if_exists set to false" do
114
+
115
+ before :each do
116
+ @options.merge! :delete_if_exists => false
117
+ end
118
+
119
+ it "should create a not-existing database" do
120
+ @database.stub!(:exists?).and_return(false)
121
+ @database.should_receive(:create!)
122
+ do_setup
123
+ end
124
+
125
+ it "should not create an existing database" do
126
+ @database.should_not_receive(:create!)
127
+ do_setup
128
+ end
129
+
130
+ end
131
+
132
+ end
133
+
134
+ describe "informations" do
135
+
136
+ it "should return database informations" do
137
+ informations = @database.informations
138
+ informations.should have_key("db_name")
139
+ informations.should have_key("doc_count")
140
+ end
141
+
142
+ end
143
+
144
+ describe "exists?" do
145
+
146
+ it "should be true" do
147
+ @database.exists?.should be_true
148
+ end
149
+
150
+ it "should be false if no database with the given name exists" do
151
+ database = CouchModel::Database.new :name => "invalid"
152
+ database.exists?.should be_false
153
+ end
154
+
155
+ end
156
+
157
+ describe "documents" do
158
+
159
+ it "should return a collection" do
160
+ @database.documents.should be_instance_of(CouchModel::Collection)
161
+ end
162
+
163
+ end
164
+
165
+ end
@@ -0,0 +1,5 @@
1
+ :id: "test_design"
2
+ :language: "javascript"
3
+ :views:
4
+ "by_related_id_and_name":
5
+ :keys: [ "related_id", "name" ]
@@ -0,0 +1,10 @@
1
+ :id: "test_design"
2
+ :language: "javascript"
3
+ :views:
4
+ "test_view":
5
+ :map:
6
+ function(document) {
7
+ };
8
+ :reduce:
9
+ function(key, values, rereduce) {
10
+ };
@@ -0,0 +1,10 @@
1
+ :id: "test_design"
2
+ :language: "javascript"
3
+ :views:
4
+ "test_view":
5
+ :map:
6
+ function(document) {
7
+ };
8
+ :reduce:
9
+ function(key, values, rereduce) {
10
+ };