From 6b199dfcd2d2cf305b176e6a1153802a1ced442e Mon Sep 17 00:00:00 2001 From: Javier Aranda Date: Sat, 6 Jan 2024 17:28:18 +0100 Subject: [PATCH] Add Country model --- app/models/country.rb | 7 +++ db/migrate/20240106160413_create_countries.rb | 13 +++++ db/schema.rb | 13 ++++- test/fixtures/countries.yml | 7 +++ test/models/country_test.rb | 51 +++++++++++++++++++ 5 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 app/models/country.rb create mode 100644 db/migrate/20240106160413_create_countries.rb create mode 100644 test/fixtures/countries.yml create mode 100644 test/models/country_test.rb diff --git a/app/models/country.rb b/app/models/country.rb new file mode 100644 index 0000000..61dc16c --- /dev/null +++ b/app/models/country.rb @@ -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 diff --git a/db/migrate/20240106160413_create_countries.rb b/db/migrate/20240106160413_create_countries.rb new file mode 100644 index 0000000..1944be0 --- /dev/null +++ b/db/migrate/20240106160413_create_countries.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index a36150c..d70aef6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2023_12_24_131910) do +ActiveRecord::Schema[7.1].define(version: 2024_01_06_160413) do create_table "archive", primary_key: "created_at", id: :datetime, force: :cascade do |t| t.float "solar_power", null: false t.float "solar_energy", null: false @@ -69,6 +69,17 @@ t.datetime "maxtime", null: false end + create_table "countries", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", 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"], name: "index_countries_on_code", unique: true + end + create_table "energy_price", primary_key: "datetime", id: :datetime, force: :cascade do |t| t.float "import" t.float "export" diff --git a/test/fixtures/countries.yml b/test/fixtures/countries.yml new file mode 100644 index 0000000..615bbf2 --- /dev/null +++ b/test/fixtures/countries.yml @@ -0,0 +1,7 @@ +spain: + id: 1 + code: "ES" + name: "EspaƱa" + p1_name: "Punta" + p2_name: "Llano" + p3_name: "Valle" diff --git a/test/models/country_test.rb b/test/models/country_test.rb new file mode 100644 index 0000000..924684e --- /dev/null +++ b/test/models/country_test.rb @@ -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