<commit_before>require 'spec_helper'

describe BabySqueel::ActiveRecord::WhereChain do
  describe '#has' do
    it 'wheres on an attribute' do
      relation = Post.where.has {
        title == 'OJ Simpson'
      }

      expect(relation).to produce_sql(<<-EOSQL)
        SELECT "posts".* FROM "posts"
        WHERE "posts"."title" = 'OJ Simpson'
      EOSQL
    end

    it 'wheres on associations' do
      relation = Post.joins(:author).where.has {
        author.name == '<NAME>'
      }

      expect(relation).to produce_sql(<<-EOSQL)
        SELECT "posts".* FROM "posts"
        INNER JOIN "authors" ON "authors"."id" = "posts"."author_id"
        WHERE "authors"."name" = '<NAME>'
      EOSQL
    end

    it 'wheres using functions' do
      relation = Post.joins(:author).where.has {
        coalesce(title, author.name) == 'meatloaf'
      }

      expect(relation).to produce_sql(<<-EOSQL)
        SELECT "posts".* FROM "posts"
        INNER JOIN "authors" ON "authors"."id" = "posts"."author_id"
        WHERE coalesce("posts"."title", "authors"."name") = 'meatloaf'
      EOSQL
    end

    it 'wheres using operations' do
      relation = Post.where.has { (id + 1) == 2 }

      expect(relation).to produce_sql(<<-EOSQL)
        SELECT "posts".* FROM "posts"
        WHERE ("posts"."id" + 1) = 2
      EOSQL
    end

    it 'wheres using complex conditions', :pre_ar42 do
      relation = Post.joins(:author).where.has {
        (title =~ 'Simp%').or(author.name == 'meatloaf')
      }

      expect(relation).to produce_sql(<<-EOSQL)
        SELECT "posts".* FROM "posts"
        INNER JOIN "authors" ON "authors"."id" = "posts"."author_id"
        WHERE (("posts"."title" LIKE 'Simp%' OR "authors"."name" = 'meatloaf'))
      EOSQL
    end

    it 'wheres using complex conditions', :post_ar42 do
      relation = Post.joins(:author).where.has {
        (title =~ 'Simp%').or(author.name == 'meatloaf')
      }

      expect(relation).to produce_sql(<<-EOSQL)
        SELECT "posts".* FROM "posts"
        INNER JOIN "authors" ON "authors"."id" = "posts"."author_id"
        WHERE ("posts"."title" LIKE 'Simp%' OR "authors"."name" = 'meatloaf')
      EOSQL
    end
  end
end
<commit_msg>Add a spec for querying through an association<commit_after>require 'spec_helper'

describe BabySqueel::ActiveRecord::WhereChain do
  describe '#has' do
    it 'wheres on an attribute' do
      relation = Post.where.has {
        title == 'OJ Simpson'
      }

      expect(relation).to produce_sql(<<-EOSQL)
        SELECT "posts".* FROM "posts"
        WHERE "posts"."title" = 'OJ Simpson'
      EOSQL
    end

    it 'wheres on associations' do
      relation = Post.joins(:author).where.has {
        author.name == '<NAME>'
      }

      expect(relation).to produce_sql(<<-EOSQL)
        SELECT "posts".* FROM "posts"
        INNER JOIN "authors" ON "authors"."id" = "posts"."author_id"
        WHERE "authors"."name" = '<NAME>'
      EOSQL
    end

    it 'wheres using functions' do
      relation = Post.joins(:author).where.has {
        coalesce(title, author.name) == 'meatloaf'
      }

      expect(relation).to produce_sql(<<-EOSQL)
        SELECT "posts".* FROM "posts"
        INNER JOIN "authors" ON "authors"."id" = "posts"."author_id"
        WHERE coalesce("posts"."title", "authors"."name") = 'meatloaf'
      EOSQL
    end

    it 'wheres using operations' do
      relation = Post.where.has { (id + 1) == 2 }

      expect(relation).to produce_sql(<<-EOSQL)
        SELECT "posts".* FROM "posts"
        WHERE ("posts"."id" + 1) = 2
      EOSQL
    end

    it 'wheres using complex conditions', :pre_ar42 do
      relation = Post.joins(:author).where.has {
        (title =~ 'Simp%').or(author.name == 'meatloaf')
      }

      expect(relation).to produce_sql(<<-EOSQL)
        SELECT "posts".* FROM "posts"
        INNER JOIN "authors" ON "authors"."id" = "posts"."author_id"
        WHERE (("posts"."title" LIKE 'Simp%' OR "authors"."name" = 'meatloaf'))
      EOSQL
    end

    it 'wheres using complex conditions', :post_ar42 do
      relation = Post.joins(:author).where.has {
        (title =~ 'Simp%').or(author.name == 'meatloaf')
      }

      expect(relation).to produce_sql(<<-EOSQL)
        SELECT "posts".* FROM "posts"
        INNER JOIN "authors" ON "authors"."id" = "posts"."author_id"
        WHERE ("posts"."title" LIKE 'Simp%' OR "authors"."name" = 'meatloaf')
      EOSQL
    end

    it 'can query through associations' do
      relation = Post.joins(author: :comments).where.has {
        author.comments.id > 0
      }

      expect(relation).to produce_sql(<<-EOSQL)
        SELECT "posts".* FROM "posts"
        INNER JOIN "authors" ON "authors"."id" = "posts"."author_id"
        INNER JOIN "comments" ON "comments"."author_id" = "authors"."id"
        WHERE ("comments"."id" > 0)
      EOSQL
    end
  end
end

{'fraction_non_alphanumeric': 0.15462012320328541, 'fraction_numerical': 0.003696098562628337, 'mean_word_length': 2.4399717514124295, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 9, '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': '7583667', 'n_tokens_mistral': 1731, 'n_tokens_neox': 1579, 'n_words': 391}