Skip to content

Commit

Permalink
wip: rewriting type system, remove sugar, start type state pattern fo…
Browse files Browse the repository at this point in the history
…r v4
  • Loading branch information
mefellows committed Jun 8, 2022
1 parent b017cad commit 672956a
Show file tree
Hide file tree
Showing 36 changed files with 3,987 additions and 643 deletions.
10 changes: 0 additions & 10 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ The new package is migratable, with the the `/v2` major version bump

i.e. `github.com/pact-foundation/pact-go/v2`

### General

#### Sugar interface

The following package exposes aliases for the most commonly used interfaces for Matchers, common types and builders:

```
. "github.com/pact-foundation/pact-go/v2/sugar"
```

### Consumer

#### Primary Interface
Expand Down
11 changes: 0 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,6 @@ pact-go help

## Usage

### Sugar API

A convenience package exists with common interfaces in the `sugar` package. You can use a dot import to use it:

```golang
import (
// ...
. "github.com/pact-foundation/pact-go/v2/sugar"
)
```

### Consumer package

The consumer interface is in the package: `github.com/pact-foundation/pact-go/v2/consumer`.
Expand Down
4 changes: 2 additions & 2 deletions consumer/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// collaboration test cases, and Provider contract test verification.
package consumer

// TODO: setup a proper state machine to prevent actions
// TODO: setup a proper state machine to prevent actions -> use type state fluent pattern
// Current issues
// 1. Setup needs to be initialised to get a port -> should be resolved by creating the server at the point of verification
// 2. Ensure that interactions are properly cleared
Expand Down Expand Up @@ -115,7 +115,7 @@ func (p *httpMockProvider) configure() error {
p.config.ClientTimeout = 10 * time.Second
}

p.mockserver = native.NewPact(p.config.Consumer, p.config.Provider)
p.mockserver = native.NewHTTPPact(p.config.Consumer, p.config.Provider)
switch p.specificationVersion {
case models.V2:
p.mockserver.WithSpecificationVersion(native.SPECIFICATION_VERSION_V2)
Expand Down
23 changes: 18 additions & 5 deletions consumer/http_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import (
"github.com/pact-foundation/pact-go/v2/models"
)

// HTTPMockProviderV2 is the entrypoint for V3 http consumer tests
// V2HTTPMockProvider is the entrypoint for V3 http consumer tests
//
// This object is not thread safe
type HTTPMockProviderV2 struct {
type V2HTTPMockProvider struct {
*httpMockProvider
}

// NewV2Pact configures a new V2 HTTP Mock Provider for consumer tests
func NewV2Pact(config MockHTTPProviderConfig) (*HTTPMockProviderV2, error) {
provider := &HTTPMockProviderV2{
func NewV2Pact(config MockHTTPProviderConfig) (*V2HTTPMockProvider, error) {
provider := &V2HTTPMockProvider{
httpMockProvider: &httpMockProvider{
config: config,
specificationVersion: models.V2,
Expand All @@ -32,7 +32,7 @@ func NewV2Pact(config MockHTTPProviderConfig) (*HTTPMockProviderV2, error) {

// AddInteraction creates a new Pact interaction, initialising all
// required things. Will automatically start a Mock Service if none running.
func (p *HTTPMockProviderV2) AddInteraction() *InteractionV2 {
func (p *V2HTTPMockProvider) AddInteraction() *InteractionV2 {
log.Println("[DEBUG] pact add v2 interaction")
interaction := p.httpMockProvider.mockserver.NewInteraction("")

Expand All @@ -45,3 +45,16 @@ func (p *HTTPMockProviderV2) AddInteraction() *InteractionV2 {

return i
}

// InteractionV2 sets up an expected request/response on a mock server
// and is replayed on the provider side for verification
type InteractionV2 struct {
Interaction
}

// Given specifies a provider state. Optional.
func (i *InteractionV2) Given(state string) *InteractionV2 {
i.interaction.Given(state)

return i
}
27 changes: 22 additions & 5 deletions consumer/http_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (
"github.com/pact-foundation/pact-go/v2/models"
)

// HTTPMockProviderV3 is the entrypoint for V3 http consumer tests
// V3HTTPMockProvider is the entrypoint for V3 http consumer tests
// This object is not thread safe
type HTTPMockProviderV3 struct {
type V3HTTPMockProvider struct {
*httpMockProvider
}

// NewV3Pact configures a new V3 HTTP Mock Provider for consumer tests
func NewV3Pact(config MockHTTPProviderConfig) (*HTTPMockProviderV3, error) {
provider := &HTTPMockProviderV3{
func NewV3Pact(config MockHTTPProviderConfig) (*V3HTTPMockProvider, error) {
provider := &V3HTTPMockProvider{
httpMockProvider: &httpMockProvider{
config: config,
specificationVersion: models.V3,
Expand All @@ -31,7 +31,7 @@ func NewV3Pact(config MockHTTPProviderConfig) (*HTTPMockProviderV3, error) {

// AddInteraction creates a new Pact interaction, initialising all
// required things. Will automatically start a Mock Service if none running.
func (p *HTTPMockProviderV3) AddInteraction() *InteractionV3 {
func (p *V3HTTPMockProvider) AddInteraction() *InteractionV3 {
log.Println("[DEBUG] pact add v3 interaction")
interaction := p.httpMockProvider.mockserver.NewInteraction("")

Expand All @@ -44,3 +44,20 @@ func (p *HTTPMockProviderV3) AddInteraction() *InteractionV3 {

return i
}

// InteractionV3 sets up an expected request/response on a mock server
// and is replayed on the provider side for verification
type InteractionV3 struct {
Interaction
}

// Given specifies a provider state, may be called multiple times. Optional.
func (i *InteractionV3) Given(state models.V3ProviderState) *InteractionV3 {
if len(state.Parameters) > 0 {
i.Interaction.interaction.GivenWithParameter(state.Name, state.Parameters)
} else {
i.Interaction.interaction.Given(state.Name)
}

return i
}
14 changes: 0 additions & 14 deletions consumer/interaction_v2.go

This file was deleted.

127 changes: 0 additions & 127 deletions consumer/interaction_v2_test.go

This file was deleted.

20 changes: 0 additions & 20 deletions consumer/interaction_v3.go

This file was deleted.

9 changes: 6 additions & 3 deletions examples/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import (
"net/http"
"testing"

. "github.com/pact-foundation/pact-go/v2/sugar"
"github.com/pact-foundation/pact-go/v2/consumer"
"github.com/pact-foundation/pact-go/v2/matchers"
"github.com/stretchr/testify/assert"
)

type S = matchers.S

func TestProductAPIClient(t *testing.T) {
// Specify the two applications in the integration we are testing
// NOTE: this can usually be extracted out of the individual test for re-use)
mockProvider, err := NewV2Pact(MockHTTPProviderConfig{
mockProvider, err := consumer.NewV2Pact(consumer.MockHTTPProviderConfig{
Consumer: "PactGoProductAPIConsumer",
Provider: "PactGoProductAPI",
})
Expand All @@ -29,7 +32,7 @@ func TestProductAPIClient(t *testing.T) {
WithBodyMatch(&Product{})

// Act: test our API client behaves correctly
err = mockProvider.ExecuteTest(t, func(config MockServerConfig) error {
err = mockProvider.ExecuteTest(t, func(config consumer.MockServerConfig) error {
// Initialise the API client and point it at the Pact mock server
client := newClient(config.Host, config.Port)

Expand Down
Loading

0 comments on commit 672956a

Please sign in to comment.