couchrest_model/spec/couchrest/core/couchrest_spec.rb

201 lines
6.2 KiB
Ruby
Raw Normal View History

require File.expand_path("../../../spec_helper", __FILE__)
2008-03-18 19:37:10 +01:00
describe CouchRest do
before(:each) do
2008-07-05 01:56:09 +02:00
@cr = CouchRest.new(COUCHHOST)
2008-03-18 19:37:10 +01:00
begin
2008-07-05 01:56:09 +02:00
@db = @cr.database(TESTDB)
2008-07-19 21:25:02 +02:00
@db.delete! rescue nil
2008-03-19 16:57:20 +01:00
end
end
after(:each) do
begin
2008-07-19 21:25:02 +02:00
@db.delete! rescue nil
2008-03-18 19:37:10 +01:00
end
end
describe "getting info" do
it "should list databases" do
@cr.databases.should be_an_instance_of(Array)
end
it "should get info" do
@cr.info["couchdb"].should == "Welcome"
2008-03-18 19:37:10 +01:00
@cr.info.class.should == Hash
end
end
2008-06-15 20:43:05 +02:00
it "should restart" do
@cr.restart!
end
it "should provide one-time access to uuids" do
@cr.next_uuid.should_not be_nil
2008-06-15 20:43:05 +02:00
end
2008-03-18 19:37:10 +01:00
describe "initializing a database" do
it "should return a db" do
2008-07-05 01:56:09 +02:00
db = @cr.database(TESTDB)
2008-03-18 19:37:10 +01:00
db.should be_an_instance_of(CouchRest::Database)
db.host.should == @cr.uri
end
end
describe "parsing urls" do
it "should parse just a dbname" do
db = CouchRest.parse "my-db"
db[:database].should == "my-db"
2008-12-14 12:05:02 +01:00
db[:host].should == "127.0.0.1:5984"
end
it "should parse a host and db" do
2008-12-14 12:05:02 +01:00
db = CouchRest.parse "127.0.0.1/my-db"
db[:database].should == "my-db"
2008-12-14 12:05:02 +01:00
db[:host].should == "127.0.0.1"
end
it "should parse a host and db with http" do
2008-12-14 12:05:02 +01:00
db = CouchRest.parse "http://127.0.0.1/my-db"
db[:database].should == "my-db"
2008-12-14 12:05:02 +01:00
db[:host].should == "127.0.0.1"
end
it "should parse a host with a port and db" do
2008-12-14 12:05:02 +01:00
db = CouchRest.parse "127.0.0.1:5555/my-db"
db[:database].should == "my-db"
2008-12-14 12:05:02 +01:00
db[:host].should == "127.0.0.1:5555"
end
it "should parse a host with a port and db with http" do
2008-12-14 12:05:02 +01:00
db = CouchRest.parse "http://127.0.0.1:5555/my-db"
db[:database].should == "my-db"
2008-12-14 12:05:02 +01:00
db[:host].should == "127.0.0.1:5555"
end
it "should parse just a host" do
2008-12-14 12:05:02 +01:00
db = CouchRest.parse "http://127.0.0.1:5555/"
db[:database].should be_nil
2008-12-14 12:05:02 +01:00
db[:host].should == "127.0.0.1:5555"
end
it "should parse just a host no slash" do
2008-12-14 12:05:02 +01:00
db = CouchRest.parse "http://127.0.0.1:5555"
db[:host].should == "127.0.0.1:5555"
db[:database].should be_nil
end
it "should get docid" do
2008-12-14 12:05:02 +01:00
db = CouchRest.parse "127.0.0.1:5555/my-db/my-doc"
db[:database].should == "my-db"
2008-12-14 12:05:02 +01:00
db[:host].should == "127.0.0.1:5555"
db[:doc].should == "my-doc"
end
it "should get docid with http" do
2008-12-14 12:05:02 +01:00
db = CouchRest.parse "http://127.0.0.1:5555/my-db/my-doc"
db[:database].should == "my-db"
2008-12-14 12:05:02 +01:00
db[:host].should == "127.0.0.1:5555"
db[:doc].should == "my-doc"
end
2008-09-15 00:49:42 +02:00
it "should parse a host and db" do
db = CouchRest.parse "127.0.0.1/my-db"
db[:database].should == "my-db"
db[:host].should == "127.0.0.1"
end
it "should parse a host and db with http" do
db = CouchRest.parse "http://127.0.0.1/my-db"
db[:database].should == "my-db"
db[:host].should == "127.0.0.1"
end
it "should parse a host with a port and db" do
db = CouchRest.parse "127.0.0.1:5555/my-db"
db[:database].should == "my-db"
db[:host].should == "127.0.0.1:5555"
end
it "should parse a host with a port and db with http" do
db = CouchRest.parse "http://127.0.0.1:5555/my-db"
db[:database].should == "my-db"
db[:host].should == "127.0.0.1:5555"
end
it "should parse just a host" do
db = CouchRest.parse "http://127.0.0.1:5555/"
db[:database].should be_nil
db[:host].should == "127.0.0.1:5555"
end
it "should parse just a host no slash" do
db = CouchRest.parse "http://127.0.0.1:5555"
db[:host].should == "127.0.0.1:5555"
db[:database].should be_nil
end
it "should get docid" do
db = CouchRest.parse "127.0.0.1:5555/my-db/my-doc"
db[:database].should == "my-db"
db[:host].should == "127.0.0.1:5555"
db[:doc].should == "my-doc"
end
it "should get docid with http" do
db = CouchRest.parse "http://127.0.0.1:5555/my-db/my-doc"
db[:database].should == "my-db"
db[:host].should == "127.0.0.1:5555"
db[:doc].should == "my-doc"
end
end
2008-09-07 21:54:10 +02:00
describe "easy initializing a database adapter" do
it "should be possible without an explicit CouchRest instantiation" do
2008-12-14 12:05:02 +01:00
db = CouchRest.database "http://127.0.0.1:5984/couchrest-test"
2008-09-07 21:54:10 +02:00
db.should be_an_instance_of(CouchRest::Database)
2008-12-14 12:05:02 +01:00
db.host.should == "127.0.0.1:5984"
2008-09-07 21:54:10 +02:00
end
2008-09-29 01:03:15 +02:00
# TODO add https support (need test environment...)
# it "should work with https" # do
2008-12-14 12:05:02 +01:00
# db = CouchRest.database "https://127.0.0.1:5984/couchrest-test"
# db.host.should == "https://127.0.0.1:5984"
2008-09-29 01:03:15 +02:00
# end
2008-09-07 21:54:10 +02:00
it "should not create the database automatically" do
2008-12-14 12:05:02 +01:00
db = CouchRest.database "http://127.0.0.1:5984/couchrest-test"
2008-09-07 21:54:10 +02:00
lambda{db.info}.should raise_error(RestClient::ResourceNotFound)
end
end
describe "ensuring the db exists" do
it "should be super easy" do
2008-12-14 12:05:02 +01:00
db = CouchRest.database! "http://127.0.0.1:5984/couchrest-test-2"
2008-09-29 01:03:15 +02:00
db.name.should == 'couchrest-test-2'
db.info["db_name"].should == 'couchrest-test-2'
end
end
2008-03-18 19:37:10 +01:00
describe "successfully creating a database" do
it "should start without a database" do
2008-07-05 01:56:09 +02:00
@cr.databases.should_not include(TESTDB)
2008-03-18 19:37:10 +01:00
end
it "should return the created database" do
2008-07-05 01:56:09 +02:00
db = @cr.create_db(TESTDB)
2008-03-18 19:37:10 +01:00
db.should be_an_instance_of(CouchRest::Database)
end
it "should create the database" do
2008-07-05 01:56:09 +02:00
db = @cr.create_db(TESTDB)
@cr.databases.should include(TESTDB)
2008-03-18 19:37:10 +01:00
end
end
describe "failing to create a database because the name is taken" do
before(:each) do
2008-07-05 01:56:09 +02:00
db = @cr.create_db(TESTDB)
2008-03-18 19:37:10 +01:00
end
it "should start with the test database" do
2008-07-05 01:56:09 +02:00
@cr.databases.should include(TESTDB)
2008-03-18 19:37:10 +01:00
end
it "should PUT the database and raise an error" do
lambda{
2008-07-05 01:56:09 +02:00
@cr.create_db(TESTDB)
2008-03-18 19:37:10 +01:00
}.should raise_error(RestClient::Request::RequestFailed)
end
end
2009-01-11 04:52:08 +01:00
describe "using a proxy for RestClient connections" do
it "should set proxy url for RestClient" do
CouchRest.proxy 'http://localhost:8888/'
2009-07-14 10:43:40 +02:00
proxy_uri = URI.parse(HttpAbstraction.proxy)
2009-01-11 04:52:08 +01:00
proxy_uri.host.should eql( 'localhost' )
proxy_uri.port.should eql( 8888 )
CouchRest.proxy nil
end
end
2008-03-18 19:37:10 +01:00
end