|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe RedisCounters::Dumpers::Engine do |
| 4 | + let(:dumper) do |
| 5 | + RedisCounters::Dumpers::Engine.build do |
| 6 | + name :stats_totals |
| 7 | + fields record_id: :integer, |
| 8 | + column_id: :integer, |
| 9 | + value: :integer, |
| 10 | + date: :date |
| 11 | + |
| 12 | + destination do |
| 13 | + model StatsByDay |
| 14 | + take :record_id, :column_id, :hits, :date |
| 15 | + key_fields :record_id, :column_id, :date |
| 16 | + increment_fields :hits |
| 17 | + map :hits, to: :value |
| 18 | + condition 'target.date = :date' |
| 19 | + end |
| 20 | + |
| 21 | + destination do |
| 22 | + model StatsTotal |
| 23 | + take :record_id, :column_id, :hits |
| 24 | + key_fields :record_id, :column_id |
| 25 | + increment_fields :hits |
| 26 | + map :hits, to: :value |
| 27 | + end |
| 28 | + |
| 29 | + destination do |
| 30 | + model StatsAggTotal |
| 31 | + take :record_id, :hits |
| 32 | + key_fields :record_id |
| 33 | + increment_fields :hits |
| 34 | + map :hits, to: 'sum(value)' |
| 35 | + group_by :record_id |
| 36 | + end |
| 37 | + |
| 38 | + on_before_merge do |dumper, _connection| |
| 39 | + dumper.common_params = {date: dumper.date.strftime('%Y-%m-%d')} |
| 40 | + end |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + let(:prev_date) { Date.new(2015, 1, 19) } |
| 45 | + let(:prev_date_s) { prev_date.strftime('%Y-%m-%d') } |
| 46 | + |
| 47 | + let(:date) { Date.new(2015, 1, 20) } |
| 48 | + let(:date_s) { date.strftime('%Y-%m-%d') } |
| 49 | + |
| 50 | + let(:counter) do |
| 51 | + RedisCounters.create_counter(Redis.current, |
| 52 | + counter_class: RedisCounters::HashCounter, |
| 53 | + counter_name: :record_hits_by_day, |
| 54 | + group_keys: [:record_id, :column_id], |
| 55 | + partition_keys: [:date] |
| 56 | + ) |
| 57 | + end |
| 58 | + |
| 59 | + before do |
| 60 | + allow(dumper).to receive(:redis_session).and_return(MockRedis.new) |
| 61 | + end |
| 62 | + |
| 63 | + describe '#process!' do |
| 64 | + before do |
| 65 | + counter.increment(date: prev_date_s, record_id: 1, column_id: 100) |
| 66 | + counter.increment(date: prev_date_s, record_id: 1, column_id: 200) |
| 67 | + counter.increment(date: prev_date_s, record_id: 1, column_id: 200) |
| 68 | + counter.increment(date: prev_date_s, record_id: 2, column_id: 100) |
| 69 | + |
| 70 | + dumper.process!(counter, prev_date) |
| 71 | + |
| 72 | + counter.increment(date: date_s, record_id: 1, column_id: 100) |
| 73 | + counter.increment(date: date_s, record_id: 1, column_id: 200) |
| 74 | + counter.increment(date: date_s, record_id: 1, column_id: 200) |
| 75 | + counter.increment(date: date_s, record_id: 2, column_id: 100) |
| 76 | + |
| 77 | + dumper.process!(counter, date) |
| 78 | + end |
| 79 | + |
| 80 | + Then { expect(StatsByDay.count).to eq 6 } |
| 81 | + And { expect(StatsByDay.where(record_id: 1, column_id: 100, date: prev_date).first.hits).to eq 1 } |
| 82 | + And { expect(StatsByDay.where(record_id: 1, column_id: 200, date: prev_date).first.hits).to eq 2 } |
| 83 | + And { expect(StatsByDay.where(record_id: 2, column_id: 100, date: prev_date).first.hits).to eq 1 } |
| 84 | + And { expect(StatsByDay.where(record_id: 1, column_id: 100, date: date).first.hits).to eq 1 } |
| 85 | + And { expect(StatsByDay.where(record_id: 1, column_id: 200, date: date).first.hits).to eq 2 } |
| 86 | + And { expect(StatsByDay.where(record_id: 2, column_id: 100, date: date).first.hits).to eq 1 } |
| 87 | + |
| 88 | + And { expect(StatsTotal.count).to eq 3 } |
| 89 | + And { expect(StatsTotal.where(record_id: 1, column_id: 100).first.hits).to eq 2 } |
| 90 | + And { expect(StatsTotal.where(record_id: 1, column_id: 200).first.hits).to eq 4 } |
| 91 | + And { expect(StatsTotal.where(record_id: 2, column_id: 100).first.hits).to eq 2 } |
| 92 | + |
| 93 | + And { expect(StatsAggTotal.count).to eq 2 } |
| 94 | + And { expect(StatsAggTotal.where(record_id: 1).first.hits).to eq 6 } |
| 95 | + And { expect(StatsAggTotal.where(record_id: 2).first.hits).to eq 2 } |
| 96 | + end |
| 97 | +end |
0 commit comments