Skip to content

Commit

Permalink
Merge pull request joshsoftware#186 from BandanaPandey/ui_check
Browse files Browse the repository at this point in the history
Fixed Scoring for closed issue
  • Loading branch information
sethu authored Nov 8, 2016
2 parents 2ab7cde + c12eeea commit a33abb2
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 47 deletions.
20 changes: 14 additions & 6 deletions app/models/activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,20 @@ class Activity
end

def calculate_score_and_set
words = description.to_s.split(/\W+/)
self.auto_score = case words.length
when 0..25 then 0
when 26..40 then 1
else 2
end
# Scoring of only opened_issue, reopened_issue and created_comment should be done, for others score should be zero
# Here event_type would be either issue or comment.
# For event_type :issue and event_action ["opened", "reopened"] scoring should be done
# And for event_type :comment and event_action ["created"] scoring should be done
if ActivitiesFetcher::TRACKING_EVENTS.values.include?(event_type) and eval "#{event_type.upcase}_CONSIDERED_FOR_SCORING.include?(event_action)"
words = description.to_s.split(/\W+/)
self.auto_score = case words.length
when 0..25 then 0
when 26..40 then 1
else 2
end
else
self.auto_score = 0
end
self.save
end

Expand Down
10 changes: 5 additions & 5 deletions test/controllers/judging_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,35 @@ def setup
super
get_stub
@goal = create :goal
@round = create :round, name: 'first', from_date: Date.today.beginning_of_month, end_date: Date.today.end_of_month, status: :open
@round = create :round, :open, name: 'first'
@user = create :user, auth_token: Faker::Lorem.word, goal: @goal, is_judge: true
@org = create :organization
@org.users << @user
@repo = create :repository_with_activity_and_commits, organization: @org
end

test 'commits' do
old_commit = create :commit, commit_date: @round.from_date - 1.day
commit = create :commit
sign_in @user
get :commits
assert_response :success
assert_template :commits

commits = assigns(:commits)
assert commits.count, 1
assert_not commits.include? old_commit
assert commits.include? commit
end

test 'activities' do
old_activity = create :activity, commented_on: @round.from_date - 1.day
activity = create :activity
sign_in @user
get :activities
assert_response :success
assert_template :activities

activities = assigns(:activities)
assert activities.count, 1
assert_not activities.include? old_activity
assert activities.include? activity
end

test 'comments' do
Expand Down
9 changes: 9 additions & 0 deletions test/factories/activities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,14 @@
commented_on { DateTime.now }
association :user
association :round

trait :issue do
event_type :issue
end

trait :comment do
event_type :comment
end

end
end
69 changes: 47 additions & 22 deletions test/models/activity_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,34 @@

class ActivityTest < ActiveSupport::TestCase

def setup
super
@round_1 = create :round, :open
end

test "score should be 0 if total no of words are less than 25" do
activity = create(:activity, description: Faker::Lorem.words, user: create(:user))
activity = create(:activity, :comment, description: Faker::Lorem.words, user: create(:user), event_action: 'created')
assert_equal activity.auto_score, nil
activity.calculate_score_and_set
assert_equal activity.auto_score, 0
end

test "score should be 1 if no of words are greater than 25 but less than 40" do
activity = create(:activity, description: Faker::Lorem.words(30, true).join(' '), user: create(:user))
activity = create(:activity, :comment, description: Faker::Lorem.words(30, true).join(' '), user: create(:user), event_action: 'created')
assert_equal activity.auto_score, nil
activity.calculate_score_and_set
assert_equal activity.auto_score, 1
end

test "score should be 2 if no of words greater than 40" do
activity = create(:activity, description: Faker::Lorem.words(45, true).join(' '), user: create(:user))
activity = create(:activity, :issue, description: Faker::Lorem.words(45, true).join(' '), user: create(:user), event_action: 'opened')
assert_equal activity.auto_score, nil
activity.calculate_score_and_set
assert_equal activity.auto_score, 2
end

test "activity max rating should be 2" do
activity = create(:activity, description: Faker::Lorem.words, user: create(:user))
activity = create(:activity, :comment, description: Faker::Lorem.words, user: create(:user), event_action: 'created')
max_rating = 2
assert_equal activity.max_rating, max_rating
end
Expand All @@ -35,44 +40,64 @@ class ActivityTest < ActiveSupport::TestCase
end

