<commit_before>require 'sqlstmt/select'

module SqlStmt

class TestSelect < DohTest::TestGroup
  def test_includes_table
    sqlb = Select.new.table('target t')
    assert(sqlb.includes_table?('target'))
    assert(sqlb.includes_table?('t'))
    assert(!sqlb.includes_table?('blah'))
  end

  def test_minimum_requirements
    assert_raises(SqlStmt::Error) { Select.new.table('target').to_s }
    assert_raises(SqlStmt::Error) { Select.new.table('target').no_where.to_s }
    assert_raises(SqlStmt::Error) { Select.new.table('target').optional_where.to_s }
    assert_equal('SELECT blah FROM target', Select.new.table('target').optional_where.field('blah').to_sql)
  end

  def test_stuff
    assert_equal('SELECT blah FROM source', Select.new.table('source').field('blah').no_where.to_s)
    assert_equal('SELECT DISTINCT blah FROM source', Select.new.table('source').field('blah').no_where.distinct.to_s)
    assert_equal('SELECT blah FROM source WHERE source_id = 1', Select.new.table('source').field('blah').where('source_id = 1').to_s)
    assert_equal('SELECT blah FROM source s', Select.new.table('source s').field('blah').no_where.to_s)
    assert_equal('SELECT blah FROM source s JOIN other o ON s.blah_id = o.blah_id', Select.new.table('source s').join('other o', 's.blah_id = o.blah_id').field('blah').no_where.to_s)
    assert_equal('SELECT blah FROM source s LEFT JOIN other o ON s.blah_id = o.blah_id', Select.new.table('source s').left_join('other o', 's.blah_id = o.blah_id').field('blah').no_where.to_s)
    assert_equal('SELECT blah,blee FROM source', Select.new.table('source').field('blah','blee').no_where.to_s)
    assert_equal('SELECT blah FROM source HAVING blee > 0', Select.new.table('source').field('blah').no_where.having('blee > 0').to_s)
  end

  def test_duplicate_joins
    base_sqlb = Select.new.table('source s').field('frog').no_where

    sqlb = base_sqlb.dup
    4.times { sqlb.join('other o', 's.blah_id = o.blah_id') }
    sqlb.optional_join('other o', 'z.blee_id = o.blee_id')
    assert_equal('SELECT frog FROM source s JOIN other o ON s.blah_id = o.blah_id', sqlb.to_s)

    sqlb = base_sqlb.dup
    sqlb.optional_join('other o', 'z.blee_id = o.blee_id')
    assert_equal('SELECT frog FROM source s JOIN other o ON z.blee_id = o.blee_id', sqlb.to_s)
  end

  def test_join_with_multiple_conditions
    %i(join left_join).each do |method|
      sqlb = Select.new.table('source s').field('frog').no_where.send(method, 'other o', 'z.blee_id = o.blee_id', 'z.other_field = o.other_field')
      method_sql = method.to_s.upcase.sub('_', ' ')
      assert_equal("SELECT frog FROM source s #{method_sql} other o ON z.blee_id = o.blee_id AND z.other_field = o.other_field", sqlb.to_s)<commit_msg>split test into duplicate and optional cases<commit_after>require 'sqlstmt/select'

module SqlStmt

class TestSelect < DohTest::TestGroup
  def test_includes_table
    sqlb = Select.new.table('target t')
    assert(sqlb.includes_table?('target'))
    assert(sqlb.includes_table?('t'))
    assert(!sqlb.includes_table?('blah'))
  end

  def test_minimum_requirements
    assert_raises(SqlStmt::Error) { Select.new.table('target').to_s }
    assert_raises(SqlStmt::Error) { Select.new.table('target').no_where.to_s }
    assert_raises(SqlStmt::Error) { Select.new.table('target').optional_where.to_s }
    assert_equal('SELECT blah FROM target', Select.new.table('target').optional_where.field('blah').to_sql)
  end

  def test_stuff
    assert_equal('SELECT blah FROM source', Select.new.table('source').field('blah').no_where.to_s)
    assert_equal('SELECT DISTINCT blah FROM source', Select.new.table('source').field('blah').no_where.distinct.to_s)
    assert_equal('SELECT blah FROM source WHERE source_id = 1', Select.new.table('source').field('blah').where('source_id = 1').to_s)
    assert_equal('SELECT blah FROM source s', Select.new.table('source s').field('blah').no_where.to_s)
    assert_equal('SELECT blah FROM source s JOIN other o ON s.blah_id = o.blah_id', Select.new.table('source s').join('other o', 's.blah_id = o.blah_id').field('blah').no_where.to_s)
    assert_equal('SELECT blah FROM source s LEFT JOIN other o ON s.blah_id = o.blah_id', Select.new.table('source s').left_join('other o', 's.blah_id = o.blah_id').field('blah').no_where.to_s)
    assert_equal('SELECT blah,blee FROM source', Select.new.table('source').field('blah','blee').no_where.to_s)
    assert_equal('SELECT blah FROM source HAVING blee > 0', Select.new.table('source').field('blah').no_where.having('blee > 0').to_s)
  end

  def test_duplicate_joins
    sqlb = Select.new.table('source s').field('frog').no_where
    4.times { sqlb.join('other o', 's.blah_id = o.blah_id') }
    assert_equal('SELECT frog FROM source s JOIN other o ON s.blah_id = o.blah_id', sqlb.to_s)
  end

  def test_optional_join
    sqlb = Select.new.table('source s').field('frog').no_where
    sqlb.join('other o', 's.blah_id = o.blah_id')
    sqlb.optional_join('other o', 'z.blee_id = o.blee_id')
    assert_equal('SELECT frog FROM source s JOIN other o ON s.blah_id = o.blah_id', sqlb.to_s)
  end

  def test_join_with_multiple_conditions
    %i(join left_join).each do |method|
      sqlb = Select.new.table('source s').field('frog').no_where.send(method, 'other o', 'z.blee_id = o.blee_id', 'z.other_field = o.other_field')
      method_sql = method.to_s.upcase.sub('_', ' ')
      assert_equal("SELECT frog FROM source s #{method_sql} other o ON z.blee_id = o.blee_id AND z.other_field = o.other_field", sqlb.to_s)
{'fraction_non_alphanumeric': 0.1552536231884058, 'fraction_numerical': 0.0018115942028985507, 'mean_word_length': 4.733125649013499, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '6914373', 'n_tokens_mistral': 2045, 'n_tokens_neox': 1990, 'n_words': 379}