Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 270 lines (228 sloc) 9.931 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
require 'spec_helper'
require 'couch_potato/rspec'

describe CouchPotato::RSpec::MapToMatcher do

  describe "basic map function" do
    before(:each) do
      @view_spec = stub(:map_function => "function(doc) {emit(doc.name, doc.tags.length);}")
    end

    it "should pass if the given function emits the expected javascript" do
      @view_spec.should map({:name => 'horst', :tags => ['person', 'male']}).to(['horst', 2])
    end

    it "should not pass if the given function emits different javascript" do
      @view_spec.should_not map({:name => 'horst', :tags => ['person', 'male']}).to(['horst', 3])
    end
  end

  describe "functions emitting multiple times" do
    before(:each) do
      @view_spec = stub(:map_function => "function(doc) {emit(doc.name, doc.tags.length); emit(doc.tags[0], doc.tags[1])};")
    end

    it "should pass if the given function emits the expected javascript" do
      @view_spec.should map({:name => 'horst', :tags => ['person', 'male']}).to(['horst', 2], ['person', 'male'])
    end

    it "should return false if the given function emits different javascript" do
      @view_spec.should_not map({:name => 'horst', :tags => ['person', 'male']}).to(['horst', 2], ['male', 'person'])
    end
  end

  it "should work with date emit values" do
    spec = stub(:map_function => "function(doc) { emit(null, new Date(1368802800000)); }")
    spec.should map({}).to([nil, "2013-05-17T15:00:00.000Z"])
  end

  it "should work with commonJS modules" do
    spec = stub(
      :map_function => "function(doc) { var test = require('views/lib/test'); emit(null, test.test); }",
      :lib => {:test => "exports.test = 'test';"}
    )
    spec.should map({}).to([nil, "test"])
  end

  describe "failing specs" do
    before(:each) do
      @view_spec = stub(:map_function => "function(doc) {emit(doc.name, null)}")
    end

    it "should have a nice error message for failing should" do
      lambda {
        @view_spec.should map({:name => 'bill'}).to(['linus', nil])
      }.should raise_error('Expected to map to [["linus", nil]] but got [["bill", nil]].')
    end

    it "should have a nice error message for failing should not" do
      lambda {
        @view_spec.should_not map({:name => 'bill'}).to(['bill', nil])
      }.should raise_error('Expected not to map to [["bill", nil]] but did.')
    end
  end
end

describe CouchPotato::RSpec::ReduceToMatcher do
  before(:each) do
    @view_spec = stub(:reduce_function => "function(keys, values, rereduce) {
if(rereduce) {
return(sum(values) * 2);
} else {
return(sum(values));
};
}")
  end

  it "should pass if the given function return the expected javascript" do
    @view_spec.should reduce([], [1, 2, 3]).to(6)
  end

  it "should not pass if the given function returns different javascript" do
    @view_spec.should_not reduce([], [1, 2, 3]).to(7)
  end

  it "should work with date return values" do
    spec = stub(:reduce_function => "function() { return new Date(1368802800000); }")
    spec.should reduce([], []).to("2013-05-17T15:00:00.000Z")
  end

  describe "rereduce" do
    it "should pass if the given function return the expected javascript" do
      @view_spec.should rereduce([], [1, 2, 3]).to(12)
    end

    it "should not pass if the given function returns different javascript" do
      @view_spec.should_not rereduce([], [1, 2, 3]).to(13)
    end
  end

  describe 'failing specs' do

    it "should have a nice error message for failing should" do
      lambda {
        @view_spec.should reduce([], [1, 2, 3]).to(7)
      }.should raise_error('Expected to reduce to 7 but got 6.')
    end

    it "should have a nice error message for failing should not" do
      lambda {
        @view_spec.should_not reduce([], [1, 2, 3]).to(6)
      }.should raise_error('Expected not to reduce to 6 but did.')
    end
  end
end

describe CouchPotato::RSpec::MapReduceToMatcher do
  before(:each) do
    @view_spec = stub(
      :map_function => "function(doc) {
for (var i in doc.numbers)
emit([doc.age, doc.name], doc.numbers[i]);
}",
      :reduce_function => "function (keys, values, rereduce) {
