完善e2e测试与后端测试,微信小程序端UI层测试暂未完成
This commit was merged in pull request #52.
This commit is contained in:
+219
@@ -0,0 +1,219 @@
|
||||
package cn.novalon.gym.manage.datacount.handler;
|
||||
|
||||
import cn.novalon.gym.manage.datacount.domain.*;
|
||||
import cn.novalon.gym.manage.datacount.service.IDataStatisticsService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.mock.web.reactive.function.server.MockServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class DataStatisticsHandlerTest {
|
||||
|
||||
@Mock
|
||||
private IDataStatisticsService dataStatisticsService;
|
||||
|
||||
private DataStatisticsHandler handler;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
handler = new DataStatisticsHandler();
|
||||
java.lang.reflect.Field field = DataStatisticsHandler.class.getDeclaredField("dataStatisticsService");
|
||||
field.setAccessible(true);
|
||||
field.set(handler, dataStatisticsService);
|
||||
}
|
||||
|
||||
// ==================== getStatisticsSummary ====================
|
||||
|
||||
@Test
|
||||
void getStatisticsSummary_shouldReturnOkWithSummary() {
|
||||
StatisticsSummary summary = createTestSummary();
|
||||
when(dataStatisticsService.getStatisticsSummaryWithCache(any(StatisticsQuery.class))).thenReturn(Mono.just(summary));
|
||||
|
||||
MockServerRequest request = MockServerRequest.builder()
|
||||
.queryParam("periodType", "DAY")
|
||||
.build();
|
||||
|
||||
Mono<ServerResponse> result = handler.getStatisticsSummary(request);
|
||||
|
||||
ServerResponse response = result.block();
|
||||
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getStatisticsSummary_shouldReturnOkEvenOnError() {
|
||||
when(dataStatisticsService.getStatisticsSummaryWithCache(any(StatisticsQuery.class)))
|
||||
.thenReturn(Mono.error(new RuntimeException("Service error")));
|
||||
|
||||
MockServerRequest request = MockServerRequest.builder()
|
||||
.queryParam("periodType", "DAY")
|
||||
.build();
|
||||
|
||||
// Error handler returns empty/default summary with 200 OK
|
||||
Mono<ServerResponse> result = handler.getStatisticsSummary(request);
|
||||
|
||||
ServerResponse response = result.block();
|
||||
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
|
||||
// ==================== getMemberStatistics ====================
|
||||
|
||||
@Test
|
||||
void getMemberStatistics_shouldReturnOk() {
|
||||
MemberStatistics stats = new MemberStatistics();
|
||||
when(dataStatisticsService.getMemberStatistics(any(StatisticsQuery.class))).thenReturn(Mono.just(stats));
|
||||
|
||||
MockServerRequest request = MockServerRequest.builder()
|
||||
.queryParam("periodType", "WEEK")
|
||||
.build();
|
||||
|
||||
Mono<ServerResponse> result = handler.getMemberStatistics(request);
|
||||
|
||||
ServerResponse response = result.block();
|
||||
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getMemberStatistics_shouldReturnOkEvenOnError() {
|
||||
when(dataStatisticsService.getMemberStatistics(any(StatisticsQuery.class)))
|
||||
.thenReturn(Mono.error(new RuntimeException("Service error")));
|
||||
|
||||
MockServerRequest request = MockServerRequest.builder()
|
||||
.queryParam("periodType", "WEEK")
|
||||
.build();
|
||||
|
||||
Mono<ServerResponse> result = handler.getMemberStatistics(request);
|
||||
|
||||
ServerResponse response = result.block();
|
||||
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
|
||||
// ==================== getBookingStatistics ====================
|
||||
|
||||
@Test
|
||||
void getBookingStatistics_shouldReturnOk() {
|
||||
BookingStatistics stats = new BookingStatistics();
|
||||
when(dataStatisticsService.getBookingStatistics(any(StatisticsQuery.class))).thenReturn(Mono.just(stats));
|
||||
|
||||
MockServerRequest request = MockServerRequest.builder()
|
||||
.queryParam("periodType", "MONTH")
|
||||
.build();
|
||||
|
||||
Mono<ServerResponse> result = handler.getBookingStatistics(request);
|
||||
|
||||
ServerResponse response = result.block();
|
||||
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
|
||||
// ==================== getSignInStatistics ====================
|
||||
|
||||
@Test
|
||||
void getSignInStatistics_shouldReturnOk() {
|
||||
SignInStatistics stats = new SignInStatistics();
|
||||
when(dataStatisticsService.getSignInStatistics(any(StatisticsQuery.class))).thenReturn(Mono.just(stats));
|
||||
|
||||
MockServerRequest request = MockServerRequest.builder()
|
||||
.queryParam("periodType", "MONTH")
|
||||
.build();
|
||||
|
||||
Mono<ServerResponse> result = handler.getSignInStatistics(request);
|
||||
|
||||
ServerResponse response = result.block();
|
||||
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
|
||||
// ==================== queryHistoricalStatistics ====================
|
||||
|
||||
@Test
|
||||
void queryHistoricalStatistics_shouldReturnOkWithList() {
|
||||
when(dataStatisticsService.queryHistoricalStatistics(any(StatisticsQuery.class)))
|
||||
.thenReturn(Flux.just(createTestDataStatistics()));
|
||||
|
||||
MockServerRequest request = MockServerRequest.builder()
|
||||
.queryParam("periodType", "YEAR")
|
||||
.build();
|
||||
|
||||
Mono<ServerResponse> result = handler.queryHistoricalStatistics(request);
|
||||
|
||||
ServerResponse response = result.block();
|
||||
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryHistoricalStatistics_shouldReturnOkWhenEmpty() {
|
||||
when(dataStatisticsService.queryHistoricalStatistics(any(StatisticsQuery.class)))
|
||||
.thenReturn(Flux.empty());
|
||||
|
||||
MockServerRequest request = MockServerRequest.builder()
|
||||
.queryParam("periodType", "YEAR")
|
||||
.build();
|
||||
|
||||
Mono<ServerResponse> result = handler.queryHistoricalStatistics(request);
|
||||
|
||||
ServerResponse response = result.block();
|
||||
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
|
||||
// ==================== exportStatistics ====================
|
||||
|
||||
@Test
|
||||
void exportStatistics_shouldReturnOkWithExcelContent() {
|
||||
byte[] excelData = "mock-excel-content".getBytes();
|
||||
when(dataStatisticsService.exportStatistics(any(StatisticsQuery.class))).thenReturn(Mono.just(excelData));
|
||||
|
||||
MockServerRequest request = MockServerRequest.builder()
|
||||
.queryParam("periodType", "MONTH")
|
||||
.build();
|
||||
|
||||
Mono<ServerResponse> result = handler.exportStatistics(request);
|
||||
|
||||
ServerResponse response = result.block();
|
||||
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
|
||||
// ==================== buildQueryFromRequest (via parameterized tests) ====================
|
||||
|
||||
@Test
|
||||
void getStatisticsSummary_shouldUseDefaultPeriodWhenMissing() {
|
||||
StatisticsSummary summary = createTestSummary();
|
||||
when(dataStatisticsService.getStatisticsSummaryWithCache(any(StatisticsQuery.class))).thenReturn(Mono.just(summary));
|
||||
|
||||
MockServerRequest request = MockServerRequest.builder().build();
|
||||
|
||||
Mono<ServerResponse> result = handler.getStatisticsSummary(request);
|
||||
|
||||
ServerResponse response = result.block();
|
||||
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
|
||||
// ==================== helper ====================
|
||||
|
||||
private StatisticsSummary createTestSummary() {
|
||||
StatisticsSummary summary = new StatisticsSummary();
|
||||
summary.setMemberStatistics(new MemberStatistics());
|
||||
summary.setBookingStatistics(new BookingStatistics());
|
||||
summary.setSignInStatistics(new SignInStatistics());
|
||||
summary.setCoachStatistics(new CoachStatistics());
|
||||
return summary;
|
||||
}
|
||||
|
||||
private DataStatistics createTestDataStatistics() {
|
||||
return DataStatistics.builder()
|
||||
.statType("MEMBER")
|
||||
.periodType("DAY")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user