Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • portalmec/portalmec
  • rfhferreira/cleanning-portalmec
2 results
Show changes
Showing
with 0 additions and 634 deletions
# == Schema Information
#
# Table name: object_types
#
# id :integer not null, primary key
# name :string
#
require 'test_helper'
class ObjectTypeTest < ActiveSupport::TestCase
should have_many :learning_objects
should validate_presence_of(:name)
should validate_uniqueness_of(:name)
end
# == Schema Information
#
# Table name: rates
#
# id :integer not null, primary key
# approves :boolean
# user_id :integer
# review_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
require 'test_helper'
class RateTest < ActiveSupport::TestCase
should belong_to :user
should belong_to :review
# TODO: need to stub review.user in cannot_rate_your_own method
# should validate_presence_of(:user)
# should validate_presence_of(:review)
# should validate_inclusion_of(:approves).in_array([true, false])
# TODO: shoulda try to insert user with id 0, but that's non existent and not allowed by database
# should validate_uniqueness_of(:user).scoped_to(:review)
end
# == Schema Information
#
# Table name: ratings
#
# id :integer not null, primary key
# name :string
# created_at :datetime not null
# updated_at :datetime not null
# description :string
#
require 'test_helper'
class RatingTest < ActiveSupport::TestCase
should have_many :review_ratings
should have_many(:reviews).through(:review_ratings)
end
# == Schema Information
#
# Table name: review_ratings
#
# id :integer not null, primary key
# review_id :integer
# rating_id :integer
# value :integer
# created_at :datetime not null
# updated_at :datetime not null
#
require 'test_helper'
class ReviewRatingTest < ActiveSupport::TestCase
should belong_to :review
should belong_to :rating
should validate_presence_of(:rating)
should validate_inclusion_of(:value).in_array(Array(1..5)).with_message('Review Rating must be between 1 and 5.')
# TODO: shoulda try to insert user with id 0, but that's non existent and not allowed by database
# should validate_uniqueness_of(:rating).scoped_to(:review_id)
end
# == Schema Information
#
# Table name: reviews
#
# id :integer not null, primary key
# name :string
# description :text
# pros :text
# cons :text
# reviewable_id :integer
# reviewable_type :string
# user_id :integer
# created_at :datetime not null
# updated_at :datetime not null
# rates_count :integer default("0")
#
require 'test_helper'
class ReviewTest < ActiveSupport::TestCase
should have_many :review_ratings
should have_many(:ratings).through(:review_ratings)
should have_many :rates
should have_many :complaints
should belong_to :reviewable
should belong_to :user
should validate_presence_of(:user)
should validate_presence_of(:reviewable)
should validate_inclusion_of(:reviewable_type).in_array(%w(LearningObject Collection)).with_message('Only LearningObjects and Collections are reviewable.')
end
# == Schema Information
#
# Table name: roles
#
# id :integer not null, primary key
# name :string
# description :text
# created_at :datetime not null
# updated_at :datetime not null
#
require 'test_helper'
class RoleTest < ActiveSupport::TestCase
# should have_and_belong_to_many(:users)
# should validate_uniqueness_of(:name)
# should validate_presence_of(:name)
end
# == Schema Information
#
# Table name: scores
#
# id :integer not null, primary key
# name :string
# code :string
# weight :float
# active :boolean default("true")
# created_at :datetime not null
# updated_at :datetime not null
# score_type :string default("{}"), is an Array
#
require 'test_helper'
class ScoreTest < ActiveSupport::TestCase
should have_many :score_user_categories
should have_many(:user_categories).through(:score_user_categories)
should validate_presence_of(:name)
should validate_presence_of(:code)
should validate_presence_of(:weight)
should validate_uniqueness_of(:code)
end
# == Schema Information
#
# Table name: score_user_categories
#
# id :integer not null, primary key
# score_id :integer
# user_category_id :integer
# value :float
# created_at :datetime not null
# updated_at :datetime not null
#
require 'test_helper'
class ScoreUserCategoryTest < ActiveSupport::TestCase
should belong_to :score
should belong_to :user_category
should validate_presence_of(:score)
should validate_presence_of(:user_category)
should validate_presence_of(:value)
# TODO: shoulda try to insert user_category with id 0, but that's non existent and not allowed by database
# should validate_uniqueness_of(:user_category).scoped_to(:score)
end
require 'test_helper'
class SearchTest < ActiveSupport::TestCase
should validate_presence_of(:query)
should validate_presence_of(:results_per_page)
should validate_presence_of(:order)
should validate_presence_of(:search_class)
test 'should validate search_class' do
search = Search.new(:search_class => 'invalid')
assert_not search.valid?
end
end
# == Schema Information
#
# Table name: shares
#
# id :integer not null, primary key
# shareable_id :integer
# shareable_type :string
# user_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
require 'test_helper'
class ShareTest < ActiveSupport::TestCase
should belong_to :user
should belong_to(:shareable).counter_cache(true)
should validate_presence_of(:user)
should validate_presence_of(:shareable)
end
require 'test_helper'
class TagTest < ActiveSupport::TestCase
should have_many :taggings
should validate_presence_of :name
test 'an user can tag a learning object' do
john = users(:john)
lo = learning_objects(:one)
assert john.tag(lo, with: ['Tag'])
# check lo_one tags
assert_count 2, lo.tags
assert_count 2, john.taggings
end
test 'an user can untag a learning object' do
john = users(:john)
lo = learning_objects(:one)
assert john.untag(lo, 'Tag')
# check lo_one tags
assert_count 1, lo.tags
assert_count 1, john.taggings
end
test 'an user can tag a collection' do
john = users(:john)
collection = collections(:ufpr)
assert john.tag(collection, with: ['Tag'])
# check ufpr tags
assert_count 3, collection.tags
assert_count 2, john.taggings
end
test 'an user can untag a collection' do
john = users(:john)
collection = collections(:ufpr)
assert john.untag(collection, 'Tag')
# check ufpr tags
assert_count 2, collection.tags
assert_count 1, john.taggings
end
end
require 'test_helper'
class TaggingTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
# == Schema Information
#
# Table name: user_categories
#
# id :integer not null, primary key
# name :string
# reference :float
# created_at :datetime not null
# updated_at :datetime not null
#
require 'test_helper'
class UserCategoryTest < ActiveSupport::TestCase
should have_many :score_user_categories
should have_many(:scores).through(:score_user_categories)
should validate_presence_of(:name)
should validate_uniqueness_of(:name)
end
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# email :string default(""), not null
# encrypted_password :string default(""), not null
# reset_password_token :string
# reset_password_sent_at :datetime
# remember_created_at :datetime
# sign_in_count :integer default("0"), not null
# current_sign_in_at :datetime
# last_sign_in_at :datetime
# current_sign_in_ip :string
# last_sign_in_ip :string
# created_at :datetime not null
# updated_at :datetime not null
# provider :string default("email"), not null
# uid :string default(""), not null
# confirmation_token :string
# confirmed_at :datetime
# confirmation_sent_at :datetime
# unconfirmed_email :string
# tokens :text
# name :string
# avatar_file_name :string
# avatar_content_type :string
# avatar_file_size :integer
# avatar_updated_at :datetime
# bookmarks_count :integer default("0")
# user_category_id :integer
# score :float default("0.0")
# follows_count :integer default("0")
#
require 'test_helper'
class UserTest < ActiveSupport::TestCase
should have_and_belong_to_many(:roles)
should have_and_belong_to_many(:institutions)
should have_many :bookmarks
should have_many(:bookmark_collections).through(:bookmarks).source(:bookmarkable)
should have_many(:bookmark_learning_objects).through(:bookmarks).source(:bookmarkable)
should have_many :taggings
should have_many :collections
should have_many :learning_objects
should have_many :views
should have_many :downloads
should have_many :likes
should have_many :shares
should have_many :follows
should have_many :reviews
should have_many :applications
test 'an user can follow another user' do
john = users(:john)
jack = users(:jack)
assert john.follow(jack)
# check jack followers
assert_count 1, jack.followers
assert_count 1, john.follows
assert john.following? jack
end
test 'an user can follow a collection' do
john = users(:john)
ufpr_collection = collections(:ufpr)
assert john.follow(ufpr_collection)
# check ufpr followers
assert_count 1, ufpr_collection.followers
assert_count 1, john.follows
assert john.following? ufpr_collection
end
test 'an user can unfollow a collection' do
john = users(:john)
ufpr_collection = collections(:ufpr)
assert john.follow(ufpr_collection)
# check ufpr followers
assert_count 1, ufpr_collection.followers
assert_count 1, john.follows
assert john.following? ufpr_collection
john.unfollow(ufpr_collection)
# check ufpr followers
assert_count 0, ufpr_collection.followers
assert_count 0, john.follows
assert_not john.following? ufpr_collection
end
end
# == Schema Information
#
# Table name: views
#
# id :integer not null, primary key
# viewable_id :integer
# viewable_type :string
# user_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
require 'test_helper'
class ViewTest < ActiveSupport::TestCase
should belong_to :user
should belong_to(:viewable).counter_cache(true)
should validate_presence_of(:user)
should validate_presence_of(:viewable)
end
require 'test_helper'
class FeedbackServiceTest < ActiveSupport::TestCase
PORTALMEC_PROJECT_ID = 633
mock = MiniTest::Mock.new
report_service = GitlabBugreporterService.new(mock)
feedback = FeedbackService.new(report_service)
test 'initializing bug reporter service' do
assert_equal feedback.get_report_service, report_service
end
test 'reporting bug' do
bug = Bug.new
bug.title = 'Título'
bug.description = 'Descrição do bug'
mock.expect(:create_issue, nil, [PORTALMEC_PROJECT_ID, bug.title, description: bug.description])
assert feedback.report_bug(bug)
end
end
require 'test_helper'
class InstitutionImporterTest < ActiveSupport::TestCase
test 'importing learning object with metadata publisher = nil' do
importer = InstitutionImporter.new()
lo = learning_objects(:lo_complete)
importer.items = [lo]
importer.import
assert_not_nil Institution.find_by_name(lo.publisher.name)
end
test 'importing learning object with metadata publisher != nil' do
importer = InstitutionImporter.new()
lo = learning_objects(:lo_metadata)
importer.items = [lo]
importer.import
assert_not_nil Institution.find_by_name(lo.publisher.name)
end
test 'importing learning object with institution that already exists' do
importer = InstitutionImporter.new()
lo1 = learning_objects(:lo_metadata)
lo2 = learning_objects(:lo_metadata2)
importer.items = [lo1, lo2]
importer.import
assert_equal Institution.find_by_name(lo1.publisher.name), Institution.find_by_name(lo2.publisher.name)
end
end
require 'test_helper'
class ScoreCalculatorServiceTest < ActiveSupport::TestCase
test 'reputation calculation' do
hash_stub = {
'submitted_objects': 60,
'reviews_score_average': 4.8,
'learning_objects_in_best_collections': 4,
'learning_objects_best_score': 736,
'learning_objects_average_score': 680,
'review_approval_average': 98,
'followers_count': 300,
'learning_objects_recently_submitted': 5,
'collections_score_average': 700
}
calculator = ScoreCalculatorService.new(users(:one))
calculator.stub :object_hash, hash_stub do
assert_equal 0.677361, calculator.calculate
end
end
test 'score calculation' do
hash_stub = {
'normalized_review_average': (5.0 / 5.0),
'normalized_thumbnail': 1,
'normalized_views': 0.1,
'normalized_likes': 0.9311,
'normalized_downloads': 0.125,
'normalized_shares': 0.1,
'normalized_description': 1,
'user_category': (3.0 / 5.0),
'normalized_collected': 0.5,
'normalized_bookmarked': 0.56
}
calculator = ScoreCalculatorService.new(learning_objects(:one))
calculator.stub :object_hash, hash_stub do
assert_equal 0.656472, calculator.calculate
end
end
end
\ No newline at end of file
require 'test_helper'
class SearchServiceTest < ActiveSupport::TestCase
test 'fetch learning object with all search params' do
reindex LearningObject
search = SearchService.search(Search.new(los_complete_search), users(:john))
search.each {|o| assert_instance_of LearningObject, o}
end
test 'fetch all public collections' do
reindex Collection
search = SearchService.search(Search.new(collections_search), users(:john))
search.each {|o| assert_instance_of Collection, o}
end
test 'fetch users named john' do
reindex User
search = SearchService.search(Search.new(users_john_search), users(:john))
assert_equal [users(:john), users(:one)], search
end
test 'autocomplete users named john' do
reindex User
search = SearchService.autocomplete(Search.new(users_john), users(:john))
assert_equal users_autocomplete_hashes([users(:john), users(:one)]), search
end
private
def los_complete_search
default_params.merge(
query: 'teste',
search_class: 'LearningObject',
tags: 'Matemática',
types: 'Imagem'
)
end
def collections_search
default_params.merge(search_class: 'Collection')
end
def users_john_search
default_params.merge(users_john)
end
def users_john
{
query: 'John',
search_class: 'User'
}
end
def default_params
{
page: 1,
results_per_page: 10,
order: 'score'
}
end
def reindex(klass)
klass.reindex
klass.searchkick_index.refresh
end
def users_autocomplete_hashes(users = [])
users.map do |user|
{
id: user.id,
name: user.name,
type: 'User',
thumbnail: user.avatar.url(:thumb)
}
end
end
end
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'minitest/mock'
require 'minitest/reporters'
# disable public activity gem for testing purpouses
require 'public_activity/testing'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# load seeds
# Rails.application.load_seed
# Add more helper methods to be used by all tests here...
def assert_count(expected, actual = 0)
assert_equal expected.to_i, actual.count
end
# authenticate requests using devise_token_auth
def auth_request(user)
auth_application
sign_in user
@request.headers.merge!(user.create_new_auth_token)
end
def auth_application
@request.headers['PortalMEC-AppID'] = applications(:test_app).application_id
end
def mock
MiniTest::Mock.new
end
# config.after :all do
# ActiveRecord::Base.subclasses.each(&:delete_all)
# end
Minitest::Reporters.use!
PublicActivity.enabled = false
end