본문 바로가기
반응형
Test Code/Test Code(eng)

AppointmentCommandService Class - Test Code

by brightGarden02 2022. 11. 11.

I wrote test code per one class.

I chose AppointmentCommandService to write test code.

Before wrtting test code, I put data in database.

 

I classified into 3 categories in one method.

"given, when, then"

 

given: something is given.

when: something is done.

then: verification

 

 

@Test
void appointmentStatusTest() throws Exception {

    //given
    Appointment appointment1 = appointmentRepository.findById(1L).get();

    //when
    appointmentCommandService.updateAppointmentStatus(appointment1.getId(), AppointmentStatus.COMPLETE);

    //then
    assertThat(appointment1.getStatus()).isEqualTo(AppointmentStatus.COMPLETE);
}

given: Appointment is given

when: use method to change appointmentStatus to COMPLETE

then: verify appointmentStatus is equal to COMPLETE

 

 

 

@Test
void updateAppointmentTest() throws Exception {

    //given
    Appointment appointment = appointmentRepository.findById(4L).get();

    String newDateStr ="2021-11-28 09:40:00.000";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
    LocalDateTime newDate = LocalDateTime.parse(newDateStr, formatter);


    //when
    appointment.updateAppointmentDate(newDate);


    //then
    assertThat(appointment.getDate()).isEqualTo(newDate);
}

given: Appointment is given. newDate is given.

when: use method to change date to newDate

then: verify appointment.getDate() is equal to newDate

 

 

 

Now I am going to test for appointment progress.

The appointment progress is divided into 3 levels.

1. Check if the appointment date time is in 10-minute increments

@Test
void appointmentCheckRightDateTest() throws Exception {

    //given
    AppointmentFormDto appointmentFormDto = new AppointmentFormDto();

    //when
    appointmentFormDto.setDate("2022-11-10T14:00:00");
    LocalDateTime date = LocalDateTime.parse(appointmentFormDto.getDate());


    //then
    assertThat(date.getMinute() % 10).isEqualTo(0);
}

given: AppointmentFormDto is given

when: setDate AppointmentFromDto, bring LocalDateTime date

then: verify date.getMinute() could be divided by 10

 

 

 

2. Check if newInputDate is in hospital's opening-hours

@Test
void appointmentCheckHospitalTimeTest() throws Exception {

    //given
    AppointmentFormDto appointmentFormDto = new AppointmentFormDto();
    appointmentFormDto.setDate("2022-11-07T14:10:00");
    String inputDate = appointmentFormDto.getDate();
    LocalDateTime date = LocalDateTime.parse(inputDate);

    Hospital hospital = hospitalRepository.findById(1L).get();


    //when
    String inputDateHourMinute = date.getHour() + ":" + date.getMinute();

    DayOfWeek dayOfWeek = date.getDayOfWeek();
    String timeOfHospital = hospital.getOpeningHours().split("/")[dayOfWeek.getValue() -1];
    String[] times = timeOfHospital.split("~", 2);


    //then
    assertThat(inputDateHourMinute).isBetween(times[0], times[1]);
}

given: AppointmentFormDto is given, Hospital is given

when: make just hour and minute. make hospital's opening-hour in string array.

then: verify inputDatehourMinute is between start-opening-hour and close-opening-hour

 

 

 

3. Chen when someone has already an appointment

@Test
void appointmentWhenAlreadyAppointmentTest() throws Exception {

    //given
    Doctor doctor = doctorRepository.findById(6L).get();
    AppointmentFormDto appointmentFormDto = new AppointmentFormDto();
    appointmentFormDto.setDate("2022-10-03T10:30:00");
    String inputDate = appointmentFormDto.getDate();
    LocalDateTime date = LocalDateTime.parse(inputDate);


    //when
    Optional<Appointment> appointment = appointmentRepository.findOneByDateAndDoctorId(date, doctor.getId());


    //then
    assertThat(appointment.isPresent()).isTrue();
}

given: Doctor is given. AppointmentFormDto is given.

when: bring Optional through date and doctorID

then: verify appointment is present

 

 

'Test Code > Test Code(eng)' 카테고리의 다른 글

AppointmentQueryService Class - Test Code  (0) 2022.11.10

댓글


반응형
반응형