Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: PG::NotNullViolation (MAYBE-RAILS-DP) #1802

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/controllers/budget_categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def show
else
@budget_category = Current.family.budget_categories.find(params[:id])
@recent_transactions = @recent_transactions.joins("LEFT JOIN categories ON categories.id = account_transactions.category_id")
.where("categories.id = ? OR categories.parent_id = ?", @budget_category.category.id, @budget_category.category.id)
.where("categories.id = ? OR categories.parent_id = ?", @budget_category.category.id, @budget_category.category.id)
end

@recent_transactions = @recent_transactions.order("account_entries.date DESC, ABS(account_entries.amount) DESC").take(3)
Expand All @@ -30,6 +30,8 @@ def update

private
def budget_category_params
params.require(:budget_category).permit(:budgeted_spending)
params.require(:budget_category).permit(:budgeted_spending).tap do |bc_params|
bc_params[:budgeted_spending] = 0.0 if bc_params[:budgeted_spending].blank?
end
end
end
63 changes: 63 additions & 0 deletions spec/controllers/budget_categories_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require 'rails_helper'

RSpec.describe BudgetCategoriesController, type: :controller do
let(:family) { create(:family) }
let(:user) { create(:user, family: family) }
let(:budget) { create(:budget, family: family) }
let(:category) { create(:category, family: family) }
let(:budget_category) { create(:budget_category, budget: budget, category: category) }

before do
sign_in(user)
end

describe 'PATCH #update' do
context 'with valid parameters' do
it 'updates the budgeted_spending' do
patch :update, params: {

Check failure on line 17 in spec/controllers/budget_categories_controller_spec.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
id: budget_category.id,

Check failure on line 18 in spec/controllers/budget_categories_controller_spec.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
budget_id: budget.id,

Check failure on line 19 in spec/controllers/budget_categories_controller_spec.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
budget_category: { budgeted_spending: 100.0 }
}

expect(budget_category.reload.budgeted_spending).to eq(100.0)
expect(response).to redirect_to(budget_budget_categories_path(budget))
end

it 'sets budgeted_spending to 0.0 when blank' do
patch :update, params: {

Check failure on line 28 in spec/controllers/budget_categories_controller_spec.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
id: budget_category.id,

Check failure on line 29 in spec/controllers/budget_categories_controller_spec.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
budget_id: budget.id,

Check failure on line 30 in spec/controllers/budget_categories_controller_spec.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
budget_category: { budgeted_spending: '' }
}

expect(budget_category.reload.budgeted_spending).to eq(0.0)
expect(response).to redirect_to(budget_budget_categories_path(budget))
end

it 'sets budgeted_spending to 0.0 when nil' do
patch :update, params: {

Check failure on line 39 in spec/controllers/budget_categories_controller_spec.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
id: budget_category.id,

Check failure on line 40 in spec/controllers/budget_categories_controller_spec.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
budget_id: budget.id,

Check failure on line 41 in spec/controllers/budget_categories_controller_spec.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
budget_category: { budgeted_spending: nil }
}

expect(budget_category.reload.budgeted_spending).to eq(0.0)
expect(response).to redirect_to(budget_budget_categories_path(budget))
end
end

context 'with invalid parameters' do
it 'renders show with unprocessable_entity status when validation fails' do
patch :update, params: {

Check failure on line 52 in spec/controllers/budget_categories_controller_spec.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
id: budget_category.id,
budget_id: budget.id,
budget_category: { budgeted_spending: 'invalid' }
}

expect(response).to render_template(:show)
expect(response).to have_http_status(:unprocessable_entity)
end
end
end
end