require 'spec_helper' RSpec.describe InstDataShipper::Destinations::HostedData do let(:dumper) { TestDumper.new } let(:dest) { described_class.new("", "hosted-data://key@hosted-data.local", dumper) } describe "#preinitialize_dump" do let(:context) { { incremental_since: 1.day.ago, force_full_tables: [], } } let(:response_body) { { created_at: 1.hour.ago.iso8601, tags: ["ids-schema=abc123"], } } before :each do allow(dumper).to receive(:schema_digest).and_return("abc123") stub_request(:get, "https://hosted-data.local/api/v1/custom_dumps/last").with(query: hash_including()).to_return do |req| { body: response_body.to_json, headers: {'Content-Type' => 'application/json'}, } end end it "leaves incremental_since alone if last dump is newer" do x = context[:incremental_since] dest.preinitialize_dump(context) expect(context[:incremental_since]).to eq x end it "bumps back incremental_since if last dump is older" do response_body[:created_at] = 2.days.ago.iso8601 dest.preinitialize_dump(context) expect(context[:incremental_since]).to eq(response_body[:created_at]) end it "nils incremental_since if last dump is of a different schema" do response_body[:tags] = ["ids-schema=def456"] dest.preinitialize_dump(context) expect(context[:incremental_since]).to be nil end context "per-table forcing" do before :each do response_body[:schema] = { "test_table" => { ids_meta: { table_warehouse_name: "test_table", table_schema_hash: "def456", }, } } end it "does not trigger force-full if the schema hash matches" do allow(dumper).to receive(:table_schema_hash).and_return("def456") dest.preinitialize_dump(context) expect(context[:force_full_tables] || []).to_not include "test_table" end it "triggers force-full if the schema hash does not match" do allow(dumper).to receive(:table_schema_hash).and_return("def123") dest.preinitialize_dump(context) expect(context[:force_full_tables]).to include "test_table" end it "triggers force-full if versions do not match" do response_body[:schema]["table_with_version"] = { ids_meta: { table_warehouse_name: "table_with_version", table_schema_version: "1.1" } } dest.preinitialize_dump(context) expect(context[:force_full_tables]).to include "table_with_version" end it "does not trigger force-full if the versions match" do response_body[:schema]["table_with_version"] = { ids_meta: { table_warehouse_name: "table_with_version", table_schema_version: "1.0" } } dest.preinitialize_dump(context) expect(context[:force_full_tables] || []).to_not include "table_with_version" end it "does not consider the schema hash if a version is present" do response_body[:schema]["table_with_version"] = { ids_meta: { table_warehouse_name: "table_with_version", table_schema_version: "1.0", table_schema_hash: "not_matching" } } dest.preinitialize_dump(context) expect(context[:force_full_tables]).to_not include "table_with_version" end end end end