Skip to content
This repository has been archived by the owner on Feb 11, 2025. It is now read-only.

Commit

Permalink
Remove error string comparison from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mchompalova committed Apr 6, 2020
1 parent f0107b8 commit bc174f7
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 22 deletions.
8 changes: 4 additions & 4 deletions annotations/annotations_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/Financial-Times/go-ft-http/fthttp"
tidUtils "github.com/Financial-Times/transactionid-utils-go"
"github.com/husobee/vestigo"
"github.com/satori/go.uuid"
uuid "github.com/satori/go.uuid"
"github.com/stretchr/testify/assert"
)

Expand All @@ -34,7 +34,7 @@ func TestUnhappyAnnotationsAPIGTG(t *testing.T) {

annotationsAPI := NewUPPAnnotationsAPI(testClient, annotationsServerMock.URL+"/content/%v/annotations", testAPIKey)
err := annotationsAPI.GTG()
assert.EqualError(t, err, "gtg returned a non-200 HTTP status [503]: I am not happy!")
assert.Error(t, err)
}

func TestAnnotationsAPIGTGWrongAPIKey(t *testing.T) {
Expand All @@ -43,13 +43,13 @@ func TestAnnotationsAPIGTGWrongAPIKey(t *testing.T) {

annotationsAPI := NewUPPAnnotationsAPI(testClient, annotationsServerMock.URL+"/content/%v/annotations", "a-non-existing-key")
err := annotationsAPI.GTG()
assert.EqualError(t, err, "gtg returned a non-200 HTTP status [401]: unauthorized")
assert.Error(t, err)
}

func TestAnnotationsAPIGTGInvalidURL(t *testing.T) {
annotationsAPI := NewUPPAnnotationsAPI(testClient, ":#", testAPIKey)
err := annotationsAPI.GTG()
assert.EqualError(t, err, "gtg request error: parse :: missing protocol scheme")
assert.Error(t, err)
}

func TestAnnotationsAPIGTGConnectionError(t *testing.T) {
Expand Down
25 changes: 15 additions & 10 deletions annotations/annotations_rw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"

Expand Down Expand Up @@ -80,7 +81,7 @@ func TestRead500Error(t *testing.T) {
rw := NewRW(testClient, s.URL)
ctx := tidUtils.TransactionAwareContext(context.Background(), tid)
_, _, found, err := rw.Read(ctx, testContentUUID)
assert.EqualError(t, err, "annotations RW returned an unexpected HTTP status code in read operation: 500")
assert.Error(t, err)
assert.False(t, found)
}

Expand All @@ -90,7 +91,8 @@ func TestReadHTTPRequestError(t *testing.T) {
rw := NewRW(testClient, ":#")
ctx := tidUtils.TransactionAwareContext(context.Background(), tid)
_, _, found, err := rw.Read(ctx, testContentUUID)
assert.EqualError(t, err, "parse :: missing protocol scheme")
assert.Error(t, err.(*url.Error))
assert.Equal(t, err.(*url.Error).Op, "parse")
assert.False(t, found)
}

Expand All @@ -100,7 +102,8 @@ func TestReadHTTPCallError(t *testing.T) {
rw := NewRW(testClient, "")
ctx := tidUtils.TransactionAwareContext(context.Background(), tid)
_, _, found, err := rw.Read(ctx, testContentUUID)
assert.EqualError(t, err, "Get /drafts/content/db4daee0-2b84-465a-addb-fc8938a608db/annotations: unsupported protocol scheme \"\"")
assert.Error(t, err.(*url.Error))
assert.Equal(t, err.(*url.Error).Op, "Get")
assert.False(t, found)
}

Expand All @@ -112,7 +115,7 @@ func TestReadInvalidBodyError(t *testing.T) {
rw := NewRW(testClient, s.URL)
ctx := tidUtils.TransactionAwareContext(context.Background(), tid)
_, _, found, err := rw.Read(ctx, testContentUUID)
assert.EqualError(t, err, "invalid character 'i' looking for beginning of object key string")
assert.Error(t, err)
assert.False(t, found)
}

Expand Down Expand Up @@ -171,7 +174,7 @@ func TestUnhappyWriteStatus500(t *testing.T) {
rw := NewRW(testClient, s.URL)
ctx := tidUtils.TransactionAwareContext(context.Background(), tid)
_, err := rw.Write(ctx, testContentUUID, &expectedCanonicalizedAnnotations, oldHash)
assert.EqualError(t, err, "annotations RW returned an unexpected HTTP status code in write operation: 500")
assert.Error(t, err)
}

func TestWriteHTTPRequestError(t *testing.T) {
Expand All @@ -180,7 +183,8 @@ func TestWriteHTTPRequestError(t *testing.T) {
rw := NewRW(testClient, ":#")
ctx := tidUtils.TransactionAwareContext(context.Background(), tid)
_, err := rw.Write(ctx, testContentUUID, &expectedCanonicalizedAnnotations, oldHash)
assert.EqualError(t, err, "parse :: missing protocol scheme")
assert.Equal(t, err, err.(*url.Error))
assert.Equal(t, err.(*url.Error).Op, "parse")
}

func TestWriteHTTPCallError(t *testing.T) {
Expand All @@ -189,7 +193,8 @@ func TestWriteHTTPCallError(t *testing.T) {
rw := NewRW(testClient, "")
ctx := tidUtils.TransactionAwareContext(context.Background(), tid)
_, err := rw.Write(ctx, testContentUUID, &expectedCanonicalizedAnnotations, oldHash)
assert.EqualError(t, err, "Put /drafts/content/db4daee0-2b84-465a-addb-fc8938a608db/annotations: unsupported protocol scheme \"\"")
assert.Equal(t, err, err.(*url.Error))
assert.Equal(t, err.(*url.Error).Op, "Put")
}

func TestWriteMissingTID(t *testing.T) {
Expand Down Expand Up @@ -264,21 +269,21 @@ func TestRWHappyGTG(t *testing.T) {
func TestRWHTTPRequestErrorGTG(t *testing.T) {
rw := NewRW(testClient, ":#")
err := rw.GTG()
assert.EqualError(t, err, "gtg HTTP request error: parse :: missing protocol scheme")
assert.Error(t, err)
}

func TestRWHTTPCallErrorGTG(t *testing.T) {
rw := NewRW(testClient, "")
err := rw.GTG()
assert.EqualError(t, err, "gtg HTTP call error: Get /__gtg: unsupported protocol scheme \"\"")
assert.Error(t, err)
}

func TestRW503GTG(t *testing.T) {
s := newAnnotationsRWGTGServerMock(t, http.StatusServiceUnavailable, "service unavailable")
defer s.Close()
rw := NewRW(testClient, s.URL)
err := rw.GTG()
assert.EqualError(t, err, "gtg returned unexpected status 503: service unavailable")
assert.Error(t, err)
}

func newAnnotationsRWGTGServerMock(t *testing.T, status int, body string) *httptest.Server {
Expand Down
15 changes: 9 additions & 6 deletions concept/read_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
"net"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"

"github.com/Financial-Times/go-ft-http/fthttp"
tidUtils "github.com/Financial-Times/transactionid-utils-go"
"github.com/Pallinder/go-randomdata"
"github.com/husobee/vestigo"
"github.com/satori/go.uuid"
uuid "github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
logTest "github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -93,7 +94,8 @@ func TestGetConceptsByIDsBuildingHTTPRequestError(t *testing.T) {

ctx := tidUtils.TransactionAwareContext(context.Background(), tidUtils.NewTransactionID())
_, err := csAPI.GetConceptsByIDs(ctx, []string{"an-id"})
assert.EqualError(t, err, "parse :: missing protocol scheme")
assert.Equal(t, err, err.(*url.Error))
assert.Equal(t, err.(*url.Error).Op, "parse")
}

func TestGetConceptsByIDsHTTPCallError(t *testing.T) {
Expand All @@ -105,7 +107,8 @@ func TestGetConceptsByIDsHTTPCallError(t *testing.T) {

ctx := tidUtils.TransactionAwareContext(context.Background(), tidUtils.NewTransactionID())
_, err := csAPI.GetConceptsByIDs(ctx, []string{"an-id"})
assert.EqualError(t, err, "Get ?ids=an-id: unsupported protocol scheme \"\"")
assert.Equal(t, err, err.(*url.Error))
assert.Equal(t, err.(*url.Error).Op, "Get")
}

func TestGetConceptsByIDsNon200HTTPStatus(t *testing.T) {
Expand All @@ -119,7 +122,7 @@ func TestGetConceptsByIDsNon200HTTPStatus(t *testing.T) {

ctx := tidUtils.TransactionAwareContext(context.Background(), tidUtils.NewTransactionID())
_, err := csAPI.GetConceptsByIDs(ctx, []string{"an-id"})
assert.EqualError(t, err, "concept search API returned a non-200 HTTP status code: 503")
assert.Error(t, err)
}

func TestGetConceptsByIDsUnmarshallingPayloadError(t *testing.T) {
Expand All @@ -133,7 +136,7 @@ func TestGetConceptsByIDsUnmarshallingPayloadError(t *testing.T) {

ctx := tidUtils.TransactionAwareContext(context.Background(), tidUtils.NewTransactionID())
_, err := csAPI.GetConceptsByIDs(ctx, []string{"an-id"})
assert.EqualError(t, err, "invalid character '}' looking for beginning of value")
assert.Error(t, err)
}

func TestHappyGTG(t *testing.T) {
Expand Down Expand Up @@ -177,7 +180,7 @@ func TestUnhappyGTG(t *testing.T) {
csAPI := NewReadAPI(testClient, s.URL, apiKey, batchSize)

err := csAPI.GTG()
assert.EqualError(t, err, "concept search API returned a non-200 HTTP status code: 503")
assert.Error(t, err)
}

func generateConcepts(n int) map[string]Concept {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/sirupsen/logrus v0.0.0-20170713114250-a3f95b5c4235
github.com/stretchr/objx v0.1.1 // indirect
github.com/stretchr/testify v0.0.0-20180319223459-c679ae2cc0cb
golang.org/x/exp/errors v0.0.0-20200320212757-167ffe94c325
golang.org/x/net v0.0.0-20170719084000-02ac38e2528f // indirect
golang.org/x/sys v0.0.0-20170718161335-cd2c276457ed // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v0.0.0-20180319223459-c679ae2cc0cb h1:Idl4I/YpJ3WG7+/dNrHOGfnClSTk6iesxWxAjzvemdg=
github.com/stretchr/testify v0.0.0-20180319223459-c679ae2cc0cb/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/exp v0.0.0-20200320212757-167ffe94c325 h1:iPGJw87eUJvke9YLYKX0jIwLHiIrY/kXcFSgOpjav28=
golang.org/x/exp/errors v0.0.0-20200320212757-167ffe94c325 h1:IjaHpcTC71OjHsc3rr3pLdN6DYypM+R9HErovy6CmHE=
golang.org/x/exp/errors v0.0.0-20200320212757-167ffe94c325/go.mod h1:YgqsNsAu4fTvlab/7uiYK9LJrCIzKg/NiZUIH1/ayqo=
golang.org/x/net v0.0.0-20170719084000-02ac38e2528f h1:8TxU+eLCw23NIJ1TwakDJwcN5ikXvWBezX3nPZenP30=
golang.org/x/net v0.0.0-20170719084000-02ac38e2528f/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sys v0.0.0-20170718161335-cd2c276457ed h1:E8Urb17nGW+8kKyvrTl+0Rs9GhsQd9nU93B429bmayg=
Expand Down
3 changes: 1 addition & 2 deletions handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,8 +688,7 @@ func TestFetchFromAnnotationsAPIWithInvalidURL(t *testing.T) {

assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
assert.NoError(t, err)
assert.JSONEq(t, "{\"message\":\"Failed to read annotations: parse :: missing protocol scheme\"}", string(body))

assert.Contains(t, string(body), "Failed to read annotations")
rw.AssertExpectations(t)
aug.AssertExpectations(t)
}
Expand Down

0 comments on commit bc174f7

Please sign in to comment.