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

Pact json being overwritten by each test #71

Closed
gallamine opened this issue Mar 19, 2018 · 5 comments
Closed

Pact json being overwritten by each test #71

gallamine opened this issue Mar 19, 2018 · 5 comments

Comments

@gallamine
Copy link

Software versions

  • OSX 10.13 High Sierra
  • Pact-go 0.0.11
  • go version go1.9.2 darwin/amd64
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/william.cox/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/js/8ycxvlln2ljgdjyy834g14rr0000gq/T/go-build598867400=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

I'm writing Pact tests as part of a Golang application test system. I have several functions that each run a Pact test. They overwrite the previous functions pact definition such that my final pact.json only has the last run test. How do I have separate test functions that all append to the pact test?

Example:

func TestDiffMetadataPact(t *testing.T) {

	pact := &dsl.Pact{
		Port: 6666,
		Consumer: "Loader",
		Provider: "score_server",
		Host: "localhost",
	}
	defer pact.Teardown()
        //....


	var test = func() error {

		URL := fmt.Sprintf("http://localhost:%d/scores", pact.Server.Port)
		//test code goes here

		return err
	}

	pact.
		AddInteraction().
		Given("There exists some data").
		UponReceiving("A HEAD request").
		WithRequest(dsl.Request{
			Method:  "HEAD",
			Path:    "/scores",
			Query: "threshold=0.9",
			Headers: map[string]string{"If-Modified-Since": imsString,
									   "x-api-key": "<api key>"},
		}).
		WillRespondWith(dsl.Response{
			Status:  200,
			Headers: map[string]string{"Content-Type": "text/plain",
									   "X-Api-Content-Length": contentLength,
									   "last-modified": time.Now().Format(time.RFC1123)},
		})

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

func TestDiffContentPact(t *testing.T) {

	pact := &dsl.Pact{
		Port: 6666,
		Consumer: "Loader",
		Provider: "score_server",
		Host: "localhost",
	}
	defer pact.Teardown()
        // ...
	var test = func() error {

		URL := fmt.Sprintf("http://localhost:%d/scores", pact.Server.Port)
		/// test code here ... 

		return err
	}

	pact.
		AddInteraction().
		Given("The score_server has data to send us").
		UponReceiving("a GET request").
		WithRequest(dsl.Request{
			Method:  "GET",
			Path:    "/scores",
			Query: "threshold=0.9",
			Headers: map[string]string{"If-Modified-Since": imsString,
									   "x-api-key": "<api key>"},
		}).
		WillRespondWith(dsl.Response{
			Status:  200,
			Headers: map[string]string{"Content-Type": "text/plain",
									   "X-Api-Content-Length": contentLength,
									   "last-modified": time.Now().Format(time.RFC1123)},
			Body: "R 5510B5E8-A504-36DB-B46A-9478A03F84D4 0.0",
		})

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

When those tests pass and I look in pacts/loader-score_server.json I only see:

{
  "consumer": {
    "name": "Loader"
  },
  "provider": {
    "name": "score_server"
  },
  "interactions": [
    {
      "description": "a GET request",
      "providerState": "The score_server has data to send us",
      "request": {
        "method": "GET",
        "path": "/scores",
        "query": "threshold=0.9",
        "headers": {
          "If-Modified-Since": "Mon, 19 Mar 2018 16:48:29 EDT",
          "x-api-key": "<api key>"
        }
      },
      "response": {
        "status": 200,
        "headers": {
          "Content-Type": "text/plain",
          "X-Api-Content-Length": "100",
          "last-modified": "Mon, 19 Mar 2018 17:48:30 EDT"
        },
        "body": "R 5510B5E8-A504-36DB-B46A-9478A03F84D4 0.0"
      }
    }
  ],
  "metadata": {
    "pactSpecification": {
      "version": "2.0.0"
    }
  }
}

but that's only the last run test, not both of them.

@mefellows
Copy link
Member

Hi @gallamine, this is expected behaviour.

Check out splitting tests across multiple files. Basically, each time you instantiate a Pact struct with the same set of consumers/providers, we need to know if it's a new set of interactions, or just an addition to the previous. By default, we assume it's a new set that replaces the old.

Check out how the example consumer test does this with TestMain. Alternatively, set the pact file write mode to update.

@gallamine
Copy link
Author

This is helpful. Thanks! I've tried modifying my pact to look like this:

pact := &dsl.Pact{
		Port: 6666,
		Consumer: "Loader",
		Provider: "score_server",
		Host: "localhost",
		PactFileWriteMode: "update",
	}

in both test cases, but the behavior still persists. I've thus moved to using Golang subtests to only use one pact definition.

@mefellows
Copy link
Member

Mmm that shouldn't happen though, I'll reopen and investigate

@mefellows
Copy link
Member

mefellows commented Mar 20, 2018

OK unfortunately my docs were out of date @gallamine, the parameter should have been merge instead of update. Also, the behaviour of that flag changed and I hadn't updated. I've pushed an update just now and tested locally - a new beta build should be pushed up shortly . Could you please let me know if that fixes it?

mefellows added a commit that referenced this issue Mar 22, 2018
- Allows path, query and headers to contain Matchers
in order.
- Update integration test examples to demonstrate capability
- Speeds up test suite significantly

BREAKING CHANGE

Change updates the field definitions within the Request and
Response types (See MatcherMap and MatcherString).

Fixes #71
@mefellows
Copy link
Member

Closing this due to inactivity. I believe the issue is sorted, please re-open if there are further issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants