activecouch 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README +42 -0
- data/Rakefile +31 -0
- data/VERSION +1 -0
- data/lib/active_couch.rb +18 -0
- data/lib/active_couch/associations.rb +1 -0
- data/lib/active_couch/associations/has_many_association.rb +35 -0
- data/lib/active_couch/attribute.rb +46 -0
- data/lib/active_couch/base.rb +458 -0
- data/lib/active_couch/callbacks.rb +81 -0
- data/lib/active_couch/connection.rb +155 -0
- data/lib/active_couch/errors.rb +17 -0
- data/lib/active_couch/migrations.rb +3 -0
- data/lib/active_couch/migrations/errors.rb +4 -0
- data/lib/active_couch/migrations/migration.rb +77 -0
- data/lib/active_couch/migrations/migrator.rb +53 -0
- data/lib/active_couch/support.rb +2 -0
- data/lib/active_couch/support/extensions.rb +92 -0
- data/lib/active_couch/support/inflections.rb +52 -0
- data/lib/active_couch/support/inflector.rb +280 -0
- data/spec/attribute/initialize_spec.rb +138 -0
- data/spec/base/after_delete_spec.rb +107 -0
- data/spec/base/after_save_spec.rb +99 -0
- data/spec/base/before_delete_spec.rb +106 -0
- data/spec/base/before_save_spec.rb +98 -0
- data/spec/base/create_spec.rb +27 -0
- data/spec/base/database_spec.rb +65 -0
- data/spec/base/delete_spec.rb +78 -0
- data/spec/base/find_spec.rb +165 -0
- data/spec/base/from_json_spec.rb +48 -0
- data/spec/base/has_many_spec.rb +81 -0
- data/spec/base/has_spec.rb +76 -0
- data/spec/base/id_spec.rb +22 -0
- data/spec/base/initialize_spec.rb +43 -0
- data/spec/base/module_spec.rb +18 -0
- data/spec/base/nested_class_spec.rb +19 -0
- data/spec/base/rev_spec.rb +16 -0
- data/spec/base/save_spec.rb +65 -0
- data/spec/base/site_spec.rb +41 -0
- data/spec/base/to_json_spec.rb +64 -0
- data/spec/connection/initialize_spec.rb +28 -0
- data/spec/has_many_association/initialize_spec.rb +33 -0
- data/spec/migration/define_spec.rb +29 -0
- data/spec/migration/include_attributes_spec.rb +30 -0
- data/spec/migration/view_js_spec.rb +46 -0
- data/spec/migration/with_filter_spec.rb +13 -0
- data/spec/migration/with_key_spec.rb +13 -0
- data/spec/migrator/create_database_spec.rb +48 -0
- data/spec/migrator/delete_database_spec.rb +46 -0
- data/spec/migrator/migrate_spec.rb +99 -0
- data/spec/spec_helper.rb +9 -0
- metadata +104 -0
@@ -0,0 +1,13 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper.rb'
|
2
|
+
|
3
|
+
describe "ActiveCouch::Migration #with_filter method" do
|
4
|
+
it "should set @filter correctly" do
|
5
|
+
class ByLatitude < ActiveCouch::Migration
|
6
|
+
define :for_db => 'hotels' do
|
7
|
+
with_filter 'doc.name == "Hilton"'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
# Assertion
|
11
|
+
ByLatitude.instance_variable_get("@filter").should == 'doc.name == "Hilton"'
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper.rb'
|
2
|
+
|
3
|
+
describe "ActiveCouch::Migration #with_key method" do
|
4
|
+
it "should set @key correctly" do
|
5
|
+
class ByName < ActiveCouch::Migration
|
6
|
+
define :by_name, :for_db => 'people' do
|
7
|
+
with_key 'name'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
ByName.instance_variable_get("@key").should == 'name'
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper.rb'
|
2
|
+
|
3
|
+
describe ActiveCouch::Migrator, "#create_database with site and name" do
|
4
|
+
before(:each) do
|
5
|
+
@conn = mock(ActiveCouch::Connection)
|
6
|
+
end
|
7
|
+
|
8
|
+
def mock_connection_and_response(options = {})
|
9
|
+
ActiveCouch::Connection.should_receive(:new).with(options[:site]).and_return(@conn)
|
10
|
+
@conn.should_receive(:put).with("/#{options[:database_name]}", '{}').and_return(@response)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should create a new Connection to the given site and send a PUT with the database name" do
|
14
|
+
@response = mock(Object, :code => '201')
|
15
|
+
|
16
|
+
# Mock and place expectations on the the connection and response out explicitly instead of using
|
17
|
+
# mock_connection_and_response so that it's clearer what we're testing in this spec.
|
18
|
+
ActiveCouch::Connection.should_receive(:new).with('http://test.host:5984/').and_return(@conn)
|
19
|
+
@conn.should_receive(:put).with('/test', '{}').and_return(@response)
|
20
|
+
|
21
|
+
ActiveCouch::Migrator.create_database('http://test.host:5984/', 'test')
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should return true if the response code is HTTP status 201" do
|
25
|
+
mock_connection_and_response(:site => 'http://test.host:5984/', :database_name => 'test')
|
26
|
+
@response.should_receive(:code).and_return('201')
|
27
|
+
|
28
|
+
ActiveCouch::Migrator.create_database('http://test.host:5984/', 'test').should == true
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should raise an ActiveCouch::MigrationError if the response code is not HTTP status 201" do
|
32
|
+
mock_connection_and_response(:site => 'http://test.host:5984/', :database_name => 'test')
|
33
|
+
@response.should_receive(:code).any_number_of_times.and_return('500')
|
34
|
+
|
35
|
+
lambda {
|
36
|
+
ActiveCouch::Migrator.create_database('http://test.host:5984/', 'test')
|
37
|
+
}.should raise_error(ActiveCouch::MigrationError)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should raise an ActiveCouch::MigrationError with a 'Database exists' message if the response code is HTTP status 409" do
|
41
|
+
mock_connection_and_response(:site => 'http://test.host:5984/', :database_name => 'test')
|
42
|
+
@response.should_receive(:code).any_number_of_times.and_return('409')
|
43
|
+
|
44
|
+
lambda {
|
45
|
+
ActiveCouch::Migrator.create_database('http://test.host:5984/', 'test')
|
46
|
+
}.should raise_error(ActiveCouch::MigrationError, 'Database exists')
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper.rb'
|
2
|
+
|
3
|
+
describe ActiveCouch::Migrator, "#delete_database with site and name" do
|
4
|
+
before(:each) do
|
5
|
+
@conn = mock(ActiveCouch::Connection)
|
6
|
+
end
|
7
|
+
|
8
|
+
def mock_connection_and_response(options = {})
|
9
|
+
ActiveCouch::Connection.should_receive(:new).with(options[:site]).and_return(@conn)
|
10
|
+
@conn.should_receive(:delete).with("/#{options[:database_name]}").and_return(@response)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should create a new Connection to the given site and send a DELETE with the database name" do
|
14
|
+
@response = mock(Object, :code => '202')
|
15
|
+
|
16
|
+
ActiveCouch::Connection.should_receive(:new).with('http://test.host:5984/').and_return(@conn)
|
17
|
+
@conn.should_receive(:delete).with('/delete_me').and_return(@response)
|
18
|
+
|
19
|
+
ActiveCouch::Migrator.delete_database('http://test.host:5984/', 'delete_me')
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should return true if the response code is HTTP status 202" do
|
23
|
+
mock_connection_and_response(:site => 'http://test.host:5984/', :database_name => 'delete_me')
|
24
|
+
@response.should_receive(:code).and_return('202')
|
25
|
+
|
26
|
+
ActiveCouch::Migrator.delete_database('http://test.host:5984/', 'delete_me').should == true
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should raise an ActiveCouch::MigrationError if the response code is not HTTP status 202" do
|
30
|
+
mock_connection_and_response(:site => 'http://test.host:5984/', :database_name => 'delete_me')
|
31
|
+
@response.should_receive(:code).any_number_of_times.and_return('500')
|
32
|
+
|
33
|
+
lambda {
|
34
|
+
ActiveCouch::Migrator.delete_database('http://test.host:5984/', 'delete_me')
|
35
|
+
}.should raise_error(ActiveCouch::MigrationError)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should raise an ActiveCouch::MigrationError with a 'Database does not exist' message if the response code is HTTP status 404" do
|
39
|
+
mock_connection_and_response(:site => 'http://test.host:5984/', :database_name => 'delete_me')
|
40
|
+
@response.should_receive(:code).any_number_of_times.and_return('404')
|
41
|
+
|
42
|
+
lambda {
|
43
|
+
ActiveCouch::Migrator.delete_database('http://test.host:5984/', 'delete_me')
|
44
|
+
}.should raise_error(ActiveCouch::MigrationError, "Database 'delete_me' does not exist")
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper.rb'
|
2
|
+
|
3
|
+
require 'net/http'
|
4
|
+
require 'uri'
|
5
|
+
|
6
|
+
describe ActiveCouch::Migrator, "#migrate (that actually connects to a CouchDB server)" do
|
7
|
+
before(:each) do
|
8
|
+
class ByFace < ActiveCouch::Migration
|
9
|
+
define :for_db => 'ac_test_3' do
|
10
|
+
with_key 'face'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
ActiveCouch::Migrator.create_database('http://localhost:5984/', 'ac_test_3')
|
15
|
+
end
|
16
|
+
|
17
|
+
after(:each) do
|
18
|
+
ActiveCouch::Migrator.delete_database('http://localhost:5984/', 'ac_test_3')
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should be able to create a permanent view when sent the migrate method" do
|
22
|
+
ActiveCouch::Migrator.migrate('http://localhost:5984', ByFace).should == true
|
23
|
+
# This is the view document. To actually query this particular view, the URL to be used
|
24
|
+
# is http://#{host}:#{port}/ac_test_1/_view/by_face/by_face
|
25
|
+
# A little unwieldy I know, but the point of ActiveCouch is to abstract this unwieldiness
|
26
|
+
response = Net::HTTP.get_response URI.parse("http://localhost:5984/ac_test_3/_design/by_face")
|
27
|
+
response.code.should == '200'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe ActiveCouch::Migrator, "#migrate with site and migration" do
|
32
|
+
before(:all) do
|
33
|
+
class ByFace < ActiveCouch::Migration
|
34
|
+
define :for_db => 'test_db' do
|
35
|
+
with_key 'face'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
before(:each) do
|
41
|
+
@conn = mock(ActiveCouch::Connection)
|
42
|
+
end
|
43
|
+
|
44
|
+
after(:all) do
|
45
|
+
Object.send :remove_const, :ByFace
|
46
|
+
end
|
47
|
+
|
48
|
+
def mock_connection_and_response(options = {})
|
49
|
+
ByFace.should_receive(:view).any_number_of_times.and_return('by_face')
|
50
|
+
ByFace.should_receive(:view_js).any_number_of_times.and_return('{ "some" => "view json" }')
|
51
|
+
|
52
|
+
ActiveCouch::Connection.should_receive(:new).with(options[:site]).and_return(@conn)
|
53
|
+
@conn.should_receive(:put).with('/test_db/_design/by_face', '{ "some" => "view json" }').and_return(@response)
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should create a new Connection to the given site and send a PUT to the view URL" do
|
57
|
+
@response = mock(Object, :code => '201')
|
58
|
+
|
59
|
+
ByFace.should_receive(:view).any_number_of_times.and_return('by_face')
|
60
|
+
ByFace.should_receive(:view_js).any_number_of_times.and_return('{ "some" => "view json" }')
|
61
|
+
|
62
|
+
ActiveCouch::Connection.should_receive(:new).with('http://test.host:5984/').and_return(@conn)
|
63
|
+
@conn.should_receive(:put).with('/test_db/_design/by_face', '{ "some" => "view json" }').and_return(@response)
|
64
|
+
|
65
|
+
ActiveCouch::Migrator.migrate('http://test.host:5984/', ByFace)
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should return true if the response code is HTTP status 201" do
|
69
|
+
mock_connection_and_response(:site => 'http://test.host:5984/')
|
70
|
+
@response.should_receive(:code).any_number_of_times.and_return('201')
|
71
|
+
|
72
|
+
ActiveCouch::Migrator.migrate('http://test.host:5984/', ByFace).should == true
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should raise an ActiveCouch::MigrationError if the response code is not HTTP status 201" do
|
76
|
+
mock_connection_and_response(:site => 'http://test.host:5984/')
|
77
|
+
@response.should_receive(:code).any_number_of_times.and_return('500')
|
78
|
+
|
79
|
+
lambda {
|
80
|
+
ActiveCouch::Migrator.migrate('http://test.host:5984/', ByFace)
|
81
|
+
}.should raise_error(ActiveCouch::MigrationError)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should raise an ActiveCouch::MigrationError if the migration has no view" do
|
85
|
+
ByFace.should_receive(:view).and_return(nil)
|
86
|
+
|
87
|
+
lambda {
|
88
|
+
ActiveCouch::Migrator.migrate('http://test.host:5984/', ByFace)
|
89
|
+
}.should raise_error(ActiveCouch::MigrationError)
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should raise an ActiveCouch::MigrationError if the migration has no database" do
|
93
|
+
ByFace.should_receive(:database).and_return(nil)
|
94
|
+
|
95
|
+
lambda {
|
96
|
+
ActiveCouch::Migrator.migrate('http://test.host:5984/', ByFace)
|
97
|
+
}.should raise_error(ActiveCouch::MigrationError)
|
98
|
+
end
|
99
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.4
|
3
|
+
specification_version: 1
|
4
|
+
name: activecouch
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.1.0
|
7
|
+
date: 2008-01-23 00:00:00 +08:00
|
8
|
+
summary: Ruby-based wrapper for CouchDB
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: arun.thampi@gmail.com, chuyeow@gmail.com
|
12
|
+
homepage: http://activecouch.googlecode.com
|
13
|
+
rubyforge_project:
|
14
|
+
description:
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Arun Thampi & Cheah Chu Yeow
|
31
|
+
files:
|
32
|
+
- MIT-LICENSE
|
33
|
+
- Rakefile
|
34
|
+
- README
|
35
|
+
- VERSION
|
36
|
+
- lib/active_couch/associations/has_many_association.rb
|
37
|
+
- lib/active_couch/associations.rb
|
38
|
+
- lib/active_couch/attribute.rb
|
39
|
+
- lib/active_couch/base.rb
|
40
|
+
- lib/active_couch/callbacks.rb
|
41
|
+
- lib/active_couch/connection.rb
|
42
|
+
- lib/active_couch/errors.rb
|
43
|
+
- lib/active_couch/migrations/errors.rb
|
44
|
+
- lib/active_couch/migrations/migration.rb
|
45
|
+
- lib/active_couch/migrations/migrator.rb
|
46
|
+
- lib/active_couch/migrations.rb
|
47
|
+
- lib/active_couch/support/extensions.rb
|
48
|
+
- lib/active_couch/support/inflections.rb
|
49
|
+
- lib/active_couch/support/inflector.rb
|
50
|
+
- lib/active_couch/support.rb
|
51
|
+
- lib/active_couch.rb
|
52
|
+
- spec/attribute/initialize_spec.rb
|
53
|
+
- spec/base/after_delete_spec.rb
|
54
|
+
- spec/base/after_save_spec.rb
|
55
|
+
- spec/base/before_delete_spec.rb
|
56
|
+
- spec/base/before_save_spec.rb
|
57
|
+
- spec/base/create_spec.rb
|
58
|
+
- spec/base/database_spec.rb
|
59
|
+
- spec/base/delete_spec.rb
|
60
|
+
- spec/base/find_spec.rb
|
61
|
+
- spec/base/from_json_spec.rb
|
62
|
+
- spec/base/has_many_spec.rb
|
63
|
+
- spec/base/has_spec.rb
|
64
|
+
- spec/base/id_spec.rb
|
65
|
+
- spec/base/initialize_spec.rb
|
66
|
+
- spec/base/module_spec.rb
|
67
|
+
- spec/base/nested_class_spec.rb
|
68
|
+
- spec/base/rev_spec.rb
|
69
|
+
- spec/base/save_spec.rb
|
70
|
+
- spec/base/site_spec.rb
|
71
|
+
- spec/base/to_json_spec.rb
|
72
|
+
- spec/connection/initialize_spec.rb
|
73
|
+
- spec/has_many_association/initialize_spec.rb
|
74
|
+
- spec/migration/define_spec.rb
|
75
|
+
- spec/migration/include_attributes_spec.rb
|
76
|
+
- spec/migration/view_js_spec.rb
|
77
|
+
- spec/migration/with_filter_spec.rb
|
78
|
+
- spec/migration/with_key_spec.rb
|
79
|
+
- spec/migrator/create_database_spec.rb
|
80
|
+
- spec/migrator/delete_database_spec.rb
|
81
|
+
- spec/migrator/migrate_spec.rb
|
82
|
+
- spec/spec_helper.rb
|
83
|
+
test_files: []
|
84
|
+
|
85
|
+
rdoc_options: []
|
86
|
+
|
87
|
+
extra_rdoc_files:
|
88
|
+
- README
|
89
|
+
executables: []
|
90
|
+
|
91
|
+
extensions: []
|
92
|
+
|
93
|
+
requirements: []
|
94
|
+
|
95
|
+
dependencies:
|
96
|
+
- !ruby/object:Gem::Dependency
|
97
|
+
name: json
|
98
|
+
version_requirement:
|
99
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 1.1.2
|
104
|
+
version:
|