|
93 | 93 | And { expect(StatsAggTotal.count).to eq 2 }
|
94 | 94 | And { expect(StatsAggTotal.where(record_id: 1).first.hits).to eq 6 }
|
95 | 95 | And { expect(StatsAggTotal.where(record_id: 2).first.hits).to eq 2 }
|
| 96 | + |
| 97 | + context 'with source conditions' do |
| 98 | + let(:dumper) do |
| 99 | + RedisCounters::Dumpers::Engine.build do |
| 100 | + name :stats_totals |
| 101 | + fields record_id: :integer, |
| 102 | + column_id: :integer, |
| 103 | + value: :integer, |
| 104 | + date: :date |
| 105 | + |
| 106 | + destination do |
| 107 | + model StatsByDay |
| 108 | + take :record_id, :column_id, :hits, :date |
| 109 | + key_fields :record_id, :column_id, :date |
| 110 | + increment_fields :hits |
| 111 | + map :hits, to: :value |
| 112 | + condition 'target.date = :date' |
| 113 | + source_condition 'column_id = 100' |
| 114 | + end |
| 115 | + |
| 116 | + destination do |
| 117 | + model StatsTotal |
| 118 | + take :record_id, :column_id, :hits |
| 119 | + key_fields :record_id, :column_id |
| 120 | + increment_fields :hits |
| 121 | + map :hits, to: :value |
| 122 | + source_condition 'column_id = 100' |
| 123 | + end |
| 124 | + |
| 125 | + destination do |
| 126 | + model StatsAggTotal |
| 127 | + take :record_id, :hits |
| 128 | + key_fields :record_id |
| 129 | + increment_fields :hits |
| 130 | + map :hits, to: 'sum(value)' |
| 131 | + group_by :record_id |
| 132 | + source_condition 'column_id = 100' |
| 133 | + end |
| 134 | + |
| 135 | + on_before_merge do |dumper, _connection| |
| 136 | + dumper.common_params = {date: dumper.date.strftime('%Y-%m-%d')} |
| 137 | + end |
| 138 | + end |
| 139 | + end |
| 140 | + |
| 141 | + Then { expect(StatsByDay.count).to eq 4 } |
| 142 | + And { expect(StatsByDay.where(record_id: 1, column_id: 100, date: prev_date).first.hits).to eq 1 } |
| 143 | + And { expect(StatsByDay.where(record_id: 2, column_id: 100, date: prev_date).first.hits).to eq 1 } |
| 144 | + And { expect(StatsByDay.where(record_id: 1, column_id: 100, date: date).first.hits).to eq 1 } |
| 145 | + And { expect(StatsByDay.where(record_id: 2, column_id: 100, date: date).first.hits).to eq 1 } |
| 146 | + |
| 147 | + And { expect(StatsTotal.count).to eq 2 } |
| 148 | + And { expect(StatsTotal.where(record_id: 1, column_id: 100).first.hits).to eq 2 } |
| 149 | + And { expect(StatsTotal.where(record_id: 2, column_id: 100).first.hits).to eq 2 } |
| 150 | + |
| 151 | + And { expect(StatsAggTotal.count).to eq 2 } |
| 152 | + And { expect(StatsAggTotal.where(record_id: 1).first.hits).to eq 2 } |
| 153 | + And { expect(StatsAggTotal.where(record_id: 2).first.hits).to eq 2 } |
| 154 | + end |
96 | 155 | end
|
97 | 156 | end
|
0 commit comments