Skip to content

Commit

Permalink
Merge pull request #1678 from tactilenews/1677_fix_charts_controller_bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwr18 authored Jul 26, 2023
2 parents 8b409af + bf496ac commit c0b4c12
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 3 deletions.
4 changes: 1 addition & 3 deletions app/controllers/charts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ def joined_outbound(group_keys)
end

def group_messages(messages, group_keys)
return [] unless messages.any?

column = messages.all? { |message| message.is_a? Request } ? :broadcasted_at : :created_at
column = messages.any? && messages.all? { |message| message.is_a? Request } ? :broadcasted_at : :created_at
messages = messages.group_by_day_of_week(column, format: '%A', week_start: :monday) if group_keys.include?(:day_of_week)
return messages unless group_keys.include?(:hour_of_day)

Expand Down
155 changes: 155 additions & 0 deletions spec/requests/charts_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'Charts' do
let(:user) { create(:user) }
let(:last_friday_midnight) { Time.zone.today.beginning_of_day.prev_occurring(:friday) }
let(:request) { create(:request, broadcasted_at: last_friday_midnight) }
let(:message) { create(:message, created_at: last_friday_midnight, request: request) }
let(:data) do
[
{ x: '00:00', y: 0 },
{ x: '01:00', y: 0 },
{ x: '02:00', y: 0 },
{ x: '03:00', y: 0 },
{ x: '04:00', y: 0 },
{ x: '05:00', y: 0 },
{ x: '06:00', y: 0 },
{ x: '07:00', y: 0 },
{ x: '08:00', y: 0 },
{ x: '09:00', y: 0 },
{ x: '10:00', y: 0 },
{ x: '11:00', y: 0 },
{ x: '12:00', y: 0 },
{ x: '13:00', y: 0 },
{ x: '14:00', y: 0 },
{ x: '15:00', y: 0 },
{ x: '16:00', y: 0 },
{ x: '17:00', y: 0 },
{ x: '18:00', y: 0 },
{ x: '19:00', y: 0 },
{ x: '20:00', y: 0 },
{ x: '21:00', y: 0 },
{ x: '22:00', y: 0 },
{ x: '23:00', y: 0 }
].freeze
end
let(:data_dup) { data.dup.map(&:dup) }
let(:friday) { series.find { |hash| hash[:name] == 'Freitag' } }
let(:series) do
I18n.t('date.day_names').reverse.rotate(-1).map do |day|
{ name: day, data: data }
end
end

describe 'GET /charts/day-and-time-replies' do
before do
message
data_dup.first[:y] = 1
friday[:data] = data_dup
end

subject { -> { get charts_day_and_time_replies_path(as: user) } }

it 'responds with a series of each day and inbound grouped messages' do
subject.call
expect(response.body).to eq(series.to_json)
end
end

describe 'GET /charts/day-and-time-requests' do
subject { -> { get charts_day_and_time_requests_path(as: user) } }

context 'no request, no chat messages' do
before { series.map { |hash| hash[:data] = [] } }

it 'returns empty data array' do
subject.call
expect(response.body).to eq(series.to_json)
end
end

context 'request, no chat messages' do
before do
request
data_dup.first[:y] = 1
friday[:data] = data_dup
end

it 'responds with a series of each day and outbound grouped messages' do
subject.call
expect(response.body).to eq(series.to_json)
end
end

context 'request, with chat message' do
before do
request
message.update(broadcasted: false, sender: user, text: 'ChatMessage')
data_dup.first[:y] = 2
friday[:data] = data_dup
end

it 'responds with a series of each day and outbound grouped messages' do
subject.call
expect(response.body).to eq(series.to_json)
end
end
end

describe 'GET /charts/day-requests-replies' do
subject { -> { get charts_day_requests_replies_path(as: user) } }
let(:data) do
I18n.t('date.day_names').rotate(1).map do |day|
{ x: day, y: 0 }
end
end
let(:series) do
[{ name: I18n.t('shared.community'), data: data },
{ name: I18n.t('shared.editorial'), data: data }]
end

context 'no requests, no replies' do
it 'returns empty data array' do
subject.call
expect(response.body).to eq(series.to_json)
end
end

context 'request, no replies' do
let(:editorial) { series.find { |hash| hash[:name] == 'Redaktion' } }
let(:friday) { data_dup.find { |hash| hash[:x] == 'Freitag' } }

before do
request
friday[:y] = 1
editorial[:data] = data_dup
end

it 'responds with a series of each day and outbound grouped messages' do
subject.call
expect(response.body).to eq(series.to_json)
end
end

context 'request and replies' do
let(:editorial) { series.find { |hash| hash[:name] == 'Redaktion' } }
let(:community) { series.find { |hash| hash[:name] == 'Community' } }
let(:friday) { data_dup.find { |hash| hash[:x] == 'Freitag' } }

before do
request
message
friday[:y] = 1
editorial[:data] = data_dup
community[:data] = data_dup
end

it 'responds with a series of each day and outbound grouped messages' do
subject.call
expect(response.body).to eq(series.to_json)
end
end
end
end

0 comments on commit c0b4c12

Please sign in to comment.