<commit_before>require 'spec_helper'

describe FreshConnection do
  before(:each) do
    @user = User.where(id: 1).first
  end

  context "access to slave" do
    it "select from User is to access to slave1" do
      data = [
        @user.name,
        Address.first.user.name,
        Tel.first.user.name,
      ]

      expect(data).to be_all{|n| n.include?("slave1")}
    end

    it "select from Address is to access to slave1" do
      data = [
        Address.first.prefecture,
        @user.address.prefecture
      ]

      expect(data).to be_all{|n| n.include?("slave1")}
    end

    it "select from Address is to access to slave2" do
      data = [
        Tel.first.number,
        @user.tels.first.number
      ]

      expect(data).to be_all{|n| n.include?("slave2")}
    end

    it "select with join is to access to slave1" do
      name = User.joins(:address).where("addresses.user_id = 1").where(id: 1).first.name
      expect(name).to be_include("slave1")
    end

    it "pluck is access to slave1" do
      expect(User.where(id: 1).pluck(:name).first).to be_include("slave")
    end

    it "pluck returns empty array when result of condition is empty" do
      expect(User.limit(0).pluck(:name)).to be_empty
    end

    it "count is access to slave" do
      expect(User.where(name: "Other").count).to eq(2)
    end
  end

  context "access to master" do
    it "in transaction" do
      User.transaction do
        expect(@user.name).to be_include("slave1")
        data = [
          Address.first.user.name,
          Address.first.prefecture,
          Tel.first.number,
          Tel.first.user.name,
          @user.address.prefecture,
          @user.tels.first.number,
          User.joins(:address).where(id: 1).where("addresses.user_id = 1").first.name,
          User.where(id: 1).pluck(:name).first
        ]
        expect(data).to be_all{|n| n.include?("master")}

        expect(User.where(name: "Other").count).to eq(1)
      end
    end

    it "specify read_master" do
      data = [
        Address.read_master.first.prefecture,
        Address.includes(:user).read_master.first.user.name,
        Tel.read_master.first.number,
        Tel.includes(:user).read_master.first.user.name,
        @user.tels.read_master.first.number,
        User.where(id: 1).includes(:tels).read_master.first.tels.first.number,
        User.where(id: 1).includes(:address).read_master.first.address.prefecture,
        User.where(id: 1).joins(:address).where("addresses.user_id = 1").read_master.first.name,
        User.where(id: 1).read_master.pluck(:name).first
      ]

      expect(data).to be_all{|n| n.include?("master")}
      expect(User.where(name: "Other").read_master.count).to eq(1)
    end

    it "specify readonly(false)" do
      data = [
        Address.readonly(false).first.prefecture,
        Address.includes(:user).readonly(false).first.user.name,
        Tel.readonly(false).first.number,
        Tel.includes(:user).readonly(false).first.user.name,
        @user.tels.readonly(false).first.number,
        User.where(id: 1).includes(:tels).readonly(false).first.tels.first.number,
        User.where(id: 1).includes(:address).readonly(false).first.address.prefecture,
        User.where(id: 1).joins(:address).where("addresses.user_id = 1").readonly(false).first.name,
        User.where(id: 1).readonly(false).pluck(:name).first
      ]

      expect(data).to be_all{|n| n.include?("master")}
      expect(User.where(name: "Other").readonly(false).count).to eq(1)
      expect(User.where(name: "Other").count(:readonly => false)).to eq(1)
    end
  end

  context "master_db_only model always access to master" do
    class Address3 < ActiveRecord::Base
      self.table_name = "addresses"
      master_db_only!
    end

    class Master < ActiveRecord::Base
      self.abstract_class = true<commit_msg>warning silenced<commit_after>require 'spec_helper'

describe FreshConnection do
  before(:each) do
    @user = User.where(id: 1).first
  end

  context "access to slave" do
    it "select from User is to access to slave1" do
      data = [
        @user.name,
        Address.first.user.name,
        Tel.first.user.name,
      ]

      expect(data).to be_all{|n| n.include?("slave1")}
    end

    it "select from Address is to access to slave1" do
      data = [
        Address.first.prefecture,
        @user.address.prefecture
      ]

      expect(data).to be_all{|n| n.include?("slave1")}
    end

    it "select from Address is to access to slave2" do
      data = [
        Tel.first.number,
        @user.tels.first.number
      ]

      expect(data).to be_all{|n| n.include?("slave2")}
    end

    it "select with join is to access to slave1" do
      name = User.joins(:address).where("addresses.user_id = 1").where(id: 1).first.name
      expect(name).to be_include("slave1")
    end

    it "pluck is access to slave1" do
      expect(User.where(id: 1).pluck(:name).first).to be_include("slave")
    end

    it "pluck returns empty array when result of condition is empty" do
      expect(User.limit(0).pluck(:name)).to be_empty
    end

    it "count is access to slave" do
      expect(User.where(name: "Other").count).to eq(2)
    end
  end

  context "access to master" do
    it "in transaction" do
      User.transaction do
        expect(@user.name).to be_include("slave1")
        data = [
          Address.first.user.name,
          Address.first.prefecture,
          Tel.first.number,
          Tel.first.user.name,
          @user.address.prefecture,
          @user.tels.first.number,
          User.joins(:address).where(id: 1).where("addresses.user_id = 1").first.name,
          User.where(id: 1).pluck(:name).first
        ]
        expect(data).to be_all{|n| n.include?("master")}

        expect(User.where(name: "Other").count).to eq(1)
      end
    end

    it "specify read_master" do
      data = [
        Address.read_master.first.prefecture,
        Address.includes(:user).read_master.first.user.name,
        Tel.read_master.first.number,
        Tel.includes(:user).read_master.first.user.name,
        @user.tels.read_master.first.number,
        User.where(id: 1).includes(:tels).read_master.first.tels.first.number,
        User.where(id: 1).includes(:address).read_master.first.address.prefecture,
        User.where(id: 1).joins(:address).where("addresses.user_id = 1").read_master.first.name,
        User.where(id: 1).read_master.pluck(:name).first
      ]

      expect(data).to be_all{|n| n.include?("master")}
      expect(User.where(name: "Other").read_master.count).to eq(1)
    end

    it "specify readonly(false)" do
      ActiveSupport::Deprecation.silence do
        data = [
          Address.readonly(false).first.prefecture,
          Address.includes(:user).readonly(false).first.user.name,
          Tel.readonly(false).first.number,
          Tel.includes(:user).readonly(false).first.user.name,
          @user.tels.readonly(false).first.number,
          User.where(id: 1).includes(:tels).readonly(false).first.tels.first.number,
          User.where(id: 1).includes(:address).readonly(false).first.address.prefecture,
          User.where(id: 1).joins(:address).where("addresses.user_id = 1").readonly(false).first.name,
          User.where(id: 1).readonly(false).pluck(:name).first
        ]

        expect(data).to be_all{|n| n.include?("master")}
        expect(User.where(name: "Other").readonly(false).count).to eq(1)
        expect(User.where(name: "Other").count(:readonly => false)).to eq(1)
      end
    end
  end

  context "master_db_only model always access to master" do
    class Address3 < ActiveRecord::Base
      self.table_name = "addresses"
      master_db_only!
    end

    class Master < ActiveRecord::Base
      self.abstract_class = true
{'fraction_non_alphanumeric': 0.141573610576306, 'fraction_numerical': 0.008728019509690668, 'mean_word_length': 2.6107506950880444, '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': '6627047', 'n_tokens_mistral': 2614, 'n_tokens_neox': 2490, 'n_words': 469}