From ec9b1eddafa035e8dbc043700bfff71fc02a4263 Mon Sep 17 00:00:00 2001 From: Gautam Rege Date: Thu, 25 Aug 2016 09:02:49 +0530 Subject: [PATCH] refs #88 Added more tests for Hackathons --- Gemfile | 2 +- app/models/hackathon.rb | 4 +++ app/models/subscription.rb | 4 +-- test/factories/hackathons.rb | 9 ++--- test/factories/repositories.rb | 12 +++++++ test/models/hackathon_test.rb | 65 +++++++++++++++++++++++----------- 6 files changed, 67 insertions(+), 29 deletions(-) diff --git a/Gemfile b/Gemfile index 527faaff..953e93cf 100644 --- a/Gemfile +++ b/Gemfile @@ -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" @@ -58,6 +57,7 @@ group :test do end group :development do + gem 'web-console', '~> 2.0' gem 'haml-rails' gem 'quiet_assets' end diff --git a/app/models/hackathon.rb b/app/models/hackathon.rb index b77a590a..78528c63 100644 --- a/app/models/hackathon.rb +++ b/app/models/hackathon.rb @@ -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 diff --git a/app/models/subscription.rb b/app/models/subscription.rb index 14861506..41a2a6d6 100644 --- a/app/models/subscription.rb +++ b/app/models/subscription.rb @@ -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 diff --git a/test/factories/hackathons.rb b/test/factories/hackathons.rb index c135c483..06c4eb21 100644 --- a/test/factories/hackathons.rb +++ b/test/factories/hackathons.rb @@ -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 diff --git a/test/factories/repositories.rb b/test/factories/repositories.rb index 905f1b2f..6b5293d0 100644 --- a/test/factories/repositories.rb +++ b/test/factories/repositories.rb @@ -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 diff --git a/test/models/hackathon_test.rb b/test/models/hackathon_test.rb index 8dadf6cd..7b38e062 100644 --- a/test/models/hackathon_test.rb +++ b/test/models/hackathon_test.rb @@ -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