diff options
| author | Robert | 2017-06-09 17:04:38 +0200 |
|---|---|---|
| committer | Robert | 2017-06-09 17:25:06 +0200 |
| commit | 63a893c22feca2c26cd8eecef2e6deb8ff97bd26 (patch) | |
| tree | a82f97b3732379167760a57012cc3197afd9b846 /spec | |
| parent | 38e5a5329541a54f98d771c3aa252b91b823b94f (diff) | |
| download | chouette-core-63a893c22feca2c26cd8eecef2e6deb8ff97bd26.tar.bz2 | |
refs 3604 @12h all tests pass
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/lib/af83/cloning/clone_schema_spec.rb | 46 | ||||
| -rw-r--r-- | spec/support/bare_sql.rb | 58 | ||||
| -rw-r--r-- | spec/support/hash.rb | 6 | ||||
| -rw-r--r-- | spec/support/pg_catalog.rb | 19 | ||||
| -rw-r--r-- | spec/workers/referential_cloning_worker_spec.rb | 7 |
5 files changed, 106 insertions, 30 deletions
diff --git a/spec/lib/af83/cloning/clone_schema_spec.rb b/spec/lib/af83/cloning/clone_schema_spec.rb index 5e441cc8e..3d541f3e9 100644 --- a/spec/lib/af83/cloning/clone_schema_spec.rb +++ b/spec/lib/af83/cloning/clone_schema_spec.rb @@ -13,8 +13,6 @@ RSpec.describe AF83::SchemaCloner, type: :pg_catalog do end it "table information is correctly duplicated" do - expect_same_sequence_params("#{child_table}_id_seq") - expect_same_sequence_params("#{parent_table}_id_seq") expect(get_table_information(source_schema, child_table)) .to eq([{"table_schema"=>"source_schema", "table_name"=>"children", @@ -42,22 +40,44 @@ RSpec.describe AF83::SchemaCloner, type: :pg_catalog do "commit_action"=>nil}]) end + it "table content is the same and sequences are synchronized" do + expect_same_content(parent_table) + expect_same_content(child_table) + + expect_same_sequence_params("#{parent_table}_id_seq") + expect_same_sequence_params("#{child_table}_id_seq") + end + + it "has correctly updated default values" do + child_table_pk_default = get_columns(target_schema, child_table) + .find{ |col| col["column_name"] == "id" }["column_default"] + expect( child_table_pk_default ).to eq("nextval('#{target_schema}.children_id_seq'::regclass)") + end - xit "has the correct foreign keys" do + 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 - xit "the data has been copied" do - end + xit "it has the correct unique keys UNTESTABLE SO FAR" do + insert source_schema, child_table, "#{parent_table}_id" => 1, some_key: 400 + insert target_schema, child_table, "#{parent_table}_id" => 1, some_key: 400 + reinsert_sql = "INSERT INTO #{source_schema}.#{child_table} (#{parent_table}_id, some_key) VALUES (1, 400)" + expect{ execute(reinsert_sql) rescue nil}.not_to change{ execute("SELECT COUNT(*) FROM #{source_schema}.#{child_table}") } - xit "it has the correct unique keys" - + # expect{ insert(target_schema, child_table, "#{parent_table}_id" => 1, some_key: 400) }.to raise_error(ActiveRecord::RecordNotUnique) end - xit "inserts are independent" do + it "inserts are independent" do + insert source_schema, child_table, "#{parent_table}_id" => 1, some_key: 400 + insert target_schema, child_table, "#{parent_table}_id" => 1, some_key: 400 + last_source = get_content(source_schema, child_table).last + last_target = get_content(target_schema, child_table).last + + expect( last_source ).to eq("id"=>"3", "parents_id"=>"1", "some_key"=>"400", "is_orphan"=>"f") + expect( last_target ).to eq("id"=>"3", "parents_id"=>"1", "some_key"=>"400", "is_orphan"=>"f") end end @@ -77,13 +97,17 @@ RSpec.describe AF83::SchemaCloner, type: :pg_catalog do is_orphan boolean DEFAULT false ); - CREATE UNIQUE INDEX #{source_schema}.#{child_table}_some_key_idx ON #{source_schema}.#{child_table} (some_key); + CREATE UNIQUE INDEX #{child_table}_some_key_idx ON #{source_schema}.#{child_table} (some_key); ALTER TABLE #{source_schema}.#{child_table} ADD CONSTRAINT #{child_table}_#{parent_table} FOREIGN KEY( #{parent_table}_id ) REFERENCES #{source_schema}.#{parent_table}(id); - INSERT INTO #{source_schema}.#{parent_table} VALUES (100); - INSERT INTO #{source_schema}.#{child_table} VALUES (1, 100); + + INSERT INTO #{source_schema}.#{parent_table} VALUES (DEFAULT); + INSERT INTO #{source_schema}.#{parent_table} VALUES (DEFAULT); EOSQL + insert source_schema, child_table, "#{parent_table}_id" => 1, some_key: 200 + insert source_schema, child_table, "#{parent_table}_id" => 2, some_key: 300, is_orphan: true end + end diff --git a/spec/support/bare_sql.rb b/spec/support/bare_sql.rb new file mode 100644 index 000000000..03a50ef77 --- /dev/null +++ b/spec/support/bare_sql.rb @@ -0,0 +1,58 @@ +module Support + module BareSQL + + def insert(schema, table, values) + execute "INSERT INTO #{schema}.#{table} (#{_keys(values)}) VALUES (#{_values values})" + end + + def execute(sql) + base_connection.execute(sql) + end + + def expect_same_content(table_name) + expected_content = get_content(source_schema, table_name) + actual_content = get_content(target_schema, table_name) + expect( actual_content ).to eq(expected_content) + end + + def expect_same_sequence_params(sequence_name) + expected_seq = Hash.without(get_sequences(source_schema, sequence_name).first, 'log_cnt') + actual_seq = Hash.without(get_sequences(target_schema, sequence_name).first, 'log_cnt') + expect( actual_seq ).to eq(expected_seq) + end + + def get_content(schema_name, table_name) + execute("SELECT * FROM #{schema_name}.#{table_name}").to_a + end + + private + + def base_connection + ActiveRecord::Base.connection + end + + def _keys(values) + values.keys.map(&:to_s).join(", ") + end + + def _values(values) + values + .values + .map(&method(:_format)) + .join(', ') + end + + def _format(val) + case val + when String + "'#{val}'" + when TrueClass + "'t'" + when FalseClass + "'f'" + else + val.to_s + end + end + end +end diff --git a/spec/support/hash.rb b/spec/support/hash.rb new file mode 100644 index 000000000..ec9a2f895 --- /dev/null +++ b/spec/support/hash.rb @@ -0,0 +1,6 @@ +class << Hash + def without(hash, *keys) + nk = hash.keys - keys + Hash[*nk.zip(hash.values_at(*nk)).flatten] + end +end diff --git a/spec/support/pg_catalog.rb b/spec/support/pg_catalog.rb index b902acf82..38c75aabe 100644 --- a/spec/support/pg_catalog.rb +++ b/spec/support/pg_catalog.rb @@ -1,17 +1,11 @@ module Support module PGCatalog - # TODO: Check what of the follwowing can be done with ActiveRecord. E.g. - # @connection.foreign_keys(table)... - - def expect_same_sequence_params(sequence_name) - expected_seq = get_sequences(source_schema, sequence_name) - actual_seq = get_sequences(target_schema, sequence_name) - expect( actual_seq ).to eq(expected_seq) - end + include BareSQL def get_columns(schema_name, table_name) - execute("SELECT * from information_schema.columns WHERE table_name = '#{table_name}' AND table_schema = '#{schema_name}'") + execute("SELECT column_name, column_default FROM information_schema.columns WHERE table_name = '#{table_name}' AND table_schema = '#{schema_name}'").to_a end + def get_foreign_keys(schema_oid, table_name) schema_oid = get_schema_oid(schema_oid) unless Integer === schema_oid return [] unless schema_oid @@ -41,13 +35,6 @@ module Support private - def base_connection - ActiveRecord::Base.connection - end - - def execute(sql) - base_connection.execute(sql) - end def foreign_key_query(schema_oid, table_name) <<-EOQ diff --git a/spec/workers/referential_cloning_worker_spec.rb b/spec/workers/referential_cloning_worker_spec.rb index c531e02a9..52ed8913b 100644 --- a/spec/workers/referential_cloning_worker_spec.rb +++ b/spec/workers/referential_cloning_worker_spec.rb @@ -17,12 +17,13 @@ RSpec.describe ReferentialCloningWorker do let( :target_schema ){ "target_schema" } let( :referential_cloning ){ OpenStruct.new(source_referential: make_referential(source_schema), target_referential: make_referential(target_schema)) } + let( :cloner ){ 'cloner' } + 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( AF83::SchemaCloner ).to receive(:new).with( source_schema, target_schema ).and_return(cloner) + expect( cloner ).to receive(:clone_schema) expect( referential_cloning ).to receive(:run!) end |
