<commit_before>require 'rails_helper'

describe Util::DbManager do

  subject { described_class.new }

  context 'when managing the databases' do
    it 'should restore the public db from current dump file - then both dbs should be identical' do
     stub_request(:get, "https://prsinfo.clinicaltrials.gov/results_definitions.html").
         with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
         to_return(status: 200, body: "", headers: {})

     stub_request(:get, "https://prsinfo.clinicaltrials.gov/definitions.html").
           with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
           to_return(status: 200, body: "", headers: {})

      dm=Util::DbManager.new(:load_event=>Support::LoadEvent.create({:event_type=>'incremental',:status=>'running',:description=>'',:problems=>''}))
      fm=Util::FileManager.new
      dm.dump_database
      fm.save_static_copy
      dm.refresh_public_db

      back_db_con = ActiveRecord::Base.establish_connection(ENV["AACT_BACK_DATABASE_URL"]).connection
      back_tables=back_db_con.execute("select * from information_schema.tables where table_schema='ctgov'")
      back_table_count=back_db_con.execute("select count(*) from information_schema.tables where table_schema='ctgov'").first['count'].to_i
      pub_db_con = PublicBase.connection
      pub_table_count=pub_db_con.execute("select count(*) from information_schema.tables where table_schema='ctgov'").first['count'].to_i
      pub_tables=pub_db_con.execute("select * from information_schema.tables where table_schema='ctgov'")
      # both dbs should have all the same tables except schema_migrations table is removed from public db
      expect(back_table_count).to eq(pub_table_count)

      pub_study_count=pub_db_con.execute('select count(*) from studies').first['count'].to_i
      pub_outcome_count=pub_db_con.execute('select count(*) from outcomes').first['count'].to_i
      expect(Study.count).to eq(pub_study_count)
      expect(Outcome.count).to eq(pub_outcome_count)
    end
  end
end
<commit_msg>reset public database connection before checking Study count there<commit_after>require 'rails_helper'

describe Util::DbManager do

  subject { described_class.new }

  context 'when managing the databases' do
    it 'should restore the public db from current dump file - then both dbs should be identical' do
      stub_request(:get, "https://prsinfo.clinicaltrials.gov/results_definitions.html").
         with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
         to_return(status: 200, body: "", headers: {})

      stub_request(:get, "https://prsinfo.clinicaltrials.gov/definitions.html").
           with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
           to_return(status: 200, body: "", headers: {})

      dm=Util::DbManager.new(:load_event=>Support::LoadEvent.create({:event_type=>'incremental',:status=>'running',:description=>'',:problems=>''}))
      fm=Util::FileManager.new
      dm.dump_database
      fm.save_static_copy
      dm.refresh_public_db

      back_con = ActiveRecord::Base.establish_connection(ENV["AACT_BACK_DATABASE_URL"]).connection
      back_tables=back_con.execute("select * from information_schema.tables where table_schema='ctgov'")
      back_table_count=back_con.execute("select count(*) from information_schema.tables where table_schema='ctgov'").first['count'].to_i
      PublicBase.establish_connection(
        adapter: 'postgresql',
        encoding: 'utf8',
        hostname: ENV['AACT_PUBLIC_HOSTNAME'],
        database: ENV['AACT_PUBLIC_DATABASE_NAME'],
        username: ENV['DB_SUPER_USERNAME'])
      pub_con = PublicBase.connection
      pub_table_count=pub_con.execute("select count(*) from information_schema.tables where table_schema='ctgov'").first['count'].to_i
      pub_tables=pub_con.execute("select * from information_schema.tables where table_schema='ctgov'")
      # both dbs should have all the same tables except schema_migrations table is removed from public db
      expect(back_table_count).to eq(pub_table_count)

      pub_study_count=pub_con.execute('select count(*) from studies').first['count'].to_i
      pub_outcome_count=pub_con.execute('select count(*) from outcomes').first['count'].to_i
      expect(Study.count).to eq(pub_study_count)
      expect(Outcome.count).to eq(pub_outcome_count)
    end
  end
end

{'fraction_non_alphanumeric': 0.14391626689925863, 'fraction_numerical': 0.008068033144352376, 'mean_word_length': 4.798988621997472, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 4, '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': '5514229', 'n_tokens_mistral': 1493, 'n_tokens_neox': 1439, 'n_words': 247}