test "activities count of user should be incremented after activity is created" do
activity = create(:activity, description: Faker::Lorem.words)
activity = create(:activity, :issue, description: Faker::Lorem.words)
assert_equal activity.user.activities_count, 1
end

test "consider for scoring scope should not retrive closed event_actions" do
opened_activity = create(:activity, description: Faker::Lorem.words, event_type: 'issue', event_action: 'opened')
closed_activity = create(:activity, description: Faker::Lorem.words, event_type: 'issue', event_action: 'closed')
assert_equal Activity.considered_for_scoring.count, 1
assert_equal Activity.considered_for_scoring.first, opened_activity
opened_issue = create(:activity, :issue, description: Faker::Lorem.words, event_action: 'opened')
closed_issue = create(:activity, :issue, description: Faker::Lorem.words, event_action: 'closed')
created_comment = create(:activity, :comment, description: Faker::Lorem.words, event_action: 'created')
deleted_comment = create(:activity, :comment, description: Faker::Lorem.words, event_action: 'deleted')
edited_comment = create(:activity, :comment, description: Faker::Lorem.words, event_action: 'edited')
activities = Activity.considered_for_scoring.to_a
assert_equal activities.count, 2
assert_includes activities, opened_issue
assert_includes activities, created_comment
refute_includes activities, closed_issue
refute_includes activities, edited_comment
refute_includes activities, deleted_comment
end

test "scoring scope should retrive created comment event_actions" do
created_comment = create(:activity, description: Faker::Lorem.words, event_type: 'comment', event_action: 'created')
opened_activity = create(:activity, description: Faker::Lorem.words, event_type: 'issue', event_action: 'opened')
closed_activity = create(:activity, description: Faker::Lorem.words, event_type: 'issue', event_action: 'closed')
assert_equal Activity.considered_for_scoring.count, 2
test "scoring of only opened and reopened issue should be done" do
description = "CodeCuriosity is a platform that encourages contributions to open source. Everyone is rewarded for their efforts, no matter how big or small they are. This is not a winner takes all competition"
closed_issue = create(:activity, :issue, description: description, event_action: 'closed')
opened_issue = create(:activity, :issue, description: description, event_action: 'opened')
reopened_issue = create(:activity, :issue, description: description, event_action: 'reopened')
assert_equal 3, Activity.count

closed_issue.calculate_score_and_set
opened_issue.calculate_score_and_set
reopened_issue.calculate_score_and_set

assert_equal closed_issue.auto_score, 0
assert_equal opened_issue.auto_score, 1
assert_equal reopened_issue.auto_score, 1
end

test "scoring scope should not retrive edited or deleted comment event_actions" do
edited_comment = create(:activity, description: Faker::Lorem.words, event_type: 'comment', event_action: 'edited')
deleted_comment = create(:activity, description: Faker::Lorem.words, event_type: 'comment', event_action: 'deleted')
opened_activity = create(:activity, description: Faker::Lorem.words, event_type: 'issue', event_action: 'opened')
closed_activity = create(:activity, description: Faker::Lorem.words, event_type: 'issue', event_action: 'closed')
assert_equal Activity.considered_for_scoring.count, 1
test "scoring of only created comment should be done" do
description = "CodeCuriosity is a platform that encourages contributions to open source. Everyone is rewarded for their efforts, no matter how big or small they are. This is not a winner takes all competition"
created_comment = create(:activity, :comment, description: description, event_action: 'created')
edited_comment = create(:activity, :comment, description: description, event_action: 'edited')
created_comment.calculate_score_and_set
edited_comment.calculate_score_and_set

assert_equal created_comment.auto_score, 1
assert_equal edited_comment.auto_score, 0
end

test "round should be present" do
activity = create(:activity, description: Faker::Lorem.words)
activity = create(:activity, :issue, description: Faker::Lorem.words)
assert activity.round.present?
end

test "proper round is assigned" do
skip "this feature needs to be added"
round_1 = create :round, from_date: Date.today.beginning_of_month, end_date: Date.today.end_of_month
round_2 = create :round, from_date: Date.today.beginning_of_month - 1.month, end_date: Date.today.end_of_month - 1.month
activity_1 = create :activity, description: Faker::Lorem.words, commented_on: Date.today, round: nil
assert_equal activity_1.round, round_1
activity_2 = create :activity, description: Faker::Lorem.words, commented_on: Date.today - 1.month, round: nil
assert_equal activity_2.round, round_2
end

