From d47a0ca274a6165e09fac63b4daa5480ac1d88a3 Mon Sep 17 00:00:00 2001 From: Andrew Nicholson Date: Fri, 14 May 2021 09:17:08 -0400 Subject: [PATCH 1/3] add rspec and add done column to todos --- .rspec | 1 + Gemfile | 2 + Gemfile.lock | 15 +++ app/controllers/todos_controller.rb | 4 + .../20210514131422_add_done_to_todos.rb | 5 + db/schema.rb | 3 +- spec/spec_helper.rb | 100 ++++++++++++++++++ 7 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 .rspec create mode 100644 db/migrate/20210514131422_add_done_to_todos.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 d60054e..9b73ce1 100644 --- a/Gemfile +++ b/Gemfile @@ -48,3 +48,5 @@ end gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] gem "faker", "~> 2.17" + +gem "rspec", "~> 3.10" diff --git a/Gemfile.lock b/Gemfile.lock index d5ec1a2..2c35166 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -67,6 +67,7 @@ GEM byebug (11.1.3) concurrent-ruby (1.1.8) crass (1.0.6) + diff-lcs (1.4.4) erubi (1.10.0) faker (2.17.0) i18n (>= 1.6, < 2) @@ -134,6 +135,19 @@ GEM rb-fsevent (0.11.0) rb-inotify (0.10.1) ffi (~> 1.0) + rspec (3.10.0) + rspec-core (~> 3.10.0) + rspec-expectations (~> 3.10.0) + rspec-mocks (~> 3.10.0) + rspec-core (3.10.1) + rspec-support (~> 3.10.0) + rspec-expectations (3.10.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.10.0) + rspec-mocks (3.10.2) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.10.0) + rspec-support (3.10.2) sass-rails (6.0.0) sassc-rails (~> 2.1, >= 2.1.1) sassc (2.4.0) @@ -188,6 +202,7 @@ DEPENDENCIES puma (~> 5.0) rack-mini-profiler (~> 2.0) rails (~> 6.1.3, >= 6.1.3.2) + rspec (~> 3.10) sass-rails (>= 6) spring turbolinks (~> 5) diff --git a/app/controllers/todos_controller.rb b/app/controllers/todos_controller.rb index e1e7dbd..5c60c52 100644 --- a/app/controllers/todos_controller.rb +++ b/app/controllers/todos_controller.rb @@ -56,6 +56,10 @@ def destroy end end + def reporting + + end + private # Use callbacks to share common setup or constraints between actions. def set_todo diff --git a/db/migrate/20210514131422_add_done_to_todos.rb b/db/migrate/20210514131422_add_done_to_todos.rb new file mode 100644 index 0000000..8975e8b --- /dev/null +++ b/db/migrate/20210514131422_add_done_to_todos.rb @@ -0,0 +1,5 @@ +class AddDoneToTodos < ActiveRecord::Migration[6.1] + def change + add_column :todos, :done, :boolean + end +end diff --git a/db/schema.rb b/db/schema.rb index 4c75453..9d89e2c 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.define(version: 2021_05_13_223853) do +ActiveRecord::Schema.define(version: 2021_05_14_131422) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -20,6 +20,7 @@ t.integer "index" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.boolean "done" end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..251aa51 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,100 @@ +# This file was generated by the `rspec --init` 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 http://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: + # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ + # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode + config.disable_monkey_patching! + + # This setting enables warnings. It's recommended, but in some cases may + # be too noisy due to issues in dependencies. + config.warnings = true + + # 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 8755054a796cb7483e161b4f07949f0d5d93632b Mon Sep 17 00:00:00 2001 From: Andrew Nicholson Date: Fri, 14 May 2021 09:33:48 -0400 Subject: [PATCH 2/3] reporting on todos --- .rspec | 2 +- Gemfile | 2 + Gemfile.lock | 9 ++++ app/controllers/todos_controller.rb | 7 ++- spec/controllers/todos_controller_spec.rb | 19 +++++++ spec/rails_helper.rb | 64 +++++++++++++++++++++++ spec/spec_helper.rb | 6 +-- 7 files changed, 102 insertions(+), 7 deletions(-) create mode 100644 spec/controllers/todos_controller_spec.rb create mode 100644 spec/rails_helper.rb diff --git a/.rspec b/.rspec index c99d2e7..a35c44f 100644 --- a/.rspec +++ b/.rspec @@ -1 +1 @@ ---require spec_helper +--require rails_helper diff --git a/Gemfile b/Gemfile index 9b73ce1..64716d2 100644 --- a/Gemfile +++ b/Gemfile @@ -50,3 +50,5 @@ gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] gem "faker", "~> 2.17" gem "rspec", "~> 3.10" + +gem "rspec-rails", "~> 5.0" diff --git a/Gemfile.lock b/Gemfile.lock index 2c35166..417a8cd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -147,6 +147,14 @@ GEM rspec-mocks (3.10.2) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.10.0) + rspec-rails (5.0.1) + 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.10.2) sass-rails (6.0.0) sassc-rails (~> 2.1, >= 2.1.1) @@ -203,6 +211,7 @@ DEPENDENCIES rack-mini-profiler (~> 2.0) rails (~> 6.1.3, >= 6.1.3.2) rspec (~> 3.10) + rspec-rails (~> 5.0) sass-rails (>= 6) spring turbolinks (~> 5) diff --git a/app/controllers/todos_controller.rb b/app/controllers/todos_controller.rb index 5c60c52..96226a0 100644 --- a/app/controllers/todos_controller.rb +++ b/app/controllers/todos_controller.rb @@ -57,7 +57,12 @@ def destroy end def reporting - + num_done = Todo.where(done: true).count + num_not_done = Todo.where(done: false).count + + respond_to do |f| + f.json { render json: { done: num_done, num_not_done: num_not_done } } + end end private diff --git a/spec/controllers/todos_controller_spec.rb b/spec/controllers/todos_controller_spec.rb new file mode 100644 index 0000000..0f0797c --- /dev/null +++ b/spec/controllers/todos_controller_spec.rb @@ -0,0 +1,19 @@ +describe TodosController, type: :controller do + describe '#reporting' do + let(:done_todos) do + rand(1..10).times do + Todo.create(done: true) + end + end + let(:not_done_todos) do + rand(1..10).times do + Todo.create + end + end + + it 'responds with number of done and not done todos' do + get :reporting, params: { format: 'json' } + expect(response.body).to eql({ done: done_todos, num_not_done: not_done_todos }.to_json) + end + end +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 0000000..00345af --- /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 File.expand_path('../config/environment', __dir__) +# 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 index 251aa51..ce33d66 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,4 +1,4 @@ -# This file was generated by the `rspec --init` command. Conventionally, all +# 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 @@ -66,10 +66,6 @@ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode config.disable_monkey_patching! - # This setting enables warnings. It's recommended, but in some cases may - # be too noisy due to issues in dependencies. - config.warnings = true - # 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. From 09b0562fcbd605614ad771c39ce4c79dae9478cb Mon Sep 17 00:00:00 2001 From: Yura Kuzhiy Date: Thu, 17 Nov 2022 14:00:27 +0200 Subject: [PATCH 3/3] Use sqlite --- .ruby-version | 2 +- Gemfile | 2 +- Gemfile.lock | 12 ++++++++---- app_test | Bin 0 -> 28672 bytes config/database.yml | 11 +++-------- 5 files changed, 13 insertions(+), 14 deletions(-) create mode 100644 app_test diff --git a/.ruby-version b/.ruby-version index 45418de..4efbd8f 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -ruby-3.0.1 +ruby-3.0.2 diff --git a/Gemfile b/Gemfile index 0f1c3fd..a554ee4 100644 --- a/Gemfile +++ b/Gemfile @@ -6,7 +6,7 @@ ruby '3.0.2' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main' gem 'rails', '~> 6.1.3', '>= 6.1.3.2' # Use postgresql as the database for Active Record -gem 'pg', '~> 1.1' +gem 'sqlite3' # Use Puma as the app server gem 'puma', '~> 5.0' # Use SCSS for stylesheets diff --git a/Gemfile.lock b/Gemfile.lock index 417a8cd..b277f6f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -92,9 +92,10 @@ GEM minitest (5.14.4) msgpack (1.4.2) nio4r (2.5.7) + nokogiri (1.11.3-x86_64-darwin) + racc (~> 1.4) nokogiri (1.11.3-x86_64-linux) racc (~> 1.4) - pg (1.2.3) puma (5.3.1) nio4r (~> 2.0) racc (1.5.2) @@ -175,6 +176,8 @@ GEM actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) + sqlite3 (1.5.3-x86_64-darwin) + sqlite3 (1.5.3-x86_64-linux) thor (1.1.0) tilt (2.0.10) turbolinks (5.2.1) @@ -198,6 +201,7 @@ GEM zeitwerk (2.4.2) PLATFORMS + x86_64-darwin-21 x86_64-linux DEPENDENCIES @@ -206,7 +210,6 @@ DEPENDENCIES faker (~> 2.17) jbuilder (~> 2.7) listen (~> 3.3) - pg (~> 1.1) puma (~> 5.0) rack-mini-profiler (~> 2.0) rails (~> 6.1.3, >= 6.1.3.2) @@ -214,13 +217,14 @@ DEPENDENCIES rspec-rails (~> 5.0) sass-rails (>= 6) spring + sqlite3 turbolinks (~> 5) tzinfo-data web-console (>= 4.1.0) webpacker (~> 5.0) RUBY VERSION - ruby 3.0.1p64 + ruby 3.0.2p107 BUNDLED WITH - 2.2.17 + 2.2.22 diff --git a/app_test b/app_test new file mode 100644 index 0000000000000000000000000000000000000000..bc22541a0a509264e3f464a5cba686d39f2bc935 GIT binary patch literal 28672 zcmeI)Jx|*}7zc3MfuvLrhf0-~N$MUAH)51C z&!Jo=;`)sHEtW4Adi_O{laEQo=q82N>9qFFk|jUz*jaqs6pK2Cp75}J?hLzK#~2pP zoTW0_;-Y;1yv-B4N32DimVX#g?uS8qBQQ1ybE7^{Ib~5SI`?w!W!5iKu*cHc-r}ef zZVc=gH$vlLmOM&k%8ruOW@qL7P7WQ7m>r+6z-3*WKc}gI`~RmhAJ7(?6HN=}dvZq8 zH2KR!drTgBVcag1f&O|smgMYvI!R{MA&dI6B=%l?du>Z}+N^Df>Cv4c;t_M7HwUIg z`_c6Vxtn<+M)ATxT&59*V)6%tse~(+6xbjD0SG_<0uX=z1Rwwb2tWV=5O}BpSpPrN zCyTrw009U<00Izz00bZa0SG_<0)GXP^}nM2lGH0vV1obzAOHafKmY;|fB*y_009U< z;NA)36s1rqmW-lhn5B|gwalE{i!2$Yu~aHO7Fj9kg`{4nSNASK+%NR^{=Tr?$<4Q$GrW77I9MF&{osscKrIYdYWmFD3O_ uy}Ug`)ItCP5P$##AOHafKmY;|fB*y_@ZSoksTsNNt%Q - host: host.docker.internal - port: 5432 - username: postgres + timeout: 5000 development: <<: *default - database: app_development + database: db/development.sqlite3 # The specified database role being used to connect to postgres. # To create additional roles in postgres see `$ createuser --help`.