Skip to content

Commit

Permalink
test(duplication_tracing): add duplication test for lesson plan items
Browse files Browse the repository at this point in the history
  • Loading branch information
bivanalhar committed Mar 1, 2023
1 parent 7ce047e commit ff0d405
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 52 deletions.
9 changes: 0 additions & 9 deletions app/models/course/assessment/question/programming.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@ class Course::Assessment::Question::Programming < ApplicationRecord # rubocop:di
# The table name for this model is singular.
self.table_name = table_name.singularize

<<<<<<< HEAD
# Maximum CPU time a programming question can allow before the evaluation gets killed.
DEFAULT_CPU_TIMEOUT = 30.seconds
=======
# Default programming timeout limit, only will be used if course is undefined
DEFAULT_CPU_TIMEOUT = 30
>>>>>>> 9cd9c4429 (style: fix hound issue regarding code styling in backend side)

# Maximum memory (in MB) the programming question can allow.
# Do NOT change this to num.megabytes as the ProgramingEvaluationService expects it in MB.
Expand All @@ -38,11 +33,7 @@ class Course::Assessment::Question::Programming < ApplicationRecord # rubocop:di
validates :import_job_id, uniqueness: { allow_nil: true, if: :import_job_id_changed? }
validates :language, presence: true

<<<<<<< HEAD
validate -> { validate_time_limit }
=======
# validate -> { validate_time_limit(course) }
>>>>>>> b309137ff (feat(assessment_settings): setting permissions for programming timeout limit)
validate :validate_codaveri_question

belongs_to :import_job, class_name: TrackableJob::Job.name, inverse_of: nil, optional: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,8 @@ class << self
# @return [Course::Assessment::ProgrammingCodaveriEvaluationService::Result]
#
# @raise [Timeout::Error] When the operation times out.
<<<<<<< HEAD
<<<<<<< HEAD
def execute(course_title, question, answer, timeout = nil)
new(course_title, question, answer, timeout).execute
=======
def execute(question, answer, course, timeout = nil)
new(question, answer, course, timeout).execute
>>>>>>> b309137ff (feat(assessment_settings): setting permissions for programming timeout limit)
=======
def execute(question, answer, timeout = nil)
new(question, answer, timeout).execute
>>>>>>> 9cd9c4429 (style: fix hound issue regarding code styling in backend side)
end
end

Expand All @@ -103,25 +93,13 @@ def execute

private

<<<<<<< HEAD
def initialize(course_title, question, answer, timeout)
=======
def prog_timeout_lim(course)
course ? course.programming_timeout_limit : DEFAULT_CPU_TIMEOUT
end

def initialize(question, answer, timeout)
>>>>>>> 9cd9c4429 (style: fix hound issue regarding code styling in backend side)
@question = question
@answer = answer
@course = question.course
@language = question.language
@memory_limit = question.memory_limit || MEMORY_LIMIT
<<<<<<< HEAD
@time_limit = question.time_limit ? [question.time_limit, question.max_time_limit].min : question.max_time_limit
=======
@time_limit = question.time_limit ? [question.time_limit, prog_timeout_lim(@course)].min : prog_timeout_lim(@course)
>>>>>>> 9cd9c4429 (style: fix hound issue regarding code styling in backend side)
@timeout = timeout || DEFAULT_TIMEOUT

@answer_object = { api_version: 'latest',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ def import_from_package(package)
# @return [Course::Assessment::ProgrammingEvaluationService::Result]
def evaluate_package(package)
Course::Assessment::ProgrammingEvaluationService.
<<<<<<< HEAD
execute(@question.language, @question.memory_limit, @question.time_limit, @question.max_time_limit, package.path)
=======
execute(@question.course, @question, @question.time_limit, package.path)
>>>>>>> 9cd9c4429 (style: fix hound issue regarding code styling in backend side)
end

# Saves the templates and tests to the question.
Expand Down
1 change: 0 additions & 1 deletion lib/autoload/duplicator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ def duplicate_object(source_object) # rubocop:disable Metrics/AbcSize
source_id: source_object.acting_as.id,
creator: @options[:current_user],
updater: @options[:current_user])
byebug
end
end
end
Expand Down
23 changes: 22 additions & 1 deletion spec/factories/duplication_traceable_lesson_plan_items.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
# frozen_string_literal: true
FactoryBot.define do
factory :duplication_traceable_lesson_plan_items, class: DuplicationTraceable::LessonPlanItem.name do
factory :lesson_plan_item, class: Course::LessonPlan::Item.name do
course
base_exp { rand(1..10) * 100 }
time_bonus_exp { rand(1..10) * 100 }
start_at { 1.day.ago }
bonus_end_at { 1.day.from_now }
end_at { nil }
sequence(:title) { |n| "Example Lesson Plan Item #{n}" }
published { false }
has_personal_times { true }
affects_personal_times { true }

