From 8b4ec38c80c30689caed6cda1d1808ebc96bb175 Mon Sep 17 00:00:00 2001 From: Jared Beck Date: Wed, 22 Aug 2018 21:39:16 -0400 Subject: [PATCH] Testing joins, as recommended by Sean https://github.com/paper-trail-gem/paper_trail/pull/1143#issuecomment-415132567 --- .rubocop.yml | 4 +++ spec/dummy_app/app/models/management.rb | 7 +++++ spec/models/family/celebrity_family_spec.rb | 12 ++++++++ spec/models/management_spec.rb | 34 +++++++++++++++++++++ spec/models/person_spec.rb | 2 +- spec/models/song_spec.rb | 17 +++++++++++ 6 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 spec/dummy_app/app/models/management.rb create mode 100644 spec/models/management_spec.rb create mode 100644 spec/models/song_spec.rb diff --git a/.rubocop.yml b/.rubocop.yml index 7bb5fa0f..04aff53f 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -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. diff --git a/spec/dummy_app/app/models/management.rb b/spec/dummy_app/app/models/management.rb new file mode 100644 index 00000000..3e983040 --- /dev/null +++ b/spec/dummy_app/app/models/management.rb @@ -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 diff --git a/spec/models/family/celebrity_family_spec.rb b/spec/models/family/celebrity_family_spec.rb index 2d12c9b7..6d93f2c3 100644 --- a/spec/models/family/celebrity_family_spec.rb +++ b/spec/models/family/celebrity_family_spec.rb @@ -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( diff --git a/spec/models/management_spec.rb b/spec/models/management_spec.rb new file mode 100644 index 00000000..bf3cb927 --- /dev/null +++ b/spec/models/management_spec.rb @@ -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 diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb index 0ac64169..fe45779c 100644 --- a/spec/models/person_spec.rb +++ b/spec/models/person_spec.rb @@ -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 diff --git a/spec/models/song_spec.rb b/spec/models/song_spec.rb new file mode 100644 index 00000000..91f44e95 --- /dev/null +++ b/spec/models/song_spec.rb @@ -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