deploy_couch 0.0.1
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.
- data/.gitignore +2 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +14 -0
- data/README +0 -0
- data/Rakefile +1 -0
- data/bin/deploy_couch +16 -0
- data/deploy_couch.gemspec +20 -0
- data/deploy_couch.rake +0 -0
- data/lib/deploy_couch/config.rb +44 -0
- data/lib/deploy_couch/couch_db_schema.rb +58 -0
- data/lib/deploy_couch/delta.rb +36 -0
- data/lib/deploy_couch/delta_loader.rb +37 -0
- data/lib/deploy_couch/delta_processor.rb +60 -0
- data/lib/deploy_couch/deploy.rb +70 -0
- data/lib/deploy_couch/http_client.rb +48 -0
- data/lib/deploy_couch/repository.rb +71 -0
- data/lib/deploy_couch/version.rb +3 -0
- data/lib/deploy_couch.rb +13 -0
- data/spec/couchdb.yml +6 -0
- data/spec/integration/config_spec.rb +41 -0
- data/spec/integration/database_populator.rb +37 -0
- data/spec/integration/delta_loader_spec.rb +54 -0
- data/spec/integration/delta_processor_spec.rb +52 -0
- data/spec/integration/deltas/11_update_phone_with_std_codes.yml +13 -0
- data/spec/integration/deltas/12_delete_customer_name_1.yml +19 -0
- data/spec/integration/deltas/13_copy_and_create_new_customer.yml +20 -0
- data/spec/integration/deltas/1_add_address_to_customer.yml +14 -0
- data/spec/integration/deltas/2_add_phone_to_customer.yml +13 -0
- data/spec/integration/deltas/5_not_a_yaml_file.rb +0 -0
- data/spec/integration/deploy_spec.rb +56 -0
- data/spec/integration/repository_spec.rb +40 -0
- data/spec/spec_helper.rb +17 -0
- data/spec/unit/db_schema_spec.rb +66 -0
- data/spec/unit/delta_processor_spec.rb +61 -0
- data/spec/unit/deploy_spec.rb +94 -0
- data/spec/unit/repository_spec.rb +128 -0
- metadata +102 -0
@@ -0,0 +1,52 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
require File.dirname(__FILE__) + '/database_populator'
|
3
|
+
|
4
|
+
module DeployCouch
|
5
|
+
|
6
|
+
describe DeltaProcessor, "integration" do
|
7
|
+
|
8
|
+
before :all do
|
9
|
+
DatabasePopulator.new("test").with_type("customer").with_records(30).build
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
it "integration load relavent documents and apply delta" do
|
14
|
+
config = Config.create_from_file(File.dirname(__FILE__) + '/../couchdb.yml')
|
15
|
+
repository = Repository.new(config)
|
16
|
+
map_function = "function map(doc){ doc.address = 'new address'; return 'update';}"
|
17
|
+
rollback_function = "function map(doc){ delete doc.address; return 'update';}"
|
18
|
+
delta = Delta.new(1,'file_name',"customer",map_function,rollback_function)
|
19
|
+
delta_processor = DeltaProcessor.new(1,config,delta,repository)
|
20
|
+
delta_processor.apply
|
21
|
+
|
22
|
+
repository.get_documents("{\"map\":\"function (doc){if(doc.type=='customer'){emit(null,doc);}}\"}") do |row|
|
23
|
+
row['value']["address"].should == 'new address'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
it "integration load relavent documents apply delta and rollback" do
|
28
|
+
config = Config.create_from_file(File.dirname(__FILE__) + '/../couchdb.yml')
|
29
|
+
repository = Repository.new(config)
|
30
|
+
map_function = "function map(doc){ doc.address = 'new address'; return 'update';}"
|
31
|
+
rollback_function = "function map(doc){ delete doc.address; return 'update';}"
|
32
|
+
delta = Delta.new(1,'file_name',"customer",map_function,rollback_function)
|
33
|
+
delta_processor = DeltaProcessor.new(1,config,delta,repository)
|
34
|
+
delta_processor.apply
|
35
|
+
|
36
|
+
repository.get_documents("{\"map\":\"function (doc){if(doc.type=='customer'){emit(null,doc);}}\"}") do |row|
|
37
|
+
row['value']["address"].should == 'new address'
|
38
|
+
end
|
39
|
+
|
40
|
+
delta_processor.rollback
|
41
|
+
|
42
|
+
repository.get_documents("{\"map\":\"function (doc){if(doc.type=='customer'){emit(null,doc);}}\"}") do |row|
|
43
|
+
row['value'].has_key?("address").should == false
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
type: customer
|
2
|
+
map_function:
|
3
|
+
function map(doc)
|
4
|
+
{
|
5
|
+
if(doc.name == 'name_1')
|
6
|
+
return 'delete';
|
7
|
+
return null;
|
8
|
+
}
|
9
|
+
rollback_function:
|
10
|
+
function map(doc)
|
11
|
+
{
|
12
|
+
if(doc.name == 'name_2')
|
13
|
+
{
|
14
|
+
delete doc._id;
|
15
|
+
doc.name = 'name_1';
|
16
|
+
return 'create';
|
17
|
+
}
|
18
|
+
return null;
|
19
|
+
}
|
@@ -0,0 +1,20 @@
|
|
1
|
+
type: customer
|
2
|
+
map_function:
|
3
|
+
function map(doc)
|
4
|
+
{
|
5
|
+
if(doc.name == 'name_2')
|
6
|
+
{
|
7
|
+
delete doc._id;
|
8
|
+
doc.name = 'new name';
|
9
|
+
return 'create';
|
10
|
+
}
|
11
|
+
return null;
|
12
|
+
}
|
13
|
+
rollback_function:
|
14
|
+
function map(doc)
|
15
|
+
{
|
16
|
+
if(doc.name == 'new name')
|
17
|
+
return 'delete';
|
18
|
+
return null;
|
19
|
+
}
|
20
|
+
|
File without changes
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
require File.dirname(__FILE__) + '/database_populator'
|
3
|
+
|
4
|
+
module DeployCouch
|
5
|
+
|
6
|
+
describe Deploy, "load and execute deltas" do
|
7
|
+
before :all do
|
8
|
+
config = Config.create_from_file(File.dirname(__FILE__) + '/../couchdb.yml')
|
9
|
+
default_schema = {"_id"=>"schema__schema_document_key__",config.doc_type_field=>"__schema__", 'applied_deltas'=>[1,11], "type_versions"=>{'customer'=>3}}
|
10
|
+
DatabasePopulator.new("test").with_type("customer").with_schema(default_schema).with_records(30).build
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
it "load and execute deltas" do
|
15
|
+
config = Config.create_from_file(File.dirname(__FILE__) + '/../couchdb.yml')
|
16
|
+
deploy = Deploy.new(config)
|
17
|
+
deltas = deploy.run
|
18
|
+
deltas.count.should == 2
|
19
|
+
deltas[0].file_name.should == "12_delete_customer_name_1.yml"
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe Deploy, "load and execute deltas first database" do
|
25
|
+
|
26
|
+
before :all do
|
27
|
+
DatabasePopulator.new("test").with_type("customer").with_records(30).build
|
28
|
+
end
|
29
|
+
|
30
|
+
it "load and execute deltas" do
|
31
|
+
config = Config.create_from_file(File.dirname(__FILE__) + '/../couchdb.yml')
|
32
|
+
deploy = Deploy.new(config)
|
33
|
+
deltas = deploy.run
|
34
|
+
deltas.count.should == 5
|
35
|
+
deltas[0].file_name.should == "1_add_address_to_customer.yml"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe Deploy, "execute deltas and rollback all deltas" do
|
40
|
+
|
41
|
+
before :all do
|
42
|
+
DatabasePopulator.new("test").with_type("customer").with_records(30).build
|
43
|
+
end
|
44
|
+
|
45
|
+
it "load and execute deltas" do
|
46
|
+
config = Config.create_from_file(File.dirname(__FILE__) + '/../couchdb.yml')
|
47
|
+
deploy = Deploy.new(config)
|
48
|
+
deploy.run
|
49
|
+
deltas = deploy.rollback
|
50
|
+
deltas.count.should == 5
|
51
|
+
deltas[0].file_name.should == "13_copy_and_create_new_customer.yml"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
require File.dirname(__FILE__) + '/database_populator'
|
3
|
+
|
4
|
+
module DeployCouch
|
5
|
+
describe Repository, "execute a delta" do
|
6
|
+
|
7
|
+
before :all do
|
8
|
+
DatabasePopulator.new("db").with_type("customer").with_records(2).build
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
it "hit couchdb and load relavent documents to apply delta" do
|
13
|
+
map_function = '{"map":"function(doc) {emit(doc._id,doc);}"}'
|
14
|
+
config = Config.create_from_file(File.dirname(__FILE__) + '/../couchdb.yml')
|
15
|
+
config.merge_config({"database"=>"db"})
|
16
|
+
repository = Repository.new(config)
|
17
|
+
rows = []
|
18
|
+
repository.get_documents(map_function) do |row|
|
19
|
+
rows.push(row)
|
20
|
+
end
|
21
|
+
rows.count.should == 2
|
22
|
+
end
|
23
|
+
|
24
|
+
it "create schema document if it does not exist and get schema to verify" do
|
25
|
+
config = Config.create_from_file(File.dirname(__FILE__) + '/../couchdb.yml')
|
26
|
+
config.merge_config({"database"=>"db"})
|
27
|
+
schema = {"_id"=>"schema__schema_document_key__","type"=>"__schema__", 'applied_deltas'=>[], "type_versions"=>{}}
|
28
|
+
repository = Repository.new(config)
|
29
|
+
rows = []
|
30
|
+
repository.create_schema(schema)
|
31
|
+
schema_doc = repository.get_schema
|
32
|
+
schema_doc['_id'].should == schema['_id']
|
33
|
+
schema_doc['type'].should == schema['type']
|
34
|
+
schema_doc['applied_deltas'].should == schema['applied_deltas']
|
35
|
+
schema_doc['type_versions'].should == schema['type_versions']
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rspec'
|
3
|
+
require File.dirname(__FILE__) + '/../lib/deploy_couch'
|
4
|
+
|
5
|
+
|
6
|
+
def get_couchdb_config
|
7
|
+
DeployCouch::Config.new({"hostname"=>"localhost","port"=>1234,"database"=>"db",'delta_path'=>"path/to/deltas","config_folder_path" => "/somefolder","doc_type_field"=>"type","type_version_field" => 'type_version' })
|
8
|
+
end
|
9
|
+
|
10
|
+
def create_json_response(total_rows,num_of_records,offset=0)
|
11
|
+
h = {"total_rows"=> total_rows,"offset" => offset, "rows" => []}
|
12
|
+
(1..num_of_records).each do |x|
|
13
|
+
h["rows"].push({"id"=>x})
|
14
|
+
end
|
15
|
+
JSON.generate(h)
|
16
|
+
end
|
17
|
+
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
module DeployCouch
|
4
|
+
|
5
|
+
describe DbSchema, "Manage Schema" do
|
6
|
+
|
7
|
+
it "load schema document" do
|
8
|
+
repository = mock(Repository)
|
9
|
+
repository.should_receive(:get_schema).and_return({"_id"=>"special_key","type"=>"__schema__", 'applied_deltas'=>[1,2], "type_versions"=>{"customer"=>10}})
|
10
|
+
schema = DbSchema.load_or_create(get_couchdb_config,repository)
|
11
|
+
schema.applied_deltas.should == [1,2]
|
12
|
+
schema.type_versions.should == {"customer"=> 10}
|
13
|
+
end
|
14
|
+
|
15
|
+
it "create if schema document does not exist" do
|
16
|
+
repository = mock(Repository)
|
17
|
+
repository.should_receive(:get_schema).ordered
|
18
|
+
repository.should_receive(:create_schema).ordered
|
19
|
+
repository.should_receive(:get_schema).ordered.and_return({"_id"=>"special_key","type"=>"__schema__", 'applied_deltas'=>[], "type_versions"=>{}})
|
20
|
+
schema = DbSchema.load_or_create(get_couchdb_config,repository)
|
21
|
+
schema.applied_deltas.should == []
|
22
|
+
schema.type_versions.should == {}
|
23
|
+
end
|
24
|
+
|
25
|
+
it "on completion update schema document" do
|
26
|
+
schema_doc = {"_id"=>"special_key","type"=>"__schema__", 'applied_deltas'=>[], "type_versions"=>{}}
|
27
|
+
updated_schema_doc = {"_id"=>"special_key","type"=>"__schema__", 'applied_deltas'=>[1], "type_versions"=>{'type'=>1}}
|
28
|
+
repository = mock(Repository)
|
29
|
+
repository.should_receive(:put_document).ordered.with(updated_schema_doc)
|
30
|
+
repository.should_receive(:get_schema).ordered.and_return(updated_schema_doc)
|
31
|
+
schema = DbSchema.new(schema_doc,repository)
|
32
|
+
schema.completed(Delta.new(1,'file_name','type','map_function','rollback_function'))
|
33
|
+
schema.applied_deltas.should == [1]
|
34
|
+
schema.type_versions.should == {'type'=>1}
|
35
|
+
end
|
36
|
+
|
37
|
+
it "on rollback update schema document" do
|
38
|
+
schema_doc = {"_id"=>"special_key","type"=>"__schema__", 'applied_deltas'=>[1], "type_versions"=>{'type'=>1}}
|
39
|
+
updated_schema_doc = {"_id"=>"special_key","type"=>"__schema__", 'applied_deltas'=>[], "type_versions"=>{'type'=>0}}
|
40
|
+
repository = mock(Repository)
|
41
|
+
repository.should_receive(:put_document).ordered.with(updated_schema_doc)
|
42
|
+
repository.should_receive(:get_schema).ordered.and_return(updated_schema_doc)
|
43
|
+
schema = DbSchema.new(schema_doc,repository)
|
44
|
+
schema.rollback(Delta.new(1,'file_name','type','map_function','rollback_function'))
|
45
|
+
schema.applied_deltas.should == []
|
46
|
+
schema.type_versions.should == {'type'=>0}
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
it "should get next type version for a given type" do
|
52
|
+
schema_doc = {"_id"=>"special_key","type"=>"__schema__", 'applied_deltas'=>[], "type_versions"=>{'customer'=> 20}}
|
53
|
+
repository = mock(Repository)
|
54
|
+
schema = DbSchema.new(schema_doc,repository)
|
55
|
+
schema.get_next_type_version_for('customer').should == 21
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should get next type version for a given type for non-existing schema" do
|
59
|
+
schema_doc = {"_id"=>"special_key","type"=>"__schema__", 'applied_deltas'=>[], "type_versions"=>{}}
|
60
|
+
repository = mock(Repository)
|
61
|
+
schema = DbSchema.new(schema_doc,repository)
|
62
|
+
schema.get_next_type_version_for('customer').should == 1
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
module DeployCouch
|
4
|
+
|
5
|
+
describe DeltaProcessor, "execute a delta" do
|
6
|
+
it "load relavent documents and apply delta" do
|
7
|
+
mock_repository = mock(Repository)
|
8
|
+
map_function = <<-JSON
|
9
|
+
{
|
10
|
+
"map":"function(doc){if(doc.type=='customer' && (!doc.type_version || doc.type_version < 1)){new_doc = eval(uneval(doc)); var method = map(new_doc); if(method) emit(method,new_doc);}}
|
11
|
+
function map(doc){ doc.address = 'new address';}"
|
12
|
+
}
|
13
|
+
JSON
|
14
|
+
h={'key'=> 'update' , 'value'=>{'id'=>"1",'name'=>"name_1", 'address'=>'new address'}}
|
15
|
+
mock_repository.should_receive(:get_documents_to_modify).with(map_function).and_yield(h)
|
16
|
+
mock_repository.should_receive(:put_document).with(h['value'].merge({'type_version' => 1}))
|
17
|
+
delta = Delta.new(1,'file_name',"customer","function map(doc){ doc.address = 'new address';}",'rollback_function')
|
18
|
+
next_type_version = 1
|
19
|
+
delta_processor = DeltaProcessor.new(next_type_version,get_couchdb_config,delta,mock_repository)
|
20
|
+
delta_processor.apply
|
21
|
+
end
|
22
|
+
|
23
|
+
it "load relavent documents and apply delta" do
|
24
|
+
mock_repository = mock(Repository)
|
25
|
+
map_function = <<-JSON
|
26
|
+
{
|
27
|
+
"map":"function(doc){if(doc.type=='customer' && (!doc.type_version || doc.type_version < 11)){new_doc = eval(uneval(doc)); var method = map(new_doc); if(method) emit(method,new_doc);}}
|
28
|
+
function map(doc){ doc.address = 'new address';}"
|
29
|
+
}
|
30
|
+
JSON
|
31
|
+
h={'key'=> 'delete' , 'value'=>{'id'=>"1",'name'=>"name_1", 'address'=>'new address'}}
|
32
|
+
mock_repository.should_receive(:get_documents_to_modify).with(map_function).and_yield(h)
|
33
|
+
mock_repository.should_receive(:delete_document).with(h['value'])
|
34
|
+
delta = Delta.new(1,'file_name',"customer","function map(doc){ doc.address = 'new address';}",'rollback_function')
|
35
|
+
next_type_version = 11
|
36
|
+
delta_processor = DeltaProcessor.new(next_type_version,get_couchdb_config,delta,mock_repository)
|
37
|
+
delta_processor.apply
|
38
|
+
end
|
39
|
+
|
40
|
+
it "load relavent documents and rollback delta" do
|
41
|
+
mock_repository = mock(Repository)
|
42
|
+
map_function = <<-JSON
|
43
|
+
{
|
44
|
+
"map":"function(doc){if(doc.type=='customer' && (doc.type_version >= 10)){new_doc = eval(uneval(doc)); var method = map(new_doc); if(method) emit(method,new_doc);}}
|
45
|
+
function map(doc){ delete doc.address; return 'update';}"
|
46
|
+
}
|
47
|
+
JSON
|
48
|
+
h={'key'=> 'update' , 'value'=>{'id'=>"1",'name'=>"name_1", 'address'=>'new address'}}
|
49
|
+
mock_repository.should_receive(:get_documents_to_modify).with(map_function).and_yield(h)
|
50
|
+
mock_repository.should_receive(:put_document).with(h['value'].merge({'type_version' => 9}))
|
51
|
+
delta = Delta.new(1,'file_name',"customer","function map(doc){ doc.address = 'new address';}","function map(doc){ delete doc.address; return 'update';}")
|
52
|
+
next_type_version = 11
|
53
|
+
delta_processor = DeltaProcessor.new(next_type_version,get_couchdb_config,delta,mock_repository)
|
54
|
+
delta_processor.rollback
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
module DeployCouch
|
4
|
+
describe Deploy, "load and execute deltas" do
|
5
|
+
it "load and execute deltas in correct order" do
|
6
|
+
config = get_couchdb_config
|
7
|
+
delta = Delta.new(1,'file_name','type','map function','rollback_function')
|
8
|
+
delta2 = Delta.new(10,'file_name2','type','map function','rollback_function')
|
9
|
+
deltas_map = {1=>delta,10=>delta2}
|
10
|
+
|
11
|
+
mock_delta_loader = mock(DeltaLoader)
|
12
|
+
DeltaLoader.should_receive(:new).with("/somefolder/path/to/deltas").and_return(mock_delta_loader)
|
13
|
+
mock_delta_loader.should_receive(:get_deltas).and_return(deltas_map)
|
14
|
+
|
15
|
+
mock_repository = mock(Repository)
|
16
|
+
Repository.should_receive(:new).with(config).and_return(mock_repository)
|
17
|
+
|
18
|
+
mock_delta_processor = mock(DeltaProcessor)
|
19
|
+
DeltaProcessor.should_receive(:new).with(1,config,delta,mock_repository).ordered.and_return(mock_delta_processor)
|
20
|
+
mock_delta_processor.should_receive(:apply)
|
21
|
+
|
22
|
+
mock_delta_processor2 = mock(DeltaProcessor)
|
23
|
+
DeltaProcessor.should_receive(:new).with(2,config,delta2,mock_repository).ordered.and_return(mock_delta_processor2)
|
24
|
+
mock_delta_processor2.should_receive(:apply)
|
25
|
+
|
26
|
+
mock_couch_db_schema = mock(DbSchema)
|
27
|
+
mock_couch_db_schema.should_receive(:applied_deltas).and_return([])
|
28
|
+
DbSchema.should_receive(:load_or_create).with(config,mock_repository).ordered.and_return(mock_couch_db_schema)
|
29
|
+
mock_couch_db_schema.should_receive(:get_next_type_version_for).with('type').ordered.and_return(1)
|
30
|
+
mock_couch_db_schema.should_receive(:completed).ordered.with(delta)
|
31
|
+
mock_couch_db_schema.should_receive(:get_next_type_version_for).with('type').ordered.and_return(2)
|
32
|
+
mock_couch_db_schema.should_receive(:completed).ordered.with(delta2)
|
33
|
+
|
34
|
+
deploy = Deploy.new(config)
|
35
|
+
deploy.run
|
36
|
+
end
|
37
|
+
|
38
|
+
it "executes unapplied deltas only in correct order" do
|
39
|
+
config = get_couchdb_config
|
40
|
+
delta = Delta.new(1,'file_name','type','map function','rollback_function')
|
41
|
+
delta2 = Delta.new(10,'file_name2','type','map function','rollback_function')
|
42
|
+
deltas_map = {1=>delta,10=>delta2}
|
43
|
+
|
44
|
+
mock_delta_loader = mock(DeltaLoader)
|
45
|
+
DeltaLoader.should_receive(:new).with("/somefolder/path/to/deltas").and_return(mock_delta_loader)
|
46
|
+
mock_delta_loader.should_receive(:get_deltas).and_return(deltas_map)
|
47
|
+
|
48
|
+
mock_repository = mock(Repository)
|
49
|
+
Repository.should_receive(:new).with(config).and_return(mock_repository)
|
50
|
+
|
51
|
+
mock_delta_processor = mock(DeltaProcessor)
|
52
|
+
DeltaProcessor.should_receive(:new).with(1,config,delta2,mock_repository).and_return(mock_delta_processor)
|
53
|
+
mock_delta_processor.should_receive(:apply)
|
54
|
+
|
55
|
+
mock_couch_db_schema = mock(DbSchema)
|
56
|
+
mock_couch_db_schema.should_receive(:applied_deltas).and_return([1])
|
57
|
+
DbSchema.should_receive(:load_or_create).with(config,mock_repository).ordered.and_return(mock_couch_db_schema)
|
58
|
+
mock_couch_db_schema.should_receive(:get_next_type_version_for).with('type').ordered.and_return(1)
|
59
|
+
mock_couch_db_schema.should_receive(:completed).with(delta2)
|
60
|
+
|
61
|
+
deploy = Deploy.new(config)
|
62
|
+
deploy.run
|
63
|
+
end
|
64
|
+
|
65
|
+
it "executes rollback deltas" do
|
66
|
+
config = get_couchdb_config
|
67
|
+
delta = Delta.new(1,'file_name','type','map function','rollback_function')
|
68
|
+
delta2 = Delta.new(10,'file_name2','type','map function','rollback_function')
|
69
|
+
deltas_map = {1=>delta,10=>delta2}
|
70
|
+
|
71
|
+
mock_delta_loader = mock(DeltaLoader)
|
72
|
+
DeltaLoader.should_receive(:new).with("/somefolder/path/to/deltas").and_return(mock_delta_loader)
|
73
|
+
mock_delta_loader.should_receive(:get_deltas).and_return(deltas_map)
|
74
|
+
|
75
|
+
mock_repository = mock(Repository)
|
76
|
+
Repository.should_receive(:new).with(config).and_return(mock_repository)
|
77
|
+
|
78
|
+
mock_delta_processor = mock(DeltaProcessor)
|
79
|
+
DeltaProcessor.should_receive(:new).with(2,config,delta,mock_repository).and_return(mock_delta_processor)
|
80
|
+
mock_delta_processor.should_receive(:rollback)
|
81
|
+
|
82
|
+
mock_couch_db_schema = mock(DbSchema)
|
83
|
+
mock_couch_db_schema.should_receive(:applied_deltas).and_return([1])
|
84
|
+
DbSchema.should_receive(:load_or_create).with(config,mock_repository).ordered.and_return(mock_couch_db_schema)
|
85
|
+
mock_couch_db_schema.should_receive(:get_next_type_version_for).with('type').ordered.and_return(2)
|
86
|
+
mock_couch_db_schema.should_receive(:rollback).with(delta)
|
87
|
+
|
88
|
+
deploy = Deploy.new(config)
|
89
|
+
deploy.rollback
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,128 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
module DeployCouch
|
4
|
+
|
5
|
+
describe Repository, "execute a delta" do
|
6
|
+
it "load relavent documents" do
|
7
|
+
map_function = "{'map':'function(doc){emit(null,doc);}'}"
|
8
|
+
mock_server = mock(Server)
|
9
|
+
Server.should_receive(:new).with("localhost",1234).and_return(mock_server)
|
10
|
+
mock_response = mock(Net::HTTPResponse)
|
11
|
+
mock_server.should_receive(:post).with("/db/_temp_view?limit=10&skip=0",map_function).and_return(mock_response)
|
12
|
+
json = create_json_response(1,1)
|
13
|
+
mock_response.should_receive(:body).and_return(json)
|
14
|
+
|
15
|
+
repository = Repository.new(get_couchdb_config)
|
16
|
+
rows = []
|
17
|
+
repository.get_documents(map_function) do |row|
|
18
|
+
rows.push(row)
|
19
|
+
end
|
20
|
+
rows.count.should == 1
|
21
|
+
end
|
22
|
+
|
23
|
+
it "load relavent documents" do
|
24
|
+
map_function = "{'map':'function(doc){emit(null,doc);}'}"
|
25
|
+
mock_server = mock(Server)
|
26
|
+
Server.should_receive(:new).with("localhost",1234).and_return(mock_server)
|
27
|
+
mock_response = mock(Net::HTTPResponse)
|
28
|
+
mock_server.should_receive(:post).with("/db/_temp_view?limit=10&skip=0",map_function).and_return(mock_response)
|
29
|
+
json = create_json_response(15,10)
|
30
|
+
mock_response.should_receive(:body).and_return(json)
|
31
|
+
|
32
|
+
mock_response = mock(Net::HTTPResponse)
|
33
|
+
mock_server.should_receive(:post).with("/db/_temp_view?limit=10&skip=10",map_function).and_return(mock_response)
|
34
|
+
json = create_json_response(15,5,10)
|
35
|
+
mock_response.should_receive(:body).and_return(json)
|
36
|
+
|
37
|
+
|
38
|
+
repository = Repository.new(get_couchdb_config)
|
39
|
+
rows = []
|
40
|
+
repository.get_documents(map_function) do |row|
|
41
|
+
rows.push(row)
|
42
|
+
end
|
43
|
+
rows.count.should == 15
|
44
|
+
end
|
45
|
+
|
46
|
+
it "put document to update document" do
|
47
|
+
json = {"_id"=> 1757, "name" => "name_1"}
|
48
|
+
mock_server = mock(Server)
|
49
|
+
Server.should_receive(:new).with("localhost",1234).and_return(mock_server)
|
50
|
+
mock_response = mock(Net::HTTPResponse)
|
51
|
+
mock_server.should_receive(:put).with("/db/#{json['_id']}",json.to_json).and_return(mock_response)
|
52
|
+
|
53
|
+
repository = Repository.new(get_couchdb_config)
|
54
|
+
rows = []
|
55
|
+
repository.put_document(json)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "delete document" do
|
59
|
+
json = {"_id"=> 1757, "name" => "name_1","_rev"=> 10}
|
60
|
+
mock_server = mock(Server)
|
61
|
+
Server.should_receive(:new).with("localhost",1234).and_return(mock_server)
|
62
|
+
mock_response = mock(Net::HTTPResponse)
|
63
|
+
mock_server.should_receive(:delete).with("/db/#{json['_id']}?rev=10").and_return(mock_response)
|
64
|
+
|
65
|
+
repository = Repository.new(get_couchdb_config)
|
66
|
+
rows = []
|
67
|
+
repository.delete_document(json)
|
68
|
+
end
|
69
|
+
|
70
|
+
it "get schema document" do
|
71
|
+
json = {"_id"=> 1757, "name" => "name_1","_rev"=> 10}
|
72
|
+
mock_server = mock(Server)
|
73
|
+
Server.should_receive(:new).with("localhost",1234).and_return(mock_server)
|
74
|
+
mock_response = mock(Net::HTTPResponse)
|
75
|
+
schema = {"_id"=>"special_key","type"=>"__schema__", 'applied_deltas'=>[1,2], "type_versions"=>{"customer"=>10}}
|
76
|
+
mock_response.should_receive(:body).and_return(schema.to_json)
|
77
|
+
mock_response.should_receive(:kind_of?).with(Net::HTTPSuccess).and_return(true)
|
78
|
+
mock_server.should_receive(:get).with("/db/schema__schema_document_key__", {:suppress_exceptions=>true}).and_return(mock_response)
|
79
|
+
|
80
|
+
repository = Repository.new(get_couchdb_config)
|
81
|
+
rows = []
|
82
|
+
repository.get_schema.should == schema
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
it "load relavent documents to apply delta with paging" do
|
87
|
+
map_function = "{'map':'function(doc){emit(null,doc);}'}"
|
88
|
+
mock_server = mock(Server)
|
89
|
+
Server.should_receive(:new).with("localhost",1234).and_return(mock_server)
|
90
|
+
mock_response = mock(Net::HTTPResponse)
|
91
|
+
mock_server.should_receive(:post).with("/db/_temp_view?limit=10",map_function).and_return(mock_response)
|
92
|
+
json = create_json_response(15,10)
|
93
|
+
mock_response.should_receive(:body).and_return(json)
|
94
|
+
|
95
|
+
mock_response = mock(Net::HTTPResponse)
|
96
|
+
mock_server.should_receive(:post).with("/db/_temp_view?limit=10",map_function).and_return(mock_response)
|
97
|
+
json = create_json_response(5,5,0)
|
98
|
+
mock_response.should_receive(:body).and_return(json)
|
99
|
+
|
100
|
+
|
101
|
+
repository = Repository.new(get_couchdb_config)
|
102
|
+
rows = []
|
103
|
+
repository.get_documents_to_modify(map_function) do |row|
|
104
|
+
rows.push(row)
|
105
|
+
end
|
106
|
+
rows.count.should == 15
|
107
|
+
end
|
108
|
+
|
109
|
+
it "load relavent documents to apply delta with paging for page size rows" do
|
110
|
+
map_function = "{'map':'function(doc){emit(null,doc);}'}"
|
111
|
+
mock_server = mock(Server)
|
112
|
+
Server.should_receive(:new).with("localhost",1234).and_return(mock_server)
|
113
|
+
mock_response = mock(Net::HTTPResponse)
|
114
|
+
mock_server.should_receive(:post).with("/db/_temp_view?limit=10",map_function).and_return(mock_response)
|
115
|
+
json = create_json_response(10,10)
|
116
|
+
mock_response.should_receive(:body).and_return(json)
|
117
|
+
|
118
|
+
repository = Repository.new(get_couchdb_config)
|
119
|
+
rows = []
|
120
|
+
repository.get_documents_to_modify(map_function) do |row|
|
121
|
+
rows.push(row)
|
122
|
+
end
|
123
|
+
rows.count.should == 10
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|