<commit_before># encoding: UTF-8

require "helper"

class TestScanning < Test::Unit::TestCase

  include Helper::Client

  def test_scan_basic
    r.debug :populate, 1000

    cursor = 0
    all_keys   = []
    loop {
      cursor, keys = r.scan cursor
      all_keys += keys
      break if cursor == "0"
    }

    assert_equal 1000, all_keys.uniq.size
  end

  def test_scan_count
    r.debug :populate, 1000

    cursor = 0
    all_keys   = []
    loop {
      cursor, keys = r.scan cursor, count: 5
      all_keys += keys
      break if cursor == "0"
    }

    assert_equal 1000, all_keys.uniq.size
  end

  def test_scan_match
    r.debug :populate, 1000

    cursor = 0
    all_keys   = []
    loop {
      cursor, keys = r.scan cursor, match: "key:1??"
      all_keys += keys
      break if cursor == "0"
    }

    assert_equal 100, all_keys.uniq.size
  end

  def test_sscan_with_encoding
    [:intset, :hashtable].each do |enc|
      r.del "set"

      prefix = ""
      prefix = "ele:" if enc == :hashtable

      elements = []
      100.times { |j| elements << "#{prefix}#{j}" }

      r.sadd "set", elements

      assert_equal enc.to_s, r.object("encoding", "set")

      cursor = 0
      all_keys   = []
      loop {
        cursor, keys = r.sscan "set", cursor
        all_keys += keys
        break if cursor == "0"
      }

      assert_equal 100, all_keys.uniq.size
    end
  end

  def test_hscan_with_encoding
    [:ziplist, :hashtable].each do |enc|
      r.del "set"

      count = 1000
      count = 30 if enc == :ziplist

      elements = []
      count.times { |j| elements << "key:#{j}" << j.to_s }

      r.hmset "hash", *elements

      assert_equal enc.to_s, r.object("encoding", "hash")

      cursor = 0
      all_keys   = []
      loop {
        cursor, keys = r.hscan "hash", cursor
        all_keys += keys
        break if cursor == "0"
      }

      keys2 = []
      all_keys.each_slice(2) do |k, v|
        assert_equal "key:#{v}", k
        keys2 << k
      end

      assert_equal count, keys2.uniq.size
    end
  end

  def test_zscan_with_encoding
    [:ziplist, :skiplist].each do |enc|
      r.del "zset"

      count = 1000
      count = 30 if enc == :ziplist

      elements = []
      count.times { |j| elements << j << "key:#{j}" }

      r.zadd "zset", elements

      assert_equal enc.to_s, r.object("encoding", "zset")

      cursor = 0
      all_keys   = []
      loop {
        cursor, keys = r.zscan "zset", cursor
        all_keys += keys
        break if cursor == "0"
      }

      keys2 = []
      all_keys.each_slice(2) do |k, v|
        assert_equal "key:#{v}", k<commit_msg>Only run SCAN tests on recent redis version<commit_after># encoding: UTF-8

require "helper"

class TestScanning < Test::Unit::TestCase

  include Helper::Client

  def test_scan_basic
    return if version < "2.7.105"

    r.debug :populate, 1000

    cursor = 0
    all_keys   = []
    loop {
      cursor, keys = r.scan cursor
      all_keys += keys
      break if cursor == "0"
    }

    assert_equal 1000, all_keys.uniq.size
  end

  def test_scan_count
    return if version < "2.7.105"

    r.debug :populate, 1000

    cursor = 0
    all_keys   = []
    loop {
      cursor, keys = r.scan cursor, count: 5
      all_keys += keys
      break if cursor == "0"
    }

    assert_equal 1000, all_keys.uniq.size
  end

  def test_scan_match
    return if version < "2.7.105"

    r.debug :populate, 1000

    cursor = 0
    all_keys   = []
    loop {
      cursor, keys = r.scan cursor, match: "key:1??"
      all_keys += keys
      break if cursor == "0"
    }

    assert_equal 100, all_keys.uniq.size
  end

  def test_sscan_with_encoding
    return if version < "2.7.105"

    [:intset, :hashtable].each do |enc|
      r.del "set"

      prefix = ""
      prefix = "ele:" if enc == :hashtable

      elements = []
      100.times { |j| elements << "#{prefix}#{j}" }

      r.sadd "set", elements

      assert_equal enc.to_s, r.object("encoding", "set")

      cursor = 0
      all_keys   = []
      loop {
        cursor, keys = r.sscan "set", cursor
        all_keys += keys
        break if cursor == "0"
      }

      assert_equal 100, all_keys.uniq.size
    end
  end

  def test_hscan_with_encoding
    return if version < "2.7.105"

    [:ziplist, :hashtable].each do |enc|
      r.del "set"

      count = 1000
      count = 30 if enc == :ziplist

      elements = []
      count.times { |j| elements << "key:#{j}" << j.to_s }

      r.hmset "hash", *elements

      assert_equal enc.to_s, r.object("encoding", "hash")

      cursor = 0
      all_keys   = []
      loop {
        cursor, keys = r.hscan "hash", cursor
        all_keys += keys
        break if cursor == "0"
      }

      keys2 = []
      all_keys.each_slice(2) do |k, v|
        assert_equal "key:#{v}", k
        keys2 << k
      end

      assert_equal count, keys2.uniq.size
    end
  end

  def test_zscan_with_encoding
    return if version < "2.7.105"

    [:ziplist, :skiplist].each do |enc|
      r.del "zset"

      count = 1000
      count = 30 if enc == :ziplist

      elements = []
      count.times { |j| elements << j << "key:#{j}" }

      r.zadd "zset", elements

      assert_equal enc.to_s, r.object("encoding", "zset")

      cursor = 0
      all_keys   = []
      loop {
        cursor, keys = r.zscan "zset", cursor
        all_keys += keys
        break if cursor == "0"
      }

      keys2 = []
      all_keys.each_slice(2) do |k, v|
        assert_equal "key:#{v}", k
{'fraction_non_alphanumeric': 0.1258158085569253, 'fraction_numerical': 0.027918781725888325, 'mean_word_length': 1.8975840336134453, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 3, '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': '7353093', 'n_tokens_mistral': 2078, 'n_tokens_neox': 1897, 'n_words': 508}