From de23aff8641cc3afa731f363a7b6e194e5f3acc0 Mon Sep 17 00:00:00 2001 From: bivanalhar Date: Tue, 28 Feb 2023 17:31:33 +0800 Subject: [PATCH] test(duplication_tracing): add duplication test for lesson plan items --- .../course/assessment/question/programming.rb | 13 ++--------- ...programming_codaveri_evaluation_service.rb | 23 ------------------- .../question/programming_import_service.rb | 4 ---- lib/autoload/duplicator.rb | 1 - ...duplication_traceable_lesson_plan_items.rb | 23 ++++++++++++++++++- spec/models/course/assessment_spec.rb | 4 ---- .../lesson_plan/lesson_plan_item_spec.rb | 4 ++++ ...sment_spec.rb => lesson_plan_item_spec.rb} | 22 +++++++++--------- 8 files changed, 39 insertions(+), 55 deletions(-) rename spec/models/duplication_traceable/{assessment_spec.rb => lesson_plan_item_spec.rb} (52%) diff --git a/app/models/course/assessment/question/programming.rb b/app/models/course/assessment/question/programming.rb index 16a6c31ed60..f30ce100611 100644 --- a/app/models/course/assessment/question/programming.rb +++ b/app/models/course/assessment/question/programming.rb @@ -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. @@ -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 @@ -187,7 +178,7 @@ def process_package # rubocop:disable Metrics/CyclomaticComplexity, Metrics/Perc def evaluate_package execute_after_commit do import_job = - Course::Assessment::Question::ProgrammingImportJob.perform_later(self, attachment, course) + Course::Assessment::Question::ProgrammingImportJob.perform_later(self, attachment) update_column(:import_job_id, import_job.job_id) end end @@ -203,7 +194,7 @@ def process_new_package execute_after_commit do new_attachment.save! import_job = - Course::Assessment::Question::ProgrammingImportJob.perform_later(self, new_attachment, course) + Course::Assessment::Question::ProgrammingImportJob.perform_later(self, new_attachment) update_column(:import_job_id, import_job.job_id) end end diff --git a/app/services/course/assessment/programming_codaveri_evaluation_service.rb b/app/services/course/assessment/programming_codaveri_evaluation_service.rb index 82240287bdf..0f3f085d4fb 100644 --- a/app/services/course/assessment/programming_codaveri_evaluation_service.rb +++ b/app/services/course/assessment/programming_codaveri_evaluation_service.rb @@ -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 @@ -103,25 +93,12 @@ 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', diff --git a/app/services/course/assessment/question/programming_import_service.rb b/app/services/course/assessment/question/programming_import_service.rb index ef7a85594f5..ff043f22b7f 100644 --- a/app/services/course/assessment/question/programming_import_service.rb +++ b/app/services/course/assessment/question/programming_import_service.rb @@ -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. diff --git a/lib/autoload/duplicator.rb b/lib/autoload/duplicator.rb index c614b3b9f15..bf37cdb6b3f 100644 --- a/lib/autoload/duplicator.rb +++ b/lib/autoload/duplicator.rb @@ -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 diff --git a/spec/factories/duplication_traceable_lesson_plan_items.rb b/spec/factories/duplication_traceable_lesson_plan_items.rb index 53b284a4077..295f98233bb 100644 --- a/spec/factories/duplication_traceable_lesson_plan_items.rb +++ b/spec/factories/duplication_traceable_lesson_plan_items.rb @@ -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 diff --git a/spec/models/course/assessment_spec.rb b/spec/models/course/assessment_spec.rb index a0b233d7fd7..d6c41de4f3e 100644 --- a/spec/models/course/assessment_spec.rb +++ b/spec/models/course/assessment_spec.rb @@ -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 diff --git a/spec/models/course/lesson_plan/lesson_plan_item_spec.rb b/spec/models/course/lesson_plan/lesson_plan_item_spec.rb index 53d2a96be9a..03d1622a5cf 100644 --- a/spec/models/course/lesson_plan/lesson_plan_item_spec.rb +++ b/spec/models/course/lesson_plan/lesson_plan_item_spec.rb @@ -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 diff --git a/spec/models/duplication_traceable/assessment_spec.rb b/spec/models/duplication_traceable/lesson_plan_item_spec.rb similarity index 52% rename from spec/models/duplication_traceable/assessment_spec.rb rename to spec/models/duplication_traceable/lesson_plan_item_spec.rb index 04a5cb6b3fc..6831481044b 100644 --- a/spec/models/duplication_traceable/assessment_spec.rb +++ b/spec/models/duplication_traceable/lesson_plan_item_spec.rb @@ -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