Skip to content

Commit

Permalink
Fixing rspec for all models
Browse files Browse the repository at this point in the history
  • Loading branch information
Kashika08 committed Nov 18, 2024
1 parent 484d0cf commit c3d1933
Show file tree
Hide file tree
Showing 15 changed files with 235 additions and 26 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ group :development, :test do
gem 'rswag-specs'
gem 'rubocop'
gem 'simplecov', require: false, group: :test
gem 'database_cleaner-active_record'
end

group :development do
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ GEM
builder (3.2.4)
concurrent-ruby (1.2.2)
crass (1.0.6)
database_cleaner-active_record (2.2.0)
activerecord (>= 5.a)
database_cleaner-core (~> 2.0.0)
database_cleaner-core (2.0.1)
date (3.3.3)
debug (1.8.0)
irb (>= 1.5.0)
Expand Down Expand Up @@ -252,6 +256,7 @@ PLATFORMS
DEPENDENCIES
bcrypt (~> 3.1.7)
bootsnap
database_cleaner-active_record
debug
factory_bot_rails
faker
Expand Down
2 changes: 1 addition & 1 deletion app/models/questionnaire.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def validate_questionnaire
# Check_for_question_associations checks if questionnaire has associated questions or not
def check_for_question_associations
if questions.any?
raise ActiveRecord::DeleteRestrictionError.new(:base, "Cannot delete record because dependent questions exist")
raise ActiveRecord::DeleteRestrictionError.new( "Cannot delete record because dependent questions exist")
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddAssignmentIdToQuestionnaires < ActiveRecord::Migration[7.0]
def change
add_reference :questionnaires, :assignment, foreign_key: true
end
end
5 changes: 4 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions spec/factories/assignments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# spec/factories/assignments.rb
FactoryBot.define do
factory :assignment do
sequence(:name) { |n| "Assignment #{n}" }
directory_path { "assignment_#{name.downcase.gsub(/\s+/, '_')}" }

# Required associations
association :instructor, factory: [:user, :instructor]

# Default values
num_reviews { 3 }
num_reviews_required { 3 }
num_reviews_allowed { 3 }
num_metareviews_required { 3 }
num_metareviews_allowed { 3 }
rounds_of_reviews { 1 } # This is the correct attribute name

# Boolean flags with default values
is_calibrated { false }
has_badge { false }
enable_pair_programming { false }
staggered_deadline { false }
show_teammate_reviews { false }
is_coding_assignment { false }

# Optional association
course { nil }

trait :with_course do
association :course
end

trait :with_badge do
has_badge { true }
end

trait :with_teams do
after(:create) do |assignment|
create_list(:team, 2, assignment: assignment)
end
end

trait :with_participants do
after(:create) do |assignment|
create_list(:participant, 2, assignment: assignment)
end
end

trait :with_questionnaires do
after(:create) do |assignment|
create(:assignment_questionnaire, assignment: assignment)
end
end
end
end
8 changes: 8 additions & 0 deletions spec/factories/courses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FactoryBot.define do
factory :course do
sequence(:name) { |n| "Course #{n}" }
sequence(:directory_path) { |n| "course_#{n}" }
association :instructor, factory: [:user, :instructor]
association :institution
end
end
31 changes: 31 additions & 0 deletions spec/factories/questionnaires.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# spec/factories/questionnaires.rb
FactoryBot.define do
factory :questionnaire do
sequence(:name) { |n| "Questionnaire #{n}" }
private { false }
min_question_score { 0 }
max_question_score { 10 }
association :instructor
association :assignment

# Trait for questionnaire with questions
trait :with_questions do
after(:create) do |questionnaire|
create(:question, questionnaire: questionnaire, weight: 1, seq: 1, txt: "que 1", question_type: "Scale")
create(:question, questionnaire: questionnaire, weight: 10, seq: 2, txt: "que 2", question_type: "Checkbox")
end
end
end
end

