<commit_before>require "../../src/driver/*"
require "spec"
require "file_utils"
include RethinkDB::DSL

path = "/tmp/rethinkdb-lite/spec-temp-tables/#{Random::Secure.hex}"
FileUtils.rm_rf path
conn = r.local_database(path)

describe RethinkDB do
  it "maps datum array to datum array" do
    r([1, 2, 3]).map { |x| x }.run(conn).should eq (1..3).to_a
    r([1, 2, 3]).map { |x| {"value" => x.as(R::Type)}.as(R::Type) }.run(conn).should eq (1..3).map { |x| {"value" => x} }
    r([1, 2, 3]).map { |x| x }.count.run(conn).should eq 3
  end

  it "maps stream to stream" do
    r.range(1, 4).map { |x| x }.run(conn).datum.should eq (1..3).to_a
    r.range(1, 4).map { |x| {"value" => x.as(R::Type)}.as(R::Type) }.run(conn).datum.should eq (1..3).map { |x| {"value" => x} }
    r.range(1, 4).map { |x| x }.count.run(conn).datum.should eq 3
  end

  it "maps into a stream (which is coerced into array)" do
    r.range(1, 10).map { |x| r.range(0, x) }.run(conn).datum.should eq (1...10).map { |i| (0...i).to_a }
  end

  it "counts across map" do
    r.range(10000).map { |x| x }.count.run(conn).should eq 10000
  end

  it "can map an infinite stream" do
    stream = r.range.map { |x| [x.as(R::Type)].as(R::Type) }.run(conn).as RethinkDB::Cursor
    1000.times do |i|
      stream.next.should eq([i.to_i64])
    end<commit_msg>spec: map_spec: close cursor after reading<commit_after>require "../../src/driver/*"
require "spec"
require "file_utils"
include RethinkDB::DSL

path = "/tmp/rethinkdb-lite/spec-temp-tables/#{Random::Secure.hex}"
FileUtils.rm_rf path
conn = r.local_database(path)

describe RethinkDB do
  it "maps datum array to datum array" do
    r([1, 2, 3]).map { |x| x }.run(conn).should eq (1..3).to_a
    r([1, 2, 3]).map { |x| {"value" => x.as(R::Type)}.as(R::Type) }.run(conn).should eq (1..3).map { |x| {"value" => x} }
    r([1, 2, 3]).map { |x| x }.count.run(conn).should eq 3
  end

  it "maps stream to stream" do
    r.range(1, 4).map { |x| x }.run(conn).datum.should eq (1..3).to_a
    r.range(1, 4).map { |x| {"value" => x.as(R::Type)}.as(R::Type) }.run(conn).datum.should eq (1..3).map { |x| {"value" => x} }
    r.range(1, 4).map { |x| x }.count.run(conn).datum.should eq 3
  end

  it "maps into a stream (which is coerced into array)" do
    r.range(1, 10).map { |x| r.range(0, x) }.run(conn).datum.should eq (1...10).map { |i| (0...i).to_a }
  end

  it "counts across map" do
    r.range(10000).map { |x| x }.count.run(conn).should eq 10000
  end

  it "can map an infinite stream" do
    stream = r.range.map { |x| [x.as(R::Type)].as(R::Type) }.run(conn).as RethinkDB::Cursor
    1000.times do |i|
      stream.next.should eq([i.to_i64])
    end
    stream.close
{'fraction_non_alphanumeric': 0.2208611729769859, 'fraction_numerical': 0.03637713437268003, 'mean_word_length': 3.095744680851064, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '6500605', 'n_tokens_mistral': 1197, 'n_tokens_neox': 1130, 'n_words': 283}