From 7d4d645eb24a30888825f6cdf50cb3eec6ef59f7 Mon Sep 17 00:00:00 2001 From: Szymon Szukalski Date: Fri, 26 Jul 2024 15:07:54 +1000 Subject: Add test cases for multiple submissions Add test cases which exercise the logic for updating the marks available and marks obtained. Update the service method so that they are transactional and reset the transient 'updated' and 'created' flags after reloading entites to ensure accurate update/create reporting. --- .../markr/service/TestResultsService.java | 43 ++++++++++++++++------ 1 file changed, 32 insertions(+), 11 deletions(-) (limited to 'src/main/java/com/stileeducation/markr/service/TestResultsService.java') diff --git a/src/main/java/com/stileeducation/markr/service/TestResultsService.java b/src/main/java/com/stileeducation/markr/service/TestResultsService.java index a06400e..7823feb 100644 --- a/src/main/java/com/stileeducation/markr/service/TestResultsService.java +++ b/src/main/java/com/stileeducation/markr/service/TestResultsService.java @@ -8,7 +8,10 @@ import com.stileeducation.markr.entity.Student; import com.stileeducation.markr.entity.Test; import com.stileeducation.markr.entity.TestResult; import com.stileeducation.markr.exception.TestNotFoundException; +import com.stileeducation.markr.repository.StudentRepository; +import com.stileeducation.markr.repository.TestRepository; import com.stileeducation.markr.repository.TestResultRepository; +import jakarta.transaction.Transactional; import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation; import org.apache.commons.math3.stat.descriptive.rank.Percentile; import org.springframework.stereotype.Service; @@ -25,19 +28,29 @@ public class TestResultsService { private final TestResultRepository testResultRepository; + private final StudentRepository studentRepository; + + private final TestRepository testRepository; + private final StudentService studentService; private final TestService testService; - public TestResultsService(TestResultRepository testResultRepository, StudentService studentService, TestService testService) { + public TestResultsService(TestResultRepository testResultRepository, + StudentRepository studentRepository, + TestRepository testRepository, + StudentService studentService, + TestService testService) { this.testResultRepository = testResultRepository; + this.studentRepository = studentRepository; + this.testRepository = testRepository; this.studentService = studentService; this.testService = testService; } private static double[] calculatePercentages(List results, double totalMarksAvailable) { return results.stream() - .mapToDouble(result -> (double) result.getMarksAwarded() / totalMarksAvailable * 100) + .mapToDouble(result -> (double) result.getMarksObtained() / totalMarksAvailable * 100) .toArray(); } @@ -55,6 +68,7 @@ public class TestResultsService { return aggregateResponseDTO; } + @Transactional public ImportResponseDTO processTestResults(MCQTestResultsDTO testResults) { ImportResponseDTO.ImportData importData = new ImportResponseDTO.ImportData(); boolean isValid = true; @@ -70,23 +84,29 @@ public class TestResultsService { return createImportResponse(importData, isValid); } - public TestResult findOrCreateTestResult(Student student, Test test, Integer marksAwarded) { + @Transactional + public TestResult findOrCreateTestResult(Student student, Test test, Integer marksObtained) { Optional optionalTestResult = testResultRepository.findByStudentAndTest(student, test); + if (optionalTestResult.isPresent()) { TestResult testResult = optionalTestResult.get(); - if (marksAwarded > testResult.getMarksAwarded()) { - testResult.setMarksAwarded(marksAwarded); + + // Update marks if new marks are higher + if (marksObtained > testResult.getMarksObtained()) { + testResult.setMarksObtained(marksObtained); + testResult.setCreated(false); testResult.setUpdated(true); - testResultRepository.save(testResult); + return testResultRepository.save(testResult); } else { + testResult.setCreated(false); testResult.setUpdated(false); + return testResult; } - return testResult; } else { TestResult testResult = new TestResult(); testResult.setStudent(student); testResult.setTest(test); - testResult.setMarksAwarded(marksAwarded); + testResult.setMarksObtained(marksObtained); testResult.setCreated(true); return testResultRepository.save(testResult); } @@ -186,7 +206,8 @@ public class TestResultsService { dto.setCount(results.size()); } - private void processTestResult(MCQTestResultDTO mcqTestResult, ImportResponseDTO.ImportData importData) { + @Transactional + protected void processTestResult(MCQTestResultDTO mcqTestResult, ImportResponseDTO.ImportData importData) { Student student = studentService .findOrCreateStudent( @@ -228,10 +249,10 @@ public class TestResultsService { if (isValid) { response.setStatus("success"); - response.setMessage("Import operation completed successfully."); + response.setMessage("Import completed successfully"); } else { response.setStatus("failure"); - response.setMessage("Data was invalid or processing failed."); + response.setMessage("Data was invalid or processing failed"); } return response; -- cgit v1.2.3 From b59d2f0c722d2e21194ed8d66a17be8128ba7b39 Mon Sep 17 00:00:00 2001 From: Szymon Szukalski Date: Fri, 26 Jul 2024 15:15:06 +1000 Subject: Remove unused variables and tidy comments --- .../java/com/stileeducation/markr/service/TestResultsService.java | 6 ------ 1 file changed, 6 deletions(-) (limited to 'src/main/java/com/stileeducation/markr/service/TestResultsService.java') diff --git a/src/main/java/com/stileeducation/markr/service/TestResultsService.java b/src/main/java/com/stileeducation/markr/service/TestResultsService.java index 7823feb..3c941dd 100644 --- a/src/main/java/com/stileeducation/markr/service/TestResultsService.java +++ b/src/main/java/com/stileeducation/markr/service/TestResultsService.java @@ -28,10 +28,6 @@ public class TestResultsService { private final TestResultRepository testResultRepository; - private final StudentRepository studentRepository; - - private final TestRepository testRepository; - private final StudentService studentService; private final TestService testService; @@ -42,8 +38,6 @@ public class TestResultsService { StudentService studentService, TestService testService) { this.testResultRepository = testResultRepository; - this.studentRepository = studentRepository; - this.testRepository = testRepository; this.studentService = studentService; this.testService = testService; } -- cgit v1.2.3