From e7460124ee9857ec6d129f8c092f6df1e88dfdfe Mon Sep 17 00:00:00 2001 From: Andai Roland Date: Sat, 24 Sep 2022 15:13:43 +0200 Subject: [PATCH 01/14] validation of rent number implemented --- app/controllers/rents_controller.rb | 2 +- app/models/rent.rb | 2 ++ app/validators/rent_number_validator.rb | 14 ++++++++++++++ app/views/rents/_form.html.erb | 5 +++++ app/views/rents/index.html.erb | 2 ++ app/views/rents/users_index.html.erb | 2 ++ config/locales/hu.yml | 1 + db/migrate/20220922143621_add_number_to_rent.rb | 5 +++++ db/schema.rb | 3 ++- db/seeds.rb | 13 +++++++++---- 10 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 app/validators/rent_number_validator.rb create mode 100644 db/migrate/20220922143621_add_number_to_rent.rb diff --git a/app/controllers/rents_controller.rb b/app/controllers/rents_controller.rb index bd57a49..e4368a5 100644 --- a/app/controllers/rents_controller.rb +++ b/app/controllers/rents_controller.rb @@ -101,7 +101,7 @@ def set_rent # Only allow a list of trusted parameters through. def rent_params - params.require(:rent).permit(:item_id, :user_id, :begin, :end, :state) + params.require(:rent).permit(:item_id, :user_id, :begin, :end, :state, :number) end def rent_filter_params diff --git a/app/models/rent.rb b/app/models/rent.rb index 38f882f..abc564e 100644 --- a/app/models/rent.rb +++ b/app/models/rent.rb @@ -5,6 +5,8 @@ class Rent < ApplicationRecord validates :begin, presence: true, comparison: { greater_than: DateTime.now, message: " később legyen, mint a mostani időpont." } validates :end, presence: true , comparison: { greater_than: :begin, message: " később legyen, mint a kezdete." } + validates :number, presence: true, comparison: { greater_than: 0 } + validates_with RentNumberValidator #STATE MACHINE def approve! diff --git a/app/validators/rent_number_validator.rb b/app/validators/rent_number_validator.rb new file mode 100644 index 0000000..a64c3d6 --- /dev/null +++ b/app/validators/rent_number_validator.rb @@ -0,0 +1,14 @@ +class RentNumberValidator < ActiveModel::Validator + def validate(record) + #Rent.where("begin < ? && end > ?", record.end, record.begin).where.not(state: :unprocessed).sum(:number) + return if record.number.nil? + sum = 0 + rents = Rent.all + rents.each do |rent| + sum += rent.number if(rent.begin < record.end && rent.end > record.begin && rent.state != "unprocessed") + end + if sum+record.number > record.item.number + record.errors.add :base, "Erre az időpontra sajnos elfogyott a kívánt eszköz." + end + end +end diff --git a/app/views/rents/_form.html.erb b/app/views/rents/_form.html.erb index 469be30..1e58f22 100644 --- a/app/views/rents/_form.html.erb +++ b/app/views/rents/_form.html.erb @@ -24,6 +24,11 @@ <%= form.hidden_field :item_id, :value => @rent.item_id %> +
+ <%= form.label :number, 'Darabszám' %> + <%= form.number_field :number %> +
+
<%= form.label :begin, 'Kezdete' %> <%= form.datetime_field :begin %> diff --git a/app/views/rents/index.html.erb b/app/views/rents/index.html.erb index 5525317..394c6e2 100644 --- a/app/views/rents/index.html.erb +++ b/app/views/rents/index.html.erb @@ -28,6 +28,7 @@ Név Eszköz + Darabszám Kezdet Vége Állapot @@ -40,6 +41,7 @@ <%= rent.user.name %> <%= rent.item.name %> + <%= rent.number %> darab <%= rent.begin.strftime("%Y.%m.%d. %H:%M") %> <%= rent.end.strftime("%Y.%m.%d. %H:%M") %> <%= I18n.t :"rent_states.#{rent.state}" %> diff --git a/app/views/rents/users_index.html.erb b/app/views/rents/users_index.html.erb index b15b0ed..5b0422e 100644 --- a/app/views/rents/users_index.html.erb +++ b/app/views/rents/users_index.html.erb @@ -27,6 +27,7 @@ Eszköz + Darabszám Kezdet Vége Állapot @@ -37,6 +38,7 @@ <% @rents.each do |rent| %> <%= rent.item.name %> + <%= rent.number %> darab <%= rent.begin.strftime("%Y.%m.%d. %H:%M") %> <%= rent.end.strftime("%Y.%m.%d. %H:%M") %> <%= I18n.t :"rent_states.#{rent.state}" %> diff --git a/config/locales/hu.yml b/config/locales/hu.yml index 4dd77b5..778035e 100644 --- a/config/locales/hu.yml +++ b/config/locales/hu.yml @@ -17,6 +17,7 @@ hu: name: "Név" number: "Mennyiség" rent: + number: "Darabszám" begin: "Kezdete" end: "Vége" state: "Állapot" diff --git a/db/migrate/20220922143621_add_number_to_rent.rb b/db/migrate/20220922143621_add_number_to_rent.rb new file mode 100644 index 0000000..d3ca616 --- /dev/null +++ b/db/migrate/20220922143621_add_number_to_rent.rb @@ -0,0 +1,5 @@ +class AddNumberToRent < ActiveRecord::Migration[7.0] + def change + add_column :rents, :number, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 566025f..436c966 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.0].define(version: 2022_08_16_134517) do +ActiveRecord::Schema[7.0].define(version: 2022_09_22_143621) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -115,6 +115,7 @@ t.integer "state" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "number" t.index ["item_id"], name: "index_rents_on_item_id" t.index ["user_id"], name: "index_rents_on_user_id" end diff --git a/db/seeds.rb b/db/seeds.rb index ca38272..cdf364c 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -45,10 +45,15 @@ teams.each { |t| Entry.create(team_id: t[:team].id, event_id: event1.id, showed_up: false, comment: t[:comment]) } -items = [{ name: 'Pohár', number: 3 }, - { name: 'Labda', number: 5 }] +items = [{ name: 'Pohár', number: 10 }, + { name: 'Labda', number: 20 }] items.each { |item| Item.create(item) } -rents = [{ item_id: 1, user_id: 1, begin: DateTime.new(2022,6,23,4,5,6), end: DateTime.new(2022,6,30,4,5,6)}, - { item_id: 2, user_id: 2, begin: DateTime.new(2022,6,24,4,5,6), end: DateTime.new(2022,6,29,4,5,6)}] +rents =[ { item_id: 1, user_id: 1, begin: DateTime.now + 3.days, end: DateTime.now + 4.days, number: 10}, + { item_id: 2, user_id: 2, begin: DateTime.now + 3.days, end: DateTime.now + 4.days, number: 1}, + { item_id: 2, user_id: 2, begin: DateTime.now + 3.days, end: DateTime.now + 5.days, number: 2}, + { item_id: 2, user_id: 2, begin: DateTime.now + 3.days, end: DateTime.now + 9.days, number: 3}, + { item_id: 2, user_id: 2, begin: DateTime.now + 6.days, end: DateTime.now + 7.days, number: 4}, + { item_id: 2, user_id: 2, begin: DateTime.now + 6.days, end: DateTime.now + 9.days, number: 5}, + { item_id: 2, user_id: 2, begin: DateTime.now + 9.days, end: DateTime.now + 10.days, number: 6}] rents.each { |rent| Rent.create(rent) } From 9f2ed134784c5409bc87d0a204c8a35ade9a0a5d Mon Sep 17 00:00:00 2001 From: Andai Roland Date: Mon, 26 Sep 2022 19:28:37 +0200 Subject: [PATCH 02/14] feedback for availability error added --- app/validators/rent_number_validator.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/app/validators/rent_number_validator.rb b/app/validators/rent_number_validator.rb index a64c3d6..fbb73d7 100644 --- a/app/validators/rent_number_validator.rb +++ b/app/validators/rent_number_validator.rb @@ -1,14 +1,24 @@ class RentNumberValidator < ActiveModel::Validator def validate(record) #Rent.where("begin < ? && end > ?", record.end, record.begin).where.not(state: :unprocessed).sum(:number) - return if record.number.nil? + return if record.number.nil? || record.begin.nil? || record.end.nil? sum = 0 rents = Rent.all rents.each do |rent| sum += rent.number if(rent.begin < record.end && rent.end > record.begin && rent.state != "unprocessed") end if sum+record.number > record.item.number - record.errors.add :base, "Erre az időpontra sajnos elfogyott a kívánt eszköz." + record.errors.add :base, "Erre az időpontra sajnos nincs elég a kívánt eszközből." + record.errors.add :base, "A megadott napokra elérhető eszközök száma:" + day = record.begin.to_date + while(day <= record.end.to_date) + sum = 0 + rents.each do |rent| + sum += rent.number if(rent.begin.to_date <= day && rent.end.to_date >= day && rent.state != "unprocessed") + end + record.errors.add :base, day.to_s + ": " + (record.item.number - sum).to_s + " darab" + day += 1 + end end end end From b7a6184b3de8d58a22efc049744915f2dd0e30fb Mon Sep 17 00:00:00 2001 From: SepsiLaszlo Date: Wed, 28 Sep 2022 20:36:44 +0200 Subject: [PATCH 03/14] refactor to sum --- app/validators/rent_number_validator.rb | 45 +++++++++++++++---------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/app/validators/rent_number_validator.rb b/app/validators/rent_number_validator.rb index fbb73d7..9df88da 100644 --- a/app/validators/rent_number_validator.rb +++ b/app/validators/rent_number_validator.rb @@ -1,24 +1,33 @@ class RentNumberValidator < ActiveModel::Validator - def validate(record) - #Rent.where("begin < ? && end > ?", record.end, record.begin).where.not(state: :unprocessed).sum(:number) - return if record.number.nil? || record.begin.nil? || record.end.nil? + def validate(record) + #Rent.where("begin < ? && end > ?", record.end, record.begin).where.not(state: :unprocessed).sum(:number) + return if record.number.nil? || record.begin.nil? || record.end.nil? + + rent_sum = rents.sum do | rent| + return unless rent.begin < record.end + return unless rent.end > record.begin + return unless ['unprocessed', 'rejected'].include?(rent.state) + rent.number + end + + if rent_sum + record.number > record.item.number + record.errors.add :base, "Erre az időpontra sajnos nincs elég a kívánt eszközből." + record.errors.add :base, "A megadott napokra elérhető eszközök száma:" + day = record.begin.to_date + while (day <= record.end.to_date) sum = 0 - rents = Rent.all rents.each do |rent| - sum += rent.number if(rent.begin < record.end && rent.end > record.begin && rent.state != "unprocessed") - end - if sum+record.number > record.item.number - record.errors.add :base, "Erre az időpontra sajnos nincs elég a kívánt eszközből." - record.errors.add :base, "A megadott napokra elérhető eszközök száma:" - day = record.begin.to_date - while(day <= record.end.to_date) - sum = 0 - rents.each do |rent| - sum += rent.number if(rent.begin.to_date <= day && rent.end.to_date >= day && rent.state != "unprocessed") - end - record.errors.add :base, day.to_s + ": " + (record.item.number - sum).to_s + " darab" - day += 1 - end + sum += rent.number if (rent.begin.to_date <= day && rent.end.to_date >= day && rent.state != "unprocessed") end + record.errors.add :base, day.to_s + ": " + (record.item.number - sum).to_s + " darab" + day += 1 + end end + end + + private + + def rents + @rents ||= Rent.all + end end From 94aa54c6a3b84c8d1b442d96ae41277c5e723f62 Mon Sep 17 00:00:00 2001 From: SepsiLaszlo Date: Wed, 28 Sep 2022 20:38:57 +0200 Subject: [PATCH 04/14] install rspec --- .rspec | 1 + Gemfile | 1 + Gemfile.lock | 19 +++++++++ spec/rails_helper.rb | 64 ++++++++++++++++++++++++++++++ spec/spec_helper.rb | 94 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 179 insertions(+) create mode 100644 .rspec create mode 100644 spec/rails_helper.rb create mode 100644 spec/spec_helper.rb diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..c99d2e7 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--require spec_helper diff --git a/Gemfile b/Gemfile index 2bc3044..c3789c1 100644 --- a/Gemfile +++ b/Gemfile @@ -60,6 +60,7 @@ gem 'bootsnap', '>= 1.1.0', require: false group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] + gem 'rspec-rails' end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index d1c8450..d7b7fc8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -112,6 +112,7 @@ GEM railties (>= 4.1.0) responders warden (~> 1.2.3) + diff-lcs (1.5.0) digest (3.1.0) dotenv (2.7.6) dotenv-rails (2.7.6) @@ -249,6 +250,23 @@ GEM actionpack (>= 5.0) railties (>= 5.0) rexml (3.2.5) + rspec-core (3.11.0) + rspec-support (~> 3.11.0) + rspec-expectations (3.11.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.11.0) + rspec-mocks (3.11.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.11.0) + rspec-rails (5.1.2) + actionpack (>= 5.2) + activesupport (>= 5.2) + railties (>= 5.2) + rspec-core (~> 3.10) + rspec-expectations (~> 3.10) + rspec-mocks (~> 3.10) + rspec-support (~> 3.10) + rspec-support (3.11.1) ruby2_keywords (0.0.5) rubyzip (2.3.2) sassc (2.4.0) @@ -337,6 +355,7 @@ DEPENDENCIES pg (>= 0.18, < 2.0) puma (~> 4.3) rails (~> 7.0.1) + rspec-rails sassc-rails selenium-webdriver semantic-ui-sass diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 0000000..b6317b5 --- /dev/null +++ b/spec/rails_helper.rb @@ -0,0 +1,64 @@ +# This file is copied to spec/ when you run 'rails generate rspec:install' +require 'spec_helper' +ENV['RAILS_ENV'] ||= 'test' +require_relative '../config/environment' +# Prevent database truncation if the environment is production +abort("The Rails environment is running in production mode!") if Rails.env.production? +require 'rspec/rails' +# Add additional requires below this line. Rails is not loaded until this point! + +# Requires supporting ruby files with custom matchers and macros, etc, in +# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are +# run as spec files by default. This means that files in spec/support that end +# in _spec.rb will both be required and run as specs, causing the specs to be +# run twice. It is recommended that you do not name files matching this glob to +# end with _spec.rb. You can configure this pattern with the --pattern +# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. +# +# The following line is provided for convenience purposes. It has the downside +# of increasing the boot-up time by auto-requiring all files in the support +# directory. Alternatively, in the individual `*_spec.rb` files, manually +# require only the support files necessary. +# +# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f } + +# Checks for pending migrations and applies them before tests are run. +# If you are not using ActiveRecord, you can remove these lines. +begin + ActiveRecord::Migration.maintain_test_schema! +rescue ActiveRecord::PendingMigrationError => e + puts e.to_s.strip + exit 1 +end +RSpec.configure do |config| + # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures + config.fixture_path = "#{::Rails.root}/spec/fixtures" + + # If you're not using ActiveRecord, or you'd prefer not to run each of your + # examples within a transaction, remove the following line or assign false + # instead of true. + config.use_transactional_fixtures = true + + # You can uncomment this line to turn off ActiveRecord support entirely. + # config.use_active_record = false + + # RSpec Rails can automatically mix in different behaviours to your tests + # based on their file location, for example enabling you to call `get` and + # `post` in specs under `spec/controllers`. + # + # You can disable this behaviour by removing the line below, and instead + # explicitly tag your specs with their type, e.g.: + # + # RSpec.describe UsersController, type: :controller do + # # ... + # end + # + # The different available types are documented in the features, such as in + # https://relishapp.com/rspec/rspec-rails/docs + config.infer_spec_type_from_file_location! + + # Filter lines from Rails gems in backtraces. + config.filter_rails_from_backtrace! + # arbitrary gems may also be filtered via: + # config.filter_gems_from_backtrace("gem name") +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..a0d4080 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,94 @@ +# This file was generated by the `rails generate rspec:install` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. +=begin + # This allows you to limit a spec run to individual examples or groups + # you care about by tagging them with `:focus` metadata. When nothing + # is tagged with `:focus`, all examples get run. RSpec also provides + # aliases for `it`, `describe`, and `context` that include `:focus` + # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + config.filter_run_when_matching :focus + + # Allows RSpec to persist some state between runs in order to support + # the `--only-failures` and `--next-failure` CLI options. We recommend + # you configure your source control system to ignore this file. + config.example_status_persistence_file_path = "spec/examples.txt" + + # Limits the available syntax to the non-monkey patched syntax that is + # recommended. For more details, see: + # https://relishapp.com/rspec/rspec-core/docs/configuration/zero-monkey-patching-mode + config.disable_monkey_patching! + + # Many RSpec users commonly either run the entire suite or an individual + # file, and it's useful to allow more verbose output when running an + # individual spec file. + if config.files_to_run.one? + # Use the documentation formatter for detailed output, + # unless a formatter has already been configured + # (e.g. via a command-line flag). + config.default_formatter = "doc" + end + + # Print the 10 slowest examples and example groups at the + # end of the spec run, to help surface which specs are running + # particularly slow. + config.profile_examples = 10 + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = :random + + # Seed global randomization in this process using the `--seed` CLI option. + # Setting this allows you to use `--seed` to deterministically reproduce + # test failures related to randomization by passing the same `--seed` value + # as the one that triggered the failure. + Kernel.srand config.seed +=end +end From 4686338f1b0d858f2920b86da2e8856d4cce92a6 Mon Sep 17 00:00:00 2001 From: SepsiLaszlo Date: Wed, 28 Sep 2022 21:03:39 +0200 Subject: [PATCH 05/14] install simplecov --- .gitignore | 1 + Gemfile | 1 + Gemfile.lock | 8 ++++++++ spec/spec_helper.rb | 2 ++ 4 files changed, 12 insertions(+) diff --git a/.gitignore b/.gitignore index 6de91cd..744e484 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,4 @@ /.idea .env public/uploads +coverage diff --git a/Gemfile b/Gemfile index c3789c1..56c7578 100644 --- a/Gemfile +++ b/Gemfile @@ -78,6 +78,7 @@ group :test do gem 'selenium-webdriver' # Easy installation and use of chromedriver to run system tests with Chrome gem 'chromedriver-helper' + gem 'simplecov' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem diff --git a/Gemfile.lock b/Gemfile.lock index d7b7fc8..7dfacf1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -114,6 +114,7 @@ GEM warden (~> 1.2.3) diff-lcs (1.5.0) digest (3.1.0) + docile (1.4.0) dotenv (2.7.6) dotenv-rails (2.7.6) dotenv (= 2.7.6) @@ -288,6 +289,12 @@ GEM shrine (3.4.0) content_disposition (~> 1.0) down (~> 5.1) + simplecov (0.21.2) + docile (~> 1.1) + simplecov-html (~> 0.11) + simplecov_json_formatter (~> 0.1) + simplecov-html (0.12.3) + simplecov_json_formatter (0.1.4) spring (2.1.1) spring-watcher-listen (2.0.1) listen (>= 2.7, < 4.0) @@ -360,6 +367,7 @@ DEPENDENCIES selenium-webdriver semantic-ui-sass shrine + simplecov spring spring-watcher-listen (~> 2.0.0) trix-rails diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a0d4080..f9f2ba6 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,5 @@ +require 'simplecov' +SimpleCov.start 'rails' # This file was generated by the `rails generate rspec:install` command. Conventionally, all # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # The generated `.rspec` file contains `--require spec_helper` which will cause From 4671d5f3aa4e4f7bcd68b55da9376bcf0105fac7 Mon Sep 17 00:00:00 2001 From: SepsiLaszlo Date: Wed, 28 Sep 2022 21:12:59 +0200 Subject: [PATCH 06/14] add GH Action to run tests --- .github/workflows/run_specs.yml | 45 +++++++++++++++++++++++++++++++++ config/application.rb | 2 +- spec/models/rent_spec.rb | 5 ++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/run_specs.yml create mode 100644 spec/models/rent_spec.rb diff --git a/.github/workflows/run_specs.yml b/.github/workflows/run_specs.yml new file mode 100644 index 0000000..79fc95f --- /dev/null +++ b/.github/workflows/run_specs.yml @@ -0,0 +1,45 @@ +env: + RUBY_VERSION: 3.0.3 + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_DB: programmingtil_rails_1_test +# DEVISE_JWT_SECRET_KEY: ${{ secrets.DEVISE_JWT_SECRET_KEY }} + +name: Rails Specs +on: [push,pull_request] +jobs: + rspec-test: + name: RSpec + runs-on: ubuntu-20.04 + services: + postgres: + image: postgres:latest + ports: + - 5432:5432 + env: + POSTGRES_USER: ${{ env.POSTGRES_USER }} + POSTGRES_PASSWORD: ${{ env.POSTGRES_PASSWORD }} + steps: + - uses: actions/checkout@v1 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: '3.0.3' # Not needed with a .ruby-version file + bundler-cache: true # runs 'bundle install' and caches installed gems automatically + - name: Install postgres client + run: sudo apt-get install libpq-dev + - name: Install dependencies + run: | + gem install bundler + bundler install + - name: Create database + run: | + bundler exec rails db:create RAILS_ENV=test + bundler exec rails db:migrate RAILS_ENV=test + - name: Run tests + run: bundler exec rspec spec/* + - name: Upload coverage results + uses: actions/upload-artifact@master + if: always() + with: + name: coverage-report + path: coverage \ No newline at end of file diff --git a/config/application.rb b/config/application.rb index 5652589..2d3bff6 100644 --- a/config/application.rb +++ b/config/application.rb @@ -9,7 +9,7 @@ module Schorpong class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. - config.load_defaults 5.2 + # config.load_defaults 5.2 config.i18n.default_locale = :hu # Configuration for the application, engines, and railties goes here. diff --git a/spec/models/rent_spec.rb b/spec/models/rent_spec.rb new file mode 100644 index 0000000..e384e8f --- /dev/null +++ b/spec/models/rent_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Rent, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end From 0db00cb00b30ce7444fdc6ab9162096dcac7db3a Mon Sep 17 00:00:00 2001 From: SepsiLaszlo Date: Wed, 28 Sep 2022 21:38:27 +0200 Subject: [PATCH 07/14] fix deprecation warnings --- Gemfile | 2 +- Gemfile.lock | 239 +++++++++++++++++------------------ config/application.rb | 1 + config/initializers/oauth.rb | 2 +- spec/rails_helper.rb | 6 + spec/spec_helper.rb | 2 - 6 files changed, 123 insertions(+), 129 deletions(-) diff --git a/Gemfile b/Gemfile index 56c7578..ed63e8b 100644 --- a/Gemfile +++ b/Gemfile @@ -77,7 +77,7 @@ group :test do gem 'capybara', '>= 2.15' gem 'selenium-webdriver' # Easy installation and use of chromedriver to run system tests with Chrome - gem 'chromedriver-helper' + # gem 'chromedriver-helper' gem 'simplecov' end diff --git a/Gemfile.lock b/Gemfile.lock index 7dfacf1..9c27c57 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,84 +1,82 @@ GEM remote: https://rubygems.org/ specs: - actioncable (7.0.2.3) - actionpack (= 7.0.2.3) - activesupport (= 7.0.2.3) + actioncable (7.0.4) + actionpack (= 7.0.4) + activesupport (= 7.0.4) nio4r (~> 2.0) websocket-driver (>= 0.6.1) - actionmailbox (7.0.2.3) - actionpack (= 7.0.2.3) - activejob (= 7.0.2.3) - activerecord (= 7.0.2.3) - activestorage (= 7.0.2.3) - activesupport (= 7.0.2.3) + actionmailbox (7.0.4) + actionpack (= 7.0.4) + activejob (= 7.0.4) + activerecord (= 7.0.4) + activestorage (= 7.0.4) + activesupport (= 7.0.4) mail (>= 2.7.1) net-imap net-pop net-smtp - actionmailer (7.0.2.3) - actionpack (= 7.0.2.3) - actionview (= 7.0.2.3) - activejob (= 7.0.2.3) - activesupport (= 7.0.2.3) + actionmailer (7.0.4) + actionpack (= 7.0.4) + actionview (= 7.0.4) + activejob (= 7.0.4) + activesupport (= 7.0.4) mail (~> 2.5, >= 2.5.4) net-imap net-pop net-smtp rails-dom-testing (~> 2.0) - actionpack (7.0.2.3) - actionview (= 7.0.2.3) - activesupport (= 7.0.2.3) + actionpack (7.0.4) + actionview (= 7.0.4) + activesupport (= 7.0.4) rack (~> 2.0, >= 2.2.0) rack-test (>= 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.2.0) - actiontext (7.0.2.3) - actionpack (= 7.0.2.3) - activerecord (= 7.0.2.3) - activestorage (= 7.0.2.3) - activesupport (= 7.0.2.3) + actiontext (7.0.4) + actionpack (= 7.0.4) + activerecord (= 7.0.4) + activestorage (= 7.0.4) + activesupport (= 7.0.4) globalid (>= 0.6.0) nokogiri (>= 1.8.5) - actionview (7.0.2.3) - activesupport (= 7.0.2.3) + actionview (7.0.4) + activesupport (= 7.0.4) builder (~> 3.1) erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.1, >= 1.2.0) - activejob (7.0.2.3) - activesupport (= 7.0.2.3) + activejob (7.0.4) + activesupport (= 7.0.4) globalid (>= 0.3.6) - activemodel (7.0.2.3) - activesupport (= 7.0.2.3) - activerecord (7.0.2.3) - activemodel (= 7.0.2.3) - activesupport (= 7.0.2.3) - activestorage (7.0.2.3) - actionpack (= 7.0.2.3) - activejob (= 7.0.2.3) - activerecord (= 7.0.2.3) - activesupport (= 7.0.2.3) + activemodel (7.0.4) + activesupport (= 7.0.4) + activerecord (7.0.4) + activemodel (= 7.0.4) + activesupport (= 7.0.4) + activestorage (7.0.4) + actionpack (= 7.0.4) + activejob (= 7.0.4) + activerecord (= 7.0.4) + activesupport (= 7.0.4) marcel (~> 1.0) mini_mime (>= 1.1.0) - activesupport (7.0.2.3) + activesupport (7.0.4) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 1.6, < 2) minitest (>= 5.1) tzinfo (~> 2.0) - addressable (2.8.0) - public_suffix (>= 2.0.2, < 5.0) - archive-zip (0.12.0) - io-like (~> 0.3.0) + addressable (2.8.1) + public_suffix (>= 2.0.2, < 6.0) autoprefixer-rails (8.6.5) execjs - bcrypt (3.1.17) + bcrypt (3.1.18) bindex (0.8.1) - bootsnap (1.11.1) + bootsnap (1.13.0) msgpack (~> 1.2) builder (3.2.4) byebug (11.1.3) - capybara (3.36.0) + capybara (3.37.1) addressable matrix mini_mime (>= 0.1.3) @@ -93,9 +91,6 @@ GEM mime-types (>= 1.16) ssrf_filter (~> 1.0) childprocess (4.1.0) - chromedriver-helper (2.1.1) - archive-zip (~> 0.10) - nokogiri (~> 1.8) coffee-rails (4.2.2) coffee-script (>= 2.2.0) railties (>= 4.0.0) @@ -103,7 +98,7 @@ GEM coffee-script-source execjs coffee-script-source (1.12.2) - concurrent-ruby (1.1.9) + concurrent-ruby (1.1.10) content_disposition (1.0.0) crass (1.0.6) devise (4.8.1) @@ -113,41 +108,38 @@ GEM responders warden (~> 1.2.3) diff-lcs (1.5.0) - digest (3.1.0) docile (1.4.0) - dotenv (2.7.6) - dotenv-rails (2.7.6) - dotenv (= 2.7.6) + dotenv (2.8.1) + dotenv-rails (2.8.1) + dotenv (= 2.8.1) railties (>= 3.2) - down (5.3.0) + down (5.3.1) addressable (~> 2.8) - erubi (1.10.0) + erubi (1.11.0) execjs (2.8.1) - faraday (2.2.0) - faraday-net_http (~> 2.0) + faraday (2.5.2) + faraday-net_http (>= 2.0, < 3.1) ruby2_keywords (>= 0.0.4) - faraday-net_http (2.0.1) + faraday-net_http (3.0.0) ffi (1.15.5) ffi (1.15.5-x64-mingw32) globalid (1.0.0) activesupport (>= 5.0) hashie (5.0.0) - i18n (1.10.0) + i18n (1.12.0) concurrent-ruby (~> 1.0) - io-like (0.3.1) - io-wait (0.2.1) jbuilder (2.11.5) actionview (>= 5.0.0) activesupport (>= 5.0.0) - jquery-rails (4.4.0) + jquery-rails (4.5.0) rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) thor (>= 0.14, < 2.0) - jwt (2.3.0) + jwt (2.5.0) listen (3.7.1) rb-fsevent (~> 0.10, >= 0.10.3) rb-inotify (~> 0.9, >= 0.9.10) - loofah (2.15.0) + loofah (2.19.0) crass (~> 1.0.2) nokogiri (>= 1.5.9) mail (2.7.1) @@ -161,92 +153,85 @@ GEM mini_magick (4.11.0) mini_mime (1.1.2) mini_portile2 (2.8.0) - minitest (5.15.0) - msgpack (1.4.5) - multi_json (1.15.0) + minitest (5.16.3) + msgpack (1.5.6) multi_xml (0.6.0) - net-imap (0.2.3) - digest + net-imap (0.3.0) net-protocol - strscan - net-pop (0.1.1) - digest + net-pop (0.1.2) net-protocol + net-protocol (0.1.3) timeout - net-protocol (0.1.2) - io-wait - timeout - net-smtp (0.3.1) - digest + net-smtp (0.3.2) net-protocol - timeout nio4r (2.5.8) - nokogiri (1.13.3) + nokogiri (1.13.8) mini_portile2 (~> 2.8.0) racc (~> 1.4) - nokogiri (1.13.3-x64-mingw32) + nokogiri (1.13.8-x64-mingw32) racc (~> 1.4) - oauth2 (1.4.9) + oauth2 (2.0.9) faraday (>= 0.17.3, < 3.0) jwt (>= 1.0, < 3.0) - multi_json (~> 1.3) multi_xml (~> 0.5) - rack (>= 1.2, < 3) - omniauth (2.0.4) + rack (>= 1.2, < 4) + snaky_hash (~> 2.0) + version_gem (~> 1.1) + omniauth (2.1.0) hashie (>= 3.4.6) - rack (>= 1.6.2, < 3) + rack (>= 2.2.3) rack-protection - omniauth-oauth2 (1.7.2) - oauth2 (~> 1.4) - omniauth (>= 1.9, < 3) + omniauth-oauth2 (1.8.0) + oauth2 (>= 1.4, < 3) + omniauth (~> 2.0) omniauth-rails_csrf_protection (1.0.1) actionpack (>= 4.2) omniauth (~> 2.0) orm_adapter (0.5.0) paranoia (2.6.0) activerecord (>= 5.1, < 7.1) - pg (1.3.4) - pg (1.3.4-x64-mingw32) - public_suffix (4.0.6) - puma (4.3.11) + pg (1.4.3) + pg (1.4.3-x64-mingw32) + public_suffix (5.0.0) + puma (4.3.12) nio4r (~> 2.0) racc (1.6.0) - rack (2.2.3) - rack-protection (2.2.0) + rack (2.2.4) + rack-protection (3.0.1) rack - rack-test (1.1.0) - rack (>= 1.0, < 3) - rails (7.0.2.3) - actioncable (= 7.0.2.3) - actionmailbox (= 7.0.2.3) - actionmailer (= 7.0.2.3) - actionpack (= 7.0.2.3) - actiontext (= 7.0.2.3) - actionview (= 7.0.2.3) - activejob (= 7.0.2.3) - activemodel (= 7.0.2.3) - activerecord (= 7.0.2.3) - activestorage (= 7.0.2.3) - activesupport (= 7.0.2.3) + rack-test (2.0.2) + rack (>= 1.3) + rails (7.0.4) + actioncable (= 7.0.4) + actionmailbox (= 7.0.4) + actionmailer (= 7.0.4) + actionpack (= 7.0.4) + actiontext (= 7.0.4) + actionview (= 7.0.4) + activejob (= 7.0.4) + activemodel (= 7.0.4) + activerecord (= 7.0.4) + activestorage (= 7.0.4) + activesupport (= 7.0.4) bundler (>= 1.15.0) - railties (= 7.0.2.3) + railties (= 7.0.4) rails-dom-testing (2.0.3) activesupport (>= 4.2.0) nokogiri (>= 1.6) - rails-html-sanitizer (1.4.2) + rails-html-sanitizer (1.4.3) loofah (~> 2.3) - railties (7.0.2.3) - actionpack (= 7.0.2.3) - activesupport (= 7.0.2.3) + railties (7.0.4) + actionpack (= 7.0.4) + activesupport (= 7.0.4) method_source rake (>= 12.2) thor (~> 1.0) zeitwerk (~> 2.5) rake (13.0.6) - rb-fsevent (0.11.1) + rb-fsevent (0.11.2) rb-inotify (0.10.1) ffi (~> 1.0) - regexp_parser (2.2.1) + regexp_parser (2.6.0) responders (3.0.1) actionpack (>= 5.0) railties (>= 5.0) @@ -280,10 +265,11 @@ GEM sprockets (> 3.0) sprockets-rails tilt - selenium-webdriver (4.1.0) + selenium-webdriver (4.4.0) childprocess (>= 0.5, < 5.0) rexml (~> 3.2, >= 3.2.5) - rubyzip (>= 1.2.2) + rubyzip (>= 1.2.2, < 3.0) + websocket (~> 1.0) semantic-ui-sass (2.4.4.0) sassc shrine (3.4.0) @@ -295,33 +281,36 @@ GEM simplecov_json_formatter (~> 0.1) simplecov-html (0.12.3) simplecov_json_formatter (0.1.4) + snaky_hash (2.0.1) + hashie + version_gem (~> 1.1, >= 1.1.1) spring (2.1.1) spring-watcher-listen (2.0.1) listen (>= 2.7, < 4.0) spring (>= 1.2, < 3.0) - sprockets (4.0.3) + sprockets (4.1.1) concurrent-ruby (~> 1.0) rack (> 1, < 3) sprockets-rails (3.4.2) actionpack (>= 5.2) activesupport (>= 5.2) sprockets (>= 3.0.0) - ssrf_filter (1.0.7) - strscan (3.0.1) + ssrf_filter (1.1.1) thor (1.2.1) - tilt (2.0.10) - timeout (0.2.0) + tilt (2.0.11) + timeout (0.3.0) trix-rails (2.4.0) rails (> 4.1) turbolinks (5.2.1) turbolinks-source (~> 5.2) turbolinks-source (5.2.0) - tzinfo (2.0.4) + tzinfo (2.0.5) concurrent-ruby (~> 1.0) - tzinfo-data (1.2022.1) + tzinfo-data (1.2022.4) tzinfo (>= 1.0.0) uglifier (4.2.0) execjs (>= 0.3.0, < 3) + version_gem (1.1.1) warden (1.2.9) rack (>= 2.0.9) web-console (4.2.0) @@ -329,12 +318,13 @@ GEM activemodel (>= 6.0.0) bindex (>= 0.4.0) railties (>= 6.0.0) + websocket (1.2.9) websocket-driver (0.7.5) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) xpath (3.2.0) nokogiri (~> 1.8) - zeitwerk (2.5.4) + zeitwerk (2.6.0) PLATFORMS ruby @@ -347,7 +337,6 @@ DEPENDENCIES byebug capybara (>= 2.15) carrierwave (~> 1.0) - chromedriver-helper coffee-rails (~> 4.2) devise dotenv-rails diff --git a/config/application.rb b/config/application.rb index 2d3bff6..9ee2bd7 100644 --- a/config/application.rb +++ b/config/application.rb @@ -10,6 +10,7 @@ module Schorpong class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. # config.load_defaults 5.2 + config.active_record.legacy_connection_handling = false config.i18n.default_locale = :hu # Configuration for the application, engines, and railties goes here. diff --git a/config/initializers/oauth.rb b/config/initializers/oauth.rb index 63a6043..89f03a3 100644 --- a/config/initializers/oauth.rb +++ b/config/initializers/oauth.rb @@ -41,7 +41,7 @@ def raw_info session[:access_token] = access_token.token url = "/api/profile" params = {:params => { :access_token => access_token.token}} - @raw_info ||= MultiJson.decode(access_token.client.request(:get, url, params).body) + @raw_info ||= JSON.parse(access_token.client.request(:get, url, params).body) end end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index b6317b5..b0d9347 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -1,6 +1,12 @@ # This file is copied to spec/ when you run 'rails generate rspec:install' +# require 'spec_helper' ENV['RAILS_ENV'] ||= 'test' +if ENV['RAILS_ENV'] == 'test' + require 'simplecov' + SimpleCov.start 'rails' + puts "required simplecov" +end require_relative '../config/environment' # Prevent database truncation if the environment is production abort("The Rails environment is running in production mode!") if Rails.env.production? diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f9f2ba6..a0d4080 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,5 +1,3 @@ -require 'simplecov' -SimpleCov.start 'rails' # This file was generated by the `rails generate rspec:install` command. Conventionally, all # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # The generated `.rspec` file contains `--require spec_helper` which will cause From 2f70ed0fbe589c8ffb0abcab7b4a806cb9e7bd5d Mon Sep 17 00:00:00 2001 From: SepsiLaszlo Date: Thu, 29 Sep 2022 21:55:37 +0200 Subject: [PATCH 08/14] add factory_bot --- Gemfile | 2 ++ Gemfile.lock | 8 ++++++++ spec/rails_helper.rb | 12 +++++------- spec/support/factory_bot.rb | 3 +++ 4 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 spec/support/factory_bot.rb diff --git a/Gemfile b/Gemfile index ed63e8b..1025ac3 100644 --- a/Gemfile +++ b/Gemfile @@ -61,6 +61,8 @@ group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] gem 'rspec-rails' + gem 'factory_bot_rails' + gem 'timecop' end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index 9c27c57..eea5a66 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -117,6 +117,11 @@ GEM addressable (~> 2.8) erubi (1.11.0) execjs (2.8.1) + factory_bot (6.2.0) + activesupport (>= 5.0.0) + factory_bot_rails (6.2.0) + factory_bot (~> 6.2.0) + railties (>= 5.0.0) faraday (2.5.2) faraday-net_http (>= 2.0, < 3.1) ruby2_keywords (>= 0.0.4) @@ -298,6 +303,7 @@ GEM ssrf_filter (1.1.1) thor (1.2.1) tilt (2.0.11) + timecop (0.9.5) timeout (0.3.0) trix-rails (2.4.0) rails (> 4.1) @@ -340,6 +346,7 @@ DEPENDENCIES coffee-rails (~> 4.2) devise dotenv-rails + factory_bot_rails jbuilder (~> 2.9) jquery-rails listen @@ -359,6 +366,7 @@ DEPENDENCIES simplecov spring spring-watcher-listen (~> 2.0.0) + timecop trix-rails turbolinks (~> 5) tzinfo-data diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index b0d9347..a5e7636 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -1,18 +1,16 @@ # This file is copied to spec/ when you run 'rails generate rspec:install' -# +require 'simplecov' +SimpleCov.start 'rails' + require 'spec_helper' ENV['RAILS_ENV'] ||= 'test' -if ENV['RAILS_ENV'] == 'test' - require 'simplecov' - SimpleCov.start 'rails' - puts "required simplecov" -end + require_relative '../config/environment' # Prevent database truncation if the environment is production abort("The Rails environment is running in production mode!") if Rails.env.production? require 'rspec/rails' # Add additional requires below this line. Rails is not loaded until this point! - +require_relative './support/factory_bot' # Requires supporting ruby files with custom matchers and macros, etc, in # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are # run as spec files by default. This means that files in spec/support that end diff --git a/spec/support/factory_bot.rb b/spec/support/factory_bot.rb new file mode 100644 index 0000000..195f20a --- /dev/null +++ b/spec/support/factory_bot.rb @@ -0,0 +1,3 @@ +RSpec.configure do |config| + config.include FactoryBot::Syntax::Methods +end \ No newline at end of file From e6047da4783552399f661f9d0d2479d5f3adfcfa Mon Sep 17 00:00:00 2001 From: SepsiLaszlo Date: Thu, 29 Sep 2022 21:56:00 +0200 Subject: [PATCH 09/14] add test for RentNumberValidator --- app/models/rent.rb | 11 ++++++++- spec/factories/items.rb | 6 +++++ spec/factories/users.rb | 7 ++++++ spec/models/rent_spec.rb | 51 +++++++++++++++++++++++++++++++++++++++- 4 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 spec/factories/items.rb create mode 100644 spec/factories/users.rb diff --git a/app/models/rent.rb b/app/models/rent.rb index abc564e..8b757f6 100644 --- a/app/models/rent.rb +++ b/app/models/rent.rb @@ -3,11 +3,20 @@ class Rent < ApplicationRecord belongs_to :user enum state: [:unprocessed, :approved, :rejected, :taken, :returned], _default: :unprocessed - validates :begin, presence: true, comparison: { greater_than: DateTime.now, message: " később legyen, mint a mostani időpont." } + validates :begin, presence: true + validate :begin_cant_be_before_the_present validates :end, presence: true , comparison: { greater_than: :begin, message: " később legyen, mint a kezdete." } validates :number, presence: true, comparison: { greater_than: 0 } validates_with RentNumberValidator + + def begin_cant_be_before_the_present + if self.begin < Time.now + errors.add(:begin, "később legyen, mint a mostani időpont.") + end + end + + # #STATE MACHINE def approve! raise Exceptions::ForbiddenTransition unless unprocessed? diff --git a/spec/factories/items.rb b/spec/factories/items.rb new file mode 100644 index 0000000..3beed38 --- /dev/null +++ b/spec/factories/items.rb @@ -0,0 +1,6 @@ +FactoryBot.define do + factory :item do + name { 'pohár' } + number { 5 } + end +end diff --git a/spec/factories/users.rb b/spec/factories/users.rb new file mode 100644 index 0000000..6991a31 --- /dev/null +++ b/spec/factories/users.rb @@ -0,0 +1,7 @@ +FactoryBot.define do + factory :user do + name {'User'} + mail { 'user@email.com' } + password { 'secret1234' } + end +end diff --git a/spec/models/rent_spec.rb b/spec/models/rent_spec.rb index e384e8f..7bf8efe 100644 --- a/spec/models/rent_spec.rb +++ b/spec/models/rent_spec.rb @@ -1,5 +1,54 @@ require 'rails_helper' RSpec.describe Rent, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + let(:user) { create(:user) } + let(:item) { create(:item) } + + subject { Rent.new(state: :approved, item: item, user: user, + begin: DateTime.new(2022, 1, 10), + end: DateTime.new(2022, 1, 15), + number: 5) } + before(:each) do + Timecop.freeze(DateTime.new(2022, 1, 1)) + end + + after do + Timecop.return + end + it 'is valid when another rent will be taken after the end' do + Rent.create!(state: :approved, item: item, user: user, + begin: DateTime.new(2022, 1, 16), + end: DateTime.new(2022, 1, 17), + number: 5) + expect(subject).to be_valid + end + + it 'is invalid when another rent ends at the same day' do + Rent.create!(state: :approved, item: item, user: user, + begin: DateTime.new(2022, 1, 5), + end: DateTime.new(2022, 1, 10), + number: 5) + expect(subject).not_to be_valid + end + it 'is invalid when another rent begins at the same day' do + Rent.create!(state: :approved, item: item, user: user, + begin: DateTime.new(2022, 1, 15), + end: DateTime.new(2022, 1, 16), + number: 5) + expect(subject).not_to be_valid + end + it 'is invaild when another rent is at the same time' do + Rent.create!(state: :approved, item: item, user: user, + begin: DateTime.new(2022, 1, 10), + end: DateTime.new(2022, 1, 15), + number: 5) + expect(subject).not_to be_valid + end + it 'when another rent is begins before and ends after thisone' do + Rent.create!(state: :approved, item: item, user: user, + begin: DateTime.new(2022, 1, 9), + end: DateTime.new(2022, 1, 16), + number: 5) + expect(subject).not_to be_valid + end end From 9f746ffcb61e96696215992474c5ae6451f9b112 Mon Sep 17 00:00:00 2001 From: SepsiLaszlo Date: Thu, 29 Sep 2022 22:04:10 +0200 Subject: [PATCH 10/14] Revert "refactor to sum" This reverts commit b7a6184b3de8d58a22efc049744915f2dd0e30fb. --- app/validators/rent_number_validator.rb | 45 ++++++++++--------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/app/validators/rent_number_validator.rb b/app/validators/rent_number_validator.rb index 9df88da..fbb73d7 100644 --- a/app/validators/rent_number_validator.rb +++ b/app/validators/rent_number_validator.rb @@ -1,33 +1,24 @@ class RentNumberValidator < ActiveModel::Validator - def validate(record) - #Rent.where("begin < ? && end > ?", record.end, record.begin).where.not(state: :unprocessed).sum(:number) - return if record.number.nil? || record.begin.nil? || record.end.nil? - - rent_sum = rents.sum do | rent| - return unless rent.begin < record.end - return unless rent.end > record.begin - return unless ['unprocessed', 'rejected'].include?(rent.state) - rent.number - end - - if rent_sum + record.number > record.item.number - record.errors.add :base, "Erre az időpontra sajnos nincs elég a kívánt eszközből." - record.errors.add :base, "A megadott napokra elérhető eszközök száma:" - day = record.begin.to_date - while (day <= record.end.to_date) + def validate(record) + #Rent.where("begin < ? && end > ?", record.end, record.begin).where.not(state: :unprocessed).sum(:number) + return if record.number.nil? || record.begin.nil? || record.end.nil? sum = 0 + rents = Rent.all rents.each do |rent| - sum += rent.number if (rent.begin.to_date <= day && rent.end.to_date >= day && rent.state != "unprocessed") + sum += rent.number if(rent.begin < record.end && rent.end > record.begin && rent.state != "unprocessed") + end + if sum+record.number > record.item.number + record.errors.add :base, "Erre az időpontra sajnos nincs elég a kívánt eszközből." + record.errors.add :base, "A megadott napokra elérhető eszközök száma:" + day = record.begin.to_date + while(day <= record.end.to_date) + sum = 0 + rents.each do |rent| + sum += rent.number if(rent.begin.to_date <= day && rent.end.to_date >= day && rent.state != "unprocessed") + end + record.errors.add :base, day.to_s + ": " + (record.item.number - sum).to_s + " darab" + day += 1 + end end - record.errors.add :base, day.to_s + ": " + (record.item.number - sum).to_s + " darab" - day += 1 - end end - end - - private - - def rents - @rents ||= Rent.all - end end From 2d5d4e388efa9a3c2c6d3fb44682997f7544cd75 Mon Sep 17 00:00:00 2001 From: SepsiLaszlo Date: Fri, 30 Sep 2022 15:14:01 +0200 Subject: [PATCH 11/14] add test for RentNumberValidator --- spec/models/rent_spec.rb | 83 ++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 37 deletions(-) diff --git a/spec/models/rent_spec.rb b/spec/models/rent_spec.rb index 7bf8efe..81c42b7 100644 --- a/spec/models/rent_spec.rb +++ b/spec/models/rent_spec.rb @@ -3,11 +3,24 @@ RSpec.describe Rent, type: :model do let(:user) { create(:user) } let(:item) { create(:item) } + let(:pre_existing_rent) do + Rent.create!(state: :approved, item: item, user: user, + begin: DateTime.new(2022, 1, 10), + end: DateTime.new(2022, 1, 15), + number: 5) + end + let(:current_rent) do + Rent.new(state: :approved, item: item, user: user, + begin: begin_date, + end: end_date, + number: 5) + end + + subject do + pre_existing_rent + current_rent + end - subject { Rent.new(state: :approved, item: item, user: user, - begin: DateTime.new(2022, 1, 10), - end: DateTime.new(2022, 1, 15), - number: 5) } before(:each) do Timecop.freeze(DateTime.new(2022, 1, 1)) end @@ -15,40 +28,36 @@ after do Timecop.return end - it 'is valid when another rent will be taken after the end' do - Rent.create!(state: :approved, item: item, user: user, - begin: DateTime.new(2022, 1, 16), - end: DateTime.new(2022, 1, 17), - number: 5) - expect(subject).to be_valid - end - it 'is invalid when another rent ends at the same day' do - Rent.create!(state: :approved, item: item, user: user, - begin: DateTime.new(2022, 1, 5), - end: DateTime.new(2022, 1, 10), - number: 5) - expect(subject).not_to be_valid + context 'when the rent starts before the pre existing rent' do + let(:begin_date) { DateTime.new(2022, 1, 5) } + context 'and ends before the current rent' do + let(:end_date) { DateTime.new(2022, 1, 9) } + it { expect(subject).to be_valid } + end + context 'and ends on the current rent beginning date' do + let(:end_date) { DateTime.new(2022, 1, 10) } + it { expect(subject).to be_valid } + end + context 'and ends after the current_rent is take' do + let(:end_date) { DateTime.new(2022, 1, 11) } + it { expect(subject).not_to be_valid } + end end - it 'is invalid when another rent begins at the same day' do - Rent.create!(state: :approved, item: item, user: user, - begin: DateTime.new(2022, 1, 15), - end: DateTime.new(2022, 1, 16), - number: 5) - expect(subject).not_to be_valid - end - it 'is invaild when another rent is at the same time' do - Rent.create!(state: :approved, item: item, user: user, - begin: DateTime.new(2022, 1, 10), - end: DateTime.new(2022, 1, 15), - number: 5) - expect(subject).not_to be_valid - end - it 'when another rent is begins before and ends after thisone' do - Rent.create!(state: :approved, item: item, user: user, - begin: DateTime.new(2022, 1, 9), - end: DateTime.new(2022, 1, 16), - number: 5) - expect(subject).not_to be_valid + + context 'when the rent ends after the pre existing rent' do + let(:end_date) { DateTime.new(2022, 1, 20) } + context 'and begins before the current rent is back' do + let(:begin_date) { DateTime.new(2022, 1, 14) } + it { expect(subject).not_to be_valid } + end + context 'and begins whe the current rent is back' do + let(:begin_date) { DateTime.new(2022, 1, 15) } + it { expect(subject).to be_valid } + end + context 'and begins after the current rent is back' do + let(:begin_date) { DateTime.new(2022, 1, 16) } + it { expect(subject).to be_valid } + end end end From 16a9416dab3b37ee92aedb18eecfcc48e2762220 Mon Sep 17 00:00:00 2001 From: SepsiLaszlo Date: Fri, 30 Sep 2022 17:42:31 +0200 Subject: [PATCH 12/14] refactor RentNumberValidator --- app/models/rent.rb | 4 ++ app/validators/rent_number_validator.rb | 61 +++++++++++++-------- spec/models/rent_spec.rb | 72 ++++++++++++++++--------- 3 files changed, 92 insertions(+), 45 deletions(-) diff --git a/app/models/rent.rb b/app/models/rent.rb index 8b757f6..dfaf23c 100644 --- a/app/models/rent.rb +++ b/app/models/rent.rb @@ -53,5 +53,9 @@ def self.users_filter(params, id) def item Item.unscoped { super } end + + def days + (self.begin.to_date.. self.end.to_date).to_a + end end diff --git a/app/validators/rent_number_validator.rb b/app/validators/rent_number_validator.rb index fbb73d7..4d4110e 100644 --- a/app/validators/rent_number_validator.rb +++ b/app/validators/rent_number_validator.rb @@ -1,24 +1,43 @@ +# Validators are instantiated once, instance variables persist between validations, might cause bugs class RentNumberValidator < ActiveModel::Validator - def validate(record) - #Rent.where("begin < ? && end > ?", record.end, record.begin).where.not(state: :unprocessed).sum(:number) - return if record.number.nil? || record.begin.nil? || record.end.nil? - sum = 0 - rents = Rent.all - rents.each do |rent| - sum += rent.number if(rent.begin < record.end && rent.end > record.begin && rent.state != "unprocessed") - end - if sum+record.number > record.item.number - record.errors.add :base, "Erre az időpontra sajnos nincs elég a kívánt eszközből." - record.errors.add :base, "A megadott napokra elérhető eszközök száma:" - day = record.begin.to_date - while(day <= record.end.to_date) - sum = 0 - rents.each do |rent| - sum += rent.number if(rent.begin.to_date <= day && rent.end.to_date >= day && rent.state != "unprocessed") - end - record.errors.add :base, day.to_s + ": " + (record.item.number - sum).to_s + " darab" - day += 1 - end - end + def validate(record) + item = record.item + active_rents = active_rents(record) + rent_days = rent_days(active_rents) + rents_by_item = rents_by_item(rent_days, active_rents) + + record.days.each do |day| + rent_number_for_day = rents_by_item.dig(day) || 0 + if rent_number_for_day + record.number > item.number + available_item_number = item.number - rent_number_for_day + record.errors.add :base, "A kiválasztott napon(#{day}) csak #{available_item_number} darab #{item.name} elérhető." + end end + end + + private + + # hash[rent_day] = sum taken out amount item (per day) + def rents_by_item(rent_days, active_rents) + result = {} + rent_days.each do |rent_date| + result[rent_date] = 0 if result[rent_date].nil? + active_rents.each do |active_rent| + result[rent_date] += active_rent.number + end + end + result + end + + def rent_days(active_rents) + active_rents.map(&:days).flatten.uniq.sort + end + + def active_rents(record) + Rent.includes(:item) + .where(item: record.item) + .where(state: [:approved, :taken]) + .where.not('rents.end <= :begin or :end <= rents.begin', + { begin: record.begin, end: record.end }) + end end diff --git a/spec/models/rent_spec.rb b/spec/models/rent_spec.rb index 81c42b7..1366281 100644 --- a/spec/models/rent_spec.rb +++ b/spec/models/rent_spec.rb @@ -2,7 +2,7 @@ RSpec.describe Rent, type: :model do let(:user) { create(:user) } - let(:item) { create(:item) } + let(:item) { create(:item, number: 10) } let(:pre_existing_rent) do Rent.create!(state: :approved, item: item, user: user, begin: DateTime.new(2022, 1, 10), @@ -13,7 +13,7 @@ Rent.new(state: :approved, item: item, user: user, begin: begin_date, end: end_date, - number: 5) + number: rent_number) end subject do @@ -29,34 +29,58 @@ Timecop.return end - context 'when the rent starts before the pre existing rent' do - let(:begin_date) { DateTime.new(2022, 1, 5) } - context 'and ends before the current rent' do - let(:end_date) { DateTime.new(2022, 1, 9) } - it { expect(subject).to be_valid } + context 'when the rent number equals to all available item number' do + let(:rent_number) { 10 } + context 'when the rent starts before the pre existing rent' do + let(:begin_date) { DateTime.new(2022, 1, 5) } + context 'and ends before the current rent' do + let(:end_date) { DateTime.new(2022, 1, 9) } + it { expect(subject).to be_valid } + end + context 'and ends on the current rent beginning date' do + let(:end_date) { DateTime.new(2022, 1, 10) } + it { expect(subject).to be_valid } + end + context 'and ends after the current_rent is take' do + let(:end_date) { DateTime.new(2022, 1, 11) } + it { + expect(subject).not_to be_valid + } + end end - context 'and ends on the current rent beginning date' do - let(:end_date) { DateTime.new(2022, 1, 10) } - it { expect(subject).to be_valid } + + context 'when the rent ends after the pre existing rent' do + let(:end_date) { DateTime.new(2022, 1, 20) } + context 'and begins before the current rent is back' do + let(:begin_date) { DateTime.new(2022, 1, 14) } + it { expect(subject).not_to be_valid } + end + context 'and begins whe the current rent is back' do + let(:begin_date) { DateTime.new(2022, 1, 15) } + it { expect(subject).to be_valid } + end + context 'and begins after the current rent is back' do + let(:begin_date) { DateTime.new(2022, 1, 16) } + it { expect(subject).to be_valid } + end end - context 'and ends after the current_rent is take' do - let(:end_date) { DateTime.new(2022, 1, 11) } + context 'and begins and ends during the current rent' do + let(:begin_date) { DateTime.new(2022, 1, 11) } + let(:end_date) { DateTime.new(2022, 1, 14) } it { expect(subject).not_to be_valid } end - end - - context 'when the rent ends after the pre existing rent' do - let(:end_date) { DateTime.new(2022, 1, 20) } - context 'and begins before the current rent is back' do - let(:begin_date) { DateTime.new(2022, 1, 14) } + context 'and begins and ends at the same time as the current rent' do + let(:begin_date) { DateTime.new(2022, 1, 10) } + let(:end_date) { DateTime.new(2022, 1, 15) } it { expect(subject).not_to be_valid } end - context 'and begins whe the current rent is back' do - let(:begin_date) { DateTime.new(2022, 1, 15) } - it { expect(subject).to be_valid } - end - context 'and begins after the current rent is back' do - let(:begin_date) { DateTime.new(2022, 1, 16) } + end + + context 'when the rent number is less then all available item number' do + let(:rent_number) { 1 } + context 'and begins and ends at the same time as the current rent' do + let(:begin_date) { DateTime.new(2022, 1, 10) } + let(:end_date) { DateTime.new(2022, 1, 15) } it { expect(subject).to be_valid } end end From 0e07b34003b7d9e0354ea815d7ac2cefced159b6 Mon Sep 17 00:00:00 2001 From: SepsiLaszlo Date: Wed, 19 Oct 2022 17:57:00 +0200 Subject: [PATCH 13/14] refactor RentNumberValidator to intervals --- app/models/rent.rb | 4 +- app/validators/rent_number_validator.rb | 63 ++++++++++++++++--------- spec/models/rent_spec.rb | 62 ++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 23 deletions(-) diff --git a/app/models/rent.rb b/app/models/rent.rb index dfaf23c..62e36e5 100644 --- a/app/models/rent.rb +++ b/app/models/rent.rb @@ -54,8 +54,8 @@ def item Item.unscoped { super } end - def days - (self.begin.to_date.. self.end.to_date).to_a + def interval + self.begin..self.end end end diff --git a/app/validators/rent_number_validator.rb b/app/validators/rent_number_validator.rb index 4d4110e..543a736 100644 --- a/app/validators/rent_number_validator.rb +++ b/app/validators/rent_number_validator.rb @@ -1,43 +1,64 @@ # Validators are instantiated once, instance variables persist between validations, might cause bugs class RentNumberValidator < ActiveModel::Validator - def validate(record) - item = record.item - active_rents = active_rents(record) - rent_days = rent_days(active_rents) - rents_by_item = rents_by_item(rent_days, active_rents) - - record.days.each do |day| - rent_number_for_day = rents_by_item.dig(day) || 0 - if rent_number_for_day + record.number > item.number - available_item_number = item.number - rent_number_for_day - record.errors.add :base, "A kiválasztott napon(#{day}) csak #{available_item_number} darab #{item.name} elérhető." + def validate(rent) + active_rents = active_rents(rent) + intervals = intervals(rent, active_rents) + sum_items_in_intervals = sum_items_in_intervals(intervals, active_rents) + + sum_items_in_intervals.each do |interval, sum_items| + available_item_number = rent.item.number - sum_items + if available_item_number < rent.number + rent.errors.add :base, "A kiválasztott időszakban (#{(interval)}) csak #{available_item_number} darab #{rent.item.name} elérhető." end end end private - # hash[rent_day] = sum taken out amount item (per day) - def rents_by_item(rent_days, active_rents) + def formatted_interval(interval) + "#{formatted_date(interval.begin)} - #{formatted_date(interval.end)}" + end + + def formatted_date(date) + date.strftime("%Y-%m-%d %H:%M") + end + def sum_items_in_intervals(intervals, active_rents) result = {} - rent_days.each do |rent_date| - result[rent_date] = 0 if result[rent_date].nil? + intervals.each do |interval| + sum_item_number = 0 active_rents.each do |active_rent| - result[rent_date] += active_rent.number + if overlaps?(interval, active_rent.interval) + sum_item_number += active_rent.number + end end + result[formatted_interval(interval)] = sum_item_number end result end - def rent_days(active_rents) - active_rents.map(&:days).flatten.uniq.sort + def overlaps?(range1, range2) + !(range2.end <= range1.begin || range1.end <= range2.begin) + end + + def intervals(rent, active_rents) + time_points = active_rents.map { |active_rent| [active_rent.begin, active_rent.end] }.flatten + time_points = time_points.select { |time_point| time_point.between?(rent.begin, rent.end) } + time_points.push(rent.begin, rent.end) + time_points = time_points.uniq.sort + intervals = time_points.map.with_index do |time_point, index| + next_time_point = time_points[index + 1] + next unless next_time_point + + (time_point..next_time_point) + end.compact + intervals end - def active_rents(record) + def active_rents(rent) Rent.includes(:item) - .where(item: record.item) + .where(item: rent.item) .where(state: [:approved, :taken]) .where.not('rents.end <= :begin or :end <= rents.begin', - { begin: record.begin, end: record.end }) + { begin: rent.begin, end: rent.end }) end end diff --git a/spec/models/rent_spec.rb b/spec/models/rent_spec.rb index 1366281..0a1e7db 100644 --- a/spec/models/rent_spec.rb +++ b/spec/models/rent_spec.rb @@ -37,10 +37,12 @@ let(:end_date) { DateTime.new(2022, 1, 9) } it { expect(subject).to be_valid } end + context 'and ends on the current rent beginning date' do let(:end_date) { DateTime.new(2022, 1, 10) } it { expect(subject).to be_valid } end + context 'and ends after the current_rent is take' do let(:end_date) { DateTime.new(2022, 1, 11) } it { @@ -55,20 +57,24 @@ let(:begin_date) { DateTime.new(2022, 1, 14) } it { expect(subject).not_to be_valid } end + context 'and begins whe the current rent is back' do let(:begin_date) { DateTime.new(2022, 1, 15) } it { expect(subject).to be_valid } end + context 'and begins after the current rent is back' do let(:begin_date) { DateTime.new(2022, 1, 16) } it { expect(subject).to be_valid } end end + context 'and begins and ends during the current rent' do let(:begin_date) { DateTime.new(2022, 1, 11) } let(:end_date) { DateTime.new(2022, 1, 14) } it { expect(subject).not_to be_valid } end + context 'and begins and ends at the same time as the current rent' do let(:begin_date) { DateTime.new(2022, 1, 10) } let(:end_date) { DateTime.new(2022, 1, 15) } @@ -84,4 +90,60 @@ it { expect(subject).to be_valid } end end + + context "when a rent overlaps two existing rents but their is enough item" do + let(:rent_1) do + Rent.new(state: :approved, item: item, user: user, + begin: DateTime.new.change(year: 2022, month: 1, day: 5, hour: 12), + end: DateTime.new.change(year: 2022, month: 1, day: 10, hour: 12), + number: 5) + end + let(:rent_2) do + Rent.new(state: :approved, item: item, user: user, + begin: DateTime.new.change(year: 2022, month: 1, day: 10, hour: 14), + end: DateTime.new.change(year: 2022, month: 1, day: 15, hour: 12), + number: 5) + end + + let(:current_rent) do + Rent.new(state: :approved, item: item, user: user, + begin: DateTime.new.change(year: 2022, month: 1, day: 8, hour: 14), + end: DateTime.new.change(year: 2022, month: 1, day: 12, hour: 12), + number: 5) + end + it 'should be valid' do + rent_1.save! + rent_2.save! + expect(current_rent).to be_valid + end + end + + context "when a rent overlaps two existing rents but their is not enough item" do + let(:rent_1) do + Rent.new(state: :approved, item: item, user: user, + begin: DateTime.new.change(year: 2022, month: 1, day: 5, hour: 12), + end: DateTime.new.change(year: 2022, month: 1, day: 10, hour: 12), + number: 5) + end + + let(:rent_2) do + Rent.new(state: :approved, item: item, user: user, + begin: DateTime.new.change(year: 2022, month: 1, day: 10, hour: 14), + end: DateTime.new.change(year: 2022, month: 1, day: 15, hour: 12), + number: 5) + end + + let(:current_rent) do + Rent.new(state: :approved, item: item, user: user, + begin: DateTime.new.change(year: 2022, month: 1, day: 8, hour: 14), + end: DateTime.new.change(year: 2022, month: 1, day: 12, hour: 12), + number: 10) + end + + it 'should be invalid' do + rent_1.save! + rent_2.save! + expect(current_rent).not_to be_valid + end + end end From c54bd0dbdcfa37ef08e41a13988b1af9a19b468f Mon Sep 17 00:00:00 2001 From: Andai Roland Date: Sat, 5 Nov 2022 14:01:02 +0100 Subject: [PATCH 14/14] new test cases added --- spec/models/rent_spec.rb | 56 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/spec/models/rent_spec.rb b/spec/models/rent_spec.rb index 0a1e7db..d50d70b 100644 --- a/spec/models/rent_spec.rb +++ b/spec/models/rent_spec.rb @@ -146,4 +146,60 @@ expect(current_rent).not_to be_valid end end + + context "when a rent overlaps two existing rents ending at the same time but their is enough item" do + let(:rent_1) do + Rent.new(state: :approved, item: item, user: user, + begin: DateTime.new.change(year: 2022, month: 1, day: 5, hour: 12), + end: DateTime.new.change(year: 2022, month: 1, day: 10, hour: 12), + number: 3) + end + let(:rent_2) do + Rent.new(state: :approved, item: item, user: user, + begin: DateTime.new.change(year: 2022, month: 1, day: 7, hour: 12), + end: DateTime.new.change(year: 2022, month: 1, day: 10, hour: 12), + number: 3) + end + + let(:current_rent) do + Rent.new(state: :approved, item: item, user: user, + begin: DateTime.new.change(year: 2022, month: 1, day: 3, hour: 12), + end: DateTime.new.change(year: 2022, month: 1, day: 12, hour: 12), + number: 4) + end + it 'should be valid' do + rent_1.save! + rent_2.save! + expect(current_rent).to be_valid + end + end + + context "when a rent overlaps two existing rents ending at the same time but their is not enough item" do + let(:rent_1) do + Rent.new(state: :approved, item: item, user: user, + begin: DateTime.new.change(year: 2022, month: 1, day: 5, hour: 12), + end: DateTime.new.change(year: 2022, month: 1, day: 10, hour: 12), + number: 3) + end + + let(:rent_2) do + Rent.new(state: :approved, item: item, user: user, + begin: DateTime.new.change(year: 2022, month: 1, day: 7, hour: 12), + end: DateTime.new.change(year: 2022, month: 1, day: 10, hour: 12), + number: 3) + end + + let(:current_rent) do + Rent.new(state: :approved, item: item, user: user, + begin: DateTime.new.change(year: 2022, month: 1, day: 3, hour: 12), + end: DateTime.new.change(year: 2022, month: 1, day: 12, hour: 12), + number: 5) + end + + it 'should not be valid' do + rent_1.save! + rent_2.save! + expect(current_rent).not_to be_valid + end + end end