<commit_before>require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))

class RedisTest < Test::Unit::TestCase

  PORT = 6379
  OPTIONS = {:port => PORT, :db => 15, :timeout => 3}

  setup do
    @r = prepare Redis.new(OPTIONS)
  end

  context "initialize with URL" do
    test "defaults to 127.0.0.1:6379" do
      redis = Redis.connect

      assert_equal "127.0.0.1", redis.client.host
      assert_equal 6379, redis.client.port
      assert_equal 0, redis.client.db
      assert_nil redis.client.password
    end

    test "takes a url" do
      redis = Redis.connect :url => "redis://:secr3t@<EMAIL>:999/2"

      assert_equal "foo.com", redis.client.host
      assert_equal 999, redis.client.port
      assert_equal 2, redis.client.db
      assert_equal "<PASSWORD>", redis.client.password
    end

    test "uses REDIS_URL over default if available" do
      ENV["REDIS_URL"] = "redis://:secr3t@foo.com:999/2"

      redis = Redis.connect

      assert_equal "foo.com", redis.client.host
      assert_equal 999, redis.client.port
      assert_equal 2, redis.client.db
      assert_equal "<PASSWORD>", redis.client.password

      ENV.delete("REDIS_URL")
    end
  end

  context "Internals" do
    setup do
      @log = StringIO.new
      @r = Redis.new(OPTIONS.merge(:logger => ::Logger.new(@log)))
    end

    test "Logger" do
      @r.ping

      assert_match(/Redis >> PING/, @log.string)
      assert_match(/Redis >> 0.\d+ms/, @log.string)
    end

    test "Logger with pipelining" do
      @r.pipelined do<commit_msg>Make log regex more flexible for slower tests.<commit_after>require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))

class RedisTest < Test::Unit::TestCase

  PORT = 6379
  OPTIONS = {:port => PORT, :db => 15, :timeout => 3}

  setup do
    @r = prepare Redis.new(OPTIONS)
  end

  context "initialize with URL" do
    test "defaults to 127.0.0.1:6379" do
      redis = Redis.connect

      assert_equal "127.0.0.1", redis.client.host
      assert_equal 6379, redis.client.port
      assert_equal 0, redis.client.db
      assert_nil redis.client.password
    end

    test "takes a url" do
      redis = Redis.connect :url => "redis://:secr3t@foo.com:999/2"

      assert_equal "foo.com", redis.client.host
      assert_equal 999, redis.client.port
      assert_equal 2, redis.client.db
      assert_equal "<PASSWORD>", redis.client.password
    end

    test "uses REDIS_URL over default if available" do
      ENV["REDIS_URL"] = "redis://:secr3t@foo.com:999/2"

      redis = Redis.connect

      assert_equal "foo.com", redis.client.host
      assert_equal 999, redis.client.port
      assert_equal 2, redis.client.db
      assert_equal "<PASSWORD>", redis.client.password

      ENV.delete("REDIS_URL")
    end
  end

  context "Internals" do
    setup do
      @log = StringIO.new
      @r = Redis.new(OPTIONS.merge(:logger => ::Logger.new(@log)))
    end

    test "Logger" do
      @r.ping

      assert_match(/Redis >> PING/, @log.string)
      assert_match(/Redis >> \d+\.\d+ms/, @log.string)
    end

    test "Logger with pipelining" do
      @r.pipelined do
{'fraction_non_alphanumeric': 0.12643312101910828, 'fraction_numerical': 0.02961783439490446, 'mean_word_length': 2.7304038004750595, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 7, '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': '1157881', 'n_tokens_mistral': 1177, 'n_tokens_neox': 1077, 'n_words': 252}