<commit_before>require 'spec_helper'

describe GpTableCopier, :database_integration => true do
  def call_sql(sql_command, schema = schema, account = account)
    schema.with_gpdb_connection(account) do |connection|
      connection.exec_query(sql_command)
    end
  end

  def distribution_key_sql(schema_name, table_name)
    <<-DISTRIBUTION_KEY_SQL
      SELECT attname
      FROM   (SELECT *, generate_series(1, array_upper(attrnums, 1)) AS rn
      FROM   gp_distribution_policy where localoid = '#{schema_name}.#{table_name}'::regclass
      ) y, pg_attribute WHERE attrelid = '#{schema_name}.#{table_name}'::regclass::oid AND attrnums[rn] = attnum ORDER by rn;
    DISTRIBUTION_KEY_SQL
  end

  let(:account) { GpdbIntegration.real_gpdb_account }
  let(:user) { account.owner }
  let(:database) { GpdbDatabase.find_by_name_and_instance_id(GpdbIntegration.database_name, GpdbIntegration.real_gpdb_instance) }
  let(:schema) { database.schemas.find_by_name('test_schema') }
  let(:src_table_name) { "src_table" } #database.find_dataset_in_schema("base_table1", "test_schema")
  let(:sandbox) { schema } # For testing purposes, src schema = sandbox
  let(:dst_table_name) { "dst_table" }
  let(:table_def) { '"id" integer, "name" text, "id2" integer, "id3" integer, PRIMARY KEY("id2", "id3", "id")' }
  let(:distrib_def) { 'DISTRIBUTED BY("id2", "id3")' }
  let(:copier) { GpTableCopier.new(schema, src_table_name, sandbox, dst_table_name, user) }
  let(:add_rows) { true }

  before do
    refresh_chorus
  end


  context "actually running the query" do
    before do
      call_sql("drop table if exists \"#{src_table_name}\";")
      call_sql("drop table if exists \"#{dst_table_name}\";")
      call_sql("create table \"#{src_table_name}\"(#{table_def}) #{distrib_def};")
      if add_rows
        call_sql("insert into \"#{src_table_name}\"(id, name, id2, id3) values (1, 'marsbar', 3, 5);")
        call_sql("insert into \"#{src_table_name}\"(id, name, id2, id3) values (2, 'kitkat', 4, 6);")
      end
    end

    after do
      call_sql("DROP TABLE IF EXISTS \"#{schema.name}\".\"#{src_table_name}\";")
      call_sql("DROP TABLE IF EXISTS \"#{sandbox.name}\".\"#{dst_table_name}\";")
    end

    context ".run_new" do
      it "creates a new table copier and runs it" do
        GpTableCopier.run_new(schema.id, src_table_name, sandbox.id, dst_table_name, user.id)
        dest_rows = call_sql("SELECT * FROM #{dst_table_name}", sandbox)
        dest_rows.count.should == 2
      end
    end
<commit_msg>Stop dropping real tables in gptablecopier spec [#29860033]<commit_after>require 'spec_helper'

describe GpTableCopier, :database_integration => true do
  def call_sql(sql_command, schema = schema, account = account)
    schema.with_gpdb_connection(account) do |connection|
      connection.exec_query(sql_command)
    end
  end

  def distribution_key_sql(schema_name, table_name)
    <<-DISTRIBUTION_KEY_SQL
      SELECT attname
      FROM   (SELECT *, generate_series(1, array_upper(attrnums, 1)) AS rn
      FROM   gp_distribution_policy where localoid = '#{schema_name}.#{table_name}'::regclass
      ) y, pg_attribute WHERE attrelid = '#{schema_name}.#{table_name}'::regclass::oid AND attrnums[rn] = attnum ORDER by rn;
    DISTRIBUTION_KEY_SQL
  end

  let(:account) { GpdbIntegration.real_gpdb_account }
  let(:user) { account.owner }
  let(:database) { GpdbDatabase.find_by_name_and_instance_id(GpdbIntegration.database_name, GpdbIntegration.real_gpdb_instance) }
  let(:schema) { database.schemas.find_by_name('test_schema') }
  let(:src_table_name) { "src_table" } #database.find_dataset_in_schema("base_table1", "test_schema")
  let(:sandbox) { schema } # For testing purposes, src schema = sandbox
  let(:dst_table_name) { "dst_table" }
  let(:table_def) { '"id" integer, "name" text, "id2" integer, "id3" integer, PRIMARY KEY("id2", "id3", "id")' }
  let(:distrib_def) { 'DISTRIBUTED BY("id2", "id3")' }
  let(:copier) { GpTableCopier.new(schema, src_table_name, sandbox, dst_table_name, user) }
  let(:add_rows) { true }

  before do
    refresh_chorus
  end

  after do
    call_sql("DROP TABLE IF EXISTS \"#{schema.name}\".\"#{src_table_name}\";")
    call_sql("DROP TABLE IF EXISTS \"#{sandbox.name}\".\"#{dst_table_name}\";") unless (
      (dst_table_name ==  src_table_name) || dst_table_name == "other_base_table")
  end

  context "actually running the query" do
    before do
      call_sql("drop table if exists \"#{src_table_name}\";")
      call_sql("drop table if exists \"#{dst_table_name}\";")
      call_sql("create table \"#{src_table_name}\"(#{table_def}) #{distrib_def};")
      if add_rows
        call_sql("insert into \"#{src_table_name}\"(id, name, id2, id3) values (1, 'marsbar', 3, 5);")
        call_sql("insert into \"#{src_table_name}\"(id, name, id2, id3) values (2, 'kitkat', 4, 6);")
      end
    end

    context ".run_new" do
      it "creates a new table copier and runs it" do
        GpTableCopier.run_new(schema.id, src_table_name, sandbox.id, dst_table_name, user.id)
        dest_rows = call_sql("SELECT * FROM #{dst_table_name}", sandbox)
        dest_rows.count.should == 2
      end
    end

{'fraction_non_alphanumeric': 0.14219384793964016, 'fraction_numerical': 0.009286128845037725, 'mean_word_length': 4.2594099694811804, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4212838', 'n_tokens_mistral': 1874, 'n_tokens_neox': 1834, 'n_words': 392}