ubicloud/spec/lib/billing_rate_spec.rb
Maciek Sarnowicz a40eabb3de Prevent showing Burstable family for locations where it is not allowed
We got NoMethodError when viewing VM create page with burstables and location_latitude_fra feature flags enabled. This issue occurred because there is no billing rate for a burstable size for the latitude_fra location.

Add a BillingRate.unit_price_from_resource_properties that returns nil instead of raising a NoMethodError if there is no matching resource property. Use it to DRY up code in ContentGenerator::{Vm,Postgres}.

Make helpers/Vm#family and Postgres#family not return an a Burstable option if there are no valid billing_rate at the requested location for the family.
Also added a check for presence of billing rate for Nexus.assemble and PostgresResource.assemble, as a back-stop.
2025-03-03 09:02:53 -08:00

55 lines
2 KiB
Ruby

# frozen_string_literal: true
RSpec.describe BillingRate do
it "each rate has a unique ID" do
expect(described_class.rates.map { _1["id"] }.size).to eq(described_class.rates.map { _1["id"] }.uniq.size)
end
describe "#unit_price_from_resource_properties" do
it "returns unit price for VmVCpu" do
expect(described_class.unit_price_from_resource_properties("VmVCpu", "standard", "hetzner-fsn1")).to be_a(Float)
end
it "returns nil for unknown type" do
expect(described_class.unit_price_from_resource_properties("VmVCpu", "unknown", "hetzner-fsn1")).to be_nil
end
end
describe ".line_item_description" do
it "returns for VmVCpu" do
expect(described_class.line_item_description("VmVCpu", "standard", 8)).to eq("standard-8 Virtual Machine")
end
it "returns for IPAddress" do
expect(described_class.line_item_description("IPAddress", "IPv4", 1)).to eq("IPv4 Address")
end
it "raises exception for unknown type" do
expect { described_class.line_item_description("NewType", "NewFamily", 1) }.to raise_error("BUG: Unknown resource type for line item description")
end
it "each resource type has a description" do
described_class.rates.each do |rate|
expect(described_class.line_item_description(rate["resource_type"], rate["resource_family"], 1)).not_to be_nil
end
end
end
describe ".line_item_usage" do
it "returns usage by duration" do
expect(described_class.line_item_usage("VmVCpu", "standard", 5, 1)).to eq("1 minutes")
end
it "returns usage by amount" do
expect(described_class.line_item_usage("GitHubRunnerMinutes", "standard-2", 5, 1)).to eq("5 minutes")
end
it "returns usage by token" do
expect(described_class.line_item_usage("InferenceTokens", "test-model", 10, 1)).to eq("10 tokens")
end
end
it "can unambiguously find active rate" do
expect(described_class.rates.group_by { [_1["resource_type"], _1["resource_family"], _1["location"], _1["active_from"]] }).not_to be_any { |k, v| v.count != 1 }
end
end