Skip to content

Commit

Permalink
refs #88 Added more tests for Hackathons
Browse files Browse the repository at this point in the history
  • Loading branch information
gautamrege committed Aug 25, 2016
1 parent 1bedd31 commit b7e2972
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ gem 'simplecov', :require => false, :group => :test

group :development, :test do
gem 'byebug'
gem 'web-console', '~> 2.0'
gem 'spring'
gem 'minitest'
gem "minitest-rails", "~> 2.2.0"
Expand All @@ -58,6 +57,7 @@ group :test do
end

group :development do
gem 'web-console', '~> 2.0'
gem 'haml-rails'
gem 'quiet_assets'
end
4 changes: 4 additions & 0 deletions app/models/hackathon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ class Hackathon < Subscription
field :update_interval, type: Integer, default: 15

belongs_to :group

# Keep the repos information only in the Hackathon object.
# This will ensure we don't modify existing structure!
has_and_belongs_to_many :repositories, inverse_of: nil
end
4 changes: 2 additions & 2 deletions app/models/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ def activities_count
end

def commits_score
self.round.commits.where(user: user).inject(0){|r, o| r += o.final_score.to_i; r }
self.round.commits.where(user: user).inject(0){|r, o| r + o.final_score.to_i }
end

def activities_score
self.round.activities.where(user: user).inject(0){|r, o| r += o.final_score.to_i; r }
self.round.activities.where(user: user).inject(0){|r, o| r + o.final_score.to_i }
end

def update_points
Expand Down
9 changes: 3 additions & 6 deletions test/factories/hackathons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@
group
association :round, :hackathon, status: "inactive"

after(:create) do |hackathon, evaluator|
hackathon.group.owner_id = hackathon.user.id
hackathon.round.name = hackathon.group.name = "#{hackathon.user.name}'s Hackathon"
end

factory :hackathon_with_repositories do
transient do
repos_count 3
end

after(:create) do |hackathon, evaluator|
create_list(:repositories, evaluator.repos_count, hackathon: hackathon)
evaluator.repos_count.times do
hackathon.repositories << create(:repository_with_activity_and_commits)
end
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions test/factories/repositories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,17 @@
source_url { Faker::Internet.url('github.com', "/#{Faker::Lorem.word}/#{Faker::Lorem.word}") }
description { Faker::Lorem.sentence }
watchers {Faker::Number.digit}

factory :repository_with_activity_and_commits do
transient do
count 2
auto_score 2
end

after(:create) do |repo, evaluator|
create_list(:commit, evaluator.count, repository: repo, auto_score: evaluator.auto_score)
create_list(:activity, evaluator.count, repository: repo, auto_score: evaluator.auto_score)
end
end
end
end
65 changes: 45 additions & 20 deletions test/models/hackathon_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,80 @@ class HackathonTest < ActiveSupport::TestCase
def setup
Sidekiq::Testing.fake!
@hackathon = create(:hackathon, user: create(:user))
end

test "is_valid" do
assert(@hackathon.valid?)
assert(@hackathon.round.valid?)
assert(@hackathon.group.valid?)
end
@hackathon_r = create(:hackathon_with_repositories, user: create(:user))

test "does_not_create_any_goal" do
# Simulate commits and activities for this user (total user points should be: 6)
create_list(:commit, 2, repository: @hackathon_r.repositories.first,
auto_score: 2, user: @hackathon_r.user, round: @hackathon_r.round)
create_list(:activity, 2, repository: @hackathon_r.repositories.last,
auto_score: 1, user: @hackathon_r.user, round: @hackathon_r.round)
end

test "has_valid_hackathon_group" do
end

test "is_valid_if_there_is_only_group_admin_and_no_members_in_the_hackathon_group" do
test "is_valid" do
assert @hackathon.valid?
assert @hackathon.group.valid?
assert @hackathon.round.valid?
assert_equal @hackathon.round.status, "inactive"
assert_empty @hackathon.repositories
assert_not_empty @hackathon_r.repositories
end

test "has_valid_hackathon_round" do
test "round_name_and_group_name_should_be_the_same" do
assert_not_empty @hackathon.round.name
assert_not_empty @hackathon.group.name
assert_equal @hackathon.round.name, @hackathon.group.name
end

test "hackathon_round_has_initial_state_as_inactive" do
test "does_not_create_any_goal" do
assert_nil @hackathon.goal
assert_nil @hackathon_r.goal
end

test "sidekiq_job_should_be_enqueued_to_open_hackathon_round_at_start_datetime" do
end

test "update_interval_can_be_updated_if_hackathon_round_is_inactive" do
@hackathon.update_attribute(:update_interval, 5)
assert @hackathon.valid?
assert_equal 5, @hackathon.update_interval
end

test "update_interval_cannot_be_updated_if_hackathon_round_is_open" do
@hackathon.round.update_attribute(:status, "open")
@hackathon.update_attribute(:update_interval, 5)
assert @hackathon.valid? == false
assert_not_empty @hackathon.errors[:update_interval]
assert_equal 15, @hackathon.update_interval
end

test "update_interval_cannot_be_updated_if_hackathon_round_is_closed" do
@hackathon.round.update_attribute(:status, "close")
@hackathon.update_attribute(:update_interval, 5)
assert @hackathon.valid? == false
assert_not_empty @hackathon.errors[:update_interval]
assert_equal 15, @hackathon.update_interval
end

test "is_valid_if_repositories_array_is_blank" do
end

test "is_valid_if_repositories_array_has_valid_repository_ids" do
test "repositories_should_not_reference_hackathon" do #check inverse_of: nil
@hackathon_r.repositories.each do |r|
assert_equal false, r.has_attribute?(:hackathon_ids)
end
assert_raises(NoMethodError) { @hackathon_r.repositories.first.hackathons.first }
end

test "repositories_should_not_reference_hackathon" do#check inverse_of: nil
test "repositories_can_be_added_to_hackathon_if_status_is_open" do
@hackathon_r.round.update_attribute(:status, "open")
@hackathon_r.repositories << create(:repository)
assert_equal 4, @hackathon_r.repositories.count
end

# Need to add test cases to add repositories.
test "points_is_updated_if_round_spans_different_months" do # eg. Hackathon is from 30-Mar till 2-Apr
end

test "points_is_updated_for_commits_and_activity_only_for_hackathon_repositories" do
dummy = create(:repository_with_activity_and_commits)
@hackathon_r.update_points
assert_equal 6, @hackathon_r.points
end

test "points_is_updated_for_all_commits_and_activity_if_hackathon_repositories_is_blank" do
Expand Down

0 comments on commit b7e2972

Please sign in to comment.