|
| 1 | +package com.graphql.spring.boot.test; |
| 2 | + |
| 3 | +import graphql.ErrorType; |
| 4 | +import graphql.GraphQLError; |
| 5 | +import graphql.language.SourceLocation; |
| 6 | +import org.junit.jupiter.params.ParameterizedTest; |
| 7 | +import org.junit.jupiter.params.provider.Arguments; |
| 8 | +import org.junit.jupiter.params.provider.MethodSource; |
| 9 | + |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.stream.Stream; |
| 13 | + |
| 14 | +import static org.assertj.core.api.Assertions.assertThat; |
| 15 | + |
| 16 | +public class GraphQLTestErrorToStringTest { |
| 17 | + |
| 18 | + private static Stream<Arguments> toStringTestArguments() { |
| 19 | + return Stream.of( |
| 20 | + Arguments.of( |
| 21 | + new GraphQLTestError(null, null, null, null, null), |
| 22 | + "<Unspecified error>: <error message not provided>" |
| 23 | + ), |
| 24 | + Arguments.of( |
| 25 | + new GraphQLTestError("Test message", null, null, null, null), |
| 26 | + "<Unspecified error>: Test message" |
| 27 | + ), |
| 28 | + Arguments.of( |
| 29 | + new GraphQLTestError("Test message", null, ErrorType.DataFetchingException, null, null), |
| 30 | + "DataFetchingException: Test message" |
| 31 | + ), |
| 32 | + Arguments.of( |
| 33 | + new GraphQLTestError("Test message", Collections.emptyList(), ErrorType.DataFetchingException, null, null), |
| 34 | + "DataFetchingException: Test message" |
| 35 | + ), |
| 36 | + Arguments.of( |
| 37 | + new GraphQLTestError("Test message", Collections.singletonList(new SourceLocation(1, 2)), |
| 38 | + ErrorType.DataFetchingException, null, null), |
| 39 | + "DataFetchingException: Test message at line 1, column 2 in unnamed/unspecified source" |
| 40 | + ), |
| 41 | + Arguments.of( |
| 42 | + new GraphQLTestError("Test message", Collections.singletonList(new SourceLocation(1, 2)), |
| 43 | + ErrorType.DataFetchingException, Collections.emptyList(), null), |
| 44 | + "DataFetchingException: Test message at line 1, column 2 in unnamed/unspecified source" |
| 45 | + ), |
| 46 | + Arguments.of( |
| 47 | + new GraphQLTestError("Test message", Collections.singletonList(new SourceLocation(1, 2)), |
| 48 | + ErrorType.DataFetchingException, Arrays.asList("path", "to", "error"), null), |
| 49 | + "DataFetchingException: Test message at line 1, column 2 in unnamed/unspecified source. Selection path: path/to/error" |
| 50 | + ), |
| 51 | + Arguments.of( |
| 52 | + new GraphQLTestError("Test message", Collections.singletonList(new SourceLocation(1, 2, "test.graphql")), |
| 53 | + ErrorType.DataFetchingException, Arrays.asList("path", "to", "error"), null), |
| 54 | + "DataFetchingException: Test message at line 1, column 2 in test.graphql. Selection path: path/to/error" |
| 55 | + ), |
| 56 | + Arguments.of( |
| 57 | + new GraphQLTestError("Test message", Collections.singletonList(new SourceLocation(1, 2)), |
| 58 | + ErrorType.DataFetchingException, Arrays.asList("path", 123, "error"), null), |
| 59 | + "DataFetchingException: Test message at line 1, column 2 in unnamed/unspecified source. Selection path: path[123]/error" |
| 60 | + ), |
| 61 | + Arguments.of( |
| 62 | + new GraphQLTestError("Test message", Collections.singletonList(new SourceLocation(1, 2)), |
| 63 | + ErrorType.DataFetchingException, Arrays.asList("path", 123, "error"), Collections.singletonMap("please ignore", "this")), |
| 64 | + "DataFetchingException: Test message at line 1, column 2 in unnamed/unspecified source. Selection path: path[123]/error" |
| 65 | + ) |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + @ParameterizedTest(name = "{1}") |
| 70 | + @MethodSource("toStringTestArguments") |
| 71 | + void testGraphQLTestErrorToString(final GraphQLError error, final String expectedToString) { |
| 72 | + assertThat(error.toString()).isEqualTo(expectedToString); |
| 73 | + } |
| 74 | +} |
0 commit comments