Skip to content

Commit d4093a7

Browse files
committed
test rails 7.1 composite indices
1 parent be44216 commit d4093a7

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# frozen_string_literal: true
2+
3+
describe "OracleEnhancedAdapter should support composite primary" do
4+
include SchemaSpecHelper
5+
6+
before(:all) do
7+
ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
8+
schema_define do
9+
create_table :test_authors, force: true do |t|
10+
t.string :first_name, limit: 20
11+
t.string :last_name, limit: 25
12+
end
13+
14+
create_table :test_books, force: true do |t|
15+
t.string :title, limit: 20
16+
end
17+
18+
create_table :test_authors_test_books, primary_key: ["test_author_id", "test_book_id"], force: true do |t|
19+
t.integer "test_author_id", precision: 38, null: false
20+
t.integer "test_book_id", precision: 38, null: false
21+
end
22+
end
23+
end
24+
25+
after(:all) do
26+
schema_define do
27+
drop_table :test_authors
28+
drop_table :test_books
29+
drop_table :test_authors_test_books
30+
end
31+
end
32+
33+
before(:each) do
34+
class ::TestAuthor < ActiveRecord::Base
35+
has_many :test_authors_test_books
36+
has_many :test_books, through: :test_authors_test_books, inverse_of: :test_authors
37+
end
38+
class ::TestBook < ActiveRecord::Base
39+
has_many :test_authors_test_books
40+
has_many :test_authors, through: :test_authors_test_books, inverse_of: :test_books
41+
end
42+
class ::TestAuthorsTestBook < ActiveRecord::Base
43+
self.primary_key = [:test_author_id, :test_book_id]
44+
belongs_to :test_author, foreign_key: :test_author_id
45+
belongs_to :test_book, foreign_key: :test_book_id
46+
end
47+
48+
@author = TestAuthor.create!(
49+
first_name: "First",
50+
last_name: "Last",
51+
)
52+
@book = TestBook.create!(title: "Nice book")
53+
@testRel = TestAuthorsTestBook.create!(test_author: @author, test_book: @book)
54+
expect([@book]).to eq(@author.test_books)
55+
end
56+
57+
after(:each) do
58+
TestAuthor.delete_all
59+
TestBook.delete_all
60+
TestAuthorsTestBook.delete_all
61+
Object.send(:remove_const, "TestAuthor")
62+
Object.send(:remove_const, "TestBook")
63+
Object.send(:remove_const, "TestAuthorsTestBook")
64+
ActiveRecord::Base.clear_cache!
65+
end
66+
67+
it "should support distinct" do
68+
TestAuthor.distinct.count.should == 1
69+
skip "this appears to be a rails bug https://github.com/rails/rails/issues/55401"
70+
TestAuthorsTestBook.distinct.count.should == 1
71+
end
72+
73+
it "should support includes when requesting the first record by a referenced composite idx association" do
74+
expect([@book]).to eq(@author.test_books)
75+
expect(TestAuthor.includes(:test_authors_test_books).references(:test_authors_test_books).merge(TestAuthorsTestBook.where(test_author: @author)).take).to eq(@author)
76+
expect(TestAuthor.includes(:test_authors_test_books).references(:test_authors_test_books).merge(TestAuthorsTestBook.where(test_author: @author)).first).to eq(@author)
77+
end
78+
79+
it "should support includes when requesting the first record by a referenced association" do
80+
expect([@book]).to eq(@author.test_books)
81+
expect(TestAuthorsTestBook.includes(:test_author).references(:test_author).merge(TestAuthor.where(first_name: "First")).take).to eq(@testRel)
82+
expect(TestAuthorsTestBook.includes(:test_author).references(:test_author).merge(TestAuthor.where(first_name: "First")).first).to eq(@testRel)
83+
end
84+
end

0 commit comments

Comments
 (0)