Sha256: acb70004ca9248757889cfba7ef75632b42896189086ef59c573c1aecc9d7e81

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

require 'spec_helper'
module Alf
  describe Aggregator do

    let(:input){[
      {:a => 1, :sign => -1},
      {:a => 2, :sign => 1 },
      {:a => 3, :sign => -1},
      {:a => 1, :sign => -1},
    ]}

    it "should behave correctly on count" do
      Aggregator.count(:a).aggregate(input).should == 4
    end

    it "should behave correctly on sum" do
      Aggregator.sum(:a).aggregate(input).should == 7
    end

    it "should behave correctly on avg" do
      Aggregator.avg(:a).aggregate(input).should == 7.0 / 4.0
    end

    it "should behave correctly on min" do
      Aggregator.min(:a).aggregate(input).should == 1
    end

    it "should behave correctly on max" do
      Aggregator.max(:a).aggregate(input).should == 3
    end

    it "should behave correctly on concat" do
      Aggregator.concat(:a).aggregate(input).should == "1231"
      Aggregator.concat(:a, :between => " ").aggregate(input).should == "1 2 3 1"
      Aggregator.concat(:a, :before => "[", :after => "]").aggregate(input).should == "[1231]"
      Aggregator.concat(:before => "[", :after => "]"){ a }.aggregate(input).should == "[1231]"
    end

    it "should behave correctly on collect" do
      Aggregator.collect(:a).aggregate(input).should == [1, 2, 3, 1]
      Aggregator.collect{ {:a => a, :sign => sign} }.aggregate(input).should == input
    end

    it "should behave correctly on group" do
      Aggregator.group(:a).aggregate(input).should == [
        {:a => 1},
        {:a => 2},
        {:a => 3},
      ]
      Aggregator.group(:a, :sign).aggregate(input).should == [
        {:a => 1, :sign => -1},
        {:a => 2, :sign => 1 },
        {:a => 3, :sign => -1},
      ]
    end

    it "should allow specific tuple computations" do
      Aggregator.sum{ 1.0 * a * sign }.aggregate(input).should == -3.0
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
alf-0.9.0 spec/aggregator_spec.rb