return Math.max.apply(this, values);
}"
    )
    @docs = [
      {:name => "a", :age => 25, :numbers => [1, 2]},
      {:name => "b", :age => 25, :numbers => [3, 4]},
      {:name => "c", :age => 26, :numbers => [5, 6]},
      {:name => "d", :age => 27, :numbers => [7, 8]}]
  end

  it "should handle date return values" do
    spec = stub(:map_function => "function() { emit(null, null); }",
      :reduce_function => "function() { return new Date(1368802800000); }")
    spec.should map_reduce({}).to({"key" => nil, "value" => "2013-05-17T15:00:00.000Z"})
  end

  it "should handle date return values" do
    spec = stub(
      :map_function => "function() { var test = require('views/lib/test'); emit(null, test.test); }",
      :reduce_function => "function(keys, values) { return 'test' }",
      :lib => {:test => "exports.test = 'test'"})
    spec.should map_reduce({}).to({"key" => nil, "value" => "test"})
  end

  it "should handle sum function" do
    spec = stub(
      :map_function => "function(doc) { emit(null, doc.age); }",
      :reduce_function => "function(keys, values) { return sum(values); }")
    spec.should map_reduce(@docs).to({"key" => nil, "value" => 103})
  end

  context "without grouping" do
    it "should not group by key by default" do
      @view_spec.should map_reduce(@docs).to({"key" => nil, "value" => 8})
    end

    it "should group by key with :group => false" do
      @view_spec.should map_reduce(@docs).with_options(:group => false).to({"key" => nil, "value" => 8})
    end
  end

  context "with grouping" do
    [true, "exact"].each do |group_value|
      it "should group by the full key with option :group => #{group_value}" do
        @view_spec.should map_reduce(@docs).with_options(:group => group_value).to(
          {"key" => [25, "a"], "value" => 2},
          {"key" => [25, "b"], "value" => 4},
          {"key" => [26, "c"], "value" => 6},
          {"key" => [27, "d"], "value" => 8})
      end
    end

    it "should group by parts of the keys based on the :group_level option" do
      @view_spec.should map_reduce(@docs).with_options(:group_level => 1).to(
        {"key" => [25], "value" => 4},
        {"key" => [26], "value" => 6},
        {"key" => [27], "value" => 8})
    end
  end

  describe "rereducing" do
    before :each do
      @view_spec = stub(:map_function => "function(doc) {
emit(doc.name, doc.number);
}",
        :reduce_function => "function (keys, values, rereduce) {
if (rereduce) {
var result = {rereduce_values: []};
for (var v in values) {
result.rereduce_values.push(values[v].reduce_values);
}
return result;
}
return {reduce_values: values};
}")
    end

    it "should reduce and rereduce for a single emit" do
      @view_spec.should map_reduce({:name => "a", :number => 1}).to({"key" => nil, "value" => {"rereduce_values" => [[1]]}})
    end

    it "should split and reduce each half of emitted values separately and rereduce the results" do
      docs = [
        {:name => "a", :number => 1},
        {:name => "a", :number => 2},
        {:name => "a", :number => 3},
        {:name => "a", :number => 4}]
      @view_spec.should map_reduce(docs).to({"key" => nil, "value" => {"rereduce_values" => [[1, 2], [3, 4]]}})
    end

    it "should correctly split and rereduce with an odd number of emits" do
      docs = [
        {:name => "a", :number => 1},
        {:name => "a", :number => 2},
        {:name => "a", :number => 3}]
      @view_spec.should map_reduce(docs).to({"key" => nil, "value" => {"rereduce_values" => [[1], [2, 3]]}})
    end
  end

  describe "failing specs" do
    it "should have a nice error message for failing should" do
      lambda {
        @view_spec.should map_reduce(@docs).with_options(:group => false).to({"key" => nil, "value" => 9})
      }.should raise_error('Expected to map/reduce to [{"key"=>nil, "value"=>9}] but got [{"key"=>nil, "value"=>8}].')
    end

    it "should have a nice error message for failing should not" do
      lambda {
        @view_spec.should_not map_reduce(@docs).with_options(:group => false).to({"key" => nil, "value" => 8})
      }.should raise_error('Expected not to map/reduce to [{"key"=>nil, "value"=>8}] but did.')
    end
  end
end

describe CouchPotato::RSpec::ListAsMatcher do
  before(:each) do
    @view_spec = stub(:list_function => "function() {var row = getRow(); send(JSON.stringify([{text: row.text + ' world'}]));}")
  end

  it "should pass if the function return the expected json" do
    @view_spec.should list({'rows' => [{:text => 'hello'}]}).as([{'text' => 'hello world'}])
  end

  it "should not pass if the function does not return the expected json" do
    @view_spec.should_not list({'rows' => [{:text => 'hello'}]}).as([{'text' => 'hello there'}])
  end

  it "should work with date values" do
    spec = stub(:list_function => "function() { send(JSON.stringify([{date: new Date(1368802800000)}])); }")
    spec.should list({"rows" => [{}]}).as([{"date" => "2013-05-17T15:00:00.000Z"}])
  end

  describe "failing specs" do
    it "should have a nice error message for failing should" do
      lambda {
        @view_spec.should list({'rows' => [{:text => 'hello'}]}).as([{'text' => 'hello there'}])
      }.should raise_error('Expected to list as [{"text"=>"hello there"}] but got [{"text"=>"hello world"}].')
    end

    it "should have a nice error message for failing should not" do
      lambda {
        @view_spec.should_not list({'rows' => [{:text => 'hello'}]}).as([{'text' => 'hello world'}])
      }.should raise_error('Expected to not list as [{"text"=>"hello world"}] but did.')
    end
  end
end
Something went wrong with that request. Please try again.