Skip to content

Commit

Permalink
Add tests to note controller and handle edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
kcne committed Oct 16, 2024
1 parent 410ff94 commit d88ec30
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 19 deletions.
3 changes: 1 addition & 2 deletions app/controllers/notes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ class NotesController < ApplicationController
before_action :set_locale
around_action :web_timeout

### Display a list of notes by a specified user
# Display a list of notes by a specified user
def index
param! :page, Integer, :min => 1

@params = params.permit(:display_name, :from, :to, :status, :sort_by, :sort_order, :note_type)
@title = t(".title", :user => @user.display_name)
@page = (params[:page] || 1).to_i
@page_size = 10

@notes = @user.notes
.filter_hidden_notes(current_user)
.filter_by_status(params[:status])
Expand Down
8 changes: 4 additions & 4 deletions app/models/note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ class Note < ApplicationRecord
case note_type
when "commented"
joins(:comments)
.where("notes.id IN (SELECT note_id FROM note_comments WHERE author_id != ?)", user_id)
.where("notes.id IN (SELECT note_id FROM note_comments WHERE author_id != ? OR author_id IS NULL)", user_id)
.distinct
when "submitted"
joins(:comments)
.where(:note_comments => { :author_id => user_id })
.where("note_comments.id = (SELECT MIN(id) FROM note_comments WHERE note_comments.note_id = notes.id)")
.where("note_comments.id = (SELECT MIN(nc.id) FROM note_comments nc WHERE nc.note_id = notes.id)")
.distinct
else
all
Expand All @@ -72,8 +72,8 @@ class Note < ApplicationRecord

scope :filter_by_date_range, lambda { |from, to|
notes = all
notes = notes.where(:notes => { :created_at => DateTime.parse(from).. }) if from.present?
notes = notes.where(:notes => { :created_at => ..DateTime.parse(to) }) if to.present?
notes = notes.where(:created_at => DateTime.parse(from).beginning_of_day..) if from.present?
notes = notes.where(:created_at => ..DateTime.parse(to).end_of_day) if to.present?
notes
}

Expand Down
8 changes: 3 additions & 5 deletions app/views/notes/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
<div class="col-md-auto mb-3">
<%= label_tag :status, t(".status") %>
<%= select_tag :status,
options_for_select([[t(".all"), "all"], [t(".open"), "open"], [t(".closed"), "closed"]], params[:status]),
:include_blank => t(".select_status"),
options_for_select([[t(".all"), "all"], [t(".open"), "open"], [t(".closed"), "closed"]], params[:status] || "all"),
:class => "form-select" %>
</div>
<div class="col-md-auto mb-3">
<%= label_tag :note_type, t(".note_type") %>
<%= label_tag :note_type, t(".interaction_type") %>
<%= select_tag :note_type,
options_for_select([[t(".all_types"), ""], [t(".submitted"), "submitted"], [t(".commented"), "commented"]], params[:note_type]),
:include_blank => t(".select_note_type"),
options_for_select([[t(".all"), ""], [t(".submitted"), "submitted"], [t(".commented"), "commented"]], params[:note_type] || "all"),
:class => "form-select" %>
</div>
<div class="col-md-auto mb-3">
Expand Down
5 changes: 1 addition & 4 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2973,12 +2973,9 @@ en:
from: "From"
to: "To"
status: "Status"
select_status: "Select Status"
note_type: "Note Type"
all_types: "All Types"
interaction_type: "Interaction Type"
submitted: "Submitted"
commented: "Commented"
select_note_type: "Select Note Type"
show:
title: "Note: %{id}"
description: "Description"
Expand Down
128 changes: 124 additions & 4 deletions test/controllers/notes_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def test_index_success
assert_select ".content-heading a[href='#{user_path first_user}']", :text => first_user.display_name
assert_select "table.note_list tbody tr", :count => 1

# Check for a regular user (second user)
get user_notes_path(second_user)
assert_response :success
assert_select ".content-heading a[href='#{user_path second_user}']", :text => second_user.display_name
Expand All @@ -55,10 +56,6 @@ def test_index_success

session_for(moderator_user)

get user_notes_path(first_user)
assert_response :success
assert_select "table.note_list tbody tr", :count => 1

get user_notes_path(second_user)
assert_response :success
assert_select "table.note_list tbody tr", :count => 2
Expand Down Expand Up @@ -184,4 +181,127 @@ def test_new_note
assert_template "notes/new"
assert_select "#sidebar_content a[href='#{login_path(:referer => new_note_path)}']", :count => 0
end

def test_index_filter_by_status
user = create(:user)

open_note = create(:note, :status => "open")
create(:note_comment, :note => open_note, :author => user)

closed_note = create(:note, :status => "closed")
create(:note_comment, :note => closed_note, :author => user)

