<commit_before>require "../../src/reql/executor/datum.cr"
require "../../src/driver/*"
require "../../src/reql/encoder.cr"
require "spec"
include RethinkDB::DSL

describe ReQL::Datum do
  arr = [
    ReQL::Minval.new,
    [] of String,
    ["a"],
    ["a", "a"],
    ["aa", "a"],
    ["ab", "a"],
    ["b", "a"],
    ["c"],
    false,
    true,
    nil,
    -100000000001,
    -100000000000.5,
    -100000000000.0,
    -2,
    -1.6,
    -1.5,
    -1,
    0,
    1,
    1.5,
    1.6,
    2,
    100000000000.0,
    100000000000.5,
    100000000001,
    {} of String => String,
    {"a" => "b"},
    Bytes.new(0),
    Bytes[1, 1, 1],
    Bytes[1, 2],
    Bytes[1, 2, 3],
    "",
    "a",
    "aa",
    "aaa",
    "ab",
    ReQL::Maxval.new,
  ].map { |x| ReQL::Datum.new(x) }

  it "sorts in the correct order" do
    arr.each_with_index do |a, i|
      arr.each_with_index do |b, j|
        if i < j
          a.should be < b
        elsif i == j
          a.should eq b
        else
          a.should be > b
        end
      end
    end
  end

  it "sorts in the correct order after encoding to bytes" do
    arr.each_with_index do |_a, i|
      a = ReQL.encode_key(_a)
      ReQL.decode_key(a).should eq _a
      arr.each_with_index do |b, j|
        b = ReQL.encode_key(b)
        if i < j
          a.should be < b
        elsif i == j
          a.should eq b
        else
          a.should be > b
        end
      end
    end
  end

  _conn = r.connect("172.17.0.2") rescue r.connect("localhost") rescue r.connect("rethinkdb") rescue nil

  if _conn
    conn = _conn
    it "sorts in the same order on RethinkDB" do
      futures = [] of Concurrent::Future(Bool)
      arr[1..-2].each_with_index do |a, i|
        arr[1..-2].each_with_index do |b, j|
          futures << future do
            aexpr = r.expr(JSON.parse(a.to_json))
            bexpr = r.expr(JSON.parse(b.to_json))

            if i < j
              unless aexpr.lt(bexpr).run(conn).datum.bool
                err = "'#{a.inspect}' should order before '#{b.inspect}'"
                fail err
              end
            elsif i == j
              unless aexpr.eq(bexpr).run(conn).datum.bool
                err = "'#{a.inspect}' should be equal to '#{b.inspect}'"
                fail err
              end
            else
              unless aexpr.gt(bexpr).run(conn).datum.bool
                err = "'#{a.inspect}' should order after '#{b.inspect}'"
                fail err
              end
            end

            true
          end
        end
      end
      futures.each &.get
    end

    Spec.after_suite do
      conn.close<commit_msg>simplify order spec for rethinkdb<commit_after>require "../../src/reql/executor/datum.cr"
require "../../src/driver/*"
require "../../src/reql/encoder.cr"
require "spec"
include RethinkDB::DSL

describe ReQL::Datum do
  arr = [
    ReQL::Minval.new,
    [] of String,
    ["a"],
    ["a", "a"],
    ["aa", "a"],
    ["ab", "a"],
    ["b", "a"],
    ["c"],
    false,
    true,
    nil,
    -100000000001,
    -100000000000.5,
    -100000000000.0,
    -2,
    -1.6,
    -1.5,
    -1,
    0,
    1,
    1.5,
    1.6,
    2,
    100000000000.0,
    100000000000.5,
    100000000001,
    {} of String => String,
    {"a" => "b"},
    Bytes.new(0),
    Bytes[1, 1, 1],
    Bytes[1, 2],
    Bytes[1, 2, 3],
    "",
    "a",
    "aa",
    "aaa",
    "ab",
    ReQL::Maxval.new,
  ].map { |x| ReQL::Datum.new(x) }

  it "sorts in the correct order" do
    arr.each_with_index do |a, i|
      arr.each_with_index do |b, j|
        if i < j
          a.should be < b
        elsif i == j
          a.should eq b
        else
          a.should be > b
        end
      end
    end
  end

  it "sorts in the correct order after encoding to bytes" do
    arr.each_with_index do |_a, i|
      a = ReQL.encode_key(_a)
      ReQL.decode_key(a).should eq _a
      arr.each_with_index do |b, j|
        b = ReQL.encode_key(b)
        if i < j
          a.should be < b
        elsif i == j
          a.should eq b
        else
          a.should be > b
        end
      end
    end
  end

  _conn = r.connect("172.17.0.2") rescue r.connect("localhost") rescue r.connect("rethinkdb") rescue nil

  if _conn
    conn = _conn
    it "sorts in the same order on RethinkDB" do
      (1..(arr.size-3)).each do |i|
        a = arr[i]
        b = arr[i + 1]

        aexpr = r.expr(JSON.parse(a.to_json))
        bexpr = r.expr(JSON.parse(b.to_json))

        unless aexpr.lt(bexpr).run(conn).datum.bool
          err = "'#{a.inspect}' should order before '#{b.inspect}'"
          fail err
        end
      end
    end

    Spec.after_suite do
      conn.close
{'fraction_non_alphanumeric': 0.14921858274459432, 'fraction_numerical': 0.046456861485763216, 'mean_word_length': 1.5199568500539373, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 12, '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': '205845', 'n_tokens_mistral': 1880, 'n_tokens_neox': 1734, 'n_words': 439}