Skip to content

Commit

Permalink
Use id instead of number in composite key for Cpk::Book
Browse files Browse the repository at this point in the history
Use [:author_id, :id] instead of [:author_id, :number] as
the primary key of Cpk::Book.
  • Loading branch information
gmcgibbon committed Jun 26, 2023
1 parent 5eccc6a commit e224768
Show file tree
Hide file tree
Showing 19 changed files with 74 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ def test_building_the_belonging_object_for_composite_primary_key

def test_belongs_to_with_inverse_association_for_composite_primary_key
author = Cpk::Author.new(name: "John")
book = author.books.build(number: 1, title: "The Rails Way")
book = author.books.build(id: [nil, 1], title: "The Rails Way")
order = Cpk::Order.new(book: book, status: "paid")
author.save!

Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/associations/eager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1728,7 +1728,7 @@ def test_preloading_has_many_through_with_custom_scope

test "preloading has_one with cpk" do
order = Cpk::Order.create!(shop_id: 2)
book = Cpk::Book.create!(order: order, author_id: 1, number: 3)
book = Cpk::Book.create!(order: order, id: [1, 3])
assert_equal book, Cpk::Order.eager_load(:book).find_by(id: order.id).book
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1633,7 +1633,7 @@ def test_tags_has_manu_posts_through_association_with_composite_query_constraint

def test_loading_cpk_association_with_unpersisted_owner
order = Cpk::Order.create!(shop_id: 1)
book = Cpk::BookWithOrderAgreements.new(number: 2, author_id: 1, order: order)
book = Cpk::BookWithOrderAgreements.new(id: [1, 2], order: order)
order_agreement = Cpk::OrderAgreement.create!(order: order)

assert_equal([order_agreement], book.order_agreements.to_a)
Expand Down
15 changes: 2 additions & 13 deletions activerecord/test/cases/associations/has_one_associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ def test_nullification_on_destroyed_association
end

def test_nullification_on_cpk_association
book = Cpk::Book.create!(author_id: 1, number: 2)
other_book = Cpk::Book.create!(author_id: 3, number: 4)
book = Cpk::Book.create!(id: [1, 2])
other_book = Cpk::Book.create!(id: [3, 4])
order = Cpk::OrderWithNullifiedBook.create!(book: book)

order.book = other_book
Expand All @@ -156,17 +156,6 @@ def test_nullification_on_cpk_association
assert_nil book.shop_id
end

def test_nullification_on_cpk_association_with_pk_column
chapter = Cpk::Chapter.create!(author_id: 1, number: 2)
other_chapter = Cpk::Chapter.create!(author_id: 1, number: 4)
book = Cpk::NullifiedBook.create!(chapter: chapter, number: 1, author_id: 1)

book.chapter = other_chapter

assert_nil chapter.book_number
assert_not_nil chapter.author_id
end

def test_natural_assignment_to_nil_after_destroy
firm = companies(:rails_core)
old_account_id = firm.account.id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ def test_has_one_through_do_not_cache_association_reader_if_the_though_method_ha

def test_loading_cpk_association_with_unpersisted_owner
order = Cpk::Order.create!(shop_id: 1)
book = Cpk::BookWithOrderAgreements.new(number: 2, author_id: 1, order: order)
book = Cpk::BookWithOrderAgreements.new(id: [1, 2], order: order)
order_agreement = Cpk::OrderAgreement.create!(order: order)

assert_equal(order_agreement, book.order_agreement)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ def test_find_on_child_instances_with_ids_should_set_inverse_instances

def test_inverse_should_be_set_on_composite_primary_key_child
author = Cpk::Author.new(name: "John")
book = author.books.build(number: 1, title: "The Rails Way")
book = author.books.build(id: [nil, 1], title: "The Rails Way")
Cpk::Order.new(book: book, status: "paid")
author.save!

Expand Down
10 changes: 5 additions & 5 deletions activerecord/test/cases/autosave_association_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ def test_validation_does_not_validate_non_dirty_association_target

