|
| 1 | +import { FixDoubledAnswers } from '../../../../scripts/certification/fix-doubled-answers.js'; |
| 2 | +import { CertificationAssessment } from '../../../../src/certification/session-management/domain/models/CertificationAssessment.js'; |
| 3 | +import { AlgorithmEngineVersion } from '../../../../src/certification/shared/domain/models/AlgorithmEngineVersion.js'; |
| 4 | +import { ABORT_REASONS } from '../../../../src/certification/shared/domain/models/CertificationCourse.js'; |
| 5 | +import { Assessment } from '../../../../src/shared/domain/models/index.js'; |
| 6 | +import { createTempFile, databaseBuilder, expect, knex, sinon } from '../../../test-helper.js'; |
| 7 | + |
| 8 | +describe('Integration | Scripts | Certification | fix-doubled-answers', function () { |
| 9 | + it('should parse input file', async function () { |
| 10 | + const script = new FixDoubledAnswers(); |
| 11 | + const { options } = script.metaInfo; |
| 12 | + const file = 'doubled-answers.csv'; |
| 13 | + const data = |
| 14 | + 'certificationChallengeId,answerId,completionDate\n1,2,2021-01-02 8:20:45.000000+01:00\n3,4,2021-01-02 9:20:45.000000+01:00\n5,6,2021-01-02 10:20:45.000000+01:00\n'; |
| 15 | + const csvFilePath = await createTempFile(file, data); |
| 16 | + |
| 17 | + const parsedData = await options.file.coerce(csvFilePath); |
| 18 | + |
| 19 | + expect(parsedData).to.deep.equal([ |
| 20 | + { certificationChallengeId: 1, answerId: 2, completionDate: '2021-01-02 8:20:45.000000+01:00' }, |
| 21 | + { certificationChallengeId: 3, answerId: 4, completionDate: '2021-01-02 9:20:45.000000+01:00' }, |
| 22 | + { certificationChallengeId: 5, answerId: 6, completionDate: '2021-01-02 10:20:45.000000+01:00' }, |
| 23 | + ]); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should link doubled answers to another assessment', async function () { |
| 27 | + // given |
| 28 | + const certificationCourseId = 123; |
| 29 | + const secondCertificationCourseId = 456; |
| 30 | + const logger = { |
| 31 | + info: sinon.stub(), |
| 32 | + debug: sinon.stub(), |
| 33 | + }; |
| 34 | + |
| 35 | + const assessmentToUseForUpdate = await databaseBuilder.factory.buildAssessment({ |
| 36 | + id: 123, |
| 37 | + type: Assessment.types.COMPETENCE_EVALUATION, |
| 38 | + }); |
| 39 | + const user = databaseBuilder.factory.buildUser(); |
| 40 | + |
| 41 | + const firstCertificationCourse = databaseBuilder.factory.buildCertificationCourse({ |
| 42 | + id: certificationCourseId, |
| 43 | + userId: user.id, |
| 44 | + version: AlgorithmEngineVersion.V3, |
| 45 | + abortReason: ABORT_REASONS.TECHNICAL, |
| 46 | + completedAt: null, |
| 47 | + finalizedAt: new Date('2021-01-01'), |
| 48 | + }); |
| 49 | + const secondCertificationCourse = databaseBuilder.factory.buildCertificationCourse({ |
| 50 | + id: secondCertificationCourseId, |
| 51 | + userId: user.id, |
| 52 | + version: AlgorithmEngineVersion.V3, |
| 53 | + abortReason: ABORT_REASONS.TECHNICAL, |
| 54 | + completedAt: null, |
| 55 | + finalizedAt: new Date('2021-01-01'), |
| 56 | + }); |
| 57 | + |
| 58 | + const firstAssessmentId = databaseBuilder.factory.buildAssessment({ |
| 59 | + certificationCourseId: firstCertificationCourse.id, |
| 60 | + userId: user.id, |
| 61 | + type: Assessment.types.CERTIFICATION, |
| 62 | + state: 'endedDueToFinalization', |
| 63 | + }).id; |
| 64 | + const secondAssessmentId = databaseBuilder.factory.buildAssessment({ |
| 65 | + certificationCourseId: secondCertificationCourse.id, |
| 66 | + userId: user.id, |
| 67 | + type: Assessment.types.CERTIFICATION, |
| 68 | + state: 'endedDueToFinalization', |
| 69 | + }).id; |
| 70 | + |
| 71 | + const firstCertificationChallengeToKeep = databaseBuilder.factory.buildCertificationChallenge({ |
| 72 | + id: 1, |
| 73 | + challengeId: 'recYYYY', |
| 74 | + courseId: certificationCourseId, |
| 75 | + }); |
| 76 | + const firstCertificationChallengeToBeRemoved = databaseBuilder.factory.buildCertificationChallenge({ |
| 77 | + id: 2, |
| 78 | + challengeId: 'rec123', |
| 79 | + courseId: certificationCourseId, |
| 80 | + }); |
| 81 | + |
| 82 | + const secondCertificationChallengeToKeep = databaseBuilder.factory.buildCertificationChallenge({ |
| 83 | + id: 3, |
| 84 | + challengeId: 'recXXXX', |
| 85 | + courseId: secondCertificationCourseId, |
| 86 | + }); |
| 87 | + const secondCertificationChallengeToBeRemoved = databaseBuilder.factory.buildCertificationChallenge({ |
| 88 | + id: 4, |
| 89 | + challengeId: 'rec456', |
| 90 | + courseId: secondCertificationCourseId, |
| 91 | + }); |
| 92 | + |
| 93 | + const firstAnswerToBeUpdated = databaseBuilder.factory.buildAnswer({ |
| 94 | + id: 1, |
| 95 | + challengeId: 'rec123', |
| 96 | + assessmentId: firstAssessmentId, |
| 97 | + }); |
| 98 | + const secondAnswerToBeUpdated = databaseBuilder.factory.buildAnswer({ |
| 99 | + id: 2, |
| 100 | + challengeId: 'rec456', |
| 101 | + assessmentId: secondAssessmentId, |
| 102 | + }); |
| 103 | + |
| 104 | + databaseBuilder.factory.buildCertificationChallengeCapacity({ |
| 105 | + certificationChallengeId: firstCertificationChallengeToBeRemoved.id, |
| 106 | + answerId: firstAnswerToBeUpdated.id, |
| 107 | + capacity: 1, |
| 108 | + }); |
| 109 | + databaseBuilder.factory.buildCertificationChallengeCapacity({ |
| 110 | + certificationChallengeId: secondCertificationChallengeToBeRemoved.id, |
| 111 | + answerId: secondAnswerToBeUpdated.id, |
| 112 | + capacity: 2, |
| 113 | + }); |
| 114 | + |
| 115 | + await databaseBuilder.commit(); |
| 116 | + |
| 117 | + const file = [ |
| 118 | + { |
| 119 | + certificationChallengeId: firstCertificationChallengeToBeRemoved.id, |
| 120 | + answerId: firstAnswerToBeUpdated.id, |
| 121 | + completionDate: '2021-01-02 8:20:45.000000+01:00', |
| 122 | + }, |
| 123 | + { |
| 124 | + certificationChallengeId: secondCertificationChallengeToBeRemoved.id, |
| 125 | + answerId: secondAnswerToBeUpdated.id, |
| 126 | + completionDate: '2021-01-02 9:20:45.000000+01:00', |
| 127 | + }, |
| 128 | + ]; |
| 129 | + const script = new FixDoubledAnswers(); |
| 130 | + |
| 131 | + // when |
| 132 | + await script.handle({ options: { file, assessmentId: assessmentToUseForUpdate.id, dryRun: false }, logger }); |
| 133 | + |
| 134 | + // then |
| 135 | + const answers = await knex('answers').select('id', 'assessmentId').orderBy('id'); |
| 136 | + expect(answers).to.deep.equal([ |
| 137 | + { |
| 138 | + id: firstAnswerToBeUpdated.id, |
| 139 | + assessmentId: assessmentToUseForUpdate.id, |
| 140 | + }, |
| 141 | + { |
| 142 | + id: secondAnswerToBeUpdated.id, |
| 143 | + assessmentId: assessmentToUseForUpdate.id, |
| 144 | + }, |
| 145 | + ]); |
| 146 | + const certificationChallenges = await knex('certification-challenges').select('id'); |
| 147 | + expect(certificationChallenges).to.deep.equal([{ id: 1 }, { id: 3 }]); |
| 148 | + |
| 149 | + const certificationChallengeCapacities = await knex('certification-challenge-capacities'); |
| 150 | + expect(certificationChallengeCapacities).to.deep.equal([]); |
| 151 | + |
| 152 | + const assessments = await knex('assessments') |
| 153 | + .select('id', 'updatedAt', 'state', 'lastChallengeId', 'lastQuestionDate') |
| 154 | + .where({ type: Assessment.types.CERTIFICATION }); |
| 155 | + expect(assessments).to.deep.equal([ |
| 156 | + { |
| 157 | + id: firstAssessmentId, |
| 158 | + state: CertificationAssessment.states.COMPLETED, |
| 159 | + updatedAt: new Date('2021-01-02T07:20:45Z'), |
| 160 | + lastChallengeId: firstCertificationChallengeToKeep.challengeId, |
| 161 | + lastQuestionDate: new Date('2021-01-02T07:20:45Z'), |
| 162 | + }, |
| 163 | + { |
| 164 | + id: secondAssessmentId, |
| 165 | + state: CertificationAssessment.states.COMPLETED, |
| 166 | + updatedAt: new Date('2021-01-02T08:20:45Z'), |
| 167 | + lastChallengeId: secondCertificationChallengeToKeep.challengeId, |
| 168 | + lastQuestionDate: new Date('2021-01-02T08:20:45Z'), |
| 169 | + }, |
| 170 | + ]); |
| 171 | + |
| 172 | + const certificationCourses = await knex('certification-courses').select( |
| 173 | + 'id', |
| 174 | + 'completedAt', |
| 175 | + 'updatedAt', |
| 176 | + 'endedAt', |
| 177 | + 'abortReason', |
| 178 | + ); |
| 179 | + expect(certificationCourses).to.deep.equal([ |
| 180 | + { |
| 181 | + id: certificationCourseId, |
| 182 | + completedAt: new Date('2021-01-02T07:20:45Z'), |
| 183 | + updatedAt: new Date('2021-01-02T07:20:45Z'), |
| 184 | + endedAt: new Date('2021-01-02T07:20:45Z'), |
| 185 | + abortReason: null, |
| 186 | + }, |
| 187 | + { |
| 188 | + id: secondCertificationCourseId, |
| 189 | + completedAt: new Date('2021-01-02T08:20:45Z'), |
| 190 | + updatedAt: new Date('2021-01-02T08:20:45Z'), |
| 191 | + endedAt: new Date('2021-01-02T08:20:45Z'), |
| 192 | + abortReason: null, |
| 193 | + }, |
| 194 | + ]); |
| 195 | + }); |
| 196 | +}); |
0 commit comments