trait :with_bonus_end_time do
bonus_end_at { 2.days.from_now }
end

trait :with_end_time do
end_at { 3.days.from_now }
end
end

factory :duplication_traceable_lesson_plan_item, class: DuplicationTraceable::LessonPlanItem.name do
source { build(:lesson_plan_item) }
lesson_plan_item
end
Expand Down
4 changes: 0 additions & 4 deletions spec/models/course/assessment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
it { is_expected.to have_many(:submissions).dependent(:destroy) }
it { is_expected.to have_many(:conditions) }
it { is_expected.to have_many(:assessment_conditions).dependent(:destroy) }
it { is_expected.to have_one(:duplication_traceable).dependent(:destroy) }

it { should delegate_method(:source).to(:duplication_traceable).allow_nil }
it { should delegate_method(:source=).to(:duplication_traceable).with_arguments(nil).allow_nil }

let(:instance) { Instance.default }
with_tenant(:instance) do
Expand Down
4 changes: 4 additions & 0 deletions spec/models/course/lesson_plan/lesson_plan_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
without_validating_presence
end
it { is_expected.to have_many(:todos).inverse_of(:item).dependent(:destroy) }
it { is_expected.to have_one(:duplication_traceable).dependent(:destroy) }

it { should delegate_method(:source).to(:duplication_traceable).allow_nil }
it { should delegate_method(:source=).to(:duplication_traceable).with_arguments(nil).allow_nil }

let!(:instance) { Instance.default }
with_tenant(:instance) do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
# frozen_string_literal: true
require 'rails_helper'

RSpec.describe DuplicationTraceable::Assessment, type: :model do
RSpec.describe DuplicationTraceable::LessonPlanItem, type: :model do
it { is_expected.to act_as(DuplicationTraceable) }

let!(:instance) { Instance.default }
with_tenant(:instance) do
let(:course) { create(:course) }
let(:user) { create(:course_manager, course: course).user }
let(:assessment) { create(:assessment, :published_with_mcq_question, course: course) }
let(:lesson_plan_item) { create(:course_lesson_plan_item, course: course) }

subject do
build(:duplication_traceable_assessment, assessment: nil, source: nil)
build(:duplication_traceable_lesson_plan_item, lesson_plan_item: nil, source: nil)
end

describe 'validations' do
it 'validates the presence of a destination assessment' do
expect(subject).to_not be_valid
expect(subject.errors[:assessment]).not_to be_empty
expect(subject.errors[:lesson_plan_item]).not_to be_empty
end
end

describe '#source and #source=' do
it 'correctly reads and updates the source' do
expect(subject.source).to be(nil)
subject.source = assessment
expect(subject.source).to eq(assessment)
subject.source = lesson_plan_item
expect(subject.source).to eq(lesson_plan_item)
end
end

describe '.dependent_class' do
it 'returns Course::Assessment' do
expect(DuplicationTraceable::Assessment.dependent_class).to eq(Course::Assessment.name)
it 'returns Course::LessonPlan::Item' do
expect(DuplicationTraceable::LessonPlanItem.dependent_class).to eq(Course::LessonPlan::Item.name)
end
end

describe '.initialize_with_dest' do
it 'creates an instance with the dest initialized' do
traceable = DuplicationTraceable::Assessment.initialize_with_dest(assessment)
expect(traceable.assessment).to eq(assessment)
traceable = DuplicationTraceable::LessonPlanItem.initialize_with_dest(lesson_plan_item)
expect(traceable.lesson_plan_item).to eq(lesson_plan_item)
end

it 'passes in the other options correctly' do
traceable = DuplicationTraceable::Assessment.initialize_with_dest(assessment, creator: user)
traceable = DuplicationTraceable::LessonPlanItem.initialize_with_dest(lesson_plan_item, creator: user)
expect(traceable.creator).to eq(user)
end
end
Expand Down

0 comments on commit ff0d405

Please sign in to comment.