<commit_before>require 'helper'

FROM = '2011-01-01 00:00:00'

# Testing client components related to stats
class StatsTest < Fastly::TestCase
  def setup
    opts = login_opts(:api_key)

    begin
      @fastly = Fastly.new(opts)
    rescue => e
      warn e.inspect
      warn e.backtrace.join("\n")
      exit(-1)
    end
  end

  def test_regions
    regions = @fastly.regions
    assert_equal true, (regions.size > 0)
  end

  def test_usage
    usage = @fastly.usage(:from => FROM)
    assert(usage['usa'], 'Found a USA region in usage')
    assert(usage['usa']['requests'], 'USA region has a requests field')

    usage = @fastly.usage(:from => FROM, :by_service => 1)
    assert(usage['usa'], 'Found a USA region in usage')
    assert(usage['usa']['requests'].nil?, "USA region doesn't have a requests field")
  end

  def test_stats
    stats = @fastly.stats(:from => FROM)
    service1, service2 = stats.keys
    assert(stats[service1][0]['requests'], 'Found requests')
    assert(stats[service1][0]['hits'], 'Found hits')
    assert(stats[service2][0]['requests'], 'Found requests')
    assert(stats[service2][0]['hits'], 'Found hits')

    stats = @fastly.stats(:from => FROM, :field => 'requests')
    assert(stats[service1][0]['requests'],  'Found requests')
    assert(stats[service1][0]['hits'].nil?, "Didn't find hits")
    assert(stats[service2][0]['requests'],  'Found requests')
    assert(stats[service2][0]['hits'].nil?, "Didn't find hits")

    stats = @fastly.stats(:from => FROM, :service => service1)
    assert_equal(stats[0]['service_id'], service1, 'Got correct service id')
    assert(stats[0]['requests'], 'Found requests')
    assert(stats[0]['hits'], 'Found hits')

    stats = @fastly.stats(:from => FROM, :field => 'requests', :service => service1)
    assert_equal(stats[0]['service_id'], service1, 'Got correct service id')
    assert(stats[0]['requests'], 'Found requests')
    assert(stats[0]['hits'].nil?, "Didn't find hits")

    stats = @fastly.stats(:from => FROM, :aggregate => true)
    assert(stats[0]['service_id'].nil?, 'No service id')
    assert(stats[0]['requests'], 'Found requests')
    assert(stats[0]['hits'], 'Found hits')

    # stats aggregate with field
    assert_raises Fastly::Error do
      @fastly.stats(:from => FROM, :field => 'requests', :aggregate => true)
    end

    # stats aggregate with service
    assert_raises Fastly::Error do
      @fastly.stats(:from => FROM, :service => service1, :aggregate => true)
    end

    assert_raises Fastly::Error do
      @fastly.stats(:from => FROM, :service => service1, :field => 'requests', :aggregate => true)
    end
  end
end
<commit_msg>fix stats test<commit_after>require 'helper'

FROM = '2011-01-01 00:00:00'

# Testing client components related to stats
class StatsTest < Fastly::TestCase
  def setup
    opts = login_opts(:api_key)

    begin
      @fastly = Fastly.new(opts)
    rescue => e
      warn e.inspect
      warn e.backtrace.join("\n")
      exit(-1)
    end
  end

  def test_regions
    regions = @fastly.regions
    assert_equal true, (regions.size > 0)
  end

  def test_usage
    usage = @fastly.usage(:from => FROM)
    # usage can be and empty hash if no usage stats
    # this client is not responsiblew for verifying the stats api,
    # just that requests to stats work
    assert(usage)
    assert(usage['meta'])
    assert_equal 'success', usage['status']

    usage = @fastly.usage(:from => FROM, :by_service => 1)
    assert(usage)
    assert(usage['meta'])
    assert_equal 'success', usage['status']
  end

  def test_stats
    stats = @fastly.stats(:from => FROM)
    # stats can be and empty hash if no usage stats
    assert(stats)
    assert_equal 'success', stats['status']
    assert_equal 'all', stats['meta']['region']

    stats = @fastly.stats(:from => FROM, :field => 'requests')
    assert(stats)
    assert_equal 'success', stats['status']
    assert_equal 'all', stats['meta']['region']

    stats = @fastly.stats(:from => FROM, :aggregate => true)
    assert(stats)
    assert_equal 'success', stats['status']
    assert_equal 'all', stats['meta']['region']

    # stats aggregate with field
    assert_raises Fastly::Error do
      @fastly.stats(:from => FROM, :field => 'requests', :aggregate => true)
    end

    # stats aggregate with service
    assert_raises Fastly::Error do
      @fastly.stats(:from => FROM, :service => 'myserviceId', :aggregate => true)
    end

    assert_raises Fastly::Error do
      @fastly.stats(:from => FROM, :service => 'datServiceID', :field => 'requests', :aggregate => true)
    end
  end
end

{'fraction_non_alphanumeric': 0.15600522193211488, 'fraction_numerical': 0.014577893820713663, 'mean_word_length': 3.695607763023493, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 48, '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': '6230269', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1421, 'n_words': 388}