Files
ubicloud/spec/serializers/postgres_spec.rb
Furkan Sahin 12dbeb57a1 Update location references with foreign key in the controlplane
We are basically updating the location references everywhere with a
location id and adding the location relationship to the models to be
able to fetch location names when needed.
This also makes the LocationNameConverter model obsolete, so we are
removing it.

Use model id as value for Sequel::Model in resource creation form

Use id of the location as preselected value in Postgres update form
2025-03-23 15:48:19 +01:00

55 lines
3.3 KiB
Ruby

# frozen_string_literal: true
require_relative "../spec_helper"
RSpec.describe Serializers::Postgres do
let(:pg) { PostgresResource.new(name: "pg-name", location_id: Location::HETZNER_FSN1_ID).tap { _1.id = "69c0f4cd-99c1-8ed0-acfe-7b013ce2fa0b" } }
it "can serialize when no earliest/latest restore times" do
expect(pg).to receive(:strand).and_return(instance_double(Strand, label: "start", children: [])).at_least(:once)
expect(pg).to receive(:timeline).and_return(instance_double(PostgresTimeline, earliest_restore_time: nil, latest_restore_time: nil)).exactly(3)
expect(pg).to receive(:representative_server).and_return(instance_double(PostgresServer, primary?: true, vm: nil, strand: nil)).at_least(:once)
data = described_class.serialize(pg, {detailed: true})
expect(data[:earliest_restore_time]).to be_nil
expect(data[:latest_restore_time]).to be_nil
end
it "can serialize when earliest_restore_time calculation raises an exception" do
expect(pg).to receive(:strand).and_return(instance_double(Strand, label: "start", children: [])).at_least(:once)
expect(pg).to receive(:timeline).and_return(instance_double(PostgresTimeline, latest_restore_time: nil)).exactly(4)
expect(pg).to receive(:representative_server).and_return(instance_double(PostgresServer, primary?: true, vm: nil, strand: nil)).at_least(:once)
expect(pg.timeline).to receive(:earliest_restore_time).and_raise("error")
data = described_class.serialize(pg, {detailed: true})
expect(data[:earliest_restore_time]).to be_nil
expect(data[:latest_restore_time]).to be_nil
end
it "can serialize when have earliest/latest restore times" do
time = Time.now
expect(pg).to receive(:strand).and_return(instance_double(Strand, label: "start", children: [])).at_least(:once)
expect(pg).to receive(:timeline).and_return(instance_double(PostgresTimeline, earliest_restore_time: time, latest_restore_time: time)).exactly(3)
expect(pg).to receive(:representative_server).and_return(instance_double(PostgresServer, primary?: true, vm: nil, strand: nil)).at_least(:once)
data = described_class.serialize(pg, {detailed: true})
expect(data[:earliest_restore_time]).to eq(time.iso8601)
expect(data[:latest_restore_time]).to eq(time.iso8601)
end
it "can serialize when not primary" do
expect(pg).to receive(:strand).and_return(instance_double(Strand, label: "start", children: [])).at_least(:once)
expect(pg).to receive(:representative_server).and_return(instance_double(PostgresServer, primary?: false, vm: nil, strand: nil)).at_least(:once)
data = described_class.serialize(pg, {detailed: true})
expect(data[:earliest_restore_time]).to be_nil
expect(data[:latest_restore_time]).to be_nil
end
it "can serialize when there is no server" do
expect(pg).to receive(:strand).and_return(instance_double(Strand, label: "start", children: [])).at_least(:once)
expect(pg).to receive(:timeline).and_return(instance_double(PostgresTimeline, earliest_restore_time: nil, latest_restore_time: nil))
expect(pg).to receive(:representative_server).and_return(nil).at_least(:once)
data = described_class.serialize(pg, {detailed: true})
expect(data[:primary?]).to be_nil
expect(data[:earliest_restore_time]).to be_nil
expect(data[:latest_restore_time]).to be_nil
end
end