<commit_before>require 'test_helper'

class BelongsToPolymorphicFilterTest < ActiveSupport::TestCase

  schema do
    create_table "views", force: :cascade do |t|
      t.string  "subject_type"
      t.integer "subject_id"
    end

    create_table "properties" do |t|
      t.string   "name",                    limit: 255
    end

    create_table "accounts" do |t|
      t.string   "name",                    limit: 255
    end
  end

  class View < ActiveRecord::Base
    belongs_to :subject, polymorphic: true
    belongs_to :account
  end

  class Account < ActiveRecord::Base
  end

  class Property < ActiveRecord::Base
  end

  test "::filter :belongs_to => {ID: VALUE}" do
    query = View.filter(subject: {as: "BelongsToPolymorphicFilterTest::Property", name: 'Name'})
    assert_sql(<<-SQL, query)
      SELECT views.*
      FROM views
      LEFT OUTER JOIN properties properties_as_subject
        ON properties_as_subject.id = views.subject_id AND views.subject_type = 'BelongsToPolymorphicFilterTest::Property'
      WHERE properties_as_subject.name = 'Name'
    SQL
  end

  test '::filter with seperate joins' do
    query = View.filter(subject: {as: "BelongsToPolymorphicFilterTest::Property", name: 'Name'}, account: {name: 'Account'})
    assert_sql(<<-SQL, query)
      SELECT views.* FROM views
      LEFT OUTER JOIN accounts
        ON accounts.id = views.account_id
      LEFT OUTER JOIN properties properties_as_subject
        ON properties_as_subject.id = views.subject_id AND views.subject_type = 'BelongsToPolymorphicFilterTest::Property'
      WHERE properties_as_subject.name = 'Name' AND accounts.name = 'Account'
    SQL
  end

end<commit_msg>add test for filtering beyond polymorphic boundary<commit_after>require 'test_helper'

class BelongsToPolymorphicFilterTest < ActiveSupport::TestCase

  schema do
    create_table "views", force: :cascade do |t|
      t.string  "subject_type"
      t.integer "subject_id"
      t.integer  "account_id"
    end

    create_table "accounts" do |t|
      t.string   "name",                    limit: 255
    end
    
    create_table "properties" do |t|
      t.string   "name",                    limit: 255
      t.integer  "account_id"
    end
  end

  class View < ActiveRecord::Base
    belongs_to :subject, polymorphic: true
    belongs_to :account
  end

  class Account < ActiveRecord::Base
  end

  class Property < ActiveRecord::Base
    belongs_to :account
  end

  test "::filter :belongs_to => {ID: VALUE}" do
    query = View.filter(subject: {as: "BelongsToPolymorphicFilterTest::Property", name: 'Name'})
    assert_sql(<<-SQL, query)
      SELECT views.*
      FROM views
      LEFT OUTER JOIN properties properties_as_subject
        ON properties_as_subject.id = views.subject_id AND views.subject_type = 'BelongsToPolymorphicFilterTest::Property'
      WHERE properties_as_subject.name = 'Name'
    SQL
  end

  test '::filter with seperate joins' do
    query = View.filter(subject: {as: "BelongsToPolymorphicFilterTest::Property", name: 'Name'}, account: {name: 'Account'})
    assert_sql(<<-SQL, query)
      SELECT views.* FROM views
      LEFT OUTER JOIN accounts
        ON accounts.id = views.account_id
      LEFT OUTER JOIN properties properties_as_subject
        ON properties_as_subject.id = views.subject_id AND views.subject_type = 'BelongsToPolymorphicFilterTest::Property'
      WHERE properties_as_subject.name = 'Name' AND accounts.name = 'Account'
    SQL
  end
  
  test '::filter beyond polymorphic boundary' do
    query = View.filter(subject: {as: "BelongsToPolymorphicFilterTest::Property", account: {name: 'Name'}})
    assert_sql(<<-SQL, query)
      
    SQL
  end

end
{'fraction_non_alphanumeric': 0.09327548806941431, 'fraction_numerical': 0.0032537960954446853, 'mean_word_length': 3.168361581920904, 'pattern_counts': {'":': 2, '<': 21, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2559112', 'n_tokens_mistral': 1168, 'n_tokens_neox': 1097, 'n_words': 302}