Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add pact tests #21

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ install:

script:
- make travis-ci
- make publish

before_deploy:
- pip install --user bump2version
Expand Down
17 changes: 16 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
# Makefile to build go-sdk-template
include ./make/config.mk

all: build unittest lint tidy

travis-ci: build alltest lint tidy
travis-ci: install build alltest lint tidy

build:
go build ./...

unittest:
go test -race -coverprofile=coverage.txt -covermode=atomic `go list ./... | grep -v samples`

alltest: export PACT_TEST := true
alltest:
go test -race -coverprofile=coverage.txt -covermode=atomic `go list ./... | grep -v samples` -v -tags=integration
ls -al pacts

lint:
golangci-lint run

tidy:
go mod tidy

install:
@if [ ! -d pact/bin ]; then\
echo "--- Installing Pact CLI dependencies";\
curl -fsSL https://raw.githubusercontent.com/pact-foundation/pact-ruby-standalone/master/install.sh | bash;\
fi

publish: install
@echo "--- 📝 Publishing Pacts"
go run sqlv2/pact/publish.go
@echo
@echo "Pact contract publishing complete!"
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.17.0
github.com/pact-foundation/pact-go v1.6.6
github.com/stretchr/testify v1.7.0
go.mongodb.org/mongo-driver v1.8.1 // indirect
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f // indirect
Expand Down
89 changes: 89 additions & 0 deletions go.sum

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions make/config.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export PATH := $(PWD)/pact/bin:$(PATH)
export PATH
export PROVIDER_NAME = sql-service-api
export CONSUMER_NAME = go-sdk-sql-query
export PACT_DIR = $(PWD)/pacts
export LOG_DIR = $(PWD)/log
31 changes: 31 additions & 0 deletions sqlv2/pact/publish.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"fmt"
"log"
"os"
"path/filepath"

"github.com/IBM/sql-query-go-sdk/common"
"github.com/pact-foundation/pact-go/dsl"
"github.com/pact-foundation/pact-go/types"
)

func main() {
// Publish the Pacts...
p := dsl.Publisher{}

fmt.Println("Publishing Pact files to broker", os.Getenv("PACT_DIR"), os.Getenv("PACT_BROKER_URL"))
err := p.Publish(types.PublishRequest{
PactURLs: []string{filepath.FromSlash(fmt.Sprintf("%s/go-sdk-sql-query-sql-service-api.json", os.Getenv("PACT_DIR")))},
PactBroker: "https://sqlquery.pactflow.io",
ConsumerVersion: common.Version,
Tags: []string{"main"},
BrokerToken: os.Getenv("PACT_TOKEN"),
})

if err != nil {
log.Println("ERROR: ", err)
os.Exit(1)
}
}
138 changes: 138 additions & 0 deletions sqlv2/sql_v2_pact_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/**
* (C) Copyright IBM Corp. 2021.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sqlv2_test

import (
"fmt"
"log"
"os"
"testing"

"github.com/IBM/go-sdk-core/v4/core"
"github.com/IBM/sql-query-go-sdk/sqlv2"

"github.com/pact-foundation/pact-go/dsl"
)

var commonHeaders = dsl.MapMatcher{
"Content-Type": term("application/json; charset=utf-8", `application\/json`),
}

var instance string = "crn:v1:bluemix:public:sql-query:eu-de:a/f5e2ac71094077500e0d4b1ef8b9de0a:2ad2f1b6-6bce-42cd-b7ff-aa397e3afe9b::"

var client *sqlv2.SqlV2

func TestMain(m *testing.M) {
var exitCode int

// Setup Pact and related test stuff
setup()

// Run all the tests
exitCode = m.Run()

// Shutdown the Mock Service and Write pact files to disk
if err := pact.WritePact(); err != nil {
fmt.Println(err)
os.Exit(1)
}

pact.Teardown()
os.Exit(exitCode)
}

func TestClientPact_GetUser(t *testing.T) {
t.Run("tables can be listed", func(t *testing.T) {

pact.
AddInteraction().
Given("Instance has tables").
UponReceiving("A request to list all tables").
WithRequest(request{
Method: "GET",
Path: dsl.String("/tables"),
Query: dsl.MapMatcher{
"instance_crn": term(instance, "[a-zA-Z]+"),
"name_pattern": term("pluto", "[a-zA-Z]+"),
"type": term("table", "(table|view)"),
},
}).
WillRespondWith(dsl.Response{
Status: 200,
Body: dsl.Match(sqlv2.TableList{}),
Headers: commonHeaders,
})

err := pact.Verify(func() error {
// Construct an instance of the ListTablesOptions model
listTablesOptionsModel := new(sqlv2.ListTablesOptions)
listTablesOptionsModel.NamePattern = core.StringPtr("pluto")
listTablesOptionsModel.Type = core.StringPtr("table")
listTablesOptionsModel.Headers = map[string]string{}
// Expect response parsing to fail since we are receiving a text/plain response
_, _, operationErr := client.ListTables(listTablesOptionsModel)

if operationErr != nil {
t.Fatalf("Error on Verify: %v", operationErr)
}
return operationErr
})

if err != nil {
t.Fatalf("Error on Verify: %v", err)
}
})
}

// Common test data
var pact dsl.Pact

// Aliases
var term = dsl.Term

type request = dsl.Request

func setup() {
pact = createPact()

// Proactively start service to get access to the port
pact.Setup(true)

u := fmt.Sprintf("http://localhost:%d", pact.Server.Port)

var err error
client, err = sqlv2.NewSqlV2(&sqlv2.SqlV2Options{
URL: u,
Authenticator: &core.NoAuthAuthenticator{},
InstanceCrn: core.StringPtr(instance),
})

if err != nil {
log.Fatalf("Error on Verify: %v", err)
}

}

func createPact() dsl.Pact {
return dsl.Pact{
Consumer: os.Getenv("CONSUMER_NAME"),
Provider: os.Getenv("PROVIDER_NAME"),
LogDir: os.Getenv("LOG_DIR"),
PactDir: os.Getenv("PACT_DIR"),
LogLevel: "INFO",
DisableToolValidityCheck: true,
}
}