Advertisement
PikachuEXE

Test Script for rails/rails#16420

Aug 7th, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 1.86 KB | None | 0 0
  1. unless File.exist?('Gemfile')
  2.   File.write('Gemfile', <<-GEMFILE)
  3. source 'https://rubygems.org'
  4. gem 'rails', github: 'rails/rails'
  5. gem 'arel', github: 'rails/arel'
  6. gem 'sqlite3'
  7. GEMFILE
  8.  
  9.   system 'bundle'
  10. end
  11.  
  12. require 'bundler'
  13. Bundler.setup(:default)
  14.  
  15. require 'active_record'
  16. require 'minitest/autorun'
  17. require 'logger'
  18.  
  19. # This connection will do for database-independent bug reports.
  20. ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
  21. ActiveRecord::Base.logger = Logger.new(STDOUT)
  22.  
  23. ActiveRecord::Schema.define do
  24.   create_table :posts do |t|
  25.     t.integer :parent_post_id, :integer
  26.     t.integer :tree_state_updated_at, :datetime
  27.  
  28.     t.timestamps
  29.   end
  30. end
  31.  
  32. class Post < ActiveRecord::Base
  33.   belongs_to :parent_post,
  34.               touch: :tree_state_updated_at,
  35.               foreign_key: :parent_post_id,
  36.               class_name: self.name,
  37.               inverse_of: :children_posts
  38.  
  39.   has_many :children_posts,
  40.             dependent: :nullify,
  41.             foreign_key: :parent_post_id,
  42.             class_name: self.name,
  43.             inverse_of: :parent_post
  44.  
  45.   class << self
  46.     def created_recently
  47.       where(created_at: 1.day.ago..Time.now)
  48.     end
  49.  
  50.     def children_only
  51.       where.not(parent_post_id: nil)
  52.     end
  53.     def with_children_only
  54.       where(id: Post.children_only.select(:parent_post_id))
  55.     end
  56.   end
  57. end
  58.  
  59. class BugTest < Minitest::Test
  60.   def test_orders_of_scopes_does_not_affect_sql
  61.     parent_post = Post.create!
  62.     old_child_post = Post.create!(parent_post: parent_post, created_at: 1.month.ago)
  63.  
  64.     # puts Post.created_recently.with_children_only.to_sql
  65.     # puts Post.with_children_only.created_recently.to_sql
  66.  
  67.     assert Post.created_recently.with_children_only.include?(parent_post)
  68.     assert Post.with_children_only.created_recently.include?(parent_post)
  69.   end
  70. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement