<commit_before>require 'helper'

class ElasticRecord::RelationTest < MiniTest::Test
  def test_count
    create_widgets [Widget.new(id: 5, color: 'red'), Widget.new(id: 10, color: 'blue')]

    assert_equal 2, Widget.elastic_relation.count
  end

  def test_facets
    create_widgets [Widget.new(id: 5, color: 'red'), Widget.new(id: 10, color: 'blue')]

    facets = Widget.elastic_relation.facet(Widget.arelastic.facet['popular_colors'].terms('color')).facets

    assert_equal 2, facets['popular_colors']['total']
  end

  def test_aggregations
    create_widgets [Widget.new(id: 5, color: 'red'), Widget.new(id: 10, color: 'blue')]

    aggregations = Widget.elastic_relation.aggregate('popular_colors' => {'terms' => {'field' => 'color'}}).aggregations

    assert_equal 2, aggregations['popular_colors']['buckets'].size
    assert_equal %w(red blue).to_set, aggregations['popular_colors']['buckets'].map { |bucket| bucket['key'] }.to_set
  end

  def test_explain
    create_widgets [Widget.new(id: 10, color: 'blue')]

    # explain = Widget.elastic_relation.filter(color: 'blue').explain('10')
  end

  def test_to_hits<commit_msg>Make test less fragile when executing tests randomly<commit_after>require 'helper'

class ElasticRecord::RelationTest < MiniTest::Test
  def test_count
    original_count = Widget.elastic_relation.count
    create_widgets [Widget.new(id: 5, color: 'red'), Widget.new(id: 10, color: 'blue')]

    assert_equal 2, Widget.elastic_relation.count - original_count
  end

  def test_facets
    create_widgets [Widget.new(id: 5, color: 'red'), Widget.new(id: 10, color: 'blue')]

    facets = Widget.elastic_relation.facet(Widget.arelastic.facet['popular_colors'].terms('color')).facets

    assert_equal 2, facets['popular_colors']['total']
  end

  def test_aggregations
    create_widgets [Widget.new(id: 5, color: 'red'), Widget.new(id: 10, color: 'blue')]

    aggregations = Widget.elastic_relation.aggregate('popular_colors' => {'terms' => {'field' => 'color'}}).aggregations

    assert_equal 2, aggregations['popular_colors']['buckets'].size
    assert_equal %w(red blue).to_set, aggregations['popular_colors']['buckets'].map { |bucket| bucket['key'] }.to_set
  end

  def test_explain
    create_widgets [Widget.new(id: 10, color: 'blue')]

    # explain = Widget.elastic_relation.filter(color: 'blue').explain('10')
  end

  def test_to_hits
{'fraction_non_alphanumeric': 0.15023080151070078, 'fraction_numerical': 0.013428451531682753, 'mean_word_length': 5.176165803108808, 'pattern_counts': {'":': 0, '<': 5, '<?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': '349911', 'n_tokens_mistral': 858, 'n_tokens_neox': 779, 'n_words': 163}