require 'spec_helper' require 'flydata/query_based_sync/query_based_sync_context' module Flydata module QueryBasedSync describe Response do include_context 'query based sync context' let(:subject_object) { DummyResponse.new(context, 'table_1', records, query_cond) } describe '#initialize' do before do subject_object end it { expect(subject_object.instance_variable_get(:@table_meta)).to eq(table_meta_1) } end describe '#set_new_source_pos' do subject { subject_object.set_new_source_pos(required_pk_values) } context 'when require_pk_values is false' do let(:required_pk_values) { false } it 'sets current snapshot to the new source pos' do expect(context.source_pos_class).to receive(:new).with(current_snapshot) subject end end context 'when require_pk_values is true' do let(:required_pk_values) { true } it 'sets old snapshot and current snapshot with primary key values' do expect(context.source_pos_class).to receive(:new). with(previous_snapshot, current_snapshot, [{'id'=>3}]) subject end end end describe '#record_count' do subject { subject_object.record_count } context 'when records is nil' do before do subject_object.instance_variable_set(:@records, nil) end it { is_expected.to eq(0) } end context 'when records is not nil' do it { is_expected.to eq(3) } end end describe '#empty?' do subject { subject_object.empty? } context 'when records is nil' do before do subject_object.instance_variable_set(:@records, nil) end it { is_expected.to be(true) } end context 'when records is not nil but empty array' do before do subject_object.instance_variable_set(:@records, []) end it { is_expected.to be(true) } end context 'when records is not nil and not empty' do it { is_expected.to be(false) } end end describe '.create_responses' do let(:raw_result) {[ {'id'=>1, 'value'=>'a'}, {'id'=>2, 'value'=>'b'}, {'id'=>3, 'value'=>'c'}, ]} subject { DummyResponse.create_responses(context, 'table_1', raw_result, query_cond) } context 'when raw_result is empty' do let(:raw_result) { [] } it 'returns a response having empty records' do expect(subject.first.records).to eq([]) expect(subject.size).to eq(1) end end context 'when raw_result have records' do context 'when not execeeding the emit_chunk_limit' do it 'returns a response having records' do expect(subject.first.records).to eq([ {'1'=>1, '2'=>'a'}, {'1'=>2, '2'=>'b'}, {'1'=>3, '2'=>'c'}, ]) expect(subject.size).to eq(1) expect(subject.first.new_source_pos.args).to eq([ current_snapshot ]) end end context 'when execeeding the emit_chunk_limit' do before do context.params[:emit_chunk_limit] = 380 end it 'returns responses with splitted records' do expect(subject.size).to eq(2) expect(subject[0].records).to eq([ {'1'=>1, '2'=>'a'}, {'1'=>2, '2'=>'b'}, ]) expect(subject[0].new_source_pos.args).to eq([ previous_snapshot, current_snapshot, [{'id'=>2}] ]) expect(subject[1].records).to eq([ {'1'=>3, '2'=>'c'}, ]) expect(subject[1].new_source_pos.args).to eq([ current_snapshot ]) end end context 'when the number of records is same as the max_num_rows_per_query' do before do table_meta_1[:max_num_rows_per_query] = 3 end it 'returns a response with a new source pos having pk_values' do expect(subject.first.records).to eq([ {'1'=>1, '2'=>'a'}, {'1'=>2, '2'=>'b'}, {'1'=>3, '2'=>'c'}, ]) expect(subject.size).to eq(1) expect(subject.first.new_source_pos.args).to eq([ previous_snapshot, current_snapshot, [{'id'=>3}], ]) end end end end end end end