diff options
| author | RobertDober | 2017-04-18 09:44:21 +0200 |
|---|---|---|
| committer | Robert | 2017-04-18 20:05:36 +0200 |
| commit | 2bcd11d80beb632f5204672bdbd9efc8b706ca12 (patch) | |
| tree | f0aa293fb46440ac87fb71bc25798767ff83232f /spec/lib | |
| parent | d582f2868c0d8af42c18257f83974afbae619583 (diff) | |
| download | chouette-core-2bcd11d80beb632f5204672bdbd9efc8b706ca12.tar.bz2 | |
basic clone_schema spex added; Refs #2864
Diffstat (limited to 'spec/lib')
| -rwxr-xr-x[-rw-r--r--] | spec/lib/af83/stored_procedures/clone_schema_spec.rb | 122 |
1 files changed, 112 insertions, 10 deletions
diff --git a/spec/lib/af83/stored_procedures/clone_schema_spec.rb b/spec/lib/af83/stored_procedures/clone_schema_spec.rb index 646e97d9f..6ec0de5e7 100644..100755 --- a/spec/lib/af83/stored_procedures/clone_schema_spec.rb +++ b/spec/lib/af83/stored_procedures/clone_schema_spec.rb @@ -1,19 +1,121 @@ -require 'rails_helper' +require 'spec_helper' + +include Support::PGCatalog RSpec.describe StoredProcedures do + let( :source_schema ){ "source_schema" } + let( :target_schema ){ "target_schema" } + let( :child_table ){ "children" } + let( :parent_table ){ "parents" } + + before do + create_schema_with_tables + StoredProcedures.create_stored_procedure :clone_schema + end + + context "meta specs describe source schema's introspection" do + it "shows, sequences are correctly read", :meta do + expect(get_sequences(source_schema, child_table)) + .to eq([{"sequence_name"=>"#{child_table}_id_seq", + "last_value"=>"1", + "start_value"=>"1", + "increment_by"=>"1", + "max_value"=>"9223372036854775807", + "min_value"=>"1", + "cache_value"=>"1", + "log_cnt"=>"0", + "is_cycled"=>"f", + "is_called"=>"f"}]) + + expect(get_sequences(source_schema, parent_table)) + .to eq([{"sequence_name"=>"#{parent_table}_id_seq", + "last_value"=>"1", + "start_value"=>"1", + "increment_by"=>"1", + "max_value"=>"9223372036854775807", + "min_value"=>"1", + "cache_value"=>"1", + "log_cnt"=>"0", + "is_cycled"=>"f", + "is_called"=>"f"}]) + end + + it "shows foreign key constraints are correctly read" do + expect( get_foreign_keys(source_schema, child_table) ) + .to eq([{ + "constraint_name" => "children_parents", + "constraint_def" => "FOREIGN KEY (parents_id) REFERENCES source_schema.parents(id)"}]) + end + end + + context "before cloning" do + it "target schema does not exist" do + expect( get_schema_oid(target_schema) ).to be_nil + end + end - include Support::PGCatalog - context "clone_schema creates correct table in dest schema" do + context "after cloning" do before do - drop_schema!(dest_schema_name) + described_class.invoke_stored_procedure(:clone_schema, source_schema, target_schema, false) + end + + it "target schema does exist" do + expect( get_schema_oid(target_schema) ).not_to be_nil + end + + it "has the correct sequences" do + expect(get_sequences(target_schema, child_table)) + .to eq([{"sequence_name"=>"#{child_table}_id_seq", + "last_value"=>"1", + "start_value"=>"1", + "increment_by"=>"1", + "max_value"=>"9223372036854775807", + "min_value"=>"1", + "cache_value"=>"1", + "log_cnt"=>"0", + "is_cycled"=>"f", + "is_called"=>"f"}]) + + expect(get_sequences(target_schema, parent_table)) + .to eq([{"sequence_name"=>"#{parent_table}_id_seq", + "last_value"=>"1", + "start_value"=>"1", + "increment_by"=>"1", + "max_value"=>"9223372036854775807", + "min_value"=>"1", + "cache_value"=>"1", + "log_cnt"=>"0", + "is_cycled"=>"f", + "is_called"=>"f"}]) end - it "creates the schema" do - expect( get_fks!(dest_schema_name, "access_links") ).to be_empty - described_class.invoke_stored_procedure(:clone_schema, source_schema_name, dest_schema_name, true) - expect( get_fks!(dest_schema_name, "access_links") ) - .to eq( [{"constraint_name"=>"aclk_acpt_fkey", - "constraint_def"=>"FOREIGN KEY (access_point_id) REFERENCES parissudest201604_v1.access_points(id)"}]) + it "has the correct foreign keys" do + expect( get_foreign_keys(target_schema, child_table) ) + .to eq([{ + "constraint_name" => "children_parents", + "constraint_def" => "FOREIGN KEY (parents_id) REFERENCES target_schema.parents(id)"}]) end + end + end + +def create_schema_with_tables + execute("CREATE SCHEMA IF NOT EXISTS #{source_schema}") + execute <<-EOSQL + DROP SCHEMA IF EXISTS #{source_schema} CASCADE; + CREATE SCHEMA #{source_schema}; + + CREATE TABLE #{source_schema}.#{parent_table} ( + id bigserial PRIMARY KEY + ); + CREATE TABLE #{source_schema}.#{child_table} ( + id bigserial PRIMARY KEY, + #{parent_table}_id bigint + ); + ALTER TABLE #{source_schema}.#{child_table} + ADD CONSTRAINT #{child_table}_#{parent_table} + FOREIGN KEY( #{parent_table}_id ) REFERENCES #{source_schema}.#{parent_table}(id); + EOSQL +end + |
