Skip to content

Commit b1d6c01

Browse files
committed
fix: write date/time matchers in the correct format as per the spec #1617
1 parent aa11791 commit b1d6c01

File tree

9 files changed

+39
-29
lines changed

9 files changed

+39
-29
lines changed

consumer/groovy/src/test/groovy/au/com/dius/pact/consumer/groovy/MatchersSpec.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ class MatchersSpec extends Specification {
2424
'regexp' | '[0-9]+' | [match: 'regex', regex: '[0-9]+']
2525
'hexValue' | '1234' | [match: 'regex', regex: '[0-9a-fA-F]+']
2626
'ipAddress' | '1.2.3.4' | [match: 'regex', regex: '(\\d{1,3}\\.)+\\d{1,3}']
27-
'datetime' | 'yyyy-mm-dd' | [match: 'timestamp', timestamp: 'yyyy-mm-dd']
28-
'datetime' | 'yyyy-mm-dd' | [match: 'timestamp', timestamp: 'yyyy-mm-dd']
29-
'date' | 'yyyy-mm-dd' | [match: 'date', date: 'yyyy-mm-dd']
30-
'time' | 'yyyy-mm-dd' | [match: 'time', time: 'yyyy-mm-dd']
27+
'datetime' | 'yyyy-mm-dd' | [match: 'timestamp', format: 'yyyy-mm-dd']
28+
'datetime' | 'yyyy-mm-dd' | [match: 'timestamp', format: 'yyyy-mm-dd']
29+
'date' | 'yyyy-mm-dd' | [match: 'date', format: 'yyyy-mm-dd']
30+
'time' | 'yyyy-mm-dd' | [match: 'time', format: 'yyyy-mm-dd']
3131
'uuid' | '12345678-1234-1234-1234-123456789012' | [match: 'regex', regex: '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}']
3232
'equalTo' | 'value' | [match: 'equality']
3333
'includesStr' | 'value' | [match: 'include', value: 'value']

consumer/src/test/groovy/au/com/dius/pact/consumer/dsl/DslPartSpec.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,9 @@ class DslPartSpec extends Specification {
269269

270270
matcherMethod | param | matcherDefinition
271271
'regexp' | '[0-9]+' | [match: 'regex', regex: '[0-9]+']
272-
'matchTimestamp' | 'yyyy-mm-dd' | [match: 'timestamp', timestamp: 'yyyy-mm-dd']
273-
'matchDate' | 'yyyy-mm-dd' | [match: 'date', date: 'yyyy-mm-dd']
274-
'matchTime' | 'yyyy-mm-dd' | [match: 'time', time: 'yyyy-mm-dd']
272+
'matchTimestamp' | 'yyyy-mm-dd' | [match: 'timestamp', format: 'yyyy-mm-dd']
273+
'matchDate' | 'yyyy-mm-dd' | [match: 'date', format: 'yyyy-mm-dd']
274+
'matchTime' | 'yyyy-mm-dd' | [match: 'time', format: 'yyyy-mm-dd']
275275
'matchMin' | 1 | [match: 'type', min: 1]
276276
'matchMax' | 1 | [match: 'type', max: 1]
277277
'includesMatcher' | 1 | [match: 'include', value: '1']

consumer/src/test/groovy/au/com/dius/pact/consumer/dsl/LambdaDslSpec.groovy

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ class LambdaDslSpec extends Specification {
161161

162162
then:
163163
result.matchers.toMap(PactSpecVersion.V3) == [
164-
'$.dateExp': [matchers: [[match: 'date', date: 'yyyy-MM-dd']], combine: 'AND'],
165-
'$.timeExp': [matchers: [[match: 'time', time: 'HH:mm:ss']], combine: 'AND'],
166-
'$.datetimeExp': [matchers: [[match: 'timestamp', timestamp: "yyyy-MM-dd'T'HH:mm:ss"]], combine: 'AND']
164+
'$.dateExp': [matchers: [[match: 'date', format: 'yyyy-MM-dd']], combine: 'AND'],
165+
'$.timeExp': [matchers: [[match: 'time', format: 'HH:mm:ss']], combine: 'AND'],
166+
'$.datetimeExp': [matchers: [[match: 'timestamp', format: "yyyy-MM-dd'T'HH:mm:ss"]], combine: 'AND']
167167
]
168168
result.generators.toMap(PactSpecVersion.V3) == [
169169
body: [
@@ -187,9 +187,9 @@ class LambdaDslSpec extends Specification {
187187

188188
then:
189189
result.matchers.toMap(PactSpecVersion.V3) == [
190-
'[0]': [matchers: [[match: 'date', date: 'yyyy-MM-dd']], combine: 'AND'],
191-
'[1]': [matchers: [[match: 'time', time: 'HH:mm:ss']], combine: 'AND'],
192-
'[2]': [matchers: [[match: 'timestamp', timestamp: "yyyy-MM-dd'T'HH:mm:ss"]], combine: 'AND']
190+
'[0]': [matchers: [[match: 'date', format: 'yyyy-MM-dd']], combine: 'AND'],
191+
'[1]': [matchers: [[match: 'time', format: 'HH:mm:ss']], combine: 'AND'],
192+
'[2]': [matchers: [[match: 'timestamp', format: "yyyy-MM-dd'T'HH:mm:ss"]], combine: 'AND']
193193
]
194194

195195
result.generators.toMap(PactSpecVersion.V3) == [
@@ -314,7 +314,7 @@ class LambdaDslSpec extends Specification {
314314
variants: [
315315
[
316316
index: 0,
317-
rules: ['$': [matchers: [[match: 'date', date: 'yyyy-MM-dd']], combine: 'AND']],
317+
rules: ['$': [matchers: [[match: 'date', format: 'yyyy-MM-dd']], combine: 'AND']],
318318
generators: ['$': [type: 'DateTime', format: 'yyyy-MM-dd']]
319319
],
320320
[index: 1, rules: [:], generators: [:]],

consumer/src/test/groovy/au/com/dius/pact/consumer/dsl/PactDslJsonArraySpec.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ class PactDslJsonArraySpec extends Specification {
227227

228228
expect:
229229
body.matchers.toMap(PactSpecVersion.V3) == [
230-
'$[0]': [matchers: [[match: 'date', date: 'yyyy-MM-dd']], combine: 'AND'],
231-
'$[1]': [matchers: [[match: 'time', time: 'HH:mm:ss']], combine: 'AND'],
232-
'$[2]': [matchers: [[match: 'timestamp', timestamp: "yyyy-MM-dd'T'HH:mm:ss"]], combine: 'AND']]
230+
'$[0]': [matchers: [[match: 'date', format: 'yyyy-MM-dd']], combine: 'AND'],
231+
'$[1]': [matchers: [[match: 'time', format: 'HH:mm:ss']], combine: 'AND'],
232+
'$[2]': [matchers: [[match: 'timestamp', format: "yyyy-MM-dd'T'HH:mm:ss"]], combine: 'AND']]
233233

234234
body.generators.toMap(PactSpecVersion.V3) == [body: [
235235
'$[0]': [type: 'Date', format: 'yyyy-MM-dd', expression: 'today + 1 day'],

consumer/src/test/groovy/au/com/dius/pact/consumer/dsl/PactDslJsonBodySpec.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,9 @@ class PactDslJsonBodySpec extends Specification {
327327

328328
expect:
329329
body.matchers.toMap(PactSpecVersion.V3) == [
330-
'$.dateExp': [matchers: [[match: 'date', date: 'yyyy-MM-dd']], combine: 'AND'],
331-
'$.timeExp': [matchers: [[match: 'time', time: 'HH:mm:ss']], combine: 'AND'],
332-
'$.datetimeExp': [matchers: [[match: 'timestamp', timestamp: "yyyy-MM-dd'T'HH:mm:ss"]], combine: 'AND']]
330+
'$.dateExp': [matchers: [[match: 'date', format: 'yyyy-MM-dd']], combine: 'AND'],
331+
'$.timeExp': [matchers: [[match: 'time', format: 'HH:mm:ss']], combine: 'AND'],
332+
'$.datetimeExp': [matchers: [[match: 'timestamp', format: "yyyy-MM-dd'T'HH:mm:ss"]], combine: 'AND']]
333333

334334
body.generators.toMap(PactSpecVersion.V3) == [body: [
335335
'$.dateExp': [type: 'Date', format: 'yyyy-MM-dd', expression: 'today + 1 day'],

consumer/src/test/groovy/au/com/dius/pact/consumer/dsl/PactDslJsonRootValueSpec.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@ class PactDslJsonRootValueSpec extends Specification {
4444
def datetime = PactDslJsonRootValue.datetimeExpression('today + 1 hour')
4545

4646
expect:
47-
date.matchers.toMap(PactSpecVersion.V3) == [matchers: [[match: 'date', date: 'yyyy-MM-dd']], combine: 'AND']
47+
date.matchers.toMap(PactSpecVersion.V3) == [matchers: [[match: 'date', format: 'yyyy-MM-dd']], combine: 'AND']
4848
date.generators.toMap(PactSpecVersion.V3) == [body: [
4949
'': [type: 'Date', format: 'yyyy-MM-dd', expression: 'today + 1 day']]]
5050

51-
time.matchers.toMap(PactSpecVersion.V3) == [matchers: [[match: 'time', time: 'HH:mm:ss']], combine: 'AND']
51+
time.matchers.toMap(PactSpecVersion.V3) == [matchers: [[match: 'time', format: 'HH:mm:ss']], combine: 'AND']
5252
time.generators.toMap(PactSpecVersion.V3) == [body: [
5353
'': [type: 'Time', format: 'HH:mm:ss', expression: 'now + 1 hour']]]
5454

5555
datetime.matchers.toMap(PactSpecVersion.V3) == [matchers: [[
56-
match: 'timestamp', timestamp: "yyyy-MM-dd'T'HH:mm:ss"]], combine: 'AND']
56+
match: 'timestamp', format: "yyyy-MM-dd'T'HH:mm:ss"]], combine: 'AND']
5757
datetime.generators.toMap(PactSpecVersion.V3) == [body: [
5858
'': [type: 'DateTime', format: 'yyyy-MM-dd\'T\'HH:mm:ss', expression: 'today + 1 hour']]]
5959
}

core/model/src/main/kotlin/au/com/dius/pact/core/model/matchingrules/MatchingRules.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ interface MatchingRule {
235235
* Matching Rule for dates
236236
*/
237237
data class DateMatcher @JvmOverloads constructor(val format: String = "yyyy-MM-dd") : MatchingRule {
238-
override fun toMap(spec: PactSpecVersion) = mapOf("match" to "date", "date" to format)
238+
override fun toMap(spec: PactSpecVersion) = mapOf("match" to "date", "format" to format)
239239
override fun validateForVersion(pactVersion: PactSpecVersion): List<String> {
240240
return if (pactVersion < PactSpecVersion.V3) {
241241
listOf("Date matchers can only be used with Pact specification versions >= V3")
@@ -387,7 +387,7 @@ data class RegexMatcher @JvmOverloads constructor (val regex: String, val exampl
387387
* Matcher for time values
388388
*/
389389
data class TimeMatcher @JvmOverloads constructor(val format: String = "HH:mm:ss") : MatchingRule {
390-
override fun toMap(spec: PactSpecVersion) = mapOf("match" to "time", "time" to format)
390+
override fun toMap(spec: PactSpecVersion) = mapOf("match" to "time", "format" to format)
391391
override fun validateForVersion(pactVersion: PactSpecVersion): List<String> {
392392
return if (pactVersion < PactSpecVersion.V3) {
393393
listOf("Time matchers can only be used with Pact specification versions >= V3")
@@ -406,7 +406,7 @@ data class TimeMatcher @JvmOverloads constructor(val format: String = "HH:mm:ss"
406406
* Matcher for time values
407407
*/
408408
data class TimestampMatcher @JvmOverloads constructor(val format: String = "yyyy-MM-dd HH:mm:ssZZZZZ") : MatchingRule {
409-
override fun toMap(spec: PactSpecVersion) = mapOf("match" to "timestamp", "timestamp" to format)
409+
override fun toMap(spec: PactSpecVersion) = mapOf("match" to "timestamp", "format" to format)
410410
override fun validateForVersion(pactVersion: PactSpecVersion): List<String> {
411411
return if (pactVersion < PactSpecVersion.V3) {
412412
listOf("DateTime matchers can only be used with Pact specification versions >= V3")

core/model/src/test/groovy/au/com/dius/pact/core/model/matchingrules/MatchingRuleGroupSpec.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ class MatchingRuleGroupSpec extends Specification {
4343
new MinTypeMatcher(1) | [match: 'type', min: 1]
4444
new MaxTypeMatcher(1) | [match: 'type', max: 1]
4545
new MinMaxTypeMatcher(2, 3) | [match: 'type', max: 3, min: 2]
46-
new TimestampMatcher() | [match: 'timestamp', timestamp: 'yyyy-MM-dd HH:mm:ssZZZZZ']
47-
new TimeMatcher() | [match: 'time', time: 'HH:mm:ss']
48-
new DateMatcher() | [match: 'date', date: 'yyyy-MM-dd']
46+
new TimestampMatcher() | [match: 'timestamp', format: 'yyyy-MM-dd HH:mm:ssZZZZZ']
47+
new TimeMatcher() | [match: 'time', format: 'HH:mm:ss']
48+
new DateMatcher() | [match: 'date', format: 'yyyy-MM-dd']
4949
new IncludeMatcher('A') | [match: 'include', value: 'A']
5050
ValuesMatcher.INSTANCE | [match: 'values']
5151
NullMatcher.INSTANCE | [match: 'null']

core/model/src/test/groovy/au/com/dius/pact/core/model/matchingrules/MatchingRulesSpec.groovy

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,4 +361,14 @@ class MatchingRulesSpec extends Specification {
361361
[match: 'time', time: 'HH:mm'] | new TimeMatcher('HH:mm')
362362
[match: 'time', format: 'HH:mm'] | new TimeMatcher('HH:mm')
363363
}
364+
365+
def 'date/time matcher to json'() {
366+
expect:
367+
new TimestampMatcher().toMap(PactSpecVersion.V3) == [match: 'timestamp', format: 'yyyy-MM-dd HH:mm:ssZZZZZ']
368+
new TimestampMatcher('yyyy').toMap(PactSpecVersion.V3) == [match: 'timestamp', format: 'yyyy']
369+
new DateMatcher().toMap(PactSpecVersion.V3) == [match: 'date', format: 'yyyy-MM-dd']
370+
new DateMatcher('yyyy').toMap(PactSpecVersion.V3) == [match: 'date', format: 'yyyy']
371+
new TimeMatcher().toMap(PactSpecVersion.V3) == [match: 'time', format: 'HH:mm:ss']
372+
new TimeMatcher('hh').toMap(PactSpecVersion.V3) == [match: 'time', format: 'hh']
373+
}
364374
}

0 commit comments

Comments
 (0)