diff options
Diffstat (limited to 'spec')
| -rwxr-xr-x | spec/lib/af83/stored_procedures/clone_schema_spec.rb | 52 | ||||
| -rw-r--r-- | spec/support/pg_catalog.rb | 8 | ||||
| -rw-r--r-- | spec/workers/referential_cloning_worker_spec.rb | 45 |
3 files changed, 101 insertions, 4 deletions
diff --git a/spec/lib/af83/stored_procedures/clone_schema_spec.rb b/spec/lib/af83/stored_procedures/clone_schema_spec.rb index 6ec0de5e7..69422e4ef 100755 --- a/spec/lib/af83/stored_procedures/clone_schema_spec.rb +++ b/spec/lib/af83/stored_procedures/clone_schema_spec.rb @@ -14,7 +14,25 @@ RSpec.describe StoredProcedures do end context "meta specs describe source schema's introspection" do - it "shows, sequences are correctly read", :meta do + it "table information is correctly read" do + expect(get_table_information(source_schema, child_table)) + .to eq([{"table_catalog"=>"chouette_test", + "table_schema"=>"source_schema", + "table_name"=>"children", + "table_type"=>"BASE TABLE", + "self_referencing_column_name"=>nil, + "reference_generation"=>nil, + "user_defined_type_catalog"=>nil, + "user_defined_type_schema"=>nil, + "user_defined_type_name"=>nil, + "is_insertable_into"=>"YES", + "is_typed"=>"NO", + "commit_action"=>nil}]) + + expect( get_table_information(target_schema, child_table) ).to be_empty + end + + it "sequences are correctly read", :meta do expect(get_sequences(source_schema, child_table)) .to eq([{"sequence_name"=>"#{child_table}_id_seq", "last_value"=>"1", @@ -63,6 +81,36 @@ RSpec.describe StoredProcedures do expect( get_schema_oid(target_schema) ).not_to be_nil end + it "table information is correctly read" do + expect(get_table_information(source_schema, child_table)) + .to eq([{"table_catalog"=>"chouette_test", + "table_schema"=>"source_schema", + "table_name"=>"children", + "table_type"=>"BASE TABLE", + "self_referencing_column_name"=>nil, + "reference_generation"=>nil, + "user_defined_type_catalog"=>nil, + "user_defined_type_schema"=>nil, + "user_defined_type_name"=>nil, + "is_insertable_into"=>"YES", + "is_typed"=>"NO", + "commit_action"=>nil}]) + + expect( get_table_information(target_schema, child_table)) + .to eq([{"table_catalog"=>"chouette_test", + "table_schema"=>"target_schema", + "table_name"=>"children", + "table_type"=>"BASE TABLE", + "self_referencing_column_name"=>nil, + "reference_generation"=>nil, + "user_defined_type_catalog"=>nil, + "user_defined_type_schema"=>nil, + "user_defined_type_name"=>nil, + "is_insertable_into"=>"YES", + "is_typed"=>"NO", + "commit_action"=>nil}]) + end + it "has the correct sequences" do expect(get_sequences(target_schema, child_table)) .to eq([{"sequence_name"=>"#{child_table}_id_seq", @@ -95,7 +143,7 @@ RSpec.describe StoredProcedures do "constraint_name" => "children_parents", "constraint_def" => "FOREIGN KEY (parents_id) REFERENCES target_schema.parents(id)"}]) end - + end end diff --git a/spec/support/pg_catalog.rb b/spec/support/pg_catalog.rb index 3cd3966c7..dd0219482 100644 --- a/spec/support/pg_catalog.rb +++ b/spec/support/pg_catalog.rb @@ -1,6 +1,9 @@ module Support module PGCatalog + def get_columns(schema_name, table_name) + execute("SELECT * from information_schema.columns WHERE table_name = '#{table_name}' AND table_schema = '#{schema_name}'") + end def get_foreign_keys(schema_oid, table_name) schema_oid = get_schema_oid(schema_oid) unless Integer === schema_oid return [] unless schema_oid @@ -25,8 +28,9 @@ module Support end.flat_map(&:to_a) end - def table_from_schema(schema_name, table_name) - execute + def get_table_information(schema_name, table_name) + execute("SELECT * FROM information_schema.tables WHERE table_name = '#{table_name}' AND table_schema = '#{schema_name}'") + .to_a end diff --git a/spec/workers/referential_cloning_worker_spec.rb b/spec/workers/referential_cloning_worker_spec.rb new file mode 100644 index 000000000..85d771742 --- /dev/null +++ b/spec/workers/referential_cloning_worker_spec.rb @@ -0,0 +1,45 @@ +require 'spec_helper' +require 'ostruct' + +RSpec.describe ReferentialCloningWorker do + + context "given a refererntial cloning" do + + let( :id ){ double } + + let( :worker ){ described_class.new } + + + let( :source_schema ){ "source_schema" } + let( :target_schema ){ "#{source_schema}_tmp" } + let( :referential_cloning ){ OpenStruct.new(source_referential: OpenStruct.new(slug: source_schema)) } + + before do + expect( ReferentialCloning ).to receive(:find).with(id).and_return(referential_cloning) + expect( StoredProcedures ) + .to receive(:invoke_stored_procedure) + .with(:clone_schema, source_schema, target_schema, true) + + expect( worker ).to receive(:execute_sql).with( "DROP SCHEMA #{source_schema} CASCADE;" ) + + expect( referential_cloning ).to receive(:run!) + end + + it "invokes the correct stored procedure, updates the database and the AASM" do + expect( worker ).to receive(:execute_sql).with( "ALTER SCHEMA #{target_schema} RENAME TO #{source_schema};" ) + expect( referential_cloning ).to receive(:successful!) + worker.perform(id) + end + + it "handles failure correctly" do + expect( worker ) + .to receive(:execute_sql) + .with( "ALTER SCHEMA #{target_schema} RENAME TO #{source_schema};" ) + .and_raise(RuntimeError) + + expect( referential_cloning ).to receive(:failed!) + worker.perform(id) + end + end + +end |
