184 lines
5.1 KiB
Ruby
184 lines
5.1 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
|
|
|
|
shared_examples "queries only the aux table" do
|
|
it "queries only the aux table" do
|
|
expect(queries.length).to eq(1)
|
|
expect(queries.first).not_to include("JOIN")
|
|
expect(queries.first).to match(/\bvehicles_car_aux\b/)
|
|
expect(queries.first).not_to match(/\bvehicles\b/)
|
|
end
|
|
end
|
|
|
|
shared_examples "queries both tables" do
|
|
it "queries both tables" do
|
|
expect(queries.length).to eq(1)
|
|
expect(queries.first).to include("JOIN")
|
|
expect(queries.first).to match(/\bvehicles\b/)
|
|
expect(queries.first).to match(/\bvehicles_car_aux\b/)
|
|
end
|
|
end
|
|
|
|
describe "pluck" do
|
|
context "aux columns are referenced" do
|
|
let_and_capture(:queries) { Car.pluck(:fuel_type) }
|
|
|
|
it "returns the correct result" do
|
|
expect(queries.result).to eq(%w[gasoline hybrid electric])
|
|
end
|
|
|
|
it_behaves_like "queries only the aux table"
|
|
end
|
|
|
|
context "aux columns are chained on a where clause" do
|
|
let_and_capture(:queries) do
|
|
Car.where(engine_size: 1.4..1.9).pluck(:fuel_type)
|
|
end
|
|
|
|
it "returns the correct result" do
|
|
expect(queries.result).to eq(%w[hybrid electric])
|
|
end
|
|
|
|
it_behaves_like "queries only the aux table"
|
|
it "applies the BETWEEN clause" do
|
|
expect(queries.first).to include("BETWEEN")
|
|
end
|
|
end
|
|
|
|
context "main table columns are referenced" do
|
|
let(:queries) do
|
|
capture_queries { Car.where(name: "Toyota Camry").pluck(:fuel_type) }
|
|
end
|
|
|
|
it "returns the correct result" do
|
|
expect(queries.result).to eq(%w[gasoline])
|
|
end
|
|
|
|
it_behaves_like "queries both tables"
|
|
end
|
|
|
|
context "main table columns are chained on a where clause" do
|
|
let(:queries) do
|
|
capture_queries { Car.where(name: "Toyota Camry").pluck(:fuel_type) }
|
|
end
|
|
|
|
it "returns the correct result" do
|
|
expect(queries.result).to eq(%w[gasoline])
|
|
end
|
|
|
|
it_behaves_like "queries both tables"
|
|
end
|
|
|
|
context "multiple columns" do
|
|
let(:queries) do
|
|
capture_queries { Car.pluck(:fuel_type, :engine_size) }
|
|
end
|
|
|
|
it "returns the correct result" do
|
|
expect(queries.result).to eq(
|
|
[["gasoline", 2.0], ["hybrid", 1.5], ["electric", 1.8]]
|
|
)
|
|
end
|
|
|
|
it_behaves_like "queries only the aux table"
|
|
end
|
|
|
|
context "multiple columns with a where clause" do
|
|
let(:queries) do
|
|
capture_queries do
|
|
Car.where(name: "Toyota Camry").pluck(:fuel_type, :engine_size)
|
|
end
|
|
end
|
|
|
|
it "returns the correct result" do
|
|
expect(queries.result).to eq([["gasoline", 2.0]])
|
|
end
|
|
|
|
it_behaves_like "queries both tables"
|
|
end
|
|
|
|
context "querying the id column" do
|
|
let_and_capture(:queries) { Car.pluck(:id) }
|
|
|
|
it "renames the base_table_id to id" do
|
|
expect(queries.first).to include("\"base_table_id\" AS \"id\"")
|
|
end
|
|
|
|
it_behaves_like "queries only the aux table"
|
|
end
|
|
end
|
|
|
|
describe "maximum" do
|
|
context "aux columns are referenced" do
|
|
let_and_capture(:queries) { Car.maximum(:engine_size) }
|
|
|
|
it_behaves_like "queries only the aux table"
|
|
end
|
|
|
|
context "aux columns are chained on a where clause" do
|
|
let_and_capture(:queries) do
|
|
Car.where(engine_size: 1.4..1.9).maximum(:engine_size)
|
|
end
|
|
|
|
it "returns the correct result" do
|
|
expect(queries.result).to eq(1.8)
|
|
end
|
|
|
|
it_behaves_like "queries only the aux table"
|
|
end
|
|
|
|
context "main table columns are referenced" do
|
|
let_and_capture(:queries) do
|
|
Car.where(name: "Toyota Camry").maximum(:engine_size)
|
|
end
|
|
|
|
it "returns the correct result" do
|
|
expect(queries.result).to eq(2.0)
|
|
end
|
|
|
|
it_behaves_like "queries both tables"
|
|
end
|
|
|
|
context "id column is referenced" do
|
|
let_and_capture(:queries) { Car.maximum(:id) }
|
|
|
|
it "renames the base_table_id to id" do
|
|
expect(queries.first).to include('"base_table_id"')
|
|
end
|
|
|
|
it "has the right result" do
|
|
expect(queries.result).to eq(3)
|
|
end
|
|
|
|
it_behaves_like "queries only the aux table"
|
|
end
|
|
|
|
context "main table columns are chained on a where clause" do
|
|
let_and_capture(:queries) do
|
|
Car.where(name: "Toyota Camry").maximum(:engine_size)
|
|
end
|
|
|
|
it "returns the correct result" do
|
|
expect(queries.result).to eq(2.0)
|
|
end
|
|
|
|
it_behaves_like "queries both tables"
|
|
end
|
|
end
|
|
end
|
|
end
|