Skip to content

Commit

Permalink
test question helper
Browse files Browse the repository at this point in the history
  • Loading branch information
cesswairimu committed Dec 20, 2018
1 parent 715ccd4 commit a8234a2
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 8 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ group :test, :development do
gem 'test-unit'
gem 'teaspoon-mocha'
gem 'timecop'
gem 'pry-rails'
end

group :production do
Expand Down
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ GEM
ci_reporter (~> 2.0)
test-unit (>= 2.5.5, < 4.0)
climate_control (0.2.0)
coderay (1.1.2)
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
coffee-rails (4.2.2)
Expand Down Expand Up @@ -284,6 +285,11 @@ GEM
progress_bar (1.3.0)
highline (>= 1.6, < 3)
options (~> 2.3.0)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
pry-rails (0.3.8)
pry (>= 0.10.4)
public_suffix (3.0.3)
rack (2.0.6)
rack-accept (0.4.5)
Expand Down Expand Up @@ -517,6 +523,7 @@ DEPENDENCIES
phantomjs
php-serialize
progress_bar
pry-rails
rack-cors
rack-openid
rack-test (= 1.1.0)
Expand Down
5 changes: 4 additions & 1 deletion app/controllers/questions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class QuestionsController < ApplicationController
before_action :quiz_stats, only: [:index, :answered, :popular, :liked, :unanswered]
private

def filter_questions_by_tag(questions, tagnames)
Expand All @@ -16,10 +17,12 @@ def filter_questions_by_tag(questions, tagnames)
end
end

def quiz_stats
@stats = helpers.questions_stats(params[:period])
end
public

def index
@stats = helpers.filtering(params[:period])
@title = 'Questions and Answers'
set_sidebar
@questions = Node.questions
Expand Down
12 changes: 6 additions & 6 deletions app/helpers/questions_helper.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
module QuestionsHelper
SORTING_OPTIONS = %w(All Week Month Year).freeze

def filtering(period)
def questions_stats(period)
return if period.nil?

if period == 'All'
Rails.cache.fetch("all_stats", expires_in: 1.days) do
@asked = Node.questions.to_a.size
@answered = Answer.all.map(&:node).uniq.size
@asked = Node.questions.length
@answered = Answer.all.map(&:node).uniq.count
"#{@asked} questions asked and #{@answered} questions answered"
end
else
Rails.cache.fetch("#{period}_stats", expires_in: 1.days) do
@asked = Node.questions.where('created >= ?', 1.send(period.downcase).ago.to_i).to_a.size
@answered = Answer.where("created_at >= ?", 1.send(period.downcase).ago).map(&:node).uniq.size
"#{@asked} questions asked and #{@answered} questions answered in the past #{period}"
@asked = Node.questions.where('created >= ?', 1.send(period.downcase).ago.to_i).length
@answered = Answer.where("created_at >= ?", 1.send(period.downcase).ago).map(&:node).uniq.count
"#{@asked} questions asked and #{@answered} questions answered in the past #{period.downcase}"
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion app/views/questions/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<div class="col-md-12">
<div class ="row">
<div class="col-md-6" >
<% if @stats.blank? %>
<h4><i> Select a range to view questions statistics within that period</i></h4>
<% end %>
<h4><b> <%= @stats if params[:period].present? %></b></h4>
</div>
<div class="col-md-4">
<%= form_tag request.url, :method => 'get' do %>
<%= select_tag :period, options_for_select(options),onchange: "this.form.submit();", include_blank: "Filter stats", class: "form-control"%>
<%= select_tag :period, options_for_select(options),onchange: "this.form.submit();", include_blank: "Filter stats by week, month and year", class: "form-control"%>
<% end %>
</div>
</div>
Expand Down
29 changes: 29 additions & 0 deletions test/unit/helpers/questions_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'test_helper'

class QuestionsHelperTest < ActionView::TestCase
test 'should return null if period is nil' do
assert_nil questions_stats(nil)
end

test 'should return all questions if all' do
request = questions_stats("All")
asked = Node.questions.length
answered = Answer.all.map(&:node).uniq.count
assert_not_nil request
assert_includes request, asked.to_s
assert_includes request, answered.to_s
end

test "should return exact count according to period given" do
options.reject { |option| option == "All" }.each do |period|
request = questions_stats(period)
asked = Node.questions.where('created >= ?', 1.send(period.downcase).ago.to_i).length
answered = Answer.where("created_at >= ?", 1.send(period.downcase).ago).map(&:node).uniq.count
assert_includes request, asked.to_s
assert_includes request, answered.to_s
end
end

test 'it caches results' do
end
end

0 comments on commit a8234a2

Please sign in to comment.