Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add greater than zero validation for sync_interval #61

Merged
merged 2 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions server/app/contracts/sync_contracts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class Create < Dry::Validation::Contract
rule(sync: :sync_interval_unit) do
key.failure("invalid connector type") unless Sync.sync_interval_units.keys.include?(value.downcase)
end

rule("sync.sync_interval") do
key.failure("must be greater than 0") if value <= 0
end
end

class Update < Dry::Validation::Contract
Expand Down Expand Up @@ -73,6 +77,10 @@ class Update < Dry::Validation::Contract
rule(sync: :sync_interval_unit) do
key.failure("invalid connector type") if key? && !Sync.sync_interval_units.keys.include?(value.downcase)
end

rule("sync.sync_interval") do
key.failure("must be greater than 0") if value <= 0
end
end

class Destroy < Dry::Validation::Contract
Expand Down
2 changes: 1 addition & 1 deletion server/app/models/sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Sync < ApplicationRecord
validates :model_id, presence: true
validates :configuration, presence: true
validates :schedule_type, presence: true
validates :sync_interval, presence: true
validates :sync_interval, presence: true, numericality: { greater_than: 0 }
validates :sync_interval_unit, presence: true
validates :stream_name, presence: true
validates :status, presence: true
Expand Down
44 changes: 44 additions & 0 deletions server/spec/contracts/sync_contracts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,50 @@
expect(result.errors[:sync][:sync_mode]).to include("invalid sync mode")
end
end

context "with non-positive sync_interval" do
let(:invalid_inputs) { { sync: valid_inputs[:sync].merge(sync_interval: 0) } }

it "fails validation" do
result = contract.call(invalid_inputs)
expect(result.errors[:sync][:sync_interval]).to include("must be greater than 0")
end
end
end

describe SyncContracts::Update do
subject(:contract) { described_class.new }

let(:valid_inputs) do
{
id: 1,
sync: {
source_id: 1,
model_id: 2,
destination_id: 3,
schedule_type: "automated",
sync_interval: 15,
sync_interval_unit: "hours",
sync_mode: "incremental",
stream_name: "updated_stream"
}
}
end

context "with valid inputs" do
it "passes validation" do
expect(contract.call(valid_inputs)).to be_success
end
end

context "with non-positive sync_interval" do
let(:invalid_inputs) { { id: 1, sync: valid_inputs[:sync].merge(sync_interval: -1) } }

it "fails validation" do
result = contract.call(invalid_inputs)
expect(result.errors[:sync][:sync_interval]).to include("must be greater than 0")
end
end
end

describe SyncContracts::Destroy do
Expand Down
19 changes: 19 additions & 0 deletions server/spec/models/sync_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,23 @@
hash_including(options: hash_including(workflow_id: a_string_starting_with("terminate-"))))
end
end

describe "validations" do
let(:source) do
create(:connector, connector_type: "source", connector_name: "Snowflake")
end
let(:destination) { create(:connector, connector_type: "destination") }
let!(:catalog) { create(:catalog, connector: destination) }
let(:sync) { build(:sync, sync_interval: 3, sync_interval_unit: "hours", source:, destination:) }

it "validates that sync_interval is greater than 0" do
sync.sync_interval = 0
expect(sync).not_to be_valid
expect(sync.errors[:sync_interval]).to include("must be greater than 0")

sync.sync_interval = -1
expect(sync).not_to be_valid
expect(sync.errors[:sync_interval]).to include("must be greater than 0")
end
end
end
Loading