Skip to content

Commit

Permalink
chore: split out message/v4 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F committed Jul 25, 2024
1 parent ae3795e commit d2f38cf
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 42 deletions.
13 changes: 8 additions & 5 deletions message/v4/asynchronous_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/assert"
)

// Sync - no plugin
func TestAsyncTypeSystem(t *testing.T) {
p, _ := NewAsynchronousPact(Config{
Consumer: "asyncconsumer",
Expand All @@ -20,7 +21,6 @@ func TestAsyncTypeSystem(t *testing.T) {
Foo string `json:"foo"`
}

// Sync - no plugin
err := p.AddAsynchronousMessage().
Given("some state").
Given("another state").
Expand All @@ -38,8 +38,11 @@ func TestAsyncTypeSystem(t *testing.T) {

assert.NoError(t, err)

// Sync - with plugin, but no transport
// TODO: ExecuteTest has been disabled for now, because it's not very useful
}

// Sync - with plugin, but no transport
// TODO: ExecuteTest has been disabled for now, because it's not very useful
func TestAsyncTypeSystem_CsvPlugin_Matcher(t *testing.T) {
csvInteraction := `{
"request.path": "/reports/report002.csv",
"response.status": "200",
Expand All @@ -52,14 +55,14 @@ func TestAsyncTypeSystem(t *testing.T) {
}
}`

p, _ = NewAsynchronousPact(Config{
p, _ := NewAsynchronousPact(Config{
Consumer: "asyncconsumer",
Provider: "asyncprovider",
PactDir: "/tmp/",
})

// TODO: enable when there is a transport for async to test!
err = p.AddAsynchronousMessage().
err := p.AddAsynchronousMessage().
Given("some state").
ExpectsToReceive("some csv content").
UsingPlugin(PluginConfig{
Expand Down
115 changes: 78 additions & 37 deletions message/v4/synchronous_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,21 @@ import (
"errors"
"fmt"
"os"
"strings"
"testing"

"github.com/pact-foundation/pact-go/v2/log"
"github.com/stretchr/testify/assert"
)

func TestSyncTypeSystem(t *testing.T) {
func TestSyncTypeSystem_NoPlugin(t *testing.T) {
p, _ := NewSynchronousPact(Config{
Consumer: "consumer",
Provider: "provider",
PactDir: "/tmp/",
})
_ = log.SetLogLevel("INFO")

dir, _ := os.Getwd()
path := fmt.Sprintf("%s/../../internal/native/pact_plugin.proto", dir)

grpcInteraction := `{
"pact:proto": "` + path + `",
"pact:proto-service": "PactPlugin/InitPlugin",
"pact:content-type": "application/protobuf",
"request": {
"implementation": "notEmpty('pact-go-driver')",
"version": "matching(semver, '0.0.0')"
},
"response": {
"catalogue": [
{
"type": "INTERACTION",
"key": "test"
}
]
}
}`

// Sync - no plugin
_ = p.AddSynchronousMessage("some description").
err := p.AddSynchronousMessage("some description").
Given("some state").
WithRequest(func(r *SynchronousMessageWithRequestBuilder) {
r.WithJSONContent(map[string]string{"foo": "bar"})
Expand All @@ -64,8 +42,17 @@ func TestSyncTypeSystem(t *testing.T) {

return nil
})
assert.NoError(t, err)
}

// Sync - with plugin, but no transport
func TestSyncTypeSystem_CsvPlugin_Matcher(t *testing.T) {
p, _ := NewSynchronousPact(Config{
Consumer: "consumer",
Provider: "provider",
PactDir: "/tmp/",
})

// Sync - with plugin, but no transport
csvInteraction := `{
"request.path": "/reports/report002.csv",
"response.status": "200",
Expand All @@ -78,28 +65,48 @@ func TestSyncTypeSystem(t *testing.T) {
}
}`

p, _ = NewSynchronousPact(Config{
Consumer: "consumer",
Provider: "provider",
PactDir: "/tmp/",
})
_ = p.AddSynchronousMessage("some description").
err := p.AddSynchronousMessage("some description").
Given("some state").
UsingPlugin(PluginConfig{
Plugin: "csv",
Version: "0.0.1",
Version: "0.0.6",
}).
WithContents(csvInteraction, "text/csv").
ExecuteTest(t, func(m SynchronousMessage) error {
fmt.Println("Executing the CSV test")
return nil
})

p, _ = NewSynchronousPact(Config{
assert.NoError(t, err)
}
func TestSyncTypeSystem_ProtobufPlugin_Matcher_Transport(t *testing.T) {
_ = log.SetLogLevel("INFO")
p, _ := NewSynchronousPact(Config{
Consumer: "consumer",
Provider: "provider",
PactDir: "/tmp/",
})
dir, _ := os.Getwd()
path := fmt.Sprintf("%s/../../internal/native/pact_plugin.proto", strings.ReplaceAll(dir, "\\", "/"))

grpcInteraction := `{
"pact:proto": "` + path + `",
"pact:proto-service": "PactPlugin/InitPlugin",
"pact:content-type": "application/protobuf",
"request": {
"implementation": "notEmpty('pact-go-driver')",
"version": "matching(semver, '0.0.0')"
},
"response": {
"catalogue": [
{
"type": "INTERACTION",
"key": "test"
}
]
}
}`

// Sync - with plugin + transport (pass)
err := p.AddSynchronousMessage("some description").
Given("some state").
Expand All @@ -116,15 +123,49 @@ func TestSyncTypeSystem(t *testing.T) {
})

assert.Error(t, err)
// assert.Equal(t, "Did not receive any requests for path 'PactPlugin/InitPlugin'", err)
// TODO:- Work out why we get the following error message

// Error
// synchronous_message_test.go:130:
// Error Trace: /Users/saf/dev/pact-foundation/pact-go/message/v4/synchronous_message_test.go:130
// Error: Not equal:
// expected: string("Did not receive any requests for path 'PactPlugin/InitPlugin'")
// actual : *errors.errorString(&errors.errorString{s:"pact validation failed: [{Request:{Method: Path:PactPlugin/InitPlugin Query: Headers:map[] Body:<nil>} Mismatches:[] Type:}]"})
// Logs
// 2024-07-04T01:36:33.745740Z DEBUG ThreadId(01) pact_plugin_driver::plugin_manager: Got response: ShutdownMockServerResponse { ok: false, results: [MockServerResult { path: "PactPlugin/InitPlugin", error: "Did not receive any requests for path 'PactPlugin/InitPlugin'", mismatches: [] }] }
}

p, _ = NewSynchronousPact(Config{
// Sync - with plugin + transport (fail)
func TestSyncTypeSystem_ProtobufPlugin_Matcher_Transport_Fail(t *testing.T) {
p, _ := NewSynchronousPact(Config{
Consumer: "consumer",
Provider: "provider",
PactDir: "/tmp/",
})
_ = log.SetLogLevel("INFO")
dir, _ := os.Getwd()
path := fmt.Sprintf("%s/../../internal/native/pact_plugin.proto", strings.ReplaceAll(dir, "\\", "/"))

grpcInteraction := `{
"pact:proto": "` + path + `",
"pact:proto-service": "PactPlugin/InitPlugin",
"pact:content-type": "application/protobuf",
"request": {
"implementation": "notEmpty('pact-go-driver')",
"version": "matching(semver, '0.0.0')"
},
"response": {
"catalogue": [
{
"type": "INTERACTION",
"key": "test"
}
]
}
}`

// Sync - with plugin + transport (fail)
err = p.AddSynchronousMessage("some description").
err := p.AddSynchronousMessage("some description").
Given("some state").
UsingPlugin(PluginConfig{
Plugin: "protobuf",
Expand Down

0 comments on commit d2f38cf

Please sign in to comment.