test "composite primary key autosave" do
assert_nothing_raised do
Cpk::Order.create!(id: [1, 2], book: Cpk::Book.new(title: "Book", author_id: 3, number: 4))
Cpk::Order.create!(id: [1, 2], book: Cpk::Book.new(title: "Book", id: [3, 4]))
end
end
end
Expand Down Expand Up @@ -783,12 +783,12 @@ def test_assign_ids_with_belongs_to_cpk_model
end

def test_assign_ids_with_cpk_for_two_models
books = [cpk_books(:cpk_great_author_first_book).id, cpk_books(:cpk_great_author_second_book).id]
book_ids = [cpk_books(:cpk_great_author_first_book).id, cpk_books(:cpk_great_author_second_book).id]
order = cpk_orders(:cpk_groceries_order_1)

assert_empty order.books

order.book_ids = books
order.book_ids = book_ids
order.save
order.reload

Expand All @@ -799,7 +799,7 @@ def test_assign_ids_with_cpk_for_two_models
end

def test_has_one_cpk_has_one_autosave_with_id
book = Cpk::Book.create!(author_id: 1, number: 3, shop_id: 2)
book = Cpk::Book.create!(id: [1, 3], shop_id: 2)
order = Cpk::OrderWithPrimaryKeyAssociatedBook.create!(book: book, shop_id: 2)

assert_equal(book.order.id, order.id)
Expand Down Expand Up @@ -1069,7 +1069,7 @@ def test_should_destroy_a_parent_association_as_part_of_the_save_transaction_if_

# belongs_to for CPK
def test_autosave_cpk_association_should_destroy_parent_association_when_marked_for_destruction
book = Cpk::Book.new(title: "Book", author_id: 1, number: 2)
book = Cpk::Book.new(title: "Book", id: [1, 2])
Cpk::Order.create!(id: [3, 4], book: book)

book.order.mark_for_destruction
Expand Down
3 changes: 1 addition & 2 deletions activerecord/test/cases/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1011,8 +1011,7 @@ def test_dup_for_a_composite_primary_key_model
new_book = book.dup

assert_equal "The first book", new_book.title
assert_nil new_book.author_id
assert_nil new_book.number
assert_equal([nil, nil], new_book.id)
end

DeveloperSalary = Struct.new(:amount)
Expand Down
36 changes: 18 additions & 18 deletions activerecord/test/cases/batches_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -803,11 +803,11 @@ def test_find_in_batches_should_return_a_sized_enumerator

test ".find_each with multiple column ordering and using composite primary key" do
Cpk::Book.insert_all!([
{ author_id: 1, number: 1 },
{ author_id: 2, number: 1 },
{ author_id: 2, number: 2 }
{ author_id: 1, id: 1 },
{ author_id: 2, id: 1 },
{ author_id: 2, id: 2 }
])
books = Cpk::Book.order(author_id: :asc, number: :desc).to_a
books = Cpk::Book.order(author_id: :asc, id: :desc).to_a

index = 0
Cpk::Book.find_each(batch_size: 1, order: [:asc, :desc]) do |book|
Expand All @@ -819,35 +819,35 @@ def test_find_in_batches_should_return_a_sized_enumerator

test ".in_batches should start from the start option when using composite primary key with multiple column ordering" do
Cpk::Book.insert_all!([
{ author_id: 1, number: 1 },
{ author_id: 1, number: 2 },
{ author_id: 1, number: 3 }
{ author_id: 1, id: 1 },
{ author_id: 1, id: 2 },
{ author_id: 1, id: 3 }
])
second_book = Cpk::Book.order(author_id: :asc, number: :desc).second
second_book = Cpk::Book.order(author_id: :asc, id: :desc).second
relation = Cpk::Book.in_batches(of: 1, start: second_book.id, order: [:asc, :desc]).first
assert_equal second_book, relation.first
end

