Skip to content

Commit

Permalink
Testing joins, as recommended by Sean
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredbeck committed Aug 23, 2018
1 parent e907295 commit 934c1d9
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions spec/dummy_app/app/models/management.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions spec/models/family/celebrity_family_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@

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.*, max(versions.event) as event").
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(
Expand Down
34 changes: 34 additions & 0 deletions spec/models/management_spec.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion spec/models/person_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# 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
Expand Down
16 changes: 16 additions & 0 deletions spec/models/song_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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.*, max(versions.event) as event").
first
expect(result.event).to eq("create")
end
end
end

0 comments on commit 934c1d9

Please sign in to comment.