Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 282 lines (226 sloc) 7.696 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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
require 'helper'

describe Plucky::OptionsHash do
  it "delegates missing methods to the source hash" do
    hash = {:limit => 1, :skip => 1}
    options = described_class.new(hash)
    options[:skip].should == 1
    options[:limit].should == 1
    options.keys.to_set.should == [:limit, :skip].to_set
  end

  describe "#initialize_copy" do
    before do
      @original = described_class.new(:fields => {:name => true}, :sort => :name, :limit => 10)
      @cloned = @original.clone
    end

    it "duplicates source hash" do
      @cloned.source.should_not equal(@original.source)
    end

    it "clones duplicable? values" do
      @cloned[:fields].should_not equal(@original[:fields])
      @cloned[:sort].should_not equal(@original[:sort])
    end
  end

  describe "#fields?" do
    it "returns true if fields have been selected" do
      described_class.new(:fields => :name).fields?.should be(true)
    end

    it "returns false if no fields have been selected" do
      described_class.new.fields?.should be(false)
    end
  end

  describe "#[]=" do

    context ":fields" do
      before { @options = described_class.new }
      subject { @options }

      it "defaults to nil" do
        subject[:fields].should be_nil
      end

      it "returns nil if empty string" do
        subject[:fields] = ''
        subject[:fields].should be_nil
      end

      it "returns nil if empty array" do
        subject[:fields] = []
        subject[:fields].should be_nil
      end

      it "works with array" do
        subject[:fields] = %w[one two]
        subject[:fields].should == %w[one two]
      end

      # Ruby 1.9.1 was sending array [{:age => 20}],
      # instead of hash.
      it "works with array that has one hash" do
        subject[:fields] = [{:age => 20}]
        subject[:fields].should == {:age => 20}
      end

      it "flattens multi-dimensional array" do
        subject[:fields] = [[:one, :two]]
        subject[:fields].should == [:one, :two]
      end

      it "works with symbol" do
        subject[:fields] = :one
        subject[:fields].should == [:one]
      end

      it "works with array of symbols" do
        subject[:fields] = [:one, :two]
        subject[:fields].should == [:one, :two]
      end

      it "works with hash" do
        subject[:fields] = {:one => 1, :two => -1}
        subject[:fields].should == {:one => 1, :two => -1}
      end

      it "converts comma separated list to array" do
        subject[:fields] = 'one, two'
        subject[:fields].should == %w[one two]
      end

      it "converts select" do
        subject[:select] = 'one, two'
        subject[:select].should be_nil
        subject[:fields].should == %w[one two]
      end
    end

    context ":limit" do
      before { @options = described_class.new }
      subject { @options }

      it "defaults to nil" do
        subject[:limit].should be_nil
      end

      it "uses limit provided" do
        subject[:limit] = 1
        subject[:limit].should == 1
      end

      it "converts string to integer" do
        subject[:limit] = '1'
        subject[:limit].should == 1
      end
    end

    context ":skip" do
      before { @options = described_class.new }
      subject { @options }

      it "defaults to nil" do
        subject[:skip].should be_nil
      end

      it "uses limit provided" do
        subject[:skip] = 1
        subject[:skip].should == 1
      end

      it "converts string to integer" do
        subject[:skip] = '1'
        subject[:skip].should == 1
      end

      it "returns set from offset" do
        subject[:offset] = '1'
        subject[:offset].should be_nil
        subject[:skip].should == 1
      end
    end

    context ":sort" do
      before { @options = described_class.new }
      subject { @options }

      it "defaults to nil" do
        subject[:sort].should be_nil
      end

      it "works with natural order ascending" do
        subject[:sort] = {'$natural' => 1}
        subject[:sort].should == {'$natural' => 1}
      end

      it "works with natural order descending" do
        subject[:sort] = {'$natural' => -1}
        subject[:sort].should =={'$natural' => -1}
      end

      it "converts single ascending field (string)" do
        subject[:sort] = 'foo asc'
        subject[:sort].should == [['foo', 1]]

        subject[:sort] = 'foo ASC'
        subject[:sort].should == [['foo', 1]]
      end

      it "converts single descending field (string)" do
        subject[:sort] = 'foo desc'
        subject[:sort].should == [['foo', -1]]

        subject[:sort] = 'foo DESC'
        subject[:sort].should == [['foo', -1]]
      end

      it "converts multiple fields (string)" do
        subject[:sort] = 'foo desc, bar asc'
        subject[:sort].should == [['foo', -1], ['bar', 1]]
      end

      it "converts multiple fields and default no direction to ascending (string)" do
        subject[:sort] = 'foo desc, bar, baz'
        subject[:sort].should == [['foo', -1], ['bar', 1], ['baz', 1]]
      end

      it "converts symbol" do
        subject[:sort] = :name
        subject[:sort] = [['name', 1]]
      end

      it "converts operator" do
        subject[:sort] = :foo.desc
        subject[:sort].should == [['foo', -1]]
      end

      it "converts array of operators" do
        subject[:sort] = [:foo.desc, :bar.asc]
        subject[:sort].should == [['foo', -1], ['bar', 1]]
      end

      it "converts array of symbols" do
        subject[:sort] = [:first_name, :last_name]
        subject[:sort] = [['first_name', 1], ['last_name', 1]]
      end

      it "works with array and one string element" do
        subject[:sort] = ['foo, bar desc']
        subject[:sort].should == [['foo', 1], ['bar', -1]]
      end

      it "works with array of single array" do
        subject[:sort] = [['foo', -1]]
        subject[:sort].should == [['foo', -1]]
      end

      it "works with array of multiple arrays" do
        subject[:sort] = [['foo', -1], ['bar', 1]]
        subject[:sort].should == [['foo', -1], ['bar', 1]]
      end

      it "compacts nil values in array" do
        subject[:sort] = [nil, :foo.desc]
        subject[:sort].should == [['foo', -1]]
      end

      it "converts array with mix of values" do
        subject[:sort] = [:foo.desc, 'bar']
        subject[:sort].should == [['foo', -1], ['bar', 1]]
      end

      it "converts id to _id" do
        subject[:sort] = [:id.asc]
        subject[:sort].should == [['_id', 1]]
      end

      it "converts string with $natural correctly" do
        subject[:sort] = '$natural desc'
        subject[:sort].should == [['$natural', -1]]
      end
    end
  end

  describe "#merge" do
    before do
      @o1 = described_class.new(:skip => 5, :sort => :name)
      @o2 = described_class.new(:limit => 10, :skip => 15)
      @merged = @o1.merge(@o2)
    end

    it "overrides options in first with options in second" do
      @merged.should == described_class.new(:limit => 10, :skip => 15, :sort => :name)
    end

    it "returns new instance and not change either of the merged" do
      @o1[:skip].should == 5
      @o2[:sort].should be_nil
      @merged.should_not equal(@o1)
      @merged.should_not equal(@o2)
    end
  end

  describe "#merge!" do
    before do
      @o1 = described_class.new(:skip => 5, :sort => :name)
      @o2 = described_class.new(:limit => 10, :skip => 15)
      @merged = @o1.merge!(@o2)
    end

    it "overrides options in first with options in second" do
      @merged.should == described_class.new(:limit => 10, :skip => 15, :sort => :name)
    end

    it "just updates the first" do
      @merged.should equal(@o1)
    end
  end
end
Something went wrong with that request. Please try again.