From 88e087ded5e84e70d4519b57a6a3c3b36af091b8 Mon Sep 17 00:00:00 2001 From: "K. Volchenko" Date: Tue, 26 Sep 2017 14:04:15 +0300 Subject: [PATCH] Fix deprecations & specs --- apps/admin/views/tasks/index.rb | 2 +- apps/admin/views/users/index.rb | 2 +- lib/ossboard/repositories/task_repository.rb | 2 +- lib/ossboard/repositories/user_repository.rb | 2 +- .../repositories/task_repository_spec.rb | 54 ++++++++++--------- 5 files changed, 33 insertions(+), 29 deletions(-) diff --git a/apps/admin/views/tasks/index.rb b/apps/admin/views/tasks/index.rb index e0245c10..a1449ad6 100644 --- a/apps/admin/views/tasks/index.rb +++ b/apps/admin/views/tasks/index.rb @@ -3,7 +3,7 @@ class Index include Admin::View def tasks - TaskRepository.new.tasks.order{ id.desc }.as(Task).to_a + TaskRepository.new.tasks.order{ id.desc }.map_to(Task).to_a end def repository_name(task) diff --git a/apps/admin/views/users/index.rb b/apps/admin/views/users/index.rb index 3ab04386..403b8a4b 100644 --- a/apps/admin/views/users/index.rb +++ b/apps/admin/views/users/index.rb @@ -3,7 +3,7 @@ class Index include Admin::View def users - UserRepository.new.users.order{ id.desc }.as(User).to_a + UserRepository.new.users.order{ id.desc }.map_to(User).to_a end def banned_users diff --git a/lib/ossboard/repositories/task_repository.rb b/lib/ossboard/repositories/task_repository.rb index a0f6e0e8..506e8432 100644 --- a/lib/ossboard/repositories/task_repository.rb +++ b/lib/ossboard/repositories/task_repository.rb @@ -39,7 +39,7 @@ def assigned_tasks_for_user(user) private def all_from_date_request(from, status = nil) - request = tasks.where("created_at > '#{from}'").where("created_at < '#{Time.now}'") + request = tasks.where { (created_at > from) & (created_at < Time.now) } request = request.where(status: status) if status request end diff --git a/lib/ossboard/repositories/user_repository.rb b/lib/ossboard/repositories/user_repository.rb index 668b125f..e0f124e9 100644 --- a/lib/ossboard/repositories/user_repository.rb +++ b/lib/ossboard/repositories/user_repository.rb @@ -42,6 +42,6 @@ def all_with_points_and_tasks private def all_from_date_request(from) - users.where("created_at > '#{from}'").where("created_at < '#{Time.now}'") + users.where { (created_at > from) & (created_at < Time.now) } end end diff --git a/spec/ossboard/repositories/task_repository_spec.rb b/spec/ossboard/repositories/task_repository_spec.rb index 39f5d3bf..e97f58f0 100644 --- a/spec/ossboard/repositories/task_repository_spec.rb +++ b/spec/ossboard/repositories/task_repository_spec.rb @@ -126,10 +126,10 @@ end describe '#all_from_date' do - before(:all) do + before do 3.times do |i| random_days_count = i * 60 * 60 * 24 - Timecop.freeze(Time.new(2016, 02, 20) - random_days_count) do + Timecop.freeze(Time.utc(2016, 02, 20) - random_days_count) do Fabricate.create(:task, status: 'done') Fabricate.create(:task, status: 'in progress') Fabricate.create(:task, status: 'closed') @@ -140,35 +140,39 @@ Timecop.freeze(Time.now + (2 * 60 * 60 * 24)) { Fabricate.create(:task) } end - describe '#all_from_date_counted_by_status_and_day' do - before(:all) do - 3.times do |i| - Timecop.freeze(Time.utc(2016, 02, 20 + i)) do - Fabricate.create(:task, status: 'done') - Fabricate.create(:task, status: 'in progress') - Fabricate.create(:task, status: 'closed') - Fabricate.create(:task, status: 'assigned') - end - end - end - - let (:result) { repo.all_from_date_counted_by_status_and_day(Time.new(2016, 02, 20)) } - - it { expect(result).to be_a(Hash) } - it { expect(result['done'].count).to eq 2 } - it { expect(result.dig('closed', Date.new(2016, 02, 22))).to eq 1 } - end - let(:date) { Date.new(2016, 02, 18) } it 'returns array of tasks' do expect(repo.all_from_date(date)).to be_a(Array) expect(repo.all_from_date(date).count).to eq 8 end - it { expect(repo.all_from_date(date, 'in progress').count).to eq 2 } - it { expect(repo.all_from_date(date, 'done').count).to eq 2 } - it { expect(repo.all_from_date(date, 'closed').count).to eq 2 } - it { expect(repo.all_from_date(date, 'assigned').count).to eq 2 } + + it 'returns correct tasks for statuses' do + expect(repo.all_from_date(date, 'in progress').count).to eq 2 + expect(repo.all_from_date(date, 'done').count).to eq 2 + expect(repo.all_from_date(date, 'closed').count).to eq 2 + expect(repo.all_from_date(date, 'assigned').count).to eq 2 + end + end + + describe '#all_from_date_counted_by_status_and_day' do + before do + (-2..2).each do |days_count| + Timecop.freeze(Time.utc(2016, 02, 20 + days_count)) do + Fabricate.create(:task, status: 'done') + Fabricate.create(:task, status: 'in progress') + Fabricate.create(:task, status: 'closed') + Fabricate.create(:task, status: 'assigned') + end + end + end + + it 'returns correct tasks' do + result = repo.all_from_date_counted_by_status_and_day(Time.utc(2016, 02, 20)) + expect(result).to be_a(Hash) + expect(result['done'].count).to eq 2 + expect(result.dig('closed', Date.new(2016, 02, 22))).to eq 1 + end end describe '#on_moderation_for_user' do