From 7c64a5c09f5eca9012eaba165ceff5d28e6c26ce Mon Sep 17 00:00:00 2001 From: cherelemma Date: Thu, 27 Jul 2023 23:11:03 +0300 Subject: [PATCH 01/16] install pg driver bundle --- Gemfile | 1 + Gemfile.lock | 2 ++ 2 files changed, 3 insertions(+) diff --git a/Gemfile b/Gemfile index 53e18849..1731b004 100644 --- a/Gemfile +++ b/Gemfile @@ -13,6 +13,7 @@ gem 'sprockets-rails' gem 'sqlite3', '~> 1.4' # Use the Puma web server [https://github.com/puma/puma] +gem 'pg' gem 'puma', '~> 5.0' # Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails] diff --git a/Gemfile.lock b/Gemfile.lock index fbe39918..0cf5434d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -127,6 +127,7 @@ GEM nio4r (2.5.9) nokogiri (1.15.3-x64-mingw-ucrt) racc (~> 1.4) + pg (1.5.3-x64-mingw-ucrt) public_suffix (5.0.3) puma (5.6.6) nio4r (~> 2.0) @@ -218,6 +219,7 @@ DEPENDENCIES debug importmap-rails jbuilder + pg puma (~> 5.0) rails (~> 7.0.6) selenium-webdriver From eaace636186f4cf7d715c45419d179f74bbc4ac9 Mon Sep 17 00:00:00 2001 From: cherelemma Date: Thu, 27 Jul 2023 23:11:37 +0300 Subject: [PATCH 02/16] update database.yml configuration with db credentials --- config/database.yml | 81 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 10 deletions(-) diff --git a/config/database.yml b/config/database.yml index fcba57f1..4897c346 100644 --- a/config/database.yml +++ b/config/database.yml @@ -1,25 +1,86 @@ -# SQLite. Versions 3.8.0 and up are supported. -# gem install sqlite3 +# PostgreSQL. Versions 9.3 and up are supported. # -# Ensure the SQLite 3 gem is defined in your Gemfile -# gem "sqlite3" +# Install the pg driver: +# gem install pg +# On macOS with Homebrew: +# gem install pg -- --with-pg-config=/usr/local/bin/pg_config +# On macOS with MacPorts: +# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config +# On Windows: +# gem install pg +# Choose the win32 build. +# Install PostgreSQL and put its /bin directory on your path. +# +# Configure Using Gemfile +# gem "pg" # default: &default - adapter: sqlite3 + adapter: postgresql + encoding: unicode + # For details on connection pooling, see Rails configuration guide + # https://guides.rubyonrails.org/configuring.html#database-pooling pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> - timeout: 5000 development: <<: *default - database: db/development.sqlite3 + database: budget_app_development + + # The specified database role being used to connect to postgres. + # To create additional roles in postgres see `$ createuser --help`. + # When left blank, postgres will use the default role. This is + # the same name as the operating system user running Rails. + username: postgres + + # The password associated with the postgres role (username). + password: postgres#13579 + + # Connect on a TCP socket. Omitted by default since the client uses a + # domain socket that doesn't need configuration. Windows does not have + # domain sockets, so uncomment these lines. + #host: localhost + + # The TCP port the server listens on. Defaults to 5432. + # If your server runs on a different port number, change accordingly. + #port: 5432 + + # Schema search path. The server defaults to $user,public + #schema_search_path: myapp,sharedapp,public + + # Minimum log levels, in increasing order: + # debug5, debug4, debug3, debug2, debug1, + # log, notice, warning, error, fatal, and panic + # Defaults to warning. + #min_messages: notice # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default - database: db/test.sqlite3 + database: budget_app_test + username: postgres + password: postgres#13579 +# As with config/credentials.yml, you never want to store sensitive information, +# like your database password, in your source code. If your source code is +# ever seen by anyone, they now have access to your database. +# +# Instead, provide the password or a full connection URL as an environment +# variable when you boot the app. For example: +# +# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase" +# +# If the connection URL is provided in the special DATABASE_URL environment +# variable, Rails will automatically merge its configuration values on top of +# the values provided in this file. Alternatively, you can specify a connection +# URL environment variable explicitly: +# +# production: +# url: <%= ENV["MY_APP_DATABASE_URL"] %> +# +# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database +# for a full overview on how database connection configuration can be specified. +# production: - <<: *default - database: db/production.sqlite3 + <<: *default + url: <%= ENV['DATABASE_URL'] %> \ No newline at end of file From 368b07953094b1769bc408e93f8914b9fbeaf992 Mon Sep 17 00:00:00 2001 From: cherelemma Date: Thu, 27 Jul 2023 23:12:11 +0300 Subject: [PATCH 03/16] generate Users, Categories and Entities migration --- db/migrate/20230727190447_create_users.rb | 9 +++++++++ db/migrate/20230727191140_create_categories.rb | 10 ++++++++++ db/migrate/20230727191201_create_entities.rb | 10 ++++++++++ 3 files changed, 29 insertions(+) create mode 100644 db/migrate/20230727190447_create_users.rb create mode 100644 db/migrate/20230727191140_create_categories.rb create mode 100644 db/migrate/20230727191201_create_entities.rb diff --git a/db/migrate/20230727190447_create_users.rb b/db/migrate/20230727190447_create_users.rb new file mode 100644 index 00000000..dd02b096 --- /dev/null +++ b/db/migrate/20230727190447_create_users.rb @@ -0,0 +1,9 @@ +class CreateUsers < ActiveRecord::Migration[7.0] + def change + create_table :users do |t| + t.string :name + + t.timestamps + end + end +end diff --git a/db/migrate/20230727191140_create_categories.rb b/db/migrate/20230727191140_create_categories.rb new file mode 100644 index 00000000..d12cb182 --- /dev/null +++ b/db/migrate/20230727191140_create_categories.rb @@ -0,0 +1,10 @@ +class CreateCategories < ActiveRecord::Migration[7.0] + def change + create_table :categories do |t| + t.string :name + t.string :icon + + t.timestamps + end + end +end diff --git a/db/migrate/20230727191201_create_entities.rb b/db/migrate/20230727191201_create_entities.rb new file mode 100644 index 00000000..7f589f36 --- /dev/null +++ b/db/migrate/20230727191201_create_entities.rb @@ -0,0 +1,10 @@ +class CreateEntities < ActiveRecord::Migration[7.0] + def change + create_table :entities do |t| + t.string :name + t.decimal :amount + + t.timestamps + end + end +end From b7966edc6838d144072a83bb883227ab35e63b4b Mon Sep 17 00:00:00 2001 From: cherelemma Date: Thu, 27 Jul 2023 23:12:38 +0300 Subject: [PATCH 04/16] create Categories-Entities migration --- .../20230727200438_create_categories_entities.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 db/migrate/20230727200438_create_categories_entities.rb diff --git a/db/migrate/20230727200438_create_categories_entities.rb b/db/migrate/20230727200438_create_categories_entities.rb new file mode 100644 index 00000000..a245ae51 --- /dev/null +++ b/db/migrate/20230727200438_create_categories_entities.rb @@ -0,0 +1,10 @@ +class CreateCategoriesEntities < ActiveRecord::Migration[7.0] + def change + create_table :categories_entities do |t| + t.references :category, null: false, foreign_key: true + t.references :entity, null: false, foreign_key: true + + t.timestamps + end + end +end From 9edc15a23b50b7ba7961d8a22758922eea1204de Mon Sep 17 00:00:00 2001 From: cherelemma Date: Thu, 27 Jul 2023 23:13:07 +0300 Subject: [PATCH 05/16] add authors refernces to categories and entities --- .../20230727200739_add_authors_referencesto_entities.rb | 6 ++++++ .../20230727200842_add_authors_referencesto_categories.rb | 6 ++++++ 2 files changed, 12 insertions(+) create mode 100644 db/migrate/20230727200739_add_authors_referencesto_entities.rb create mode 100644 db/migrate/20230727200842_add_authors_referencesto_categories.rb diff --git a/db/migrate/20230727200739_add_authors_referencesto_entities.rb b/db/migrate/20230727200739_add_authors_referencesto_entities.rb new file mode 100644 index 00000000..117fb43a --- /dev/null +++ b/db/migrate/20230727200739_add_authors_referencesto_entities.rb @@ -0,0 +1,6 @@ +class AddAuthorsReferencestoEntities < ActiveRecord::Migration[7.0] + def change + add_reference :entities, :author, foreign_key: { to_table: :users } do + end + end +end diff --git a/db/migrate/20230727200842_add_authors_referencesto_categories.rb b/db/migrate/20230727200842_add_authors_referencesto_categories.rb new file mode 100644 index 00000000..ef568a30 --- /dev/null +++ b/db/migrate/20230727200842_add_authors_referencesto_categories.rb @@ -0,0 +1,6 @@ +class AddAuthorsReferencestoCategories < ActiveRecord::Migration[7.0] + def change + add_reference :categories, :author, foreign_key: { to_table: :users } do + end + end +end From 75d1e1f052c3edde69265c8b651aa84d2a7a8187 Mon Sep 17 00:00:00 2001 From: cherelemma Date: Thu, 27 Jul 2023 23:13:49 +0300 Subject: [PATCH 06/16] create database schema from active record migrations --- db/schema.rb | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 db/schema.rb diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 00000000..6e013c84 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,54 @@ +# 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. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema[7.0].define(version: 2023_07_27_200842) do + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + + create_table "categories", force: :cascade do |t| + t.string "name" + t.string "icon" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.bigint "author_id" + t.index ["author_id"], name: "index_categories_on_author_id" + end + + create_table "categories_entities", force: :cascade do |t| + t.bigint "category_id", null: false + t.bigint "entity_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["category_id"], name: "index_categories_entities_on_category_id" + t.index ["entity_id"], name: "index_categories_entities_on_entity_id" + end + + create_table "entities", force: :cascade do |t| + t.string "name" + t.decimal "amount" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.bigint "author_id" + t.index ["author_id"], name: "index_entities_on_author_id" + end + + create_table "users", force: :cascade do |t| + t.string "name" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_foreign_key "categories", "users", column: "author_id" + add_foreign_key "categories_entities", "categories" + add_foreign_key "categories_entities", "entities" + add_foreign_key "entities", "users", column: "author_id" +end From 67a5ad646132fd2876fa6e91d811d2b177736195 Mon Sep 17 00:00:00 2001 From: cherelemma Date: Thu, 27 Jul 2023 23:45:28 +0300 Subject: [PATCH 07/16] create user model and validate --- app/models/user.rb | 6 ++++++ test/fixtures/users.yml | 11 +++++++++++ test/models/user_test.rb | 7 +++++++ 3 files changed, 24 insertions(+) create mode 100644 app/models/user.rb create mode 100644 test/fixtures/users.yml create mode 100644 test/models/user_test.rb diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 00000000..64b8b9cf --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,6 @@ +class User < ApplicationRecord + has_many :categories, dependent: :destroy + has_many :entities, dependent: :destroy + + validates :name, presence: true +end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 00000000..d7a33292 --- /dev/null +++ b/test/fixtures/users.yml @@ -0,0 +1,11 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the "{}" from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/models/user_test.rb b/test/models/user_test.rb new file mode 100644 index 00000000..5c07f490 --- /dev/null +++ b/test/models/user_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class UserTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end From abe021777a6c9633b7de5b5205f0d07058345786 Mon Sep 17 00:00:00 2001 From: cherelemma Date: Thu, 27 Jul 2023 23:45:46 +0300 Subject: [PATCH 08/16] create category model and validate --- app/models/category.rb | 16 ++++++++++++++++ test/fixtures/categories.yml | 11 +++++++++++ test/models/category_test.rb | 7 +++++++ 3 files changed, 34 insertions(+) create mode 100644 app/models/category.rb create mode 100644 test/fixtures/categories.yml create mode 100644 test/models/category_test.rb diff --git a/app/models/category.rb b/app/models/category.rb new file mode 100644 index 00000000..60ef1ece --- /dev/null +++ b/app/models/category.rb @@ -0,0 +1,16 @@ +class Category < ApplicationRecord + belongs_to :author, class_name: 'User', foreign_key: 'author_id' + has_many :categories_entities, dependent: :destroy + has_many :entities, through: :categories_entities + + validates :name, presence: true, length: { maximum: 50 } + validates :icon, presence: true + + def self.default_icon + 'fas fa-question-circle' + end + + def total_amount + entities.sum(:amount) + end +end diff --git a/test/fixtures/categories.yml b/test/fixtures/categories.yml new file mode 100644 index 00000000..d7a33292 --- /dev/null +++ b/test/fixtures/categories.yml @@ -0,0 +1,11 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the "{}" from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/models/category_test.rb b/test/models/category_test.rb new file mode 100644 index 00000000..869357c8 --- /dev/null +++ b/test/models/category_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class CategoryTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end From c38bcde65efbbe34d69c7c108e07c3d7783d9443 Mon Sep 17 00:00:00 2001 From: cherelemma Date: Thu, 27 Jul 2023 23:46:19 +0300 Subject: [PATCH 09/16] create entiry model and validate --- app/models/entity.rb | 8 ++++++++ test/fixtures/entities.yml | 11 +++++++++++ test/models/entity_test.rb | 7 +++++++ 3 files changed, 26 insertions(+) create mode 100644 app/models/entity.rb create mode 100644 test/fixtures/entities.yml create mode 100644 test/models/entity_test.rb diff --git a/app/models/entity.rb b/app/models/entity.rb new file mode 100644 index 00000000..3b89b51b --- /dev/null +++ b/app/models/entity.rb @@ -0,0 +1,8 @@ +class Entity < ApplicationRecord + belongs_to :author, class_name: 'User', foreign_key: 'author_id' + has_many :categories_entities, dependent: :destroy + has_many :categories , through: :categories_entities + + validates :name, presence: true, length: { maximum: 50 } + validates :amount, presence: true, numericality: { greater_than: 0 } +end diff --git a/test/fixtures/entities.yml b/test/fixtures/entities.yml new file mode 100644 index 00000000..d7a33292 --- /dev/null +++ b/test/fixtures/entities.yml @@ -0,0 +1,11 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the "{}" from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/models/entity_test.rb b/test/models/entity_test.rb new file mode 100644 index 00000000..4c615969 --- /dev/null +++ b/test/models/entity_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class EntityTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end From 72a85cd32fc946bd13d37932c93d7fab2cf6c1ee Mon Sep 17 00:00:00 2001 From: cherelemma Date: Thu, 27 Jul 2023 23:46:33 +0300 Subject: [PATCH 10/16] create category-entity model and validate --- app/models/category_entity.rb | 7 +++++++ test/fixtures/category_entities.yml | 11 +++++++++++ test/models/category_entity_test.rb | 7 +++++++ 3 files changed, 25 insertions(+) create mode 100644 app/models/category_entity.rb create mode 100644 test/fixtures/category_entities.yml create mode 100644 test/models/category_entity_test.rb diff --git a/app/models/category_entity.rb b/app/models/category_entity.rb new file mode 100644 index 00000000..99ae4de4 --- /dev/null +++ b/app/models/category_entity.rb @@ -0,0 +1,7 @@ +class CategoryEntity < ApplicationRecord + belongs_to :category foreign_key :category_id + belongs_to :entity foreign_key :entity_id + + validates :category_id, presence: true + validates :entity_id, presence: true +end diff --git a/test/fixtures/category_entities.yml b/test/fixtures/category_entities.yml new file mode 100644 index 00000000..d7a33292 --- /dev/null +++ b/test/fixtures/category_entities.yml @@ -0,0 +1,11 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the "{}" from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/models/category_entity_test.rb b/test/models/category_entity_test.rb new file mode 100644 index 00000000..9baf13d8 --- /dev/null +++ b/test/models/category_entity_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class CategoryEntityTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end From 1d482bc725e9e9fd1de4710bfa5363e3dde7b363 Mon Sep 17 00:00:00 2001 From: cherelemma Date: Fri, 28 Jul 2023 00:01:45 +0300 Subject: [PATCH 11/16] create test fixtures for all models --- test/fixtures/categories.yml | 29 ++++++++++++++++++++++++----- test/fixtures/category_entities.yml | 23 ++++++++++++++++++----- test/fixtures/entities.yml | 25 ++++++++++++++++++++----- test/fixtures/users.yml | 12 +++++++----- 4 files changed, 69 insertions(+), 20 deletions(-) diff --git a/test/fixtures/categories.yml b/test/fixtures/categories.yml index d7a33292..36534d3c 100644 --- a/test/fixtures/categories.yml +++ b/test/fixtures/categories.yml @@ -4,8 +4,27 @@ # model remove the "{}" from the fixture names and add the columns immediately # below each fixture, per the syntax in the comments below # -one: {} -# column: value -# -two: {} -# column: value +category_one: + name: Category 1 + description: Category 1 description + user_id: 1 + entity_id: 1 + created_at: <%= Time.now %> + updated_at: <%= Time.now %> + +category_two: + name: Category 2 + description: Category 2 description + user_id: 1 + entity_id: 1 + created_at: <%= Time.now %> + updated_at: <%= Time.now %> + +category_three: + name: Category 3 + description: Category 3 description + user_id: 2 + entity_id: 2 + created_at: <%= Time.now %> + updated_at: <%= Time.now %> +``` diff --git a/test/fixtures/category_entities.yml b/test/fixtures/category_entities.yml index d7a33292..f866b2f6 100644 --- a/test/fixtures/category_entities.yml +++ b/test/fixtures/category_entities.yml @@ -4,8 +4,21 @@ # model remove the "{}" from the fixture names and add the columns immediately # below each fixture, per the syntax in the comments below # -one: {} -# column: value -# -two: {} -# column: value +category_entity_one: + category_id: 1 + entity_id: 1 + created_at: <%= Time.now %> + updated_at: <%= Time.now %> + +category_entity_two: + category_id: 2 + entity_id: 1 + created_at: <%= Time.now %> + updated_at: <%= Time.now %> + +category_entity_three: + category_id: 3 + entity_id: 2 + created_at: <%= Time.now %> + updated_at: <%= Time.now %> +``` diff --git a/test/fixtures/entities.yml b/test/fixtures/entities.yml index d7a33292..d3b25f8b 100644 --- a/test/fixtures/entities.yml +++ b/test/fixtures/entities.yml @@ -4,8 +4,23 @@ # model remove the "{}" from the fixture names and add the columns immediately # below each fixture, per the syntax in the comments below # -one: {} -# column: value -# -two: {} -# column: value +entity_one: + name: Entity 1 + description: Entity 1 description + user_id: 1 + created_at: <%= Time.now %> + updated_at: <%= Time.now %> + +entity_two: + name: Entity 2 + description: Entity 2 description + user_id: 2 + created_at: <%= Time.now %> + updated_at: <%= Time.now %> + +entity_three: + name: Entity 3 + description: Entity 3 description + user_id: 2 + created_at: <%= Time.now %> + updated_at: <%= Time.now %> diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index d7a33292..93d77dbc 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -4,8 +4,10 @@ # model remove the "{}" from the fixture names and add the columns immediately # below each fixture, per the syntax in the comments below # -one: {} -# column: value -# -two: {} -# column: value +user_one: + name: User 1 + email: + +user_two: + name: User 2 + email: From 03e81eab6945967358c10b811ae4f6b195a8e9ba Mon Sep 17 00:00:00 2001 From: cherelemma Date: Fri, 28 Jul 2023 00:16:04 +0300 Subject: [PATCH 12/16] write unit-test for all models --- app/models/category.rb | 2 +- app/models/entity.rb | 2 +- app/models/user.rb | 1 + test/fixtures/users.yml | 4 +-- test/models/category_entity_test.rb | 32 +++++++++++++++++-- test/models/category_test.rb | 49 +++++++++++++++++++++++++++-- test/models/entity_test.rb | 44 ++++++++++++++++++++++++-- test/models/user_test.rb | 37 ++++++++++++++++++++-- 8 files changed, 155 insertions(+), 16 deletions(-) diff --git a/app/models/category.rb b/app/models/category.rb index 60ef1ece..0b0ebf3d 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -3,7 +3,7 @@ class Category < ApplicationRecord has_many :categories_entities, dependent: :destroy has_many :entities, through: :categories_entities - validates :name, presence: true, length: { maximum: 50 } + validates :name, presence: true, length: { minimum: 3, maximum: 60 } validates :icon, presence: true def self.default_icon diff --git a/app/models/entity.rb b/app/models/entity.rb index 3b89b51b..84e2dca7 100644 --- a/app/models/entity.rb +++ b/app/models/entity.rb @@ -3,6 +3,6 @@ class Entity < ApplicationRecord has_many :categories_entities, dependent: :destroy has_many :categories , through: :categories_entities - validates :name, presence: true, length: { maximum: 50 } + validates :name, presence: true, length: { minimum: 3, maximum: 60 } validates :amount, presence: true, numericality: { greater_than: 0 } end diff --git a/app/models/user.rb b/app/models/user.rb index 64b8b9cf..e9e645f1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,4 +3,5 @@ class User < ApplicationRecord has_many :entities, dependent: :destroy validates :name, presence: true + validates :name, length: { minimum: 3, maximum: 50 } end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 93d77dbc..8433037e 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -6,8 +6,8 @@ # user_one: name: User 1 - email: + email: admin@localhost user_two: name: User 2 - email: + email: user@localhost diff --git a/test/models/category_entity_test.rb b/test/models/category_entity_test.rb index 9baf13d8..a7175d87 100644 --- a/test/models/category_entity_test.rb +++ b/test/models/category_entity_test.rb @@ -1,7 +1,33 @@ require "test_helper" class CategoryEntityTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end + test "should not save category entity without category" do + category_entity = CategoryEntity.new + assert_not category_entity.save + end + + test "should not save category entity without entity" do + category_entity = CategoryEntity.new + assert_not category_entity.save + end + + test "should save category entity with category and entity" do + category = Category.new(name: "category-1", icon: "fas fa-question-circle") + assert category.save + entity = Entity.new(name: "entity-1", amount: 1) + assert entity.save + category_entity = CategoryEntity.new(category: category, entity: entity) + assert category_entity.save + end + + test "should not save category entity with category and entity already exists" do + category = Category.new(name: "category-1", icon: "fas fa-question-circle") + assert category.save + entity = Entity.new(name: "entity-1", amount: 1) + assert entity.save + category_entity = CategoryEntity.new(category: category, entity: entity) + assert category_entity.save + category_entity = CategoryEntity.new(category: category, entity: entity) + assert_not category_entity.save + end end diff --git a/test/models/category_test.rb b/test/models/category_test.rb index 869357c8..4e7207fc 100644 --- a/test/models/category_test.rb +++ b/test/models/category_test.rb @@ -1,7 +1,50 @@ require "test_helper" class CategoryTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end + test "should not save category without name" do + category = Category.new + assert_not category.save + end + + test "should not save category with name less than 3 characters" do + category = Category.new(name: "ab") + assert_not category.save + end + + test "should not save category with name greater than 50 characters" do + category = Category.new(name: "a" * 61) + assert_not category.save + end + + test "should save category with name greater than 3 characters" do + category = Category.new(name: "category-1") + assert category.save + end + + test "should not save category without icon" do + category = Category.new(name: "category-1") + assert_not category.save + end + + test "should not save category with invalid icon" do + category = Category.new(name: "category-1", icon: "category-1") + assert_not category.save + end + + test "should save category with valid icon" do + category = Category.new(name: "category-1", icon: "fas fa-question-circle") + assert category.save + end + + test "should not save category without author" do + category = Category.new(name: "category-1", icon: "fas fa-question-circle") + assert_not category.save + end + + test "should save category with author" do + user = User.new(name: "category-1", email: "user@localhost", password: "123456") + assert user.save + category = Category.new(name: "category-1", icon: "fas fa-question-circle", author: user) + assert category.save + end end diff --git a/test/models/entity_test.rb b/test/models/entity_test.rb index 4c615969..fb7ac64d 100644 --- a/test/models/entity_test.rb +++ b/test/models/entity_test.rb @@ -1,7 +1,45 @@ require "test_helper" class EntityTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end + test "should not save entity without name" do + entity = Entity.new + assert_not entity.save + end + + test "should not save entity with name greater than 60 characters" do + entity = Entity.new(name: "x" * 61) + assert_not entity.save + end + + test "should save entity with name greater than 3 characters" do + entity = Entity.new(name: "entity-1") + assert entity.save + end + + test "should not save entity without amount" do + entity = Entity.new(name: "entity-1") + assert_not entity.save + end + + test "should not save entity with amount less than 0" do + entity = Entity.new(name: "entity-1", amount: -1) + assert_not entity.save + end + + test "should save entity with amount greater than 0" do + entity = Entity.new(name: "entity-1", amount: 1) + assert entity.save + end + + test "should not save entity without author" do + entity = Entity.new(name: "entity-1", amount: 1) + assert_not entity.save + end + + test "should save entity with author" do + user = = User.new(name: "category-1", email: "user@localhost", password: "123456") + assert user.save + entity = Entity.new(name: "entity-1", amount: 1, author: user) + assert entity.save + end end diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 5c07f490..8a1705b6 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -1,7 +1,38 @@ require "test_helper" class UserTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end + test "should not save user without name" do + user = User.new + assert_not user.save + end + + test "should not save user with name less than 3 characters" do + user = User.new(name: "ab") + assert_not user.save + end + + test "should not save user with name greater than 50 characters" do + user = User.new(name: "a" * 51) + assert_not user.save + end + + test "should save user with name greater than 3 characters" do + user = User.new(name: "abc") + assert user.save + end + + test "should not save user without email" do + user = User.new(name: "abc") + assert_not user.save + end + + test "should not save user with invalid email" do + user = User.new(name: "abc", email: "abc") + assert_not user.save + end + + test "should save user with valid email" do + user = User.new(name: "abc", email: "admin@localhost" + assert user.save + end end From c2f7da27587c5d8241ae873a2fc730a8b93931c2 Mon Sep 17 00:00:00 2001 From: cherelemma Date: Fri, 28 Jul 2023 00:29:27 +0300 Subject: [PATCH 13/16] fix rubocop linter errors --- app/models/entity.rb | 2 +- test/models/category_entity_test.rb | 24 +++++++++--------- test/models/category_test.rb | 38 ++++++++++++++--------------- test/models/entity_test.rb | 34 +++++++++++++------------- 4 files changed, 49 insertions(+), 49 deletions(-) diff --git a/app/models/entity.rb b/app/models/entity.rb index 84e2dca7..0ae02c69 100644 --- a/app/models/entity.rb +++ b/app/models/entity.rb @@ -1,7 +1,7 @@ class Entity < ApplicationRecord belongs_to :author, class_name: 'User', foreign_key: 'author_id' has_many :categories_entities, dependent: :destroy - has_many :categories , through: :categories_entities + has_many :categories, through: :categories_entities validates :name, presence: true, length: { minimum: 3, maximum: 60 } validates :amount, presence: true, numericality: { greater_than: 0 } diff --git a/test/models/category_entity_test.rb b/test/models/category_entity_test.rb index a7175d87..1b23c041 100644 --- a/test/models/category_entity_test.rb +++ b/test/models/category_entity_test.rb @@ -1,33 +1,33 @@ -require "test_helper" +require 'test_helper' class CategoryEntityTest < ActiveSupport::TestCase - test "should not save category entity without category" do + test 'should not save category entity without category' do category_entity = CategoryEntity.new assert_not category_entity.save end - test "should not save category entity without entity" do + test 'should not save category entity without entity' do category_entity = CategoryEntity.new assert_not category_entity.save end - test "should save category entity with category and entity" do - category = Category.new(name: "category-1", icon: "fas fa-question-circle") + test 'should save category entity with category and entity' do + category = Category.new(name: 'category-1', icon: 'fas fa-question-circle') assert category.save - entity = Entity.new(name: "entity-1", amount: 1) + entity = Entity.new(name: 'entity-1', amount: 1) assert entity.save - category_entity = CategoryEntity.new(category: category, entity: entity) + category_entity = CategoryEntity.new(category:, entity:) assert category_entity.save end - test "should not save category entity with category and entity already exists" do - category = Category.new(name: "category-1", icon: "fas fa-question-circle") + test 'should not save category entity with category and entity already exists' do + category = Category.new(name: 'category-1', icon: 'fas fa-question-circle') assert category.save - entity = Entity.new(name: "entity-1", amount: 1) + entity = Entity.new(name: 'entity-1', amount: 1) assert entity.save - category_entity = CategoryEntity.new(category: category, entity: entity) + category_entity = CategoryEntity.new(category:, entity:) assert category_entity.save - category_entity = CategoryEntity.new(category: category, entity: entity) + category_entity = CategoryEntity.new(category:, entity:) assert_not category_entity.save end end diff --git a/test/models/category_test.rb b/test/models/category_test.rb index 4e7207fc..fdb74362 100644 --- a/test/models/category_test.rb +++ b/test/models/category_test.rb @@ -1,50 +1,50 @@ -require "test_helper" +require 'test_helper' class CategoryTest < ActiveSupport::TestCase - test "should not save category without name" do + test 'should not save category without name' do category = Category.new assert_not category.save end - test "should not save category with name less than 3 characters" do - category = Category.new(name: "ab") + test 'should not save category with name less than 3 characters' do + category = Category.new(name: 'ab') assert_not category.save end - test "should not save category with name greater than 50 characters" do - category = Category.new(name: "a" * 61) + test 'should not save category with name greater than 50 characters' do + category = Category.new(name: 'a' * 61) assert_not category.save end - test "should save category with name greater than 3 characters" do - category = Category.new(name: "category-1") + test 'should save category with name greater than 3 characters' do + category = Category.new(name: 'category-1') assert category.save end - test "should not save category without icon" do - category = Category.new(name: "category-1") + test 'should not save category without icon' do + category = Category.new(name: 'category-1') assert_not category.save end - test "should not save category with invalid icon" do - category = Category.new(name: "category-1", icon: "category-1") + test 'should not save category with invalid icon' do + category = Category.new(name: 'category-1', icon: 'category-1') assert_not category.save end - test "should save category with valid icon" do - category = Category.new(name: "category-1", icon: "fas fa-question-circle") + test 'should save category with valid icon' do + category = Category.new(name: 'category-1', icon: 'fas fa-question-circle') assert category.save end - test "should not save category without author" do - category = Category.new(name: "category-1", icon: "fas fa-question-circle") + test 'should not save category without author' do + category = Category.new(name: 'category-1', icon: 'fas fa-question-circle') assert_not category.save end - test "should save category with author" do - user = User.new(name: "category-1", email: "user@localhost", password: "123456") + test 'should save category with author' do + user = User.new(name: 'category-1', email: 'user@localhost', password: '123456') assert user.save - category = Category.new(name: "category-1", icon: "fas fa-question-circle", author: user) + category = Category.new(name: 'category-1', icon: 'fas fa-question-circle', author: user) assert category.save end end diff --git a/test/models/entity_test.rb b/test/models/entity_test.rb index fb7ac64d..07b641e5 100644 --- a/test/models/entity_test.rb +++ b/test/models/entity_test.rb @@ -1,45 +1,45 @@ -require "test_helper" +require 'test_helper' class EntityTest < ActiveSupport::TestCase - test "should not save entity without name" do + test 'should not save entity without name' do entity = Entity.new assert_not entity.save end - test "should not save entity with name greater than 60 characters" do - entity = Entity.new(name: "x" * 61) + test 'should not save entity with name greater than 60 characters' do + entity = Entity.new(name: 'x' * 61) assert_not entity.save end - test "should save entity with name greater than 3 characters" do - entity = Entity.new(name: "entity-1") + test 'should save entity with name greater than 3 characters' do + entity = Entity.new(name: 'entity-1') assert entity.save end - test "should not save entity without amount" do - entity = Entity.new(name: "entity-1") + test 'should not save entity without amount' do + entity = Entity.new(name: 'entity-1') assert_not entity.save end - test "should not save entity with amount less than 0" do - entity = Entity.new(name: "entity-1", amount: -1) + test 'should not save entity with amount less than 0' do + entity = Entity.new(name: 'entity-1', amount: -1) assert_not entity.save end - test "should save entity with amount greater than 0" do - entity = Entity.new(name: "entity-1", amount: 1) + test 'should save entity with amount greater than 0' do + entity = Entity.new(name: 'entity-1', amount: 1) assert entity.save end - test "should not save entity without author" do - entity = Entity.new(name: "entity-1", amount: 1) + test 'should not save entity without author' do + entity = Entity.new(name: 'entity-1', amount: 1) assert_not entity.save end - test "should save entity with author" do - user = = User.new(name: "category-1", email: "user@localhost", password: "123456") + test 'should save entity with author' do + user = User.new(name: 'category-1', email: 'user@localhost', password: '123456') assert user.save - entity = Entity.new(name: "entity-1", amount: 1, author: user) + entity = Entity.new(name: 'entity-1', amount: 1, author: user) assert entity.save end end From 1e2e938da35c5a7bffa33c743f97af568aa02ab3 Mon Sep 17 00:00:00 2001 From: cherelemma Date: Fri, 28 Jul 2023 00:33:52 +0300 Subject: [PATCH 14/16] add comma between association and foreign key --- app/models/category_entity.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/category_entity.rb b/app/models/category_entity.rb index 99ae4de4..e6c30280 100644 --- a/app/models/category_entity.rb +++ b/app/models/category_entity.rb @@ -1,7 +1,7 @@ class CategoryEntity < ApplicationRecord - belongs_to :category foreign_key :category_id - belongs_to :entity foreign_key :entity_id - + belongs_to :category, foreign_key :category_id + belongs_to :entity, foreign_key :entity_id + validates :category_id, presence: true validates :entity_id, presence: true end From 6300e436fc8c6c9807d7ca940fa078ae3bc095d6 Mon Sep 17 00:00:00 2001 From: cherelemma Date: Fri, 28 Jul 2023 00:36:02 +0300 Subject: [PATCH 15/16] fix another linter errors --- test/models/user_test.rb | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 8a1705b6..14617e96 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -1,38 +1,38 @@ -require "test_helper" +require 'test_helper' class UserTest < ActiveSupport::TestCase - test "should not save user without name" do + test 'should not save user without name' do user = User.new assert_not user.save end - test "should not save user with name less than 3 characters" do - user = User.new(name: "ab") + test 'should not save user with name less than 3 characters' do + user = User.new(name: 'ab') assert_not user.save end - test "should not save user with name greater than 50 characters" do - user = User.new(name: "a" * 51) + test 'should not save user with name greater than 50 characters' do + user = User.new(name: 'a' * 51) assert_not user.save end - test "should save user with name greater than 3 characters" do - user = User.new(name: "abc") + test 'should save user with name greater than 3 characters' do + user = User.new(name: 'user') assert user.save end - test "should not save user without email" do - user = User.new(name: "abc") + test 'should not save user without email' do + user = User.new(name: 'user') assert_not user.save end - test "should not save user with invalid email" do - user = User.new(name: "abc", email: "abc") + test 'should not save user with invalid email' do + user = User.new(name: 'user', email: 'user') assert_not user.save end - test "should save user with valid email" do - user = User.new(name: "abc", email: "admin@localhost" + test 'should save user with valid email' do + user = User.new(name: 'user', email: 'admin@localhost') assert user.save end end From a47e5238ebd48bb767e6ebedb626fa7a387e4f50 Mon Sep 17 00:00:00 2001 From: cherelemma Date: Fri, 28 Jul 2023 00:39:37 +0300 Subject: [PATCH 16/16] fix another linter errors --- app/models/category_entity.rb | 6 +++--- test/models/user_test.rb | 28 ++++++++++++++-------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/app/models/category_entity.rb b/app/models/category_entity.rb index e6c30280..fa18397d 100644 --- a/app/models/category_entity.rb +++ b/app/models/category_entity.rb @@ -1,7 +1,7 @@ class CategoryEntity < ApplicationRecord - belongs_to :category, foreign_key :category_id - belongs_to :entity, foreign_key :entity_id - + belongs_to :category + belongs_to :entity + validates :category_id, presence: true validates :entity_id, presence: true end diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 8a1705b6..14617e96 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -1,38 +1,38 @@ -require "test_helper" +require 'test_helper' class UserTest < ActiveSupport::TestCase - test "should not save user without name" do + test 'should not save user without name' do user = User.new assert_not user.save end - test "should not save user with name less than 3 characters" do - user = User.new(name: "ab") + test 'should not save user with name less than 3 characters' do + user = User.new(name: 'ab') assert_not user.save end - test "should not save user with name greater than 50 characters" do - user = User.new(name: "a" * 51) + test 'should not save user with name greater than 50 characters' do + user = User.new(name: 'a' * 51) assert_not user.save end - test "should save user with name greater than 3 characters" do - user = User.new(name: "abc") + test 'should save user with name greater than 3 characters' do + user = User.new(name: 'user') assert user.save end - test "should not save user without email" do - user = User.new(name: "abc") + test 'should not save user without email' do + user = User.new(name: 'user') assert_not user.save end - test "should not save user with invalid email" do - user = User.new(name: "abc", email: "abc") + test 'should not save user with invalid email' do + user = User.new(name: 'user', email: 'user') assert_not user.save end - test "should save user with valid email" do - user = User.new(name: "abc", email: "admin@localhost" + test 'should save user with valid email' do + user = User.new(name: 'user', email: 'admin@localhost') assert user.save end end