diff --git a/spec/services/org/create_created_plan_service_spec.rb b/spec/services/org/create_created_plan_service_spec.rb index 5955f4f..4927793 100644 --- a/spec/services/org/create_created_plan_service_spec.rb +++ b/spec/services/org/create_created_plan_service_spec.rb @@ -1,8 +1,10 @@ -require 'rails_helper' +# frozen_string_literal: true + +require "rails_helper" RSpec.describe Org::CreateCreatedPlanService do let(:org) do - FactoryBot.create(:org, created_at: DateTime.new(2018,04,01)) + FactoryBot.create(:org, created_at: DateTime.new(2018, 04, 01)) end let(:template) do FactoryBot.create(:template, org: org) @@ -16,136 +18,165 @@ let(:user2) do FactoryBot.create(:user, org: org) end - #let(:creator) { Role.access_values_for(:creator).first } - #let(:administrator) { Role.access_values_for(:administrator).first } + before(:each) do - plan = FactoryBot.create(:plan, template: template, created_at: DateTime.new(2018,04,01)) - plan2 = FactoryBot.create(:plan, template: template2, created_at: DateTime.new(2018,04,03)) - plan3 = FactoryBot.create(:plan, template: template, created_at: DateTime.new(2018,05,02)) - plan4 = FactoryBot.create(:plan, template: template, created_at: DateTime.new(2018,06,02)) - plan5 = FactoryBot.create(:plan, template: template2, created_at: DateTime.new(2018,06,03)) - FactoryBot.create(:role, :creator, plan: plan, user: user1) - FactoryBot.create(:role, :administrator, plan: plan, user: user2) - FactoryBot.create(:role, :creator, plan: plan2, user: user1) - FactoryBot.create(:role, :creator, plan: plan3, user: user1) - FactoryBot.create(:role, :administrator, plan: plan4, user: user2) - FactoryBot.create(:role, :administrator, plan: plan5, user: user2) + plan = FactoryBot.create(:plan, + template: template, + created_at: DateTime.new(2018, 04, 01)) + plan2 = FactoryBot.create(:plan, + template: template2, + created_at: DateTime.new(2018, 04, 03)) + plan3 = FactoryBot.create(:plan, + template: template, + created_at: DateTime.new(2018, 05, 02)) + plan4 = FactoryBot.create(:plan, + template: template, + created_at: DateTime.new(2018, 06, 02)) + plan5 = FactoryBot.create(:plan, + template: template2, + created_at: DateTime.new(2018, 06, 03)) + FactoryBot.create(:role, + :creator, + plan: plan, + user: user1) + FactoryBot.create(:role, + :administrator, + plan: plan, + user: user2) + FactoryBot.create(:role, + :creator, + plan: plan2, + user: user1) + FactoryBot.create(:role, + :creator, + plan: plan3, + user: user1) + FactoryBot.create(:role, + :administrator, + plan: plan4, + user: user2) + FactoryBot.create(:role, + :administrator, + plan: plan5, + user: user2) end - def find_by_dates(dates: , org_id:) + def find_by_dates(dates:, org_id:) dates.map do |date| StatCreatedPlan.find_by(date: date, org_id: org_id) end end - describe '.call' do - context 'when org is passed' do + describe ".call" do + context "when org is passed" do it "generates monthly counts since org's creation" do described_class.call(org) - april, may, june, july = find_by_dates(dates: ['2018-04-30', '2018-05-31', '2018-06-30', '2018-07-31'], org_id: org.id) + april, may, june, july = find_by_dates(dates: ["2018-04-30", + "2018-05-31", + "2018-06-30", + "2018-07-31"], + org_id: org.id) counts = [april, may, june, july].map(&:count) - expect(counts).to eq([2,1,2,0]) + expect(counts).to eq([2, 1, 2, 0]) end it "generates monthly counts by template since org's creation" do described_class.call(org) - april, may, june, july = find_by_dates(dates: ['2018-04-30', '2018-05-31', '2018-06-30', '2018-07-31'], org_id: org.id) - expect(april.details).to eq( - { - 'by_template' => [ - { 'name' => template.title, 'count' => 1 }, - { 'name' => template2.title, 'count' => 1 }, - ] - } + april, may, june, july = find_by_dates(dates: ["2018-04-30", + "2018-05-31", + "2018-06-30", + "2018-07-31"], + org_id: org.id) + expect(april.details).to match_array( + "by_template" => [ + { "name" => template.title, "count" => 1 }, + { "name" => template2.title, "count" => 1 }, + ] ) - expect(may.details).to eq( - { - 'by_template' => [ - { 'name' => template.title, 'count' => 1 }, - ] - } + expect(may.details).to match_array( + "by_template" => [ + { "name" => template.title, "count" => 1 }, + ] ) - expect(june.details).to eq( - { - 'by_template' => [ - { 'name' => template.title, 'count' => 1 }, - { 'name' => template2.title, 'count' => 1 }, - ] - } + expect(june.details).to match_array( + "by_template" => [ + { "name" => template.title, "count" => 1 }, + { "name" => template2.title, "count" => 1 }, + ] ) - expect(july.details).to eq( - { - 'by_template' => [] - } + expect(july.details).to match_array( + "by_template" => [] ) end it "monthly records are either created or updated" do described_class.call(org) - april = StatCreatedPlan.where(date: '2018-04-30', org: org) + april = StatCreatedPlan.where(date: "2018-04-30", org: org) expect(april).to have(1).items expect(april.first.count).to eq(2) - new_plan = FactoryBot.create(:plan, template: template2, created_at: DateTime.new(2018,04,03)) + new_plan = FactoryBot.create(:plan, + template: template2, + created_at: DateTime.new(2018, 04, 03)) FactoryBot.create(:role, :creator, plan: new_plan, user: user1) described_class.call(org) - april = StatCreatedPlan.where(date: '2018-04-30', org: org) + april = StatCreatedPlan.where(date: "2018-04-30", org: org) expect(april).to have(1).items expect(april.first.count).to eq(3) end end - context 'when no org is passed' do - it 'generates monthly counts for each org since their creation' do + context "when no org is passed" do + it "generates monthly counts for each org since their creation" do Org.stubs(:all).returns([org]) described_class.call - april, may, june, july = find_by_dates(dates: ['2018-04-30', '2018-05-31', '2018-06-30', '2018-07-31'], org_id: org.id) + april, may, june, july = find_by_dates(dates: ["2018-04-30", + "2018-05-31", + "2018-06-30", + "2018-07-31"], + org_id: org.id) counts = [april, may, june, july].map(&:count) - expect(counts).to eq([2,1,2,0]) + expect(counts).to eq([2, 1, 2, 0]) end - it 'generates montly counts by template for each org since their creation' do + it "generates montly counts by template for each org since their creation" do Org.stubs(:all).returns([org]) described_class.call - april, may, june, july = find_by_dates(dates: ['2018-04-30', '2018-05-31', '2018-06-30', '2018-07-31'], org_id: org.id) + april, may, june, july = find_by_dates(dates: ["2018-04-30", + "2018-05-31", + "2018-06-30", + "2018-07-31"], + org_id: org.id) - expect(april.details).to eq( - { - 'by_template' => [ - { 'name' => template.title, 'count' => 1 }, - { 'name' => template2.title, 'count' => 1 }, - ] - } + expect(april.details).to match_array( + "by_template" => [ + { "name" => template.title, "count" => 1 }, + { "name" => template2.title, "count" => 1 }, + ] ) - expect(may.details).to eq( - { - 'by_template' => [ - { 'name' => template.title, 'count' => 1 }, - ] - } + expect(may.details).to match_array( + "by_template" => [ + { "name" => template.title, "count" => 1 }, + ] ) - expect(june.details).to eq( - { - 'by_template' => [ - { 'name' => template.title, 'count' => 1 }, - { 'name' => template2.title, 'count' => 1 }, - ] - } + expect(june.details).to match_array( + "by_template" => [ + { "name" => template.title, "count" => 1 }, + { "name" => template2.title, "count" => 1 }, + ] ) - expect(july.details).to eq( - { - 'by_template' => [] - } + expect(july.details).to match_array( + "by_template" => [] ) end @@ -154,16 +185,18 @@ described_class.call - april = StatCreatedPlan.where(date: '2018-04-30', org: org) + april = StatCreatedPlan.where(date: "2018-04-30", org: org) expect(april).to have(1).items expect(april.first.count).to eq(2) - new_plan = FactoryBot.create(:plan, template: template2, created_at: DateTime.new(2018,04,03)) + new_plan = FactoryBot.create(:plan, + template: template2, + created_at: DateTime.new(2018, 04, 03)) FactoryBot.create(:role, :creator, plan: new_plan, user: user1) described_class.call - april = StatCreatedPlan.where(date: '2018-04-30', org: org) + april = StatCreatedPlan.where(date: "2018-04-30", org: org) expect(april).to have(1).items expect(april.first.count).to eq(3) end diff --git a/spec/services/org/create_last_month_created_plan_service_spec.rb b/spec/services/org/create_last_month_created_plan_service_spec.rb index b1216b6..a20807d 100644 --- a/spec/services/org/create_last_month_created_plan_service_spec.rb +++ b/spec/services/org/create_last_month_created_plan_service_spec.rb @@ -1,8 +1,10 @@ -require 'rails_helper' +# frozen_string_literal: true + +require "rails_helper" RSpec.describe Org::CreateLastMonthCreatedPlanService do let(:org) do - FactoryBot.create(:org, created_at: DateTime.new(2018,04,01)) + FactoryBot.create(:org, created_at: DateTime.new(2018, 04, 01)) end let(:template) do FactoryBot.create(:template, org: org) @@ -19,63 +21,83 @@ let(:creator) { Role.access_values_for(:creator).first } let(:administrator) { Role.access_values_for(:administrator).first } before(:each) do - plan = FactoryBot.create(:plan, template: template, created_at: Date.today.last_month) - plan2 = FactoryBot.create(:plan, template: template, created_at: Date.today.last_month) - plan3 = FactoryBot.create(:plan, template: template2, created_at: Date.today.last_month) + plan = FactoryBot.create(:plan, + template: template, + created_at: Date.today.last_month) + plan2 = FactoryBot.create(:plan, + template: template, + created_at: Date.today.last_month) + plan3 = FactoryBot.create(:plan, + template: template2, + created_at: Date.today.last_month) FactoryBot.create(:role, :creator, plan: plan, user: user1) FactoryBot.create(:role, :administrator, plan: plan, user: user1) FactoryBot.create(:role, :creator, plan: plan2, user: user1) FactoryBot.create(:role, :creator, plan: plan3, user: user2) end - describe '.call' do - context 'when org is passed' do + describe ".call" do + context "when org is passed" do it "generates counts from today's last month" do described_class.call(org) - last_month_count = StatCreatedPlan.find_by(date: Date.today.last_month.end_of_month, org_id: org.id).count + last_month_count = StatCreatedPlan.find_by( + date: Date.today.last_month.end_of_month, + org_id: org.id).count expect(last_month_count).to eq(3) end it "generates counts by template from today's last month" do described_class.call(org) - last_month_details = StatCreatedPlan.find_by(date: Date.today.last_month.end_of_month, org_id: org.id).details - expect(last_month_details).to eq( - { - 'by_template' => [ - { 'name' => template.title, 'count' => 2 }, - { 'name' => template2.title, 'count' => 1 }, - ] - } + last_month_details = StatCreatedPlan.find_by( + date: Date.today.last_month.end_of_month, + org_id: org.id).details + + expect(last_month_details).to match_array( + "by_template" => [ + { "name" => template.title, "count" => 2 }, + { "name" => template2.title, "count" => 1 }, + ] ) end it "monthly records are either created or updated" do described_class.call(org) - last_month = StatCreatedPlan.where(date: Date.today.last_month.end_of_month, org_id: org.id) + last_month = StatCreatedPlan.where( + date: Date.today.last_month.end_of_month, + org_id: org.id) + expect(last_month).to have(1).items expect(last_month.first.count).to eq(3) - new_plan = FactoryBot.create(:plan, template: template2, created_at: Date.today.last_month.end_of_month) + new_plan = FactoryBot.create(:plan, + template: template2, + created_at: Date.today.last_month.end_of_month) FactoryBot.create(:role, :creator, plan: new_plan, user: user1) described_class.call(org) - last_month = StatCreatedPlan.where(date: Date.today.last_month.end_of_month, org_id: org.id) + last_month = StatCreatedPlan.where( + date: Date.today.last_month.end_of_month, + org_id: org.id) + expect(last_month).to have(1).items expect(last_month.first.count).to eq(4) end end - context 'when no org is passed' do + context "when no org is passed" do it "generates counts from today's last month" do Org.expects(:all).returns([org]) described_class.call - last_month_count = StatCreatedPlan.find_by(date: Date.today.last_month.end_of_month, org_id: org.id).count + last_month_count = StatCreatedPlan.find_by( + date: Date.today.last_month.end_of_month, + org_id: org.id).count + expect(last_month_count).to eq(3) end @@ -84,14 +106,15 @@ described_class.call - last_month_details = StatCreatedPlan.find_by(date: Date.today.last_month.end_of_month, org_id: org.id).details + last_month_details = StatCreatedPlan.find_by( + date: Date.today.last_month.end_of_month, + org_id: org.id).details + expect(last_month_details).to match_array( - { - 'by_template' => [ - { 'name' => template.title, 'count' => 2 }, - { 'name' => template2.title, 'count' => 1 }, - ] - } + "by_template" => [ + { "name" => template.title, "count" => 2 }, + { "name" => template2.title, "count" => 1 }, + ] ) end @@ -100,16 +123,22 @@ described_class.call - last_month = StatCreatedPlan.where(date: Date.today.last_month.end_of_month, org: org) + last_month = StatCreatedPlan.where( + date: Date.today.last_month.end_of_month, + org: org) + expect(last_month).to have(1).items expect(last_month.first.count).to eq(3) - new_plan = FactoryBot.create(:plan, template: template2, created_at: Date.today.last_month.end_of_month) + new_plan = FactoryBot.create(:plan, + template: template2, + created_at: Date.today.last_month.end_of_month) FactoryBot.create(:role, :creator, plan: new_plan, user: user1) described_class.call - last_month = StatCreatedPlan.where(date: Date.today.last_month.end_of_month, org: org) + last_month = StatCreatedPlan.where(date: Date.today.last_month.end_of_month, + org: org) expect(last_month).to have(1).items expect(last_month.first.count).to eq(4) end