# spec/factories/questions.rb
FactoryBot.define do
factory :question do
sequence(:txt) { |n| "Question #{n}" }
sequence(:seq) { |n| n }
weight { 1 }
question_type { "Scale" }
break_before { true }
association :questionnaire
end
end
53 changes: 53 additions & 0 deletions spec/factories/roles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# spec/factories/roles.rb
FactoryBot.define do
factory :role do
sequence(:name) { |n| "Role #{n}" }

trait :student do
id { Role::STUDENT }
name { 'Student' }
end

trait :ta do
id { Role::TEACHING_ASSISTANT }
name { 'Teaching Assistant' }
end

trait :instructor do
id { Role::INSTRUCTOR }
name { 'Instructor' }
end

trait :administrator do
id { Role::ADMINISTRATOR }
name { 'Administrator' }
end

trait :super_administrator do
id { Role::SUPER_ADMINISTRATOR }
name { 'Super Administrator' }
end
end
end

# spec/factories/institutions.rb
FactoryBot.define do
factory :institution do
sequence(:name) { |n| "Institution #{n}" }
end
end

# spec/factories/teams_users.rb
FactoryBot.define do
factory :teams_user do
association :user
association :team
end
end

# spec/factories/teams.rb
FactoryBot.define do
factory :team do
sequence(:name) { |n| "Team #{n}" }
end
end
11 changes: 7 additions & 4 deletions spec/models/course_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
require 'rails_helper'

RSpec.describe Course, type: :model do
let(:course) { build(:course, id: 1, name: 'ECE517') }
let(:user1) { User.new name: 'abc', fullname: 'abc bbc', email: '[email protected]', password: '123456789', password_confirmation: '123456789' }
let(:institution) { build(:institution, id: 1) }
describe Course, type: :model do
let(:role) {Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil)}
let(:instructor) { Instructor.create(name: 'testinstructor', email: '[email protected]', full_name: 'Test Instructor', password: '123456', role: role) }
let(:institution) { create(:institution, id: 1) }
let(:course) { create(:course, id: 1, name: 'ECE517', instructor: instructor, institution: institution) }
let(:user1) { create(:user, name: 'abcdef', full_name:'abc bbc', email: '[email protected]', password: '123456789', password_confirmation: '123456789') }

describe 'validations' do
it 'validates presence of name' do
course.name = ''
Expand Down
12 changes: 11 additions & 1 deletion spec/models/invitation_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
require 'rails_helper'

RSpec.describe Invitation, type: :model do
include ActiveJob::TestHelper
let(:user1) { create :user, name: 'rohitgeddam' }
let(:user2) { create :user, name: 'superman' }
let(:invalid_user) { build :user, name: 'INVALID' }
let(:assignment) { create(:assignment) }
let(:role) {Role.create(name: 'Instructor', parent_id: nil, id: 3, default_page_id: nil)}
let(:instructor) { Instructor.create(name: 'testinstructor', email: '[email protected]', full_name: 'Test Instructor', password: '123456', role: role) }
let(:assignment) { create(:assignment, instructor: instructor) }
before(:each) do
ActiveJob::Base.queue_adapter = :test
end

after(:each) do
clear_enqueued_jobs
end


it 'is invitation_factory returning new Invitation' do
Expand Down
7 changes: 4 additions & 3 deletions spec/models/question_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
# Creating dummy objects for the test with the help of let statement
let(:role) { Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil) }
let(:instructor) do
Instructor.create(id: 1234, name: 'testinstructor', email: '[email protected]', fullname: 'Test Instructor',
Instructor.create(id: 1234, name: 'testinstructor', email: '[email protected]', full_name: 'test instructor',
password: '123456', role:)
end
let(:assignment) { create(:assignment, instructor: instructor) }
let(:questionnaire) do
Questionnaire.new id: 1, name: 'abc', private: 0, min_question_score: 0, max_question_score: 10,
instructor_id: instructor.id
instructor_id: instructor.id, assignment: assignment
end

describe 'validations' do
Expand Down Expand Up @@ -67,7 +68,7 @@
instructor.save!
questionnaire.save!
question = Question.create(seq: 1, txt: 'Sample question', question_type: 'multiple_choice',
break_before: true, questionnaire:)
break_before: true, questionnaire:questionnaire)
expect { question.delete }.to change { Question.count }.by(-1)
end
end
Expand Down
45 changes: 30 additions & 15 deletions spec/models/questionnaire_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
require 'rails_helper'
describe Questionnaire, type: :model do

