Sha256: b8d5f1db54d99b087f9d27d94f1cab2dc797c07bf1c030a57af46b8afc6e0bf2

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

describe Arproxy::Plugin::MysqlCasualLog do
  before do
    allow_any_instance_of(Arproxy::Base).to receive(:execute)
  end

  let(:today) { Time.parse("2015-05-15 20:15:55 +0000") }
  let(:out) { StringIO.new }

  let(:raw_connection) do
    Mysql2::Client.new(host: "localhost", username: "root", database: "mysql")
  end

  let(:plugin) do
    Arproxy::Plugin::MysqlCasualLog.new(out: out, raw_connection: raw_connection)
  end

  describe "#initialize" do
    describe "@out" do
      subject { plugin.instance_variable_get(:@out) }
      it { is_expected.to eq out }
    end

    describe "@raw_connection" do
      subject { plugin.instance_variable_get(:@raw_connection) }
      it { is_expected.to eq raw_connection }
    end
  end

  describe "#execute" do
    subject { out.string.sub(/Query options:.*/, "Query options:").sub(/rows: \d+/, "rows:") }

    before do
      Timecop.freeze(today) do
        plugin.execute(sql)
      end
    end

    context "when bad query" do
      let(:sql) { "select * from user" }

      let(:explain) do
        <<-EOS
# Time: 2015-05-15 20:15:55
# Query options:
# Query: select * from user
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: user
         type: #{red bold "ALL"}
possible_keys: #{red bold "NULL"}
          key: #{red bold "NULL"}
      key_len:\s
          ref:\s
         rows:
        Extra:\s
        EOS
      end

      it { is_expected.to eq explain }
    end

    context "when bad query" do
      let(:sql) { "select 1 from user where Host = 'localhost'" }
      it { is_expected.to eq "" }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
arproxy-plugin-mysql_casual_log-0.1.0 spec/arproxy-plugin-mysql_casual_log_spec.rb