end
3 changes: 3 additions & 0 deletions test/models/commit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
class CommitTest < ActiveSupport::TestCase

def setup
super
create :round, :open
stub_get("/repos/joshsoftware/code-curiosity/commits/eb0df748bbf084ca522f5ce4ebcf508d16169b96")
.to_return(body: File.read('test/fixtures/commit.json'), status: 200,
headers: {content_type: "application/json; charset=utf-8"})
Expand Down Expand Up @@ -50,6 +52,7 @@ def test_round_is_present
end

def test_proper_round_is_assigned
skip "pending feature"
round_1 = create :round, from_date: Date.today.beginning_of_month, end_date: Date.today.end_of_month
round_2 = create :round, from_date: Date.today.beginning_of_month - 1.month, end_date: Date.today.end_of_month - 1.month
commit_1 = create(:commit, message: Faker::Lorem.sentences, commit_date: Date.today, round: nil)
Expand Down
1 change: 1 addition & 0 deletions test/models/hackathon_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class HackathonTest < ActiveSupport::TestCase

def setup
Sidekiq::Testing.fake!
create :round, :open
@hackathon = create(:hackathon, user: create(:user))
@hackathon_r = create(:hackathon_with_repositories, user: create(:user))

Expand Down
2 changes: 2 additions & 0 deletions test/models/organization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
class OrganizationTest < ActiveSupport::TestCase

def setup
super
create :round, :open
stub_get("/orgs/joshsoftware").to_return(body: File.read('test/fixtures/org.json'), status: 200,
headers: {content_type: "application/json; charset=utf-8"})
end
Expand Down
28 changes: 14 additions & 14 deletions test/models/subscription_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
require "test_helper"

class SubscriptionTest < ActiveSupport::TestCase
def setup
super
create :round, :open
end

def test_commits_count_will_be_zero_if_no_commit_exist
user = create(:user)
Expand Down Expand Up @@ -34,35 +38,31 @@ def test_activities_count_should_not_be_zero_after_creating_activities

def test_commits_score
user = create(:user)
round = create(:round)
subscription = create(:subscription, user: user, round: round)
commit = create_list(:commit, 2, :auto_score => 2, :commit_date => Faker::Time.between(DateTime.now - 1, DateTime.now), user: user, round: round)
subscription = create(:subscription, user: user, round: Round.opened)
commit = create_list(:commit, 2, :auto_score => 2, :commit_date => Faker::Time.between(DateTime.now - 1, DateTime.now), user: user, round: Round.opened)
assert_equal subscription.commits_score, 4
end

def test_total_activities_score
user = create(:user)
round = create(:round)
subscription = create(:subscription, user: user, round: round)
activity = create_list(:activity, 3, :auto_score => 1, user:user, round: round)
create(:activity, event_action: :closed, user: user, round: round, auto_score: 1)
subscription = create(:subscription, user: user, round: Round.opened)
activity = create_list(:activity, 3, :auto_score => 1, user:user)
create(:activity, event_action: :closed, user: user, auto_score: 1)
assert_equal subscription.activities_score, 0
end

def test_total_activities_score_when_event_type_is_comment_and_event_action_is_created
user = create(:user)
round = create(:round)
subscription = create(:subscription, user: user, round: round)
create(:activity, event_type: :comment, event_action: :created, user: user, round: round, auto_score: 2)
subscription = create(:subscription, user: user, round: Round.opened)
create(:activity, event_type: :comment, event_action: :created, user: user, auto_score: 2)
assert_equal subscription.activities_score, 2
end

def test_update_total_points
user = create(:user)
round = create(:round)
subscription = create(:subscription, :points => 0, user: user, round: round)
commit = create_list(:commit, 2, :auto_score => 2, user: user, round: round)
activity = create_list(:activity, 3, :auto_score => 1, user:user, round: round)
subscription = create(:subscription, :points => 0, user: user, round: Round.opened)
commit = create_list(:commit, 2, :auto_score => 2, user: user)
activity = create_list(:activity, 3, :auto_score => 1, user:user)
subscription.update_points
total_points = subscription.commits_score + subscription.activities_score
assert_equal subscription.points, total_points
Expand Down

0 comments on commit a33abb2

Please sign in to comment.