Skip to content

Commit

Permalink
Add AvailableRate model
Browse files Browse the repository at this point in the history
  • Loading branch information
javierav committed Jan 7, 2024
1 parent 83cae3a commit fa0bb4b
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/models/available_rate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AvailableRate < ApplicationRecord
belongs_to :country

validates :rate, presence: true, uniqueness: { scope: :country_id }
end
1 change: 1 addition & 0 deletions app/models/country.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Country < ApplicationRecord
has_many :available_rates, dependent: :destroy
has_many :holidays, dependent: :destroy

validates :code, presence: true, uniqueness: true
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20240107162546_create_available_rates.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateAvailableRates < ActiveRecord::Migration[7.1]
def change
create_table :available_rates do |t|
t.timestamps null: false
t.references :country, null: false, foreign_key: true
t.string :rate
t.index %i[country_id rate], unique: true
end
end
end
12 changes: 11 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions test/fixtures/available_rates.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
es_by_period:
country_id: 1
rate: "by_period"

es_fixed:
country_id: 1
rate: "fixed"

es_pvpc:
country_id: 1
rate: "pvpc"
30 changes: 30 additions & 0 deletions test/models/available_rate_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require "test_helper"

class AvailableRateTest < ActiveSupport::TestCase
test "create without country" do
available_rate = AvailableRate.new

assert_not available_rate.save
assert available_rate.errors.added?(:country, :blank)
end

test "create without rate" do
available_rate = AvailableRate.new

assert_not available_rate.save
assert available_rate.errors.added?(:rate, :blank)
end

test "create with duplicate rate" do
available_rate = AvailableRate.new(country: countries(:es), rate: "pvpc")

assert_not available_rate.save
assert available_rate.errors.added?(:rate, :taken, value: "pvpc")
end

test "create with valid attributes" do
available_rate = AvailableRate.new(country: countries(:es), rate: "test")

assert available_rate.save
end
end
4 changes: 4 additions & 0 deletions test/models/country_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,8 @@ class CountryTest < ActiveSupport::TestCase
test "associated with holidays" do
assert_equal 16, countries(:es).holidays.count
end

test "associated with available_rates" do
assert_equal 3, countries(:es).available_rates.count
end
end

0 comments on commit fa0bb4b

Please sign in to comment.