<commit_before>require "minitest_helper"
require "heroku/client"

describe Heroku::Client::PgbackupsArchive do
  let(:archive) {
    Heroku::Client::PgbackupsArchive.new(
      :pgbackups_url => "https://ip:password@pgbackups.heroku.com/client"
    )
  }
  let(:backup) { { "finished_at" => "some timestamp" } }

  it "should use a pgbackup client" do
    archive.client.class.must_equal Heroku::Client::Pgbackups
  end

  describe "given a finished_at timestamp" do
    before { archive.client.stubs(:create_transfer).returns(backup) }

    it "should capture the backup" do
      archive.capture.must_equal backup
    end

    it "should store the backup" do
      archive.stubs(:key).returns("key")
      archive.stubs(:file).returns("file")
      archive.store.class.must_equal Fog::Storage::AWS::File
    end
  end

  describe '#file' do
    before do

      archive.instance_eval do
        @backup = {}
        @backup['public_url'] = "https://raw.github.com/kjohnston/pgbackups-archive/master/pgbackups-archive.gemspec"
      end

    end

    it 'downloads the backup file' do
      archive.file.read.must_be :=~, /Gem::Specification/
    end

  end

  describe "configure the backup database" do

    describe "backup database is not configured" do
      before do
        ENV["PGBACKUPS_DATABASE_URL"] = nil
        ENV["DATABASE_URL"] = "db_url"
      end

      it "defaults to using the DATABASE_URL" do
        archive.client.expects(:create_transfer)
        .with("db_url", "db_url", nil, "BACKUP", :expire => true)
        .returns(backup)

        archive.capture
      end
    end

    describe "backup database is configured" do
      before do
        ENV["PGBACKUPS_DATABASE_URL"] = "backup_db"<commit_msg>changed to more approprate matcher<commit_after>require "minitest_helper"
require "heroku/client"

describe Heroku::Client::PgbackupsArchive do
  let(:archive) {
    Heroku::Client::PgbackupsArchive.new(
      :pgbackups_url => "https://ip:password@pgbackups.heroku.com/client"
    )
  }
  let(:backup) { { "finished_at" => "some timestamp" } }

  it "should use a pgbackup client" do
    archive.client.class.must_equal Heroku::Client::Pgbackups
  end

  describe "given a finished_at timestamp" do
    before { archive.client.stubs(:create_transfer).returns(backup) }

    it "should capture the backup" do
      archive.capture.must_equal backup
    end

    it "should store the backup" do
      archive.stubs(:key).returns("key")
      archive.stubs(:file).returns("file")
      archive.store.class.must_equal Fog::Storage::AWS::File
    end
  end

  describe '#file' do
    before do

      archive.instance_eval do
        @backup = {}
        @backup['public_url'] = "https://raw.github.com/kjohnston/pgbackups-archive/master/pgbackups-archive.gemspec"
      end

    end

    it 'downloads the backup file' do
      archive.file.read.must_match /Gem::Specification/
    end

  end

  describe "configure the backup database" do

    describe "backup database is not configured" do
      before do
        ENV["PGBACKUPS_DATABASE_URL"] = nil
        ENV["DATABASE_URL"] = "db_url"
      end

      it "defaults to using the DATABASE_URL" do
        archive.client.expects(:create_transfer)
        .with("db_url", "db_url", nil, "BACKUP", :expire => true)
        .returns(backup)

        archive.capture
      end
    end

    describe "backup database is configured" do
      before do
        ENV["PGBACKUPS_DATABASE_URL"] = "backup_db"
{'fraction_non_alphanumeric': 0.11216566005176877, 'fraction_numerical': 0.0, 'mean_word_length': 3.016166281755196, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5107553', 'n_tokens_mistral': 1139, 'n_tokens_neox': 1091, 'n_words': 251}