blob: 962ccc6d8840f3eaa6e97780997c2e853e1a7527 (
plain)
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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ClickHouse::Iterator, :click_house, feature_category: :database do
let(:query_builder) { ClickHouse::QueryBuilder.new('event_authors') }
let(:connection) { ClickHouse::Connection.new(:main) }
let(:iterator) { described_class.new(query_builder: query_builder, connection: connection) }
before do
connection.execute('INSERT INTO event_authors (author_id) SELECT number + 1 FROM numbers(10)')
end
def collect_ids_with_batch_size(of)
[].tap do |ids|
iterator.each_batch(column: :author_id, of: of) do |scope|
query = scope.select(Arel.sql('DISTINCT author_id')).to_sql
ids.concat(connection.select(query).pluck('author_id'))
end
end
end
it 'iterates correctly' do
expected_values = (1..10).to_a
expect(collect_ids_with_batch_size(3)).to match_array(expected_values)
expect(collect_ids_with_batch_size(5)).to match_array(expected_values)
expect(collect_ids_with_batch_size(10)).to match_array(expected_values)
expect(collect_ids_with_batch_size(15)).to match_array(expected_values)
end
context 'when min value is given' do
let(:iterator) { described_class.new(query_builder: query_builder, connection: connection, min_value: 5) }
it 'iterates from the given min value' do
expected_values = (5..10).to_a
expect(collect_ids_with_batch_size(5)).to match_array(expected_values)
end
end
context 'when there are no records for the given query' do
let(:query_builder) do
ClickHouse::QueryBuilder
.new('event_authors')
.where(author_id: 0)
end
it 'returns no data' do
expect(collect_ids_with_batch_size(3)).to match_array([])
end
end
end
|