test ".in_batches should end at the finish option when using composite primary key with multiple column ordering" do
Cpk::Book.insert_all!([
{ author_id: 1, number: 1 },
{ author_id: 1, number: 2 },
{ author_id: 1, number: 3 }
{ author_id: 1, id: 1 },
{ author_id: 1, id: 2 },
{ author_id: 1, id: 3 }
])
second_book = Cpk::Book.order(author_id: :asc, number: :desc).second
second_book = Cpk::Book.order(author_id: :asc, id: :desc).second
relation = Cpk::Book.in_batches(of: 1, finish: second_book.id, order: [:asc, :desc]).to_a.last
assert_equal second_book, relation.first
end

test ".in_batches with scope and multiple column ordering and using composite primary key" do
Cpk::Book.insert_all!([
{ author_id: 1, number: 1 },
{ author_id: 1, number: 2 },
{ author_id: 1, number: 3 }
{ author_id: 1, id: 1 },
{ author_id: 1, id: 2 },
{ author_id: 1, id: 3 }
])
book1, book2 = Cpk::Book.order(author_id: :asc, number: :desc).first(2)
author_id, number = book1.id
relation = Cpk::Book.where("author_id >= ? AND number < ?", author_id, number).in_batches(of: 1, order: [:asc, :desc]).first
book1, book2 = Cpk::Book.order(author_id: :asc, id: :desc).first(2)
author_id, id = book1.id
relation = Cpk::Book.where("author_id >= ? AND id < ?", author_id, id).in_batches(of: 1, order: [:asc, :desc]).first
assert_equal book2, relation.first
end
end
8 changes: 4 additions & 4 deletions activerecord/test/cases/calculations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def test_distinct_count_with_group_by_and_order_and_limit

def test_count_for_a_composite_primary_key_model
book = cpk_books(:cpk_great_author_first_book)
assert_equal(1, Cpk::Book.where(author_id: book.author_id, number: book.number).count)
assert_equal(1, Cpk::Book.where(author_id: book.author_id, id: book.id).count)
end

def test_group_by_count_for_a_composite_primary_key_model
Expand Down Expand Up @@ -957,13 +957,13 @@ def test_ids_for_a_composite_primary_key
end

def test_pluck_for_a_composite_primary_key
assert_equal Cpk::Book.all.pluck([:author_id, :number]).sort, Cpk::Book.all.ids.sort
assert_equal Cpk::Book.all.pluck([:author_id, :id]).sort, Cpk::Book.all.ids.sort
end

def test_ids_for_a_composite_primary_key_with_scope
book = cpk_books(:cpk_great_author_first_book)

assert_equal [[book.author_id, book.number]], Cpk::Book.all.where(title: book.title).ids
assert_equal [book.id], Cpk::Book.all.where(title: book.title).ids
end

def test_ids_for_a_composite_primary_key_on_loaded_relation
Expand All @@ -972,7 +972,7 @@ def test_ids_for_a_composite_primary_key_on_loaded_relation
relation.to_a

assert_predicate relation, :loaded?
assert_equal [[book.author_id, book.number]], relation.ids
assert_equal [book.id], relation.ids
end

def test_ids_with_scope
Expand Down
14 changes: 7 additions & 7 deletions activerecord/test/cases/core_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ def test_find_by_cache_does_not_duplicate_entries
def test_composite_pk_models_added_to_a_set
library = Set.new
# with primary key present
library << Cpk::Book.new(author_id: 1, number: 2)
library << Cpk::Book.new(id: [1, 2])

# duplicate
library << Cpk::Book.new(author_id: 1, number: 3)
library << Cpk::Book.new(author_id: 1, number: 3)
library << Cpk::Book.new(id: [1, 3])
library << Cpk::Book.new(id: [1, 3])

# without primary key being set
library << Cpk::Book.new(title: "Book A")
Expand All @@ -172,19 +172,19 @@ def test_composite_pk_models_added_to_a_set
end

def test_composite_pk_models_equality
assert Cpk::Book.new(author_id: 1, number: 2) == Cpk::Book.new(author_id: 1, number: 2)
assert Cpk::Book.new(id: [1, 2]) == Cpk::Book.new(id: [1, 2])

