Testing joins, as recommended by Sean

https://github.com/paper-trail-gem/paper_trail/pull/1143#issuecomment-415132567
This commit is contained in:
Jared Beck 2018-08-22 21:39:16 -04:00
parent e9072950b2
commit 8b4ec38c80
6 changed files with 75 additions and 1 deletions

View File

@ -95,6 +95,10 @@ Naming/PredicateName:
Naming/UncommunicativeMethodParamName:
Enabled: false
# This cop does not seem to work in rubocop-rspec 1.28.0
RSpec/DescribeClass:
Enabled: false
# Yes, ideally examples would be short. Is it possible to pick a limit and say,
# "no example will ever be longer than this"? Hard to say. Sometimes they're
# quite long.

View File

@ -0,0 +1,7 @@
# frozen_string_literal: true
# Note that there is no `type` column for this subclassed model, so changes to
# Management objects should result in Versions which have an item_type of
# Customer.
class Management < Customer
end

View File

@ -4,6 +4,18 @@ require "spec_helper"
module Family
RSpec.describe CelebrityFamily, type: :model, versioning: true do
describe "#joins" do
it "works on an STI model" do
described_class.create!
result = described_class.
joins(:versions).
select("families.id, max(versions.event) as event").
group("families.id").
first
expect(result.event).to eq("create")
end
end
describe "#create" do
it "creates version with item_subtype == class.name, not base_class" do
carter = described_class.create(

View File

@ -0,0 +1,34 @@
# frozen_string_literal: true
require "spec_helper"
::RSpec.describe(::Management, type: :model, versioning: true) do
it "utilises the base_class for STI classes having no type column" do
expect(Management.inheritance_column).to eq("type")
expect(Management.columns.map(&:name)).not_to include("type")
# Create, update, and destroy a Management and a Customer
customer1 = Customer.create(name: "Cust 1")
customer2 = Management.create(name: "Cust 2")
customer1.update(name: "Cust 1a")
customer2.update(name: "Cust 2a")
customer1.destroy
customer2.destroy
# All versions end up with an `item_type` of Customer
expect(
PaperTrail::Version.where(item_type: "Customer").count
).to eq(6)
expect(
PaperTrail::Version.where(item_type: "Management").count
).to eq(0)
# The item_subtype, on the other hand, is 3 and 3
expect(
PaperTrail::Version.where(item_subtype: "Customer").count
).to eq(3)
expect(
PaperTrail::Version.where(item_subtype: "Management").count
).to eq(3)
end
end

View File

@ -5,7 +5,7 @@ require "spec_helper"
# The `Person` model:
#
# - has a dozen associations of various types
# - has a custome serializer, TimeZoneSerializer, for its `time_zone` attribute
# - has a custom serializer, TimeZoneSerializer, for its `time_zone` attribute
RSpec.describe Person, type: :model, versioning: true do
describe "#time_zone" do
it "returns an ActiveSupport::TimeZone" do

17
spec/models/song_spec.rb Normal file
View File

@ -0,0 +1,17 @@
# frozen_string_literal: true
require "spec_helper"
::RSpec.describe(::Song, type: :model, versioning: true) do
describe "#joins" do
it "works" do
described_class.create!
result = described_class.
joins(:versions).
select("songs.id, max(versions.event) as event").
group("songs.id").
first
expect(result.event).to eq("create")
end
end
end