require_relative '../helper'
require 'izanami/mapper'

describe Izanami::Mapper do
  let(:redis)   { double('redis') }
  let(:options) { { redis: redis } }

  subject { Izanami::Mapper.new(options) }

  describe '#redis' do
    context 'with redis instance' do
      it 'uses the given redis instance' do
        subject.redis.should == redis
      end
    end

    context 'with redis options' do
      let(:redis_url) { ENV['IZANAMI_REDIS_URL'] }
      let(:options) { { url: redis_url } }

      it 'initializes a new redis instance' do
        subject.redis.should be_instance_of(Redis)
      end
    end
  end

  describe '#namespace' do
    context 'with a namespace' do
      let(:options) { { namespace: 'spec' } }

      it 'uses that namespace' do
        subject.namespace.should == 'spec'
      end
    end

    context 'without a namespace' do
      it 'uses the default namespace' do
        subject.namespace.should == 'izanami'
      end
    end
  end

  describe '#client' do
    it 'uses redis namespace as a proxy' do
      subject.client.should be_instance_of(Redis::Namespace)
    end
  end
end