assert_not Cpk::Book.new(author_id: 1, number: 2) == Cpk::Book.new(author_id: 1, number: 3)
assert_not Cpk::Book.new(id: [1, 2]) == Cpk::Book.new(id: [1, 3])
assert_not Cpk::Book.new == Cpk::Book.new
assert_not Cpk::Book.new(title: "Book A") == Cpk::Book.new(title: "Book B")
assert_not Cpk::Book.new(author_id: 1) == Cpk::Book.new(author_id: 1)
assert_not Cpk::Book.new(author_id: 1, title: "Same title") == Cpk::Book.new(author_id: 1, title: "Same title")
end

def test_composite_pk_models_hash
assert_equal Cpk::Book.new(author_id: 1, number: 2).hash, Cpk::Book.new(author_id: 1, number: 2).hash
assert_equal Cpk::Book.new(id: [1, 2]).hash, Cpk::Book.new(id: [1, 2]).hash

assert_not_equal Cpk::Book.new(author_id: 1, number: 2).hash, Cpk::Book.new(author_id: 1, number: 3).hash
assert_not_equal Cpk::Book.new(id: [1, 2]).hash, Cpk::Book.new(id: [1, 3]).hash
assert_not_equal Cpk::Book.new.hash, Cpk::Book.new.hash
assert_not_equal Cpk::Book.new(title: "Book A").hash, Cpk::Book.new(title: "Book B").hash
assert_not_equal Cpk::Book.new(author_id: 1).hash, Cpk::Book.new(author_id: 1).hash
Expand Down
9 changes: 5 additions & 4 deletions activerecord/test/cases/fixtures_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1691,15 +1691,16 @@ def test_generates_composite_primary_key_for_partially_filled_fixtures
alice_cpk_book = cpk_books(:cpk_great_author_first_book)

assert_not_empty(alice_cpk_book.id.compact)
assert_equal alice.id, alice_cpk_book.author_id
assert_not_nil alice_cpk_book.number
assert_equal alice_cpk_book.id.first, alice.id
assert_not_nil alice_cpk_book.id.last
end

def test_generates_composite_primary_key_ids
assert_not_empty(cpk_orders(:cpk_groceries_order_1).id.compact)

assert_not_nil(cpk_books(:cpk_great_author_first_book).author_id)
assert_not_nil(cpk_books(:cpk_great_author_first_book).number)
cpk_books(:cpk_great_author_first_book).id.each do |id_column|
assert_not_nil(id_column)
end
end

def test_generates_composite_primary_key_with_unique_components
Expand Down
4 changes: 2 additions & 2 deletions activerecord/test/cases/persistence_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1536,7 +1536,7 @@ def test_query_constraints_list_is_nil_for_non_cpk_model

def test_query_constraints_list_equals_to_composite_primary_key
assert_equal(["shop_id", "id"], Cpk::Order.query_constraints_list)
assert_equal(["author_id", "number"], Cpk::Book.query_constraints_list)
assert_equal(["author_id", "id"], Cpk::Book.query_constraints_list)
end

def test_child_keeps_parents_query_constraints
Expand All @@ -1548,7 +1548,7 @@ def test_child_keeps_parents_query_constraints
end

def test_child_keeps_parents_query_contraints_derived_from_composite_pk
assert_equal(["author_id", "number"], Cpk::BestSeller.query_constraints_list)
assert_equal(["author_id", "id"], Cpk::BestSeller.query_constraints_list)
end

def assert_uses_query_constraints_on_reload(object, columns)
Expand Down
12 changes: 5 additions & 7 deletions activerecord/test/cases/primary_keys_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_read_attribute_with_custom_primary_key
end

def test_read_attribute_with_composite_primary_key
book = Cpk::Book.new(author_id: 1, number: 2)
book = Cpk::Book.new(id: [1, 2])
assert_equal [1, 2], book.read_attribute(:id)
end

Expand Down Expand Up @@ -396,8 +396,6 @@ def test_assigning_a_composite_primary_key
book.save!

assert_equal [1, 2], book.id
assert_equal 1, book.author_id
assert_equal 2, book.number
ensure
Cpk::Book.delete_all
end
Expand All @@ -409,7 +407,7 @@ def test_assigning_a_non_array_value_to_model_with_composite_primary_key_raises
book.id = 1
end

