<commit_before>require 'spec_helper'

describe InstanceDatabasesController do
  let!(:dbconfig) {YAML.load_file("config/database.yml")[Rails.env]}
  let!(:user) { FactoryGirl.create :user}

  before do
    log_in user
  end

  describe "#index" do
    it "fails without instance id" do
      get :index
      response.code.should == "404"
    end

    context "with instance id" do
      it "when no such instance should fail" do
        get :index, :instance_id => 12345
        response.code.should == "404"
      end

      context "when no account for this instance" do
        let!(:instance) {FactoryGirl.create :instance, :host => dbconfig["host"]||"localhost", :port => dbconfig["port"], :shared => false}

        it "should fail" do
          get :index, :instance_id => instance.id
          response.code.should == "403"
        end
      end

      context "when instance and account available" do
        let!(:instance) {FactoryGirl.create :instance, :host => dbconfig["host"]||"localhost", :port => dbconfig["port"], :shared => false}
        let!(:account) {FactoryGirl.create :instance_account, :instance_id => instance.id, :owner_id => user.id, :db_username => dbconfig["username"], :db_password => dbconfig["password"]}

        context "when unable to connect to instance" do
          before do
            instance.update_attribute :host, "no.such.host"
          end

          it "should fail" do
            get :index, :instance_id => instance.id
            response.code.should == "422"
          end
        end

        context "when able to connect to instance" do
          let!(:instance) {FactoryGirl.create :instance, :host => dbconfig["host"]||"localhost", :port => dbconfig["port"], :shared => false}
          let!(:account) {FactoryGirl.create :instance_account, :instance_id => instance.id, :owner_id => user.id, :db_username => dbconfig["username"], :db_password => dbconfig["password"]}

          it "should succeed" do
            get :index, :instance_id => instance.id
            response.code.should == "200"
            decoded_response.length.should == 8
            decoded_response[0].instance_id.should == instance.id
          end
        end
      end
    end
  end
end<commit_msg>Fix flaky test that is tied to state of database<commit_after>require 'spec_helper'

describe InstanceDatabasesController do
  let!(:dbconfig) {YAML.load_file("config/database.yml")[Rails.env]}
  let!(:user) { FactoryGirl.create :user}

  before do
    log_in user
  end

  describe "#index" do
    it "fails without instance id" do
      get :index
      response.code.should == "404"
    end

    context "with instance id" do
      it "when no such instance should fail" do
        get :index, :instance_id => 12345
        response.code.should == "404"
      end

      context "when no account for this instance" do
        let!(:instance) {FactoryGirl.create :instance, :host => dbconfig["host"]||"localhost", :port => dbconfig["port"], :shared => false}

        it "should fail" do
          get :index, :instance_id => instance.id
          response.code.should == "403"
        end
      end

      context "when instance and account available" do
        let!(:instance) {FactoryGirl.create :instance, :host => dbconfig["host"]||"localhost", :port => dbconfig["port"], :shared => false}
        let!(:account) {FactoryGirl.create :instance_account, :instance_id => instance.id, :owner_id => user.id, :db_username => dbconfig["username"], :db_password => dbconfig["password"]}

        context "when unable to connect to instance" do
          before do
            instance.update_attribute :host, "no.such.host"
          end

          it "should fail" do
            get :index, :instance_id => instance.id
            response.code.should == "422"
          end
        end

        context "when able to connect to instance" do
          let!(:instance) {FactoryGirl.create :instance, :host => dbconfig["host"]||"localhost", :port => dbconfig["port"], :shared => false}
          let!(:account) {FactoryGirl.create :instance_account, :instance_id => instance.id, :owner_id => user.id, :db_username => dbconfig["username"], :db_password => dbconfig["password"]}

          it "should succeed" do
            get :index, :instance_id => instance.id
            response.code.should == "200"
            puts decoded_response
            decoded_response[0].instance_id.should == instance.id
          end
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.12725250278086764, 'fraction_numerical': 0.009566184649610678, 'mean_word_length': 2.694330320460148, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, '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': '4819521', 'n_tokens_mistral': 1321, 'n_tokens_neox': 1255, 'n_words': 366}