get user_notes_path(user), :params => { :status => "open" }
assert_response :success
assert_select "table.note_list tbody tr", :count => 1

get user_notes_path(user), :params => { :status => "closed" }
assert_response :success
assert_select "table.note_list tbody tr", :count => 1
end

def test_index_filter_by_note_type
user = create(:user)
other_user = create(:user)
anonymous_user = nil

submitted_note = create(:note)
create(:note_comment, :note => submitted_note, :author => user)

commented_note = create(:note)
create(:note_comment, :note => commented_note, :author => other_user)
create(:note_comment, :note => commented_note, :author => user)

anonymous_commented_note = create(:note)
create(:note_comment, :note => anonymous_commented_note, :author => anonymous_user)
create(:note_comment, :note => anonymous_commented_note, :author => user)

get user_notes_path(user), :params => { :note_type => "submitted" }
assert_response :success
assert_select "table.note_list tbody tr", :count => 1
assert_select "table.note_list tbody tr", :text => /#{submitted_note.id}/

get user_notes_path(user), :params => { :note_type => "commented" }
assert_response :success
assert_select "table.note_list tbody tr", :count => 2
assert_select "table.note_list tbody tr", :text => /#{commented_note.id}/
assert_select "table.note_list tbody tr", :text => /#{anonymous_commented_note.id}/

get user_notes_path(user)
assert_response :success
assert_select "table.note_list tbody tr", :count => 3
assert_select "table.note_list tbody tr", :text => /#{submitted_note.id}/
assert_select "table.note_list tbody tr", :text => /#{commented_note.id}/
assert_select "table.note_list tbody tr", :text => /#{anonymous_commented_note.id}/
end

def test_index_filter_by_date_range
user = create(:user)

old_note = create(:note, :created_at => 1.year.ago)
create(:note_comment, :note => old_note, :author => user)

recent_note = create(:note, :created_at => 1.day.ago)
create(:note_comment, :note => recent_note, :author => user)

middle_note = create(:note, :created_at => 6.months.ago)
create(:note_comment, :note => middle_note, :author => user)

very_recent_note = create(:note, :created_at => 2.hours.ago)
create(:note_comment, :note => very_recent_note, :author => user)

# Filter for notes created between 1 year ago + 1 day and 1 month ago (should only include middle_note)
get user_notes_path(user), :params => { :from => (1.year.ago + 1.day).to_date, :to => 1.month.ago.end_of_month.to_date }
assert_response :success
assert_select "table.note_list tbody tr", :count => 1
assert_select "table.note_list tbody tr", :text => /#{middle_note.id}/
end

def test_index_sort_by_params
user = create(:user)

older_note = create(:note)
create(:note_comment, :note => older_note, :author => user)
older_note.updated_at = 2.days.ago
older_note.created_at = 5.days.ago
older_note.save!

newer_note = create(:note)
create(:note_comment, :note => newer_note, :author => user)
newer_note.updated_at = 13.minutes.ago
newer_note.created_at = 3.days.ago
newer_note.save!

middle_note = create(:note)
create(:note_comment, :note => middle_note, :author => user)
middle_note.updated_at = 1.day.ago
middle_note.created_at = 4.days.ago
middle_note.save!

very_recent_note = create(:note)
create(:note_comment, :note => very_recent_note, :author => user)
very_recent_note.updated_at = 5.minutes.ago
very_recent_note.created_at = 1.day.ago
very_recent_note.save!

get user_notes_path(user), :params => { :sort_by => "updated_at", :sort_order => "asc" }
assert_response :success
assert_select "table.note_list tbody tr:first-child td:nth-child(6) time", :text => /2 days ago/
assert_select "table.note_list tbody tr:last-child td:nth-child(6) time", :text => /5 minutes ago/

get user_notes_path(user), :params => { :sort_by => "updated_at", :sort_order => "desc" }
assert_response :success
assert_select "table.note_list tbody tr:first-child td:nth-child(6) time", :text => /5 minutes ago/
assert_select "table.note_list tbody tr:last-child td:nth-child(6) time", :text => /2 days ago/

get user_notes_path(user), :params => { :sort_by => "created_at", :sort_order => "asc" }
assert_response :success
assert_select "table.note_list tbody tr:first-child td:nth-child(5) time", :text => /5 days ago/
assert_select "table.note_list tbody tr:last-child td:nth-child(5) time", :text => /1 day ago/
# created at
get user_notes_path(user), :params => { :sort_by => "created_at", :sort_order => "desc" }
assert_response :success
assert_select "table.note_list tbody tr:first-child td:nth-child(5) time", :text => /1 day ago/
assert_select "table.note_list tbody tr:last-child td:nth-child(5) time", :text => /5 days ago/
end
end

0 comments on commit d88ec30

Please sign in to comment.