<commit_before># encoding: utf-8

require 'spec_helper'

# FIXME: split this spec into smaller files
describe SQL::Generator::Emitter, '.visit' do
  include_context 'emitter'

  context 'with literal singletons' do
    assert_generates s(:true),  'TRUE'
    assert_generates s(:false), 'FALSE'
    assert_generates s(:null),  'NULL'
  end

  context 'with strings' do
    assert_generates s(:string, %q[echo 'Hello']), %q['echo ''Hello''']
  end

  context 'with integers' do
    assert_generates s(:integer, 1), '1'
  end

  context 'with floats' do
    assert_generates s(:float, 1.0), '1.0'
  end

  context 'with decimals' do
    assert_generates s(:decimal, BigDecimal('1.0')), '1.0'
  end

  context 'with dates' do
    assert_generates s(:date, Date.new(2013, 1, 1)), %q['2013-01-01']
  end

  context 'with datetimes' do
    nsec_in_seconds = Rational(1, 10**9)
    offset          = Rational(-8, 24)

    # A DateTime not in the UTC timezone
    datetime = DateTime.new(2013, 12, 31, 15, 59, 59 + nsec_in_seconds, offset)

    assert_generates(
      s(:datetime, datetime),
      %q['2013-12-31T23:59:59.000000001+00:00']  # converts to UTC
    )
  end

  context 'with times' do
    # A Time not in the UTC timezone
    time = begin
      original, ENV['TZ'] = ENV['TZ'], 'America/Vancouver'
      Time.local(2010, 12, 31, 15, 59, 59, 1).freeze
    ensure
      ENV['TZ'] = original
    end

    assert_generates(
      s(:time, time),
      %q['2010-12-31T23:59:59.000001000Z']  # converts to UTC
    )
  end

  context 'identifiers' do
    assert_generates s(:id, 'echo "oh hai"'), '"echo ""oh hai"""'
  end

  context 'unary scalars' do
    context 'with unary plus' do
      assert_generates s(:uplus, s(:integer, 1)), '+1'
    end

    context 'with unary minus' do
      assert_generates s(:uminus, s(:integer, 1)), '-1'
    end

    context 'with unary negation' do
      assert_generates s(:not, s(:true)), 'NOT TRUE'
    end
  end

  context 'unary prefix operations' do
    {
      count:  'COUNT',
      sum:    'SUM',
      min:    'MIN',
      max:    'MAX',
      avg:    'AVG',
      var:    'VAR_POP',
      stddev: 'STDDEV_POP',
      sqrt:   'SQRT',
      abs:    'ABS',
      length: 'LENGTH',
    }.each do |type, operator|
      context type.inspect do
        assert_generates(
          s(type, s(:id, 'foo')),
          %Q[#{operator} ("foo")]
        )
      end<commit_msg>Fix specs to use terms that match the new operation conventions<commit_after># encoding: utf-8

require 'spec_helper'

# FIXME: split this spec into smaller files
describe SQL::Generator::Emitter, '.visit' do
  include_context 'emitter'

  context 'with literal singletons' do
    assert_generates s(:true),  'TRUE'
    assert_generates s(:false), 'FALSE'
    assert_generates s(:null),  'NULL'
  end

  context 'with strings' do
    assert_generates s(:string, %q[echo 'Hello']), %q['echo ''Hello''']
  end

  context 'with integers' do
    assert_generates s(:integer, 1), '1'
  end

  context 'with floats' do
    assert_generates s(:float, 1.0), '1.0'
  end

  context 'with decimals' do
    assert_generates s(:decimal, BigDecimal('1.0')), '1.0'
  end

  context 'with dates' do
    assert_generates s(:date, Date.new(2013, 1, 1)), %q['2013-01-01']
  end

  context 'with datetimes' do
    nsec_in_seconds = Rational(1, 10**9)
    offset          = Rational(-8, 24)

    # A DateTime not in the UTC timezone
    datetime = DateTime.new(2013, 12, 31, 15, 59, 59 + nsec_in_seconds, offset)

    assert_generates(
      s(:datetime, datetime),
      %q['2013-12-31T23:59:59.000000001+00:00']  # converts to UTC
    )
  end

  context 'with times' do
    # A Time not in the UTC timezone
    time = begin
      original, ENV['TZ'] = ENV['TZ'], 'America/Vancouver'
      Time.local(2010, 12, 31, 15, 59, 59, 1).freeze
    ensure
      ENV['TZ'] = original
    end

    assert_generates(
      s(:time, time),
      %q['2010-12-31T23:59:59.000001000Z']  # converts to UTC
    )
  end

  context 'identifiers' do
    assert_generates s(:id, 'echo "oh hai"'), '"echo ""oh hai"""'
  end

  context 'unary prefix operations' do
    context 'with plus' do
      assert_generates s(:uplus, s(:integer, 1)), '+1'
    end

    context 'with minus' do
      assert_generates s(:uminus, s(:integer, 1)), '-1'
    end

    context 'with negation' do
      assert_generates s(:not, s(:true)), 'NOT TRUE'
    end
  end

  context 'unary function operations' do
    {
      count:  'COUNT',
      sum:    'SUM',
      min:    'MIN',
      max:    'MAX',
      avg:    'AVG',
      var:    'VAR_POP',
      stddev: 'STDDEV_POP',
      sqrt:   'SQRT',
      abs:    'ABS',
      length: 'LENGTH',
    }.each do |type, operator|
      context type.inspect do
        assert_generates(
          s(type, s(:id, 'foo')),
          %Q[#{operator} ("foo")]
        )
      end
{'fraction_non_alphanumeric': 0.14103618421052633, 'fraction_numerical': 0.047286184210526314, 'mean_word_length': 2.666164280331575, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6538959', 'n_tokens_mistral': 1913, 'n_tokens_neox': 1688, 'n_words': 489}