assert_equal("Expected value matching [\"author_id\", \"number\"], got 1.", error.message)
assert_equal("Expected value matching [\"author_id\", \"id\"], got 1.", error.message)
end

def test_id_was_composite
Expand Down Expand Up @@ -467,7 +465,7 @@ def test_dumping_composite_primary_key_out_of_order
end

def test_model_with_a_composite_primary_key
assert_equal(["author_id", "number"], Cpk::Book.primary_key)
assert_equal(["author_id", "id"], Cpk::Book.primary_key)
assert_equal(["shop_id", "id"], Cpk::Order.primary_key)
end

Expand All @@ -476,11 +474,11 @@ def composite_primary_key_is_true_for_a_cpk_model
end

def test_primary_key_values_present_for_a_composite_pk_model
assert_predicate Cpk::Book.new(author_id: 1, number: 1), :primary_key_values_present?
assert_predicate Cpk::Book.new(id: [1, 1]), :primary_key_values_present?

assert_not_predicate Cpk::Book.new, :primary_key_values_present?
assert_not_predicate Cpk::Book.new(author_id: 1), :primary_key_values_present?
assert_not_predicate Cpk::Book.new(number: 1), :primary_key_values_present?
assert_not_predicate Cpk::Book.new(id: [nil, 1]), :primary_key_values_present?
assert_not_predicate Cpk::Book.new(title: "Book A"), :primary_key_values_present?
assert_not_predicate Cpk::Book.new(author_id: 1, title: "Book A"), :primary_key_values_present?
end
Expand Down
16 changes: 8 additions & 8 deletions activerecord/test/cases/relation/where_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ def test_where_with_tuple_syntax
end

def test_where_with_tuple_syntax_on_composite_models
book_one = Cpk::Book.create!(author_id: 1, number: 2)
book_two = Cpk::Book.create!(author_id: 3, number: 4)
book_one = Cpk::Book.create!(id: [1, 2])
book_two = Cpk::Book.create!(id: [3, 4])

assert_equal [book_one], Cpk::Book.where([:author_id, :number] => [[1, 2]])
assert_equal [book_one], Cpk::Book.where([:author_id, :id] => [[1, 2]])
assert_equal [book_one, book_two].sort, Cpk::Book.where(Cpk::Book.primary_key => [[1, 2], [3, 4]]).sort
assert_empty Cpk::Book.where([:author_id, :number] => [[1, 4], [3, 2]])
assert_empty Cpk::Book.where([:author_id, :id] => [[1, 4], [3, 2]])
end

def test_where_with_tuple_syntax_with_incorrect_arity
Expand All @@ -135,12 +135,12 @@ def test_where_with_tuple_syntax_with_incorrect_arity
end

def test_where_with_tuple_syntax_and_regular_syntax_combined
book_one = Cpk::Book.create!(author_id: 1, number: 2, title: "The Alchemist")
book_two = Cpk::Book.create!(author_id: 3, number: 4, title: "The Alchemist")
book_one = Cpk::Book.create!(id: [1, 2], title: "The Alchemist")
book_two = Cpk::Book.create!(id: [3, 4], title: "The Alchemist")

assert_equal [book_one, book_two].sort, Cpk::Book.where(title: "The Alchemist").sort
assert_equal [book_one, book_two].sort, Cpk::Book.where(title: "The Alchemist", [:author_id, :number] => [[1, 2], [3, 4]]).sort
assert_equal [book_two], Cpk::Book.where(title: "The Alchemist", [:author_id, :number] => [[3, 4]])
assert_equal [book_one, book_two].sort, Cpk::Book.where(title: "The Alchemist", [:author_id, :id] => [[1, 2], [3, 4]]).sort
assert_equal [book_two], Cpk::Book.where(title: "The Alchemist", [:author_id, :id] => [[3, 4]])
end

def test_belongs_to_shallow_where
Expand Down
Loading

0 comments on commit e224768

Please sign in to comment.