56 lines
1.6 KiB
Ruby
56 lines
1.6 KiB
Ruby
# typed: false
|
|
# frozen_string_literal: true
|
|
|
|
require "spec_helper"
|
|
|
|
RSpec.describe "loading optimizations" do
|
|
context "cars table" do
|
|
before do
|
|
Car.create!(name: "Toyota Camry", fuel_type: "gasoline", engine_size: 2.0)
|
|
Car.create!(name: "Toyota Prius", fuel_type: "hybrid", engine_size: 1.5)
|
|
Car.create!(
|
|
name: "Toyota Corolla",
|
|
fuel_type: "electric",
|
|
engine_size: 1.8
|
|
)
|
|
end
|
|
|
|
it "queries only the aux table if no main table columns are referenced" do
|
|
queries =
|
|
SpecHelper.capture_queries do
|
|
expect(Car.pluck(:fuel_type)).to eq(%w[gasoline hybrid electric])
|
|
end
|
|
|
|
expect(queries.length).to eq(1)
|
|
expect(queries.first).not_to include("JOIN")
|
|
end
|
|
|
|
it "queries only the aux table if all columns are on the aux table" do
|
|
queries =
|
|
SpecHelper.capture_queries do
|
|
expect(Car.where(engine_size: 1.4..1.9).pluck(:fuel_type)).to eq(
|
|
%w[hybrid electric]
|
|
)
|
|
end
|
|
|
|
expect(queries.length).to eq(1)
|
|
expect(queries.first).not_to include("JOIN")
|
|
expect(queries.first).to include("BETWEEN")
|
|
expect(queries.first).to match(/\bvehicles_car_aux\b/)
|
|
expect(queries.first).not_to match(/\bvehicles\b/)
|
|
end
|
|
|
|
it "queries both tables if main table column is referenced" do
|
|
queries =
|
|
SpecHelper.capture_queries do
|
|
rel = Car.where(name: "Toyota Camry")
|
|
rel = rel.pluck(:fuel_type)
|
|
expect(rel).to eq(%w[gasoline])
|
|
end
|
|
|
|
expect(queries.length).to eq(1)
|
|
expect(queries.first).to include("JOIN")
|
|
end
|
|
end
|
|
end
|