Skip to content
Snippets Groups Projects
Commit 77e2bcb2 authored by Dominika Zemanovičová's avatar Dominika Zemanovičová
Browse files

Fix AnswerRepositoryTest

parent 9f27a5d5
No related branches found
No related tags found
3 merge requests!31M2,!30M2 exercise,!29M2 exercise
package org.fuseri.moduleexercise.answer;
import org.fuseri.moduleexercise.exercise.Exercise;
import org.fuseri.moduleexercise.exercise.ExerciseRepository;
import org.fuseri.moduleexercise.question.Question;
import org.fuseri.moduleexercise.question.QuestionRepository;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
......@@ -18,84 +16,76 @@ import java.util.Arrays;
import java.util.HashSet;
@DataJpaTest
public class AnswerRepositoryTest {
class AnswerRepositoryTest {
@Autowired
AnswerRepository repository;
@Autowired
QuestionRepository questionRepository;
@Autowired
ExerciseRepository exerciseRepository;
AnswerRepository answerRepository;
@Autowired
TestEntityManager entityManager;
Exercise exercise = new Exercise("name", "desc", 2, 1L, new HashSet<>());
// Question question = new Question("name","desc",2,1L,new HashSet<>());
Exercise exercise = new Exercise("name","desc",2,1L,new HashSet<>());
Question question = new Question("text", new HashSet<>(), exercise);
Question question = new Question("text",new HashSet<>(),exercise);
Answer answer = new Answer("text",false,question);
Answer answer2 = new Answer("text2",true,question);
Answer answer = new Answer("text", false, question);
Answer answer2 = new Answer("text2", true, question);
@BeforeEach
void init() {
exerciseRepository.save(exercise);
questionRepository.save(question);
entityManager.persist(exercise);
entityManager.persist(question);
}
@Test
void saveQuestion() {
Question saved = questionRepository.save(question);
void saveAnswer() {
Answer createdAnswer = answerRepository.save(answer);
Assertions.assertNotNull(saved);
Assertions.assertEquals(question, saved);
Assertions.assertNotNull(createdAnswer);
Assertions.assertEquals(answer, createdAnswer);
}
@Test
void findById() {
void findByQuestionId() {
entityManager.persist(answer);
entityManager.flush();
Answer found = repository.findById(answer.getId()).orElse(null);
// Question found = questionRepository.findById(question.getId()).orElse(null);
Answer foundAnswer = answerRepository.findById(answer.getId()).orElse(null);
Assertions.assertNotNull(found);
Assertions.assertEquals(found, answer);
Assertions.assertNotNull(foundAnswer);
Assertions.assertEquals(foundAnswer, answer);
}
@Test
void testFindAllQuestions() {
entityManager.persist(exercise);
entityManager.persist(question);
entityManager.persist(answer);
entityManager.persist(answer2);
entityManager.flush();
Page<Answer> coursePage = repository.findAll(PageRequest.of(0, 42));
Page<Answer> coursePage = answerRepository.findAll(PageRequest.of(0, 42));
Assertions.assertEquals(2, coursePage.getTotalElements());
Assertions.assertEquals(coursePage.getContent(), Arrays.asList(answer, answer2));
}
@Test
void testFindAllQuestionsEmpty() {
Page<Answer> coursePage = repository.findAll(PageRequest.of(0, 42));
Page<Answer> coursePage = answerRepository.findAll(PageRequest.of(0, 42));
Assertions.assertEquals(0, coursePage.getTotalElements());
Assertions.assertEquals(coursePage.getContent(), new ArrayList<>());
}
@Test
void testDeleteQuestion() {
void testDeleteAnswer() {
Long id = entityManager.persist(answer).getId();
entityManager.flush();
questionRepository.deleteById(id);
answerRepository.deleteById(id);
Assertions.assertTrue(questionRepository.findById(id).isEmpty());
Assertions.assertTrue(answerRepository.findById(id).isEmpty());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment