Skip to content
This repository
tag: v4.0.6
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 131 lines (113 sloc) 4.138 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
require 'spec_helper'

describe Picky::Category do

  before(:each) do
    @index = Picky::Index.new :some_index
    @source = stub :some_given_source, :each => nil
  end
  let(:category) { described_class.new(:some_category, @index, :source => @source).tap { |c| c.stub! :timed_exclaim } }

  context "unit specs" do
    let(:exact) { category.exact }
    let(:partial) { category.partial }

    describe 'clear' do
      it 'delegates to both bundles' do
        exact.should_receive(:clear).once.with()
        partial.should_receive(:clear).once.with()

        category.clear
      end
    end

    describe 'dump' do
      before(:each) do
        exact.stub! :dump
        partial.stub! :dump
      end
      it 'should dump the exact index' do
        exact.should_receive(:dump).once.with

        category.dump
      end
      it 'should dump the partial index' do
        partial.should_receive(:dump).once.with

        category.dump
      end
    end

    describe 'cache' do
      it 'should call multiple methods in order' do
        category.should_receive(:empty).once.with().ordered
        category.should_receive(:retrieve).once.with().ordered
        category.should_receive(:dump).once.with().ordered

        category.cache
      end
    end

    describe 'retrieve' do
      it 'call the right thing' do
        prepared = stub :prepared
        prepared.should_receive(:retrieve).any_number_of_times
                                          .and_yield(1, :some_token)
                                          .and_yield(2, :some_token)
                                          .and_yield(3, :some_token)
                                          .and_yield(4, :some_token)
                                          .and_yield(5, :some_token)
        category.stub! :prepared => prepared

        category.should_receive(:add_tokenized_token).once.with(1, :some_token, :<<)
        category.should_receive(:add_tokenized_token).once.with(2, :some_token, :<<)
        category.should_receive(:add_tokenized_token).once.with(3, :some_token, :<<)
        category.should_receive(:add_tokenized_token).once.with(4, :some_token, :<<)
        category.should_receive(:add_tokenized_token).once.with(5, :some_token, :<<)

        category.retrieve
      end
    end

    describe 'key_format' do
      context 'category has its own key_format' do
        before(:each) do
          category.instance_variable_set :@key_format, :other_key_format
        end
        it 'returns that key_format' do
          category.key_format.should == :other_key_format
        end
      end
      context 'category does not have its own key format' do
        before(:each) do
          category.instance_variable_set :@key_format, nil
        end
        context 'it has an index' do
          before(:each) do
            category.instance_variable_set :@index, stub(:index, :key_format => :yet_another_key_format)
          end
          it 'returns that key_format' do
            category.key_format.should == :yet_another_key_format
          end
        end
      end
    end

    describe 'source' do
      context 'with explicit source' do
        let(:category) { described_class.new(:some_category, @index, :source => [1]) }
        it 'returns the right source' do
          category.source.should == [1]
        end
      end
      context 'without explicit source' do
        let(:category) { described_class.new(:some_category, @index.tap{ |index| index.stub! :source => :index_source }) }
        it 'returns the right source' do
          category.source.should == :index_source
        end
      end
    end

    describe "index" do
      before(:each) do
        @indexer = stub :indexer, :index => nil
        category.stub! :indexer => @indexer
      end
      it "tells the indexer to index" do
        @indexer.should_receive(:prepare).once

        category.prepare
      end
    end
    describe "source" do
      context "without source" do
        it "has no problem with that" do
          lambda { described_class.new :some_name, @index }.should_not raise_error
        end
      end
    end
  end

end
Something went wrong with that request. Please try again.