community_rating_test.rb 1.38 KB
require 'test_helper'

class CommunityRatingTest < ActiveSupport::TestCase
  test "The value must be between 1 and 5" do
    cr1 = CommunityRating.new :value => -1
    cr2 = CommunityRating.new :value => 6

    assert_equal false, cr1.valid?
    assert_equal false, cr2.valid?

    assert_equal true, cr1.errors[:value].include?("must be between 1 and 5")
    assert_equal true, cr2.errors[:value].include?("must be between 1 and 5")

    cr1.value = 1
    cr1.valid?

    cr2.value = 5
    cr2.valid?

    assert_equal false, cr1.errors[:value].include?("must be between 1 and 5")
    assert_equal false, cr2.errors[:value].include?("must be between 1 and 5")
  end

  test "Should calculate community's rating average" do
    community = fast_create Community
    p1 = fast_create Person, :name=>"Person 1"
    p2 = fast_create Person, :name=>"Person 2"
    p3 = fast_create Person, :name=>"Person 3"

    CommunityRating.create! :value => 2, :community => community, :person => p1
    CommunityRating.create! :value => 3, :community => community, :person => p2
    CommunityRating.create! :value => 5, :community => community, :person => p3

    assert_equal 3, CommunityRating.average_rating(community)

    p4 = fast_create Person, :name=>"Person 4"
    CommunityRating.create! :value => 4, :community => community, :person => p4

    assert_equal 4, CommunityRating.average_rating(community)
  end
end