Skip to content

Commit

Permalink
Merge pull request #51 from RamtinS/feature/automatic-test-data
Browse files Browse the repository at this point in the history
Feature/automatic test data
  • Loading branch information
Skippyoftedal authored Apr 7, 2024
2 parents d710ec7 + b128cb3 commit 13cc2e1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,47 @@ public static void main(String[] args) {
SpringApplication.run(QuizBackendApplication.class, args);
}

/**
* A CommandLineRunner bean that runs on startup. The commandline runner should not
* be used in production. It is only used for testing purposes.
*
* @param userRepository the user repository
* @param passwordEncoder the password encoder
* @param quizRepository the quiz repository
* @param categoryRepository the category repository
* @param quizAttemptRepository the quiz attempt repository
* @return a CommandLineRunner bean
*/
@Bean
CommandLineRunner run(UserRepository userRepository, PasswordEncoder passwordEncoder,
QuizRepository quizRepository, CategoryRepository categoryRepository,
QuizAttemptRepository quizAttemptRepository) {

return args -> {


if (args.length > 0) {
log.info("Running with argument: " + args[0]);
if (args[0].equalsIgnoreCase("--data")) {
QuizTestData.addTestData(userRepository, passwordEncoder, quizRepository,
categoryRepository, quizAttemptRepository);
} else if (args[0].equalsIgnoreCase("--help")) {
log.info("Possible options: ");
log.info("data: Add test data to the database to be able to run existing " +
"quizzes");
} else {
log.error(args[0] + " is not a valid argument. Please try running with '--help' " +
"to see possible options.");
}
if (args.length == 0) {
QuizTestData.addTestData(userRepository, passwordEncoder, quizRepository,
categoryRepository,
quizAttemptRepository);
} else if (args[0].equalsIgnoreCase("--noTestData")) {
log.info("Running tests without adding data");
} else if (args[0].equalsIgnoreCase("--deleteAllData")) {
log.info("Deleting all data in the database");
quizAttemptRepository.deleteAll();
quizRepository.deleteAll();
userRepository.deleteAll();
categoryRepository.deleteAll();
} else if (args[0].equalsIgnoreCase("--help")) {
log.info(
"""
Commands:
--noTestData: Run the program without adding new data
--resetTestData: Drops all tables in the database""");
} else {
log.info("No arguments given, running without adding any data except categories");
QuizTestData.createAndSaveCategories(categoryRepository);
log.error("Invalid command. Use --help for a list of commands");
System.exit(1);
}
};
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public QuizPreviewDto mapToQuizPreviewDto(Quiz quiz) {
.title(quiz.getName())
.description(quiz.getDescription())
.open(quiz.getIsOpen())
.category(quiz.getCategory().getDescription())
.category(quiz.getCategory() != null ? quiz.getCategory().getDescription() : null)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package edu.ntnu.idatt2105.quizapp.testdata;

import edu.ntnu.idatt2105.quizapp.model.user.Role;
import edu.ntnu.idatt2105.quizapp.model.user.User;
import edu.ntnu.idatt2105.quizapp.model.quiz.Answer;
import edu.ntnu.idatt2105.quizapp.model.quiz.Category;
import edu.ntnu.idatt2105.quizapp.model.quiz.MultipleChoiceQuestion;
import edu.ntnu.idatt2105.quizapp.model.quiz.Quiz;
import edu.ntnu.idatt2105.quizapp.model.quiz.QuizAttempt;
import edu.ntnu.idatt2105.quizapp.model.quiz.TrueOrFalseQuestion;
import edu.ntnu.idatt2105.quizapp.model.user.Role;
import edu.ntnu.idatt2105.quizapp.model.user.User;
import edu.ntnu.idatt2105.quizapp.repositories.UserRepository;
import edu.ntnu.idatt2105.quizapp.repositories.quiz.CategoryRepository;
import edu.ntnu.idatt2105.quizapp.repositories.quiz.QuizAttemptRepository;
Expand All @@ -16,7 +16,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import lombok.Generated;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;

Expand All @@ -37,6 +36,14 @@ public class QuizTestData {
"Miscellaneous", "Food", "Sports", "Gaming", "Math", "Physics", "Chemistry"
};

public static boolean checkIfTestDataExists(UserRepository userRepository,
QuizRepository quizRepository,
CategoryRepository categoryRepository,
QuizAttemptRepository quizAttemptRepository) {
return userRepository.count() > 0 || quizRepository.count() > 0
|| categoryRepository.count() > 0 || quizAttemptRepository.count() > 0;
}

/**
* Adds test data to the database.
*
Expand All @@ -51,6 +58,11 @@ public static void addTestData(UserRepository userRepository, PasswordEncoder pa
CategoryRepository categoryRepository,
QuizAttemptRepository quizAttemptRepository) {
try {
if (checkIfTestDataExists(userRepository, quizRepository, categoryRepository,
quizAttemptRepository)) {
log.info("Data already exists in database, so no test data will be added.");
return;
}
List<Category> categories = createAndSaveCategories(categoryRepository);
List<User> users = createTestUsers(passwordEncoder, userRepository);
addCypressUser(passwordEncoder, userRepository);
Expand Down Expand Up @@ -92,8 +104,11 @@ public static List<Category> createAndSaveCategories(CategoryRepository category
for (String category : possibleCategories) {
Category createdCategory = Category.builder().description(category).build();
if (categoryRepository.findCategoryByDescription(category).isEmpty()) {
log.info("Adding new category");
Category savedCategory = categoryRepository.save(createdCategory);
categories.add(savedCategory);
} else {
log.info("Category already exists in database");
}
}
return categories;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ spring:
password: password
jpa:
hibernate:
ddl-auto: create
ddl-auto: update
properties:
hibernate:
format_sql: true
Expand Down

0 comments on commit 13cc2e1

Please sign in to comment.