Skip to content

Commit

Permalink
Merge pull request #26 from RamtinS/test/25-add-tests-category-and-tags
Browse files Browse the repository at this point in the history
Test/25 add tests category and tags
  • Loading branch information
JeffTabiri authored Apr 4, 2024
2 parents 63227a4 + af5b479 commit b5ae6ff
Show file tree
Hide file tree
Showing 7 changed files with 471 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import jakarta.persistence.Table;
import java.util.List;
import java.util.Objects;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand Down Expand Up @@ -38,9 +40,12 @@ public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, unique = true)
@Setter(AccessLevel.NONE)
@Getter(AccessLevel.NONE)
private Long id;

@Column(name = "description", nullable = false)
@NonNull
private String description;

@OneToMany(mappedBy = "category")
Expand All @@ -65,4 +70,5 @@ public boolean equals(Object object) {
public int hashCode() {
return Objects.hash(id, description);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;
import lombok.ToString;

Expand All @@ -36,19 +38,24 @@ public class QuizAttempt {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
@Setter(AccessLevel.NONE)
@Getter(AccessLevel.NONE)
private Long id;

@ManyToOne
@JoinColumn(name = "quiz_id")
@JoinColumn(name = "quiz_id", nullable = false)
@NonNull
private Quiz quiz;

@ManyToOne
@JoinColumn(name = "user_id")
@JoinColumn(name = "user_id", nullable = false)
@NonNull
private User user;

@Column(name = "score")
@Column(name = "score", nullable = false)
private int score;

@Column(name = "attempt_timestamp")
@Column(name = "attempt_timestamp", nullable = false)
@NonNull
private Date attemptDate;
}
5 changes: 5 additions & 0 deletions src/main/java/edu/ntnu/idatt2105/quizapp/model/quiz/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;

/**
Expand All @@ -28,10 +30,13 @@
@Getter
@Setter
public class Tag {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Setter(AccessLevel.NONE)
private Long id;

@Column(name = "description", nullable = false)
@NonNull
private String description;
}
114 changes: 114 additions & 0 deletions src/test/java/edu/ntnu/idatt2105/quizapp/model/quiz/CategoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package edu.ntnu.idatt2105.quizapp.model.quiz;

import edu.ntnu.idatt2105.quizapp.util.TestUtil;
import edu.ntnu.idatt2105.quizapp.util.quiz.QuizModelTestUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

/**
* Test class for the category model object.
*
* @author Jeffrey Yaw Annor Tabiri
* @since 4/4/2024
* @version 1.0
*/
class CategoryTest {

Category category;

@BeforeEach
void setUp() {
category = TestUtil.createCategoryA();
}

@Test
void Category_Constructor_ReturnCategory() {
//Arrange
String expected = "Physics";
Quiz expectedQuiz = QuizModelTestUtil.createQuizA();

//Act
Category category = Category.builder()
.description(expected)
.quizzes(List.of(expectedQuiz))
.build();
String actual = category.getDescription();
List<Quiz> actualQuiz = category.getQuizzes();

//Assert
assertTrue(actualQuiz.contains(expectedQuiz));
assertEquals(expected, actual);
}

@Test
void Category_ConstructorWithNullDescription_ReturnException() {
//Act and Assert
assertThrows(NullPointerException.class, () -> {
Category category = Category.builder()
.description(null)
.build();
});

}
@Test
void Category_GetDescription_ReturnDescription() {
//Arrange
String expected = "Physics";

//Act
String actual = category.getDescription();

//Assert
assertEquals(expected, actual);
}

@Test
void Category_GetQuizzes() {
//Arrange
Quiz expected = QuizModelTestUtil.createQuizA();

//Act
List<Quiz> actual = category.getQuizzes();

//Assert
assertTrue(actual.contains(expected));

}

@Test
void Category_SetDescription_ReturnsSavedDescription() {
String expected = "Food";

category.setDescription(expected);
String actual = category.getDescription();

assertEquals(expected, actual);
}

@Test
void Category_SetDescriptionNull_ThrowsException() {
//Act and Assert
assertThrows(NullPointerException.class, () -> {
category.setDescription(null);
});
}

@Test
void Category_SetQuizzes_ReturnsSavedQuiz() {
Quiz expected = QuizModelTestUtil.createQuizB();

category.setQuizzes(List.of(expected));
List<Quiz> actual = category.getQuizzes();

assertTrue(actual.contains(expected));

}

@Test
void builder() {
}
}
Loading

0 comments on commit b5ae6ff

Please sign in to comment.