From 91838e4fb3b2eaf980c11fc2d7c76676de609afc Mon Sep 17 00:00:00 2001 From: Felipe Freitag Vargas Date: Fri, 26 Jun 2020 11:52:01 -0300 Subject: [PATCH 1/7] Try to test issue with optional dependencies --- spec/todos/notes/index_spec.rb | 42 +++++++++++++++++++ spec/todos/notes/notes_spec.rb | 10 +++++ todos/app/resources/notes/model.rb | 13 ++++++ todos/app/resources/notes/resource.rb | 7 ++++ todos/config/initializers/croods.rb | 1 + .../db/migrate/20200626135719_create_notes.rb | 11 +++++ todos/db/schema.rb | 10 ++++- 7 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 spec/todos/notes/index_spec.rb create mode 100644 spec/todos/notes/notes_spec.rb create mode 100644 todos/app/resources/notes/model.rb create mode 100644 todos/app/resources/notes/resource.rb create mode 100644 todos/db/migrate/20200626135719_create_notes.rb diff --git a/spec/todos/notes/index_spec.rb b/spec/todos/notes/index_spec.rb new file mode 100644 index 0000000..d95631c --- /dev/null +++ b/spec/todos/notes/index_spec.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe 'GET /notes', type: :request do + subject { response } + + let(:project) do + current_user.projects.create! name: 'Foo' + end + + let(:list) do + project.lists.create! name: 'Foo' + end + + let(:task) do + list.tasks.create! name: 'Foo' + end + + let(:assignment) do + task.assignments.create! user: current_user + end + + let(:note) do + list.notes.create!(text: 'foo', assignment: assignment) + end + + let(:notes) do + list.notes.order(:created_at) + end + + context 'with valid request' do + before do + note.update!(assignment: nil) + + get "/notes?list_id=#{list.id}" + end + + it { is_expected.to have_http_status(:ok) } + it { expect(response.body).to eq_json(notes) } + end +end diff --git a/spec/todos/notes/notes_spec.rb b/spec/todos/notes/notes_spec.rb new file mode 100644 index 0000000..961a36e --- /dev/null +++ b/spec/todos/notes/notes_spec.rb @@ -0,0 +1,10 @@ + +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Note, type: :model do + it { is_expected.to belong_to(:assignment).optional } + it { is_expected.to belong_to(:list) } + it { is_expected.to validate_presence_of(:text) } +end \ No newline at end of file diff --git a/todos/app/resources/notes/model.rb b/todos/app/resources/notes/model.rb new file mode 100644 index 0000000..03f31cb --- /dev/null +++ b/todos/app/resources/notes/model.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module Notes + module Model + extend ActiveSupport::Concern + + included do + belongs_to :assignments, optional: true + + schema_validations auto_create: false + end + end +end diff --git a/todos/app/resources/notes/resource.rb b/todos/app/resources/notes/resource.rb new file mode 100644 index 0000000..0ac599f --- /dev/null +++ b/todos/app/resources/notes/resource.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module Notes + class Resource < ApplicationResource + filter_by :list + end +end diff --git a/todos/config/initializers/croods.rb b/todos/config/initializers/croods.rb index e17f150..0a5f5f1 100644 --- a/todos/config/initializers/croods.rb +++ b/todos/config/initializers/croods.rb @@ -7,6 +7,7 @@ :lists, :tasks, :assignments, + :notes, multi_tenancy_by: :organization ) diff --git a/todos/db/migrate/20200626135719_create_notes.rb b/todos/db/migrate/20200626135719_create_notes.rb new file mode 100644 index 0000000..3a87753 --- /dev/null +++ b/todos/db/migrate/20200626135719_create_notes.rb @@ -0,0 +1,11 @@ +class CreateNotes < ActiveRecord::Migration[5.2] + def change + create_table :notes do |t| + t.references :assignment + t.references :list, null: false + t.text :text, null: false + + t.timestamps + end + end +end diff --git a/todos/db/schema.rb b/todos/db/schema.rb index 9b8bb23..f6ce734 100644 --- a/todos/db/schema.rb +++ b/todos/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: 2020_06_25_150646) do +ActiveRecord::Schema.define(version: 2020_06_26_135719) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -84,4 +84,12 @@ t.datetime "updated_at", :null=>false end + create_table "notes", force: :cascade do |t| + t.bigint "assignment_id", :foreign_key=>{:references=>"assignments", :name=>"fk_notes_assignment_id", :on_update=>:no_action, :on_delete=>:no_action}, :index=>{:name=>"fk__notes_assignment_id"} + t.bigint "list_id", :null=>false, :foreign_key=>{:references=>"lists", :name=>"fk_notes_list_id", :on_update=>:no_action, :on_delete=>:no_action}, :index=>{:name=>"fk__notes_list_id"} + t.text "text", :null=>false + t.datetime "created_at", :null=>false + t.datetime "updated_at", :null=>false + end + end From 2653aa5e2654f94c49271b5bc2dced2fa15e23ee Mon Sep 17 00:00:00 2001 From: Felipe Freitag Vargas Date: Fri, 3 Jul 2020 09:22:33 -0300 Subject: [PATCH 2/7] Fix linter --- spec/todos/notes/notes_spec.rb | 3 +-- todos/db/migrate/20200626135719_create_notes.rb | 2 ++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/spec/todos/notes/notes_spec.rb b/spec/todos/notes/notes_spec.rb index 961a36e..bfe656a 100644 --- a/spec/todos/notes/notes_spec.rb +++ b/spec/todos/notes/notes_spec.rb @@ -1,4 +1,3 @@ - # frozen_string_literal: true require 'rails_helper' @@ -7,4 +6,4 @@ it { is_expected.to belong_to(:assignment).optional } it { is_expected.to belong_to(:list) } it { is_expected.to validate_presence_of(:text) } -end \ No newline at end of file +end diff --git a/todos/db/migrate/20200626135719_create_notes.rb b/todos/db/migrate/20200626135719_create_notes.rb index 3a87753..671561d 100644 --- a/todos/db/migrate/20200626135719_create_notes.rb +++ b/todos/db/migrate/20200626135719_create_notes.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class CreateNotes < ActiveRecord::Migration[5.2] def change create_table :notes do |t| From ace906850e77d602e31c76f971f03c4d3d7c7f6e Mon Sep 17 00:00:00 2001 From: Felipe Freitag Vargas Date: Fri, 3 Jul 2020 10:28:29 -0300 Subject: [PATCH 3/7] Test notes model --- .gitignore | 1 + Gemfile | 4 +++- Gemfile.lock | 7 +++++++ spec/support/shoulda_matchers.rb | 6 ++++++ todos/app/resources/notes/model.rb | 4 ++-- todos/app/resources/notes/resource.rb | 2 ++ 6 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 spec/support/shoulda_matchers.rb diff --git a/.gitignore b/.gitignore index c1f8602..4d9bfcc 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ todos/log/*.log todos/tmp/ .gems/ coverage/ +.byebug_history \ No newline at end of file diff --git a/Gemfile b/Gemfile index 70a37bb..5729925 100644 --- a/Gemfile +++ b/Gemfile @@ -14,4 +14,6 @@ gemspec # your gem to rubygems.org. # To use a debugger -# gem 'byebug', group: [:development, :test] +gem 'byebug', group: [:development, :test] +gem 'awesome_print', group: [:development, :test] +gem 'shoulda-matchers', group: :test diff --git a/Gemfile.lock b/Gemfile.lock index 516ec84..8e749ef 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -56,8 +56,10 @@ GEM tzinfo (~> 1.1) arel (9.0.0) ast (2.4.1) + awesome_print (1.8.0) bcrypt (3.1.13) builder (3.2.4) + byebug (11.1.3) committee (3.3.0) json_schema (~> 0.14, >= 0.14.3) openapi_parser (>= 0.6.1) @@ -212,6 +214,8 @@ GEM activerecord (~> 5.0) schema_plus_columns valuable + shoulda-matchers (4.3.0) + activesupport (>= 4.2.0) simplecov (0.17.1) docile (~> 1.1) json (>= 1.8, < 3) @@ -241,6 +245,8 @@ PLATFORMS ruby DEPENDENCIES + awesome_print + byebug croods! gem-release pg @@ -249,6 +255,7 @@ DEPENDENCIES rspec_junit_formatter (~> 0.4.1) rubocop (= 0.80.1) rubocop-rspec (= 1.38.1) + shoulda-matchers simplecov (~> 0.17.0) timecop (= 0.9.1) diff --git a/spec/support/shoulda_matchers.rb b/spec/support/shoulda_matchers.rb new file mode 100644 index 0000000..dc0c322 --- /dev/null +++ b/spec/support/shoulda_matchers.rb @@ -0,0 +1,6 @@ +Shoulda::Matchers.configure do |config| + config.integrate do |with| + with.test_framework :rspec + with.library :rails + end +end \ No newline at end of file diff --git a/todos/app/resources/notes/model.rb b/todos/app/resources/notes/model.rb index 03f31cb..ed36a3c 100644 --- a/todos/app/resources/notes/model.rb +++ b/todos/app/resources/notes/model.rb @@ -5,9 +5,9 @@ module Model extend ActiveSupport::Concern included do - belongs_to :assignments, optional: true + belongs_to :assignment, optional: true - schema_validations auto_create: false + #schema_validations auto_create: false end end end diff --git a/todos/app/resources/notes/resource.rb b/todos/app/resources/notes/resource.rb index 0ac599f..2a3b1ad 100644 --- a/todos/app/resources/notes/resource.rb +++ b/todos/app/resources/notes/resource.rb @@ -3,5 +3,7 @@ module Notes class Resource < ApplicationResource filter_by :list + + extend_model { include Notes::Model } end end From d3393334ee08fff1d8358c656e86870caee3fc46 Mon Sep 17 00:00:00 2001 From: Felipe Freitag Vargas Date: Fri, 3 Jul 2020 10:38:53 -0300 Subject: [PATCH 4/7] Fix linter --- Gemfile | 4 ++-- spec/support/shoulda_matchers.rb | 4 +++- spec/todos/notes/model_spec.rb | 9 +++++++++ todos/app/resources/notes/model.rb | 2 -- 4 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 spec/todos/notes/model_spec.rb diff --git a/Gemfile b/Gemfile index 5729925..b31f2f1 100644 --- a/Gemfile +++ b/Gemfile @@ -14,6 +14,6 @@ gemspec # your gem to rubygems.org. # To use a debugger -gem 'byebug', group: [:development, :test] -gem 'awesome_print', group: [:development, :test] +gem 'awesome_print', group: %i[development test] +gem 'byebug', group: %i[development test] gem 'shoulda-matchers', group: :test diff --git a/spec/support/shoulda_matchers.rb b/spec/support/shoulda_matchers.rb index dc0c322..edcf9dd 100644 --- a/spec/support/shoulda_matchers.rb +++ b/spec/support/shoulda_matchers.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + Shoulda::Matchers.configure do |config| config.integrate do |with| with.test_framework :rspec with.library :rails end -end \ No newline at end of file +end diff --git a/spec/todos/notes/model_spec.rb b/spec/todos/notes/model_spec.rb new file mode 100644 index 0000000..bfe656a --- /dev/null +++ b/spec/todos/notes/model_spec.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Note, type: :model do + it { is_expected.to belong_to(:assignment).optional } + it { is_expected.to belong_to(:list) } + it { is_expected.to validate_presence_of(:text) } +end diff --git a/todos/app/resources/notes/model.rb b/todos/app/resources/notes/model.rb index ed36a3c..432c3f5 100644 --- a/todos/app/resources/notes/model.rb +++ b/todos/app/resources/notes/model.rb @@ -6,8 +6,6 @@ module Model included do belongs_to :assignment, optional: true - - #schema_validations auto_create: false end end end From b500c7ba1f3bb74e027ffe76ef886db738a74409 Mon Sep 17 00:00:00 2001 From: Felipe Freitag Vargas Date: Fri, 3 Jul 2020 10:48:25 -0300 Subject: [PATCH 5/7] Test create --- spec/todos/notes/create_spec.rb | 62 +++++++++++++++++++++++++++++++++ spec/todos/notes/index_spec.rb | 11 +++++- 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 spec/todos/notes/create_spec.rb diff --git a/spec/todos/notes/create_spec.rb b/spec/todos/notes/create_spec.rb new file mode 100644 index 0000000..4762a16 --- /dev/null +++ b/spec/todos/notes/create_spec.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe 'POST /notes', type: :request do + subject { response } + + let(:params) { { text: 'Foo Bar', list_id: list.id } } + + let(:project) do + current_user.projects.create! name: 'Foo' + end + + let(:list) do + project.lists.create! name: 'Foo' + end + + let(:task) do + list.tasks.create! name: 'Foo' + end + + let(:assignment) do + task.assignments.create! user: current_user + end + + let(:note) { Note.find_by(text: 'Foo Bar') } + + context 'without the optional association' do + before do + post '/notes', params: params.to_json + end + + it { is_expected.to have_http_status(:created) } + it { expect(response.body).to eq_json(note) } + end + + context 'with the optional association' do + let(:params) do + { text: 'Foo Bar', list_id: list.id, assignment_id: assignment.id } + end + + before do + post '/notes', params: params.to_json + end + + it { is_expected.to have_http_status(:created) } + it { expect(response.body).to eq_json(note) } + end + + context 'without the required association' do + let(:params) do + { text: 'Foo Bar', assignment_id: assignment.id } + end + + before do + post '/notes', params: params.to_json + end + + it { is_expected.to have_http_status(:bad_request) } + it { expect(response.body).to include('list_id', "wasn't supplied.") } + end +end diff --git a/spec/todos/notes/index_spec.rb b/spec/todos/notes/index_spec.rb index d95631c..6222f0d 100644 --- a/spec/todos/notes/index_spec.rb +++ b/spec/todos/notes/index_spec.rb @@ -28,8 +28,17 @@ let(:notes) do list.notes.order(:created_at) end - + context 'with valid request' do + before do + get "/notes?list_id=#{list.id}" + end + + it { is_expected.to have_http_status(:ok) } + it { expect(response.body).to eq_json(notes) } + end + + context 'without the optional association' do before do note.update!(assignment: nil) From 6ae614f9488cb74d692db8fb1a7a2e4ebf5a5406 Mon Sep 17 00:00:00 2001 From: Felipe Freitag Vargas Date: Fri, 3 Jul 2020 10:58:05 -0300 Subject: [PATCH 6/7] Test update and destroy --- spec/todos/notes/destroy_spec.rb | 59 ++++++++++++++++++++++++ spec/todos/notes/update_spec.rb | 79 ++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 spec/todos/notes/destroy_spec.rb create mode 100644 spec/todos/notes/update_spec.rb diff --git a/spec/todos/notes/destroy_spec.rb b/spec/todos/notes/destroy_spec.rb new file mode 100644 index 0000000..1927aec --- /dev/null +++ b/spec/todos/notes/destroy_spec.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe 'DELETE /notes/:id', type: :request do + subject { response } + + let(:project) do + current_user.projects.create! name: 'Foo' + end + + let(:list) do + project.lists.create! name: 'Foo' + end + + let(:task) do + list.tasks.create! name: 'Foo' + end + + let(:assignment) do + task.assignments.create! user: current_user + end + + let(:note) do + list.notes.create!(text: 'foo') + end + + before do + project + list + task + assignment + note + end + + context 'without the optional association' do + before do + delete "/notes/#{note.id}" + end + + it { is_expected.to have_http_status(:ok) } + it { expect(response.body).to eq_json(note) } + it { expect(list.notes.find_by(text: 'foo')).to be_nil } + end + + context 'with the optional association' do + let(:note) do + list.notes.create!(text: 'foo', assignment: assignment) + end + + before do + delete "/notes/#{note.id}" + end + + it { is_expected.to have_http_status(:ok) } + it { expect(response.body).to eq_json(note) } + it { expect(list.notes.find_by(text: 'foo')).to be_nil } + end +end diff --git a/spec/todos/notes/update_spec.rb b/spec/todos/notes/update_spec.rb new file mode 100644 index 0000000..df1b7f8 --- /dev/null +++ b/spec/todos/notes/update_spec.rb @@ -0,0 +1,79 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe 'PUT /notes/:id', type: :request do + subject { response } + + let(:project) do + current_user.projects.create! name: 'Foo' + end + + let(:list) do + project.lists.create! name: 'Foo' + end + + let(:task) do + list.tasks.create! name: 'Foo' + end + + let(:assignment) do + task.assignments.create! user: current_user + end + + let(:note) do + list.notes.create!(text: 'foo', assignment: assignment) + end + + before do + project + list + task + assignment + note + end + + context 'with optional association' do + before do + put "/notes/#{note.id}", params: { text: 'Bar Foo' }.to_json + end + + it { is_expected.to have_http_status(:ok) } + it { expect(response.body).to eq_json(note.reload) } + end + + context 'without optional association' do + let(:note) do + list.notes.create!(text: 'foo') + end + + before do + put "/notes/#{note.id}", params: { text: 'Bar Foo' }.to_json + end + + it { is_expected.to have_http_status(:ok) } + it { expect(response.body).to eq_json(note.reload) } + end + + context 'when removing the optional association' do + before do + put "/notes/#{note.id}", params: { assignment_id: nil }.to_json + end + + it { is_expected.to have_http_status(:ok) } + it { expect(response.body).to eq_json(note.reload) } + end + + context 'when adding an optional association' do + let(:note) do + list.notes.create!(text: 'foo') + end + + before do + put "/notes/#{note.id}", params: { assignment_id: assignment.id }.to_json + end + + it { is_expected.to have_http_status(:ok) } + it { expect(response.body).to eq_json(note.reload) } + end +end From 30e1466bbca36f13bd55ace4d106eea6d4fde7c2 Mon Sep 17 00:00:00 2001 From: Felipe Freitag Vargas Date: Fri, 3 Jul 2020 10:59:16 -0300 Subject: [PATCH 7/7] Fix linter --- spec/todos/notes/create_spec.rb | 2 +- spec/todos/notes/index_spec.rb | 2 +- spec/todos/notes/update_spec.rb | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/todos/notes/create_spec.rb b/spec/todos/notes/create_spec.rb index 4762a16..18a2900 100644 --- a/spec/todos/notes/create_spec.rb +++ b/spec/todos/notes/create_spec.rb @@ -24,7 +24,7 @@ end let(:note) { Note.find_by(text: 'Foo Bar') } - + context 'without the optional association' do before do post '/notes', params: params.to_json diff --git a/spec/todos/notes/index_spec.rb b/spec/todos/notes/index_spec.rb index 6222f0d..56b9c00 100644 --- a/spec/todos/notes/index_spec.rb +++ b/spec/todos/notes/index_spec.rb @@ -28,7 +28,7 @@ let(:notes) do list.notes.order(:created_at) end - + context 'with valid request' do before do get "/notes?list_id=#{list.id}" diff --git a/spec/todos/notes/update_spec.rb b/spec/todos/notes/update_spec.rb index df1b7f8..a56adab 100644 --- a/spec/todos/notes/update_spec.rb +++ b/spec/todos/notes/update_spec.rb @@ -46,7 +46,7 @@ let(:note) do list.notes.create!(text: 'foo') end - + before do put "/notes/#{note.id}", params: { text: 'Bar Foo' }.to_json end @@ -68,7 +68,7 @@ let(:note) do list.notes.create!(text: 'foo') end - + before do put "/notes/#{note.id}", params: { assignment_id: assignment.id }.to_json end