Matt (pactflow.io / pact-js / pact-go)
Tatiana
09/30/2022, 11:54 AMpip install pact-pythonMatt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Tatiana
09/30/2022, 12:13 PM{
"consumer": {
"name": "consumer"
},
"interactions": [
{
"description": "a request to get all events",
"providerState": "I can get all events",
"request": {
"method": "GET",
"path": "/events",
"query": "limit=10&name=&offset=0"
},
"response": {
"body": {
"items": [
{
"id": "1cb9eb9e",
"name": "test event 1",
"teamId": "dummy_tid"
}
],
"totalCount": 1
},
"headers": {
"Content-Type": "application/json"
},
"matchingRules": {
"$.body.items": {
"match": "type"
},
"$.body.totalCount": {
"match": "type"
}
},
"status": 200
}
}
],
"metadata": {
"pact-js": {
"version": "10.1.4"
},
"pactRust": {
"ffi": "0.3.11",
"models": "0.4.5"
},
"pactSpecification": {
"version": "2.0.0"
}
},
"provider": {
"name": "provider"
}
}Tatiana
09/30/2022, 12:18 PMpactSpecification version manually, just put 2 instead of 3. I could see the discrepancies between actual and expected results after test run, but it didn’t apply matchers 😞Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Tatiana
09/30/2022, 12:45 PMVerifying a pact between consumer and provider
Given I can get all events
a request to get all events
with GET /events?limit=10&name=&offset=0
returns a response which
WARN: Skipping set up for provider state 'I can get all events' for consumer 'consumer' as there is no --provider-states-setup-url specified.
has status code 200
has a matching body (FAILED - 1)
includes headers
"Content-Type" which equals "application/json"
Failures:
1) Verifying a pact between consumer and provider Given I can get all events a request to get all events with GET /events?limit=10&name=&offset=0 returns a response which has a matching body
Failure/Error: expect(response_body).to match_term expected_response_body, diff_options, example
Actual: {"items":[{"name":"test event da","start":"2021-02-21T11:42:21.954913+00:00","end":"2022-11-25T11:42:21.954913+00:00","ventureDuration":120,"location":"online","focus":[],"technology":[],"prerequisites":[],"challenges":[],"landingPage":null,"privacy":"private","branding":{"themeImage":"<https://www.arabnews.com/sites/default/files/main-image/2019/02/11/1456561-87422913.jpg>"},"testId":null,"id":"d5ac5cd4","teamId":"dummy_tid","createdAt":"2022-09-29T17:45:07.613494+00:00","updatedAt":"2022-09-29T17:45:07.613502+00:00","publishedAt":null,"status":"UNPUBLISHED","lifecycle":"STARTED","reportUrl":null},{"name":"test event dd","start":"2021-01-21T11:42:21.954913+00:00","end":"2022-10-25T11:42:21.954913+00:00","ventureDuration":120,"location":"online","focus":[],"technology":[],"prerequisites":[],"challenges":[],"landingPage":null,"privacy":"private","branding":{"themeImage":"<https://www.arabnews.com/sites/default/files/main-image/2019/02/11/1456561-87422913.jpg>"},"testId":null,"id":"1cb9eb9e","teamId":"dummy_tid","createdAt":"2022-09-21T15:44:43.831196+00:00","updatedAt":"2022-09-21T15:44:43.831203+00:00","publishedAt":null,"status":"UNPUBLISHED","lifecycle":"STARTED","reportUrl":null}],"totalCount":2}
Diff
--------------------------------------
Key: - is expected
+ is actual
Matching keys and values are not shown
{
"items": [
... ,
- Pact::UnexpectedIndex,
+ Hash,
]
}
Description of differences
--------------------------------------
* Actual array is too long and should not contain a Hash at $.items[1]
1 interaction, 1 failure
Failed interactions:
PACT_DESCRIPTION='a request to get all events' PACT_PROVIDER_STATE='I can get all events' /Users/tatiana/projects/events/.venv/bin/pact-verifier --provider-base-url=<http://localhost:8000> --pact-url=./pacts/consumer-provider.json # A request to get all events given I can get all eventsTimothy Jones
10/02/2022, 2:49 AMThe consumer tests looks like:Can you share the
import statements from your tests?Tatiana
10/02/2022, 1:09 PMimport React from 'react';
import { Provider } from 'react-redux';
import { renderHook } from '@testing-library/react-hooks'
import path from "path";
import { PactV3, SpecificationVersion } from '@pact-foundation/pact';
import { like, eachLike } from '@pact-foundation/pact/src/v3/matchers';
import { useGetEventQuery, useGetEventsQuery } from "../../../src/services/events";
import { makeStore } from "../../../src/redux";
Just in case, I am running the provider-verifier as:
pact-verifier --provider-base-url=<http://localhost:8000> --pact-url=./pacts/consumer-provider.jsonTatiana
10/02/2022, 10:08 PMTimothy Jones
10/02/2022, 10:11 PMMatt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Hash is a Ruby class). The consumer was generated by the Rust core, the provider looks like a Python one via the Ruby core.Timothy Jones
10/03/2022, 1:38 AMTatiana
10/03/2022, 1:42 AMTatiana
10/03/2022, 1:44 AM{
"items": [
{
"name": "test event dd",
"start": "2021-01-21T11:42:21.954913+00:00",
"end": "2022-10-25T11:42:21.954913+00:00",
"ventureDuration": 120,
"location": "online",
"focus": [],
"technology": [],
"prerequisites": [],
"challenges": [],
"landingPage": null,
"privacy": "private",
"branding": {
"themeImage": "<https://www.arabnews.com/sites/default/files/main-image/2019/02/11/1456561-87422913.jpg>"
},
"testId": null,
"id": "1cb9eb9e-4529-41da-bb51-d92c5649ebca",
"teamId": "dummy_tid",
"createdAt": "2022-09-21T15:44:43.831196+00:00",
"updatedAt": "2022-09-21T15:44:43.831203+00:00",
"publishedAt": null,
"status": "UNPUBLISHED",
"lifecycle": "STARTED",
"reportUrl": null
},
{
"name": "test event dd",
"start": "2021-01-21T11:42:21.954913+00:00",
"end": "2022-10-25T11:42:21.954913+00:00",
"ventureDuration": 120,
"location": "online",
"focus": [],
"technology": [],
"prerequisites": [],
"challenges": [],
"landingPage": null,
"privacy": "private",
"branding": {
"themeImage": "<https://www.arabnews.com/sites/default/files/main-image/2019/02/11/1456561-87422913.jpg>"
},
"testId": null,
"id": "1cb9eb9e-4529-41da-bb51-d92c5649ebcd",
"teamId": "dummy_tid",
"createdAt": "2022-09-21T15:44:43.831196+00:00",
"updatedAt": "2022-09-21T15:44:43.831203+00:00",
"publishedAt": null,
"status": "UNPUBLISHED",
"lifecycle": "STARTED",
"reportUrl": null
}
],
"totalCount": 2
}Tatiana
10/03/2022, 1:50 AMTatiana
10/03/2022, 1:58 AMTimothy Jones
10/03/2022, 6:01 AMTimothy Jones
10/03/2022, 6:01 AMTimothy Jones
10/03/2022, 6:06 AMeachLike matcher is being applied. Are you sure you are testing with the right pact file? Are you sure another test isn’t updating it with different data?Tatiana
10/03/2022, 10:44 AMJust to confirm, this is a pact file that is generated by Pact-JS, without modification?Yes, it was generated with pact-js after test run. I didn’t change anything there manually. I generated the pact and shared it with my provider. Provider uses Python and fast api.
Tatiana
10/03/2022, 10:46 AMTatiana
10/03/2022, 10:48 AMTatiana
10/03/2022, 10:54 AMTimothy Jones
10/03/2022, 11:17 AMTimothy Jones
10/03/2022, 11:18 AM{
"consumer": {
"name": "MyConsumer"
},
"interactions": [
{
"description": "A get request to get a pet 1845563262948980200",
"providerState": "A pet 1845563262948980200 exists",
"request": {
"headers": {
"api_key": "[]"
},
"method": "GET",
"path": "/v2/pet/1845563262948980200"
},
"response": {
"body": {
"items": [
{
"id": "1cb9eb9e",
"name": "test event 1",
"teamId": "dummy_tid"
}
]
},
"headers": {
"Content-Type": "application/json"
},
"matchingRules": {
"$.body.items": {
"match": "type"
}
},
"status": 200
}
},
{
"description": "A get request to get a pet 1845563262948980200",
"providerState": "No pets exist",
"request": {
"headers": {
"api_key": "[]"
},
"method": "GET",
"path": "/v2/pet/1845563262948980200"
},
"response": {
"status": 404
}
}
],
"metadata": {
"pact-js": {
"version": "10.1.4"
},
"pactRust": {
"ffi": "0.3.12",
"models": "0.4.5"
},
"pactSpecification": {
"version": "2.0.0"
}
},
"provider": {
"name": "pactWith v3"
}
}Timothy Jones
10/03/2022, 11:18 AM"matchingRules": {
"$.body.items": {
"match": "type"
}
},
which is supposed to have a min itemTimothy Jones
10/03/2022, 11:19 AMTimothy Jones
10/03/2022, 11:20 AMTimothy Jones
10/03/2022, 11:24 AMspec: SpecificationVersion.SPECIFICATION_VERSION_V2,, then it still generates the wrong pact file.Timothy Jones
10/03/2022, 11:26 AMeachLike matcher from dsl/matchersTimothy Jones
10/03/2022, 11:27 AMTatiana
10/03/2022, 11:27 AMconst provider = new PactV3({
dir: path.resolve(process.cwd(), 'contract-tests/pacts'),
consumer: 'mousedeer-events-consumer',
provider: 'eventhub-provider',
port: 8001,
spec: SpecificationVersion.SPECIFICATION_VERSION_V2
});Tatiana
10/03/2022, 11:27 AMTatiana
10/03/2022, 11:29 AMTimothy Jones
10/03/2022, 11:29 AMPactV3 which takes the V3 matchers, but it takes an option called spec where you can tell it to use V2, but when you do that you need to use the V2 matchers. But they’re not called V2 matchers, they’re called Matchers. And, it’s not obvious that the V3 matchers might not be appropriate to pass to PactV3.Timothy Jones
10/03/2022, 11:30 AMTatiana
10/03/2022, 11:30 AMTimothy Jones
10/03/2022, 11:30 AMTimothy Jones
10/03/2022, 11:31 AMTatiana
10/03/2022, 11:32 AMReally this should be a concern of the core. The core should just figure it out for you, and users don’t have to know.+1, totally agree 👍
Matt (pactflow.io / pact-js / pact-go)
> Really this should be a concern of the core. The core should just figure it out for you, and users don’t have to know.+1 also
Matt (pactflow.io / pact-js / pact-go)
eachLike for both V2 and V3 should actually be the same (they are both going to the same core!). I’m surprised that not passing in min to that matcher changes the behaviour. This makes me believe it probably never worked in the way it was intended (you can look at the definition of both variants of the code).Timothy Jones
10/03/2022, 12:00 PMMatt (pactflow.io / pact-js / pact-go)
Timothy Jones
10/03/2022, 12:01 PMMatt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Timothy Jones
10/03/2022, 12:02 PM'pact:matcher:type': 'type',
^ This means “shaped like this”Timothy Jones
10/03/2022, 12:03 PMmin to know that it means “repeatedly shaped like this”Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Timothy Jones
10/03/2022, 12:03 PM