Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/fixture_kit/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def materialize(value)
end

def load_record(record_info)
record_info.keys.first.find_by(id: record_info.values.first)
record_info.keys.first.unscoped.find_by(id: record_info.values.first)
end
end
end
2 changes: 2 additions & 0 deletions spec/dummy/app/models/project.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class Project < ApplicationRecord
default_scope { where(deleted_at: nil) }

belongs_to :owner, class_name: "User"
has_many :tasks
has_many :comments, as: :commentable
Expand Down
3 changes: 2 additions & 1 deletion spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[8.1].define(version: 2024_01_01_000002) do
ActiveRecord::Schema[8.1].define(version: 2024_01_01_000003) do
create_table "activity_logs", force: :cascade do |t|
t.string "action", null: false
t.datetime "created_at", null: false
Expand All @@ -34,6 +34,7 @@

create_table "projects", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "deleted_at"
t.text "description"
t.string "name", null: false
t.integer "owner_id", null: false
Expand Down
16 changes: 16 additions & 0 deletions spec/dummy/fixture_kit/soft_deleted_project.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

FixtureKit.define do
owner = User.create!(name: "Project Owner", email: "owner@soft-delete.test")

active_project = Project.create!(name: "Active Project", owner: owner)

archived_project = Project.create!(name: "Archived Project", owner: owner)
archived_project.update_columns(deleted_at: Time.current)

expose(
owner: owner,
active_project: active_project,
archived_project: archived_project
)
end
14 changes: 14 additions & 0 deletions spec/dummy/spec/integration/fixture_kit_integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,20 @@ module FixtureKitIntegrationTimeHelpers
end
end

describe "unscoped record lookup" do
fixture "soft_deleted_project"

it "finds exposed records even when hidden by default_scope" do
expect(Project.count).to eq(1)
expect(Project.unscoped.count).to eq(2)

expect(fixture.archived_project.name).to eq("Archived Project")
expect(fixture.archived_project.deleted_at).to be_present
expect(fixture.active_project.name).to eq("Active Project")
puts "FKIT_ASSERT:UNSCOPED_RECORD_LOOKUP"
end
end

describe "fixture instance reader without declaration" do
it "raises a helpful error message" do
expect do
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def setup_databases
t.string :name, null: false
t.text :description
t.string :status, default: "active"
t.datetime :deleted_at
t.references :owner, null: false, foreign_key: { to_table: :users }
t.timestamps
end
Expand Down
14 changes: 14 additions & 0 deletions spec/dummy/test/integration/fixture_kit_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,20 @@ class FixtureKitAnonymousDuplicateDeclarationIntegrationTest < ActiveSupport::Te
end
end

class FixtureKitUnscopedRecordLookupIntegrationTest < ActiveSupport::TestCase
fixture "soft_deleted_project"

test "finds exposed records even when hidden by default_scope" do
assert_equal 1, Project.count
assert_equal 2, Project.unscoped.count

assert_equal "Archived Project", fixture.archived_project.name
assert_not_nil fixture.archived_project.deleted_at
assert_equal "Active Project", fixture.active_project.name
puts "FKIT_ASSERT:UNSCOPED_RECORD_LOOKUP"
end
end

class FixtureKitUndeclaredFixtureReaderIntegrationTest < ActiveSupport::TestCase
test "raises a helpful error message when fixture is called without declaration" do
error = assert_raises(RuntimeError) { fixture }
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def setup_databases
t.string :name, null: false
t.text :description
t.string :status, default: "active"
t.datetime :deleted_at
t.references :owner, null: false, foreign_key: { to_table: :users }
t.timestamps
end
Expand Down
3 changes: 2 additions & 1 deletion spec/integration/dummy_app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ def run_dummy_tests
"FKIT_ASSERT:CIRCULAR_INHERITANCE",
"FKIT_ASSERT:ANONYMOUS_NESTED_OVERRIDE",
"FKIT_ASSERT:ANONYMOUS_DUPLICATE_DECLARATION",
"FKIT_ASSERT:UNDECLARED_FIXTURE_READER"
"FKIT_ASSERT:UNDECLARED_FIXTURE_READER",
"FKIT_ASSERT:UNSCOPED_RECORD_LOOKUP"
]

if INTEGRATION_FRAMEWORK == "rspec"
Expand Down
100 changes: 0 additions & 100 deletions spec/unit/fixture_set_spec.rb

This file was deleted.