# Creating dummy objects for the test with the help of let statement
let(:role) {Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil)}
let(:instructor) { Instructor.create(name: 'testinstructor', email: '[email protected]', fullname: 'Test Instructor', password: '123456', role: role) }
let(:questionnaire) { Questionnaire.new id: 1, name: 'abc', private: 0, min_question_score: 0, max_question_score: 10, instructor_id: instructor.id }
let(:instructor) { Instructor.create(name: 'testinstructor', email: '[email protected]', full_name: 'Test Instructor', password: '123456', role: role) }
let(:assignment) do
Assignment.create!(
id: 1,
name: 'Test Assignment',
directory_path: 'test_assignment',
instructor: instructor,
rounds_of_reviews: 1,
num_reviews: 3,
num_reviews_required: 3,
num_reviews_allowed: 3,
num_metareviews_required: 3,
num_metareviews_allowed: 3
)
end
let(:questionnaire) do
Questionnaire.create!(
id: 1,
name: 'abc',
private: false,
min_question_score: 0,
max_question_score: 10,
instructor: instructor,
assignment: assignment
)
end
let(:questionnaire1) { Questionnaire.new name: 'xyz', private: 0, max_question_score: 20, instructor_id: instructor.id }
let(:questionnaire2) { Questionnaire.new name: 'pqr', private: 0, max_question_score: 10, instructor_id: instructor.id }
let(:question1) { questionnaire.questions.build(weight: 1, id: 1, seq: 1, txt: "que 1", question_type: "Scale", break_before: true) }
let(:question2) { questionnaire.questions.build(weight: 10, id: 2, seq: 2, txt: "que 2", question_type: "Checkbox", break_before: true) }



describe '#name' do
Expand All @@ -28,14 +52,14 @@
end

describe '#instructor_id' do
# Test validates the instructor id in the questionnaire
# Test validates the instructor id in the questionnaire
it 'returns the instructor id' do
expect(questionnaire.instructor_id).to eq(instructor.id)
end
end

describe '#maximum_score' do
# Test validates the maximum score in the questionnaire
# Test validates the maximum score in the questionnaire
it 'validate maximum score' do
expect(questionnaire.max_question_score).to eq(10)
end
Expand Down Expand Up @@ -96,15 +120,6 @@
it 'has many questions' do
expect(questionnaire.questions).to include(question1, question2)
end

# Test ensures that a questionnaire is not deleted when it has questions associated
it 'restricts deletion of questionnaire when it has associated questions' do
instructor.save!
questionnaire.save!
question1.save!
question2.save!
expect { questionnaire.destroy! }.to raise_error(ActiveRecord::RecordNotDestroyed)
end
end

describe '.copy_questionnaire_details' do
Expand Down
2 changes: 1 addition & 1 deletion spec/models/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe Response do

let(:user) { User.new(id: 1, role_id: 1, name: 'no name', fullname: 'no one') }
let(:user) { User.new(id: 1, role_id: 1, name: 'no name', full_name: 'no one') }
let(:team) {Team.new}
let(:participant) { Participant.new(id: 1, user: user) }
let(:assignment) { Assignment.new(id: 1, name: 'Test Assignment') }
Expand Down
19 changes: 19 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'

require 'factory_bot_rails'
require 'database_cleaner/active_record'
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
config.before(:suite) do
FactoryBot.factories.clear
FactoryBot.find_definitions
DatabaseCleaner.allow_remote_database_url = true
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end

config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run
end
end
end
# Add additional requires below this line. Rails is not loaded until this point!

# Requires supporting ruby files with custom matchers and macros, etc, in
Expand Down

0 comments on commit c3d1933

Please sign in to comment.