Sha256: 417028e912d0057009c37eb5086e18bc7ee7e811722e9f66e1eceb729d232320

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

require 'spec_helper'

module Prosperity
  describe Extractors::Total do
    it_behaves_like "an extractor"
    let(:expected_data_size) { 14 }

    let(:start_time) { 1.year.ago }
    let(:end_time) { start_time + 12.months }
    let(:period) { Periods::MONTH }

    let(:data) { subject.to_a }
    let(:metric) { UsersMetric.new }
    let(:option) { 'default' }

    subject { Extractors::Total.new(metric, option, start_time, end_time, period) }

    before do 
      User.delete_all
      [2.years.ago, 1.month.ago, 1.month.from_now].each do |time|
        User.create created_at: time
      end
    end

    context "simple scope" do
      describe "#to_a" do
        it "returns the one entry per period" do
          expect(data.size).to eq(expected_data_size)
        end

        it "returns the counts at it increases" do
          expect(data[0]).to eq(1)
          expect(data[-1]).to eq(2)
        end
      end
    end

    context "simple sql fragment" do
      let(:metric) { UsersSqlMetric.new }

      describe "#to_a" do
        it "returns the one entry per period" do
          expect(data.size).to eq(expected_data_size)
        end

        it "returns the counts at it increases" do
          expect(data[0]).to eq(1)
          expect(data[-1]).to eq(2)
        end
      end
    end

    context "with an option" do
      let(:option) { "no_results" }

      describe "#to_a" do
        it "only returns the results for that option block" do
          expect(data.all?(&:zero?)).to be(true)
        end
      end
    end

    context "ruby block" do
      let(:metric) do
        Class.new(Metric) do
          value_at do |time, period, *|
            10
          end
        end.new
      end

      describe "#to_a" do
        it "delegates to the ruby block" do
          expect(data).to eq([10] * expected_data_size)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
prosperity-0.0.11 spec/lib/prosperity/extractors/total_spec.rb