From 21db68fa8e471a0938a618b934432b6307e9981a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20S=C4=99dkowski?= Date: Wed, 20 Nov 2019 23:45:58 +0100 Subject: [PATCH 1/9] Create migration for patient. --- db/migrate/20191120224317_create_patients.rb | 11 +++++++++++ db/schema.rb | 17 +++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20191120224317_create_patients.rb diff --git a/db/migrate/20191120224317_create_patients.rb b/db/migrate/20191120224317_create_patients.rb new file mode 100644 index 0000000..332c8bb --- /dev/null +++ b/db/migrate/20191120224317_create_patients.rb @@ -0,0 +1,11 @@ +class CreatePatients < ActiveRecord::Migration[5.2] + def change + create_table :patients do |t| + t.string :first_name, null: false + t.string :last_name, null: false + t.integer :age, null: false + t.integer :gender, null: false + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 8d5a902..73cddbb 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. @@ -12,7 +10,18 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 0) do +ActiveRecord::Schema.define(version: 2019_11_20_224317) do + # These are extensions that must be enabled in order to support this database - enable_extension 'plpgsql' + enable_extension "plpgsql" + + create_table "patients", force: :cascade do |t| + t.string "first_name", null: false + t.string "last_name", null: false + t.integer "age", null: false + t.integer "gender", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + end From a67600ac396fe1d9e605cf9339477c97e7ba8957 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20S=C4=99dkowski?= Date: Wed, 20 Nov 2019 23:47:15 +0100 Subject: [PATCH 2/9] Create model file for patient with validations. --- app/models/patient.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 app/models/patient.rb diff --git a/app/models/patient.rb b/app/models/patient.rb new file mode 100644 index 0000000..0abc661 --- /dev/null +++ b/app/models/patient.rb @@ -0,0 +1,10 @@ +class Patient < ApplicationRecord + + validates :first_name, :last_name, :age, presence: true + + enum gender: { + male: 1, + female: 2, + other: 3 + } +end From 93b2864d7bcd22312b3c5c9dd8349aff0f84869c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20S=C4=99dkowski?= Date: Wed, 20 Nov 2019 23:48:55 +0100 Subject: [PATCH 3/9] Configure factory file. --- spec/factories/patients.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 spec/factories/patients.rb diff --git a/spec/factories/patients.rb b/spec/factories/patients.rb new file mode 100644 index 0000000..2e53078 --- /dev/null +++ b/spec/factories/patients.rb @@ -0,0 +1,8 @@ +FactoryBot.define do + factory :patient do + first_name { Faker::Name.first_name } + last_name { Faker::Name.last_name } + age { rand(100) } + gender { 1 } + end +end From 1070cee5aeafdcea0ac584f728d2ec6288f5c64b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20S=C4=99dkowski?= Date: Wed, 20 Nov 2019 23:51:06 +0100 Subject: [PATCH 4/9] Create first specs. --- app/models/patient.rb | 6 +++--- spec/models/patient_spec.rb | 13 +++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 spec/models/patient_spec.rb diff --git a/app/models/patient.rb b/app/models/patient.rb index 0abc661..82576dc 100644 --- a/app/models/patient.rb +++ b/app/models/patient.rb @@ -3,8 +3,8 @@ class Patient < ApplicationRecord validates :first_name, :last_name, :age, presence: true enum gender: { - male: 1, - female: 2, - other: 3 + male: 0, + female: 1, + other: 2 } end diff --git a/spec/models/patient_spec.rb b/spec/models/patient_spec.rb new file mode 100644 index 0000000..2c04207 --- /dev/null +++ b/spec/models/patient_spec.rb @@ -0,0 +1,13 @@ +require 'rails_helper' + +describe Patient do + context 'validations' do + it { should validate_presence_of :first_name } + it { should validate_presence_of :last_name } + it { should validate_presence_of :age } + end + + context 'enumerators' do + it { should define_enum_for(:gender).with_values(%w(male female other)) } + end +end From fbe80e093c0db4c04eb0157810de074ffee8e8bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20S=C4=99dkowski?= Date: Thu, 21 Nov 2019 00:01:08 +0100 Subject: [PATCH 5/9] Create Doctors migration. --- db/migrate/20191120225245_create_doctors.rb | 12 ++++++++++++ db/schema.rb | 12 +++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20191120225245_create_doctors.rb diff --git a/db/migrate/20191120225245_create_doctors.rb b/db/migrate/20191120225245_create_doctors.rb new file mode 100644 index 0000000..61c244c --- /dev/null +++ b/db/migrate/20191120225245_create_doctors.rb @@ -0,0 +1,12 @@ +class CreateDoctors < ActiveRecord::Migration[5.2] + def change + create_table :doctors do |t| + t.string :first_name, null: false + t.string :last_name, null: false + t.timestamp :practicing_from, null: false + t.integer :qualification_number, null: false, uniqe: true + t.integer :employment, null: false, default: 0 + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 73cddbb..2ab12e8 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,11 +10,21 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_11_20_224317) do +ActiveRecord::Schema.define(version: 2019_11_20_225245) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "doctors", force: :cascade do |t| + t.string "first_name", null: false + t.string "last_name", null: false + t.datetime "practicing_from", null: false + t.integer "qualification_number", null: false + t.integer "employment", default: 0, null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "patients", force: :cascade do |t| t.string "first_name", null: false t.string "last_name", null: false From 40b249458af02a1f2f1f61cbb1d51014c76991f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20S=C4=99dkowski?= Date: Thu, 21 Nov 2019 00:03:24 +0100 Subject: [PATCH 6/9] Create model validations. --- app/models/doctor.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 app/models/doctor.rb diff --git a/app/models/doctor.rb b/app/models/doctor.rb new file mode 100644 index 0000000..42af75e --- /dev/null +++ b/app/models/doctor.rb @@ -0,0 +1,12 @@ +class Doctor < ApplicationRecord + + validates :first_name, :last_name, :practicing_from, :qualification_number, :employment, presence: true + validates_uniqueness_of :qualification_number + + enum employment: { + full_time: 0, + part_time: 1, + intern: 2, + contract: 3 + } +end From 92944cc555cbfecbc00311671d979611ced4570a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20S=C4=99dkowski?= Date: Thu, 21 Nov 2019 00:06:20 +0100 Subject: [PATCH 7/9] Create doctors factory files. --- spec/factories/doctors.rb | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 spec/factories/doctors.rb diff --git a/spec/factories/doctors.rb b/spec/factories/doctors.rb new file mode 100644 index 0000000..64193d4 --- /dev/null +++ b/spec/factories/doctors.rb @@ -0,0 +1,9 @@ +FactoryBot.define do + factory :doctor do + first_name { Faker::Name.first_name } + last_name { Faker::Name.last_name } + practicing_from { Time.current - rand(20).years } + sequence(:qualification_number) { |n| n } + employment { 0 } + end +end From a7c1ddc2ad27e0a85a72c605c7713b885de531a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20S=C4=99dkowski?= Date: Thu, 21 Nov 2019 00:10:16 +0100 Subject: [PATCH 8/9] Add test cases for Doctor model. --- spec/models/doctor_spec.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 spec/models/doctor_spec.rb diff --git a/spec/models/doctor_spec.rb b/spec/models/doctor_spec.rb new file mode 100644 index 0000000..de535ae --- /dev/null +++ b/spec/models/doctor_spec.rb @@ -0,0 +1,21 @@ +require 'rails_helper' + +describe Doctor do + context 'validations' do + it { should validate_presence_of :first_name } + it { should validate_presence_of :last_name } + it { should validate_presence_of :practicing_from } + it { should validate_presence_of :qualification_number } + it { should validate_presence_of :employment } + + context 'uniqueness' do + let!(:doctor) { create(:doctor) } + + it { should validate_uniqueness_of(:qualification_number) } + end + end + + context 'enumerators' do + it { should define_enum_for(:employment).with_values(%w(full_time part_time intern contract)) } + end +end From a52335d181a97c5a2333617ee36984f44b4e1241 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20S=C4=99dkowski?= Date: Sat, 23 Nov 2019 09:00:56 +0100 Subject: [PATCH 9/9] Correct doctors migrations. --- db/migrate/20191120225245_create_doctors.rb | 4 +++- db/schema.rb | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/db/migrate/20191120225245_create_doctors.rb b/db/migrate/20191120225245_create_doctors.rb index 61c244c..23088b5 100644 --- a/db/migrate/20191120225245_create_doctors.rb +++ b/db/migrate/20191120225245_create_doctors.rb @@ -4,9 +4,11 @@ def change t.string :first_name, null: false t.string :last_name, null: false t.timestamp :practicing_from, null: false - t.integer :qualification_number, null: false, uniqe: true + t.integer :qualification_number, null: false t.integer :employment, null: false, default: 0 t.timestamps end + + add_index :doctors, :qualification_number, unique: true end end diff --git a/db/schema.rb b/db/schema.rb index 2ab12e8..4967c6e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -23,6 +23,7 @@ t.integer "employment", default: 0, null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.index ["qualification_number"], name: "index_doctors_on_qualification_number", unique: true end create_table "patients", force: :cascade do |t|