Skip to content

Commit

Permalink
Merge pull request #254 from sopt-makers/kwy_#253
Browse files Browse the repository at this point in the history
[TEST] 출석 관련 단위 테스트 추가 및 행사 출석 로직 수정
  • Loading branch information
KWY0218 authored Apr 12, 2024
2 parents cc6604e + e25492f commit a75c418
Show file tree
Hide file tree
Showing 13 changed files with 456 additions and 3 deletions.
3 changes: 3 additions & 0 deletions operation-domain/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ dependencies {

// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.14.0-rc1'

// test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ public class Attendance {
@OneToMany(mappedBy = "attendance")
private final List<SubAttendance> subAttendances = new ArrayList<>();

protected Attendance(Long id, Member member, Lecture lecture, AttendanceStatus status) {
this.id = id;
setMember(member);
setLecture(lecture);
this.status = status;
}

public Attendance(Member member, Lecture lecture) {
setMember(member);
setLecture(lecture);
Expand Down Expand Up @@ -79,13 +86,12 @@ public AttendanceStatus getStatus() {
val second = getSubAttendanceByRound(2);

return switch (this.lecture.getAttribute()) {
case SEMINAR -> {
case SEMINAR, EVENT -> {
if (first.getStatus().equals(ATTENDANCE) && second.getStatus().equals(ATTENDANCE)) {
yield ATTENDANCE;
}
yield first.getStatus().equals(ABSENT) && second.getStatus().equals(ABSENT) ? ABSENT : TARDY;
}
case EVENT -> second.getStatus().equals(ATTENDANCE) ? ATTENDANCE : ABSENT;
case ETC -> second.getStatus().equals(ATTENDANCE) ? PARTICIPATE : NOT_PARTICIPATE;
};
}
Expand All @@ -103,7 +109,7 @@ public float getScore() {
yield 0f;
}
}
case EVENT -> this.status.equals(ATTENDANCE) ? 0.5f : 0f;
case EVENT -> this.status.equals(ABSENT) ? 0f : 0.5f;
default -> 0f;
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public SubAttendance(Attendance attendance, SubLecture subLecture) {
status = ABSENT;
}

protected SubAttendance(Long id, Attendance attendance, SubLecture subLecture, AttendanceStatus status) {
this.id = id;
setAttendance(attendance);
setSubLecture(subLecture);
this.status = status;
}

private void setAttendance(Attendance attendance) {
if (Objects.nonNull(this.attendance)) {
this.attendance.getSubAttendances().remove(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ public Lecture(
this.lectureStatus = BEFORE;
}

protected Lecture(Long id, String name, Part part, int generation, String place, LocalDateTime startDate, LocalDateTime endDate, Attribute attribute, LectureStatus lectureStatus) {
this.id = id;
this.name = name;
this.part = part;
this.generation = generation;
this.place = place;
this.startDate = startDate;
this.endDate = endDate;
this.attribute = attribute;
this.lectureStatus = lectureStatus;
}

public void updateStatus(LectureStatus status) {
this.lectureStatus = status;
}
Expand All @@ -101,4 +113,9 @@ public boolean isFirst() {
public boolean isNotYetToEnd() {
return this.endDate.isAfter(LocalDateTime.now());
}

protected void setOneToMany(List<SubLecture> subLectures, List<Attendance> attendances) {
this.subLectures = subLectures;
this.attendances = attendances;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public class SubLecture {
@OneToMany(mappedBy = "subLecture")
private final List<SubAttendance> subAttendances = new ArrayList<>();

protected SubLecture(Long id, Lecture lecture, int round, LocalDateTime startAt, String code) {
this.id = id;
setLecture(lecture);
this.round = round;
this.startAt = startAt;
this.code = code;
}

public SubLecture(Lecture lecture, int round) {
setLecture(lecture);
this.round = round;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ public class Member {
@OneToMany(mappedBy = "member")
List<Attendance> attendances = new ArrayList<>();

protected Member(Long id, Long playgroundId, String name, int generation, ObYb obyb, Part part, String university, float score, String phone, List<Attendance> attendances) {
this.id = id;
this.playgroundId = playgroundId;
this.name = name;
this.generation = generation;
this.obyb = obyb;
this.part = part;
this.university = university;
this.score = score;
this.phone = phone;
this.attendances = attendances;
}

public void updateScore(float score) {
this.score += score;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package org.sopt.makers.operation.attendance.domain;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.sopt.makers.operation.common.domain.Part;
import org.sopt.makers.operation.dummy.AttendanceDummy;
import org.sopt.makers.operation.dummy.LectureDummy;
import org.sopt.makers.operation.dummy.MemberDummy;
import org.sopt.makers.operation.lecture.domain.Attribute;
import org.sopt.makers.operation.lecture.domain.Lecture;
import org.sopt.makers.operation.lecture.domain.LectureStatus;
import org.sopt.makers.operation.member.domain.Member;

import java.time.LocalDateTime;
import java.util.ArrayList;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

public class AttendanceGetScoreTest {

private Member member;

@BeforeEach
void setUp() {
member = MemberDummy.builder()
.id(1L)
.attendances(new ArrayList<>())
.build();
}

@Nested
@DisplayName("[세미나 출석 점수 반환 테스트]")
public class SeminarTest {
@Test
@DisplayName("세미나에 출석을 했으면 0f를 반환한다.")
public void getScoreTest() {
// given
Lecture lecture = new LectureDummy(1L, "서버 1차 세미나", Part.SERVER, 34, "오산역", LocalDateTime.now(), LocalDateTime.now(), Attribute.SEMINAR, LectureStatus.BEFORE);
Attendance attendance = new AttendanceDummy(1L, member, lecture, AttendanceStatus.ATTENDANCE);

// when
float score = attendance.getScore();

// then
assertThat(score).isEqualTo(0f);
}

@Test
@DisplayName("세미나에 지각을 했으면 -0.5f를 반환한다.")
public void getScoreTest2() {
// given
Lecture lecture = new LectureDummy(1L, "서버 1차 세미나", Part.SERVER, 34, "오산역", LocalDateTime.now(), LocalDateTime.now(), Attribute.SEMINAR, LectureStatus.BEFORE);
Attendance attendance = new AttendanceDummy(1L, member, lecture, AttendanceStatus.TARDY);

// when
float score = attendance.getScore();

// then
assertThat(score).isEqualTo(-0.5f);
}

@Test
@DisplayName("세미나에 결석을 했으면 -1f를 반환한다.")
public void getScoreTest3() {
// given
Lecture lecture = new LectureDummy(1L, "서버 1차 세미나", Part.SERVER, 34, "오산역", LocalDateTime.now(), LocalDateTime.now(), Attribute.SEMINAR, LectureStatus.BEFORE);
Attendance attendance = new AttendanceDummy(1L, member, lecture, AttendanceStatus.ABSENT);

// when
float score = attendance.getScore();

// then
assertThat(score).isEqualTo(-1f);
}
}

@Nested
@DisplayName("[행사 출석 점수 반환 테스트]")
public class EventTest {
@Test
@DisplayName("행사에 출석을 했으면 0.5f를 반환한다.")
public void getScoreTest() {
// given
Lecture lecture = new LectureDummy(1L, "서버 1차 세미나", Part.SERVER, 34, "오산역", LocalDateTime.now(), LocalDateTime.now(), Attribute.EVENT, LectureStatus.BEFORE);
Attendance attendance = new AttendanceDummy(1L, member, lecture, AttendanceStatus.ATTENDANCE);

// when
float score = attendance.getScore();

// then
assertThat(score).isEqualTo(0.5f);
}

@Test
@DisplayName("행사에 지각을 했으면 0.5f를 반환한다.")
public void getScoreTest2() {
// given
Lecture lecture = new LectureDummy(1L, "서버 1차 세미나", Part.SERVER, 34, "오산역", LocalDateTime.now(), LocalDateTime.now(), Attribute.EVENT, LectureStatus.BEFORE);
Attendance attendance = new AttendanceDummy(1L, member, lecture, AttendanceStatus.TARDY);

// when
float score = attendance.getScore();

// then
assertThat(score).isEqualTo(0.5f);
}

@Test
@DisplayName("행사에 결석을 했으면 0f를 반환한다.")
public void getScoreTest3() {
// given
Lecture lecture = new LectureDummy(1L, "서버 1차 세미나", Part.SERVER, 34, "오산역", LocalDateTime.now(), LocalDateTime.now(), Attribute.EVENT, LectureStatus.BEFORE);
Attendance attendance = new AttendanceDummy(1L, member, lecture, AttendanceStatus.ABSENT);

// when
float score = attendance.getScore();

// then
assertThat(score).isEqualTo(0f);
}
}
}
Loading

0 comments on commit a75c418

Please sign in to comment.