Skip to content

Commit

Permalink
Merge pull request #65 from javierav/feature/country-model
Browse files Browse the repository at this point in the history
Add Country model
  • Loading branch information
javierav authored Jan 6, 2024
2 parents 34f66e9 + 6b199df commit 27ee5b6
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
7 changes: 7 additions & 0 deletions app/models/country.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Country < ApplicationRecord
validates :code, presence: true, uniqueness: true
validates :name, presence: true
validates :p1_name, presence: true
validates :p2_name, presence: true
validates :p3_name, presence: true
end
13 changes: 13 additions & 0 deletions db/migrate/20240106160413_create_countries.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateCountries < ActiveRecord::Migration[7.1]
def change
create_table :countries do |t|
t.timestamps null: false
t.string :code, null: false
t.string :name, null: false
t.string :p1_name, null: false
t.string :p2_name, null: false
t.string :p3_name, null: false
t.index :code, unique: true
end
end
end
13 changes: 12 additions & 1 deletion db/schema.rb

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

7 changes: 7 additions & 0 deletions test/fixtures/countries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
spain:
id: 1
code: "ES"
name: "España"
p1_name: "Punta"
p2_name: "Llano"
p3_name: "Valle"
51 changes: 51 additions & 0 deletions test/models/country_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require "test_helper"

class CountryTest < ActiveSupport::TestCase
test "create without code" do
country = Country.new

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

test "create without name" do
country = Country.new

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

test "create without p1_name" do
country = Country.new

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

test "create without p2_name" do
country = Country.new

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

test "create without p3_name" do
country = Country.new

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

test "create with duplicate code" do
country = Country.new(code: "ES")

assert_not country.save
assert country.errors.added?(:code, :taken, value: "ES")
end

test "create with valid attributes" do
country = Country.new(code: "UK", name: "United Kingdom", p1_name: "On-Peak", p2_name: "Standard", p3_name: "Off-Peak")

assert country.save
end
end

0 comments on commit 27ee5b6

Please sign in to comment.