Skip to content

Commit

Permalink
#97 - Json 파일 출력기가 필드 자료형에 따라 알맞은 데이터를 표현하도록 수정
Browse files Browse the repository at this point in the history
대부분의 데이터는 그대로 문자열로 내보내되,
숫자와 boolean 처리를 할 수 있는 분기를 추가하고
`null`값은 변환 에러가 발생할 수 있으므로
그대로 내보내도록 로직 수정
  • Loading branch information
mikonu committed Aug 20, 2024
1 parent 45594f5 commit 9663584
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import uno.fastcampus.testdata.service.generator.MockDataGeneratorContext;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Slf4j
Expand All @@ -37,12 +36,22 @@ public String export(TableSchemaDto dto, Integer rowCount) {
dto.schemaFields().stream()
.sorted(Comparator.comparing(SchemaFieldDto::fieldOrder))
.forEach(field -> {
map.put(field.fieldName(), mockDataGeneratorContext.generate(
String generatedValue = mockDataGeneratorContext.generate(
field.mockDataType(),
field.blankPercent(),
field.typeOptionJson(),
field.forceValue()
));
);
if (generatedValue == null) {
map.put(field.fieldName(), null);
} else {
var jsonValue = switch (field.mockDataType().jsonType()) {
case NUMBER -> Long.valueOf(generatedValue);
case BOOLEAN -> Boolean.valueOf(generatedValue);
default -> generatedValue;
};
map.put(field.fieldName(), jsonValue);
}
});
list.add(map);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ void givenSchemaAndRowCount_whenExporting_thenReturnsJsonFormattedString() throw
)
);
int rowCount = 10;
given(mockDataGeneratorContext.generate(any(), any(), any(), any())).willReturn("test-value");
given(mockDataGeneratorContext.generate(eq(MockDataType.ROW_NUMBER), any(), any(), any())).willReturn("1");
given(mockDataGeneratorContext.generate(eq(MockDataType.NAME), any(), any(), any())).willReturn("test-name");
given(mockDataGeneratorContext.generate(eq(MockDataType.DATETIME), any(), any(), any())).willReturn("2024-01-02T03:04:05");
given(mockDataGeneratorContext.generate(eq(MockDataType.NUMBER), any(), any(), any())).willReturn(null);
given(mockDataGeneratorContext.generate(eq(MockDataType.CAR), any(), any(), any())).willReturn("test-횬다이");

// When
String result = sut.export(dto, rowCount);
Expand All @@ -68,7 +72,7 @@ void givenSchemaAndRowCount_whenExporting_thenReturnsJsonFormattedString() throw
.startsWith("[")
.endsWith("]")
.contains("""
{"id":"test-value","name":"test-value","age":"test-value","car":"test-value","created_at":"test-value"}"""
{"id":1,"name":"test-name","age":null,"car":"test-횬다이","created_at":"2024-01-02T03:04:05"}"""
);
then(mockDataGeneratorContext).should(times(rowCount * dto.schemaFields().size())).generate(any(), any(), any(), any());
then(mapper).should().writeValueAsString(any());
Expand All @@ -78,7 +82,7 @@ void givenSchemaAndRowCount_whenExporting_thenReturnsJsonFormattedString() throw
@Test
void givenSchemaAndRowCount_whenFailingToJsonFormatting_thenReturnsEmptyString() throws Exception {
// Given
TableSchemaDto dto = TableSchemaDto.of("test_schema", "uno", null, Set.of(SchemaFieldDto.of("id", MockDataType.ROW_NUMBER, 1, 0, null, null)));
TableSchemaDto dto = TableSchemaDto.of("test_schema", "uno", null, Set.of(SchemaFieldDto.of("name", MockDataType.NAME, 1, 0, null, null)));
int rowCount = 10;
given(mockDataGeneratorContext.generate(any(), any(), any(), any())).willReturn("test-value");
willThrow(JsonProcessingException.class).given(mapper).writeValueAsString(any());
Expand Down

0 comments on commit 9663584

Please sign in to comment.