Testing JSONB Version Queries for Vegetable Model in PaperTrail
This test suite validates the versioning functionality of the Vegetable model in PaperTrail, specifically focusing on JSONB-based version queries in PostgreSQL. The tests verify various methods for querying version history based on attribute changes and object states.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
paper-trail-gem/paper_trail
spec/models/vegetable_spec.rb
# frozen_string_literal: true
require "spec_helper"
require "support/performance_helpers"
if ENV["DB"] == "postgres" && JsonbVersion.table_exists?
::RSpec.describe Vegetable do
describe "queries of versions", versioning: true do
let!(:vegetable) { described_class.create(name: "Veggie", mass: 1, color: "green") }
before do
vegetable.update(name: "Fidget")
vegetable.update(name: "Digit")
described_class.create(name: "Cucumber")
end
it "return the vegetable whose name has changed" do
result = JsonbVersion.where_attribute_changes(:name).map(&:item)
expect(result).to include(vegetable)
end
it "returns the vegetable whose name was Fidget" do
result = JsonbVersion.where_object_changes_from({ name: "Fidget" }).map(&:item)
expect(result).to include(vegetable)
end
it "returns the vegetable whose name became Digit" do
result = JsonbVersion.where_object_changes_to({ name: "Digit" }).map(&:item)
expect(result).to include(vegetable)
end
it "returns the vegetable where the object was named Fidget before it changed" do
result = JsonbVersion.where_object({ name: "Fidget" }).map(&:item)
expect(result).to include(vegetable)
end
it "returns the vegetable that changed to Fidget" do
result = JsonbVersion.where_object_changes({ name: "Fidget" }).map(&:item)
expect(result).to include(vegetable)
end
end
end
end