|
| 1 | +package graphql.scalars.datetime |
| 2 | + |
| 3 | + |
| 4 | +import graphql.language.StringValue |
| 5 | +import graphql.scalars.ExtendedScalars |
| 6 | +import graphql.schema.CoercingParseLiteralException |
| 7 | +import graphql.schema.CoercingParseValueException |
| 8 | +import graphql.schema.CoercingSerializeException |
| 9 | +import spock.lang.Specification |
| 10 | +import spock.lang.Unroll |
| 11 | + |
| 12 | +import java.time.Period |
| 13 | +import java.time.temporal.ChronoUnit |
| 14 | + |
| 15 | +import static graphql.scalars.util.TestKit.mkDuration |
| 16 | +import static graphql.scalars.util.TestKit.mkStringValue |
| 17 | + |
| 18 | +class AccurateDurationScalarTest extends Specification { |
| 19 | + |
| 20 | + def coercing = ExtendedScalars.AccurateDuration.getCoercing() |
| 21 | + |
| 22 | + @Unroll |
| 23 | + def "accurateduration parseValue"() { |
| 24 | + |
| 25 | + when: |
| 26 | + def result = coercing.parseValue(input) |
| 27 | + then: |
| 28 | + result == expectedValue |
| 29 | + where: |
| 30 | + input | expectedValue |
| 31 | + "PT1S" | mkDuration("PT1S") |
| 32 | + "PT1.5S" | mkDuration("PT1.5S") |
| 33 | + "P1DT2H3M4S" | mkDuration("P1DT2H3M4S") |
| 34 | + "-P1DT2H3M4S" | mkDuration("-P1DT2H3M4S") |
| 35 | + "P1DT-2H3M4S" | mkDuration("P1DT-2H3M4S") |
| 36 | + mkDuration(amount: 123456, unit: ChronoUnit.HOURS) | mkDuration("PT123456H") |
| 37 | + } |
| 38 | + |
| 39 | + @Unroll |
| 40 | + def "accurateduration valueToLiteral"() { |
| 41 | + |
| 42 | + when: |
| 43 | + def result = coercing.valueToLiteral(input) |
| 44 | + then: |
| 45 | + result.isEqualTo(expectedValue) |
| 46 | + where: |
| 47 | + input | expectedValue |
| 48 | + "PT1S" | mkStringValue("PT1S") |
| 49 | + "PT1.5S" | mkStringValue("PT1.5S") |
| 50 | + "P1D" | mkStringValue("PT24H") |
| 51 | + "P1DT2H3M4S" | mkStringValue("PT26H3M4S") |
| 52 | + mkDuration("P1DT2H3M4S") | mkStringValue("PT26H3M4S") |
| 53 | + mkDuration("-P1DT2H3M4S") | mkStringValue("PT-26H-3M-4S") |
| 54 | + mkDuration(amount: 123456, unit: ChronoUnit.HOURS) | mkStringValue("PT123456H") |
| 55 | + } |
| 56 | + |
| 57 | + @Unroll |
| 58 | + def "accurateduration parseValue bad inputs"() { |
| 59 | + |
| 60 | + when: |
| 61 | + coercing.parseValue(input) |
| 62 | + then: |
| 63 | + thrown(expectedValue) |
| 64 | + where: |
| 65 | + input | expectedValue |
| 66 | + "P1M" | CoercingParseValueException |
| 67 | + "P1MT2H" | CoercingParseValueException |
| 68 | + "P2W" | CoercingParseValueException |
| 69 | + "P3Y" | CoercingParseValueException |
| 70 | + 123 | CoercingParseValueException |
| 71 | + "" | CoercingParseValueException |
| 72 | + Period.of(1, 2, 3) | CoercingParseValueException |
| 73 | + } |
| 74 | + |
| 75 | + def "accurateduration AST literal"() { |
| 76 | + |
| 77 | + when: |
| 78 | + def result = coercing.parseLiteral(input) |
| 79 | + then: |
| 80 | + result == expectedValue |
| 81 | + where: |
| 82 | + input | expectedValue |
| 83 | + new StringValue("P1DT2H3M4S") | mkDuration("P1DT2H3M4S") |
| 84 | + } |
| 85 | + |
| 86 | + def "accurateduration serialisation"() { |
| 87 | + |
| 88 | + when: |
| 89 | + def result = coercing.serialize(input) |
| 90 | + then: |
| 91 | + result == expectedValue |
| 92 | + where: |
| 93 | + input | expectedValue |
| 94 | + "PT1S" | "PT1S" |
| 95 | + "PT1.5S" | "PT1.5S" |
| 96 | + "P1DT2H3M4S" | "PT26H3M4S" |
| 97 | + "-P1DT2H3M4S" | "PT-26H-3M-4S" |
| 98 | + "P1DT-2H3M4S" | "PT22H3M4S" |
| 99 | + mkDuration("P1DT-2H3M4S") | "PT22H3M4S" |
| 100 | + mkDuration(amount: 123456, unit: ChronoUnit.HOURS) | "PT123456H" |
| 101 | + } |
| 102 | + |
| 103 | + def "accurateduration serialisation bad inputs"() { |
| 104 | + |
| 105 | + when: |
| 106 | + coercing.serialize(input) |
| 107 | + then: |
| 108 | + thrown(expectedValue) |
| 109 | + where: |
| 110 | + input | expectedValue |
| 111 | + "P1M" | CoercingSerializeException |
| 112 | + "PT1.5M" | CoercingSerializeException |
| 113 | + "P1MT2H" | CoercingSerializeException |
| 114 | + "P2W" | CoercingSerializeException |
| 115 | + "P3Y" | CoercingSerializeException |
| 116 | + 123 | CoercingSerializeException |
| 117 | + "" | CoercingSerializeException |
| 118 | + Period.of(1, 2, 3) | CoercingSerializeException |
| 119 | + } |
| 120 | + |
| 121 | + @Unroll |
| 122 | + def "accurateduration parseLiteral bad inputs"() { |
| 123 | + |
| 124 | + when: |
| 125 | + coercing.parseLiteral(input) |
| 126 | + then: |
| 127 | + thrown(expectedValue) |
| 128 | + where: |
| 129 | + input | expectedValue |
| 130 | + "P1M" | CoercingParseLiteralException |
| 131 | + "PT1.5M" | CoercingParseLiteralException |
| 132 | + "P1MT2H" | CoercingParseLiteralException |
| 133 | + "P2W" | CoercingParseLiteralException |
| 134 | + "P3Y" | CoercingParseLiteralException |
| 135 | + 123 | CoercingParseLiteralException |
| 136 | + "" | CoercingParseLiteralException |
| 137 | + Period.of(1, 2, 3) | CoercingParseLiteralException |
| 138 | + } |
| 139 | +} |
0 commit comments