Skip to content

Commit f7fc823

Browse files
committed
api/schema: remove MethodTestCase in favor of AssertSchema()
MethodTestCase had lots of problems, most caused by a good degree of ambiguity in what we were actually testing. AssertSchema(), on the other hand, simplifies this problem greatly by correctly identifying the responsibilities of an *api.RequestSchema. An *api.RequestSchema is responsible for one thing, and one thing only: to provide a template for requests to be made against the API, completely regardless of protocol or implementation. Most notably, these responsibilities do not include: - Preforming an HTTP request, or - reading the response of an http request So MethodTestCase gets simplified to a simple assert.Equal(). We expect to get some *api.RequestSchema out, and we expect it to have certain parameters. Now, all of the API's Service tests are rather simple: create a schema, compare, rinse and repeat. The only thing left to do is test the schema of the request and response payload. This is what I had tried to do with the original MethodTestCase implementation, but the way the system is desinged, trying to accomplish this involves integrating with too many unrelated parts. To address this, I plan to introduce a JSON Schema implementation that compares generated request/reponse payloads with their ideal types.
1 parent 7b73f83 commit f7fc823

File tree

3 files changed

+33
-84
lines changed

3 files changed

+33
-84
lines changed

api/lock_api_test.go

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,19 @@ package api_test
33
import (
44
"net/http"
55
"testing"
6-
"time"
76

87
"github.com/github/git-lfs/api"
9-
"github.com/stretchr/testify/assert"
108
)
119

1210
var LockService api.LockService
1311

1412
func TestSuccessfullyObtainingALock(t *testing.T) {
15-
now := time.Now()
16-
17-
schema, resp := LockService.Lock(&api.LockRequest{
18-
Path: "/path/to/file",
19-
LatestRemoteCommit: "deadbeef",
20-
Committer: api.Committer{
21-
Name: "Jane Doe",
22-
23-
},
24-
})
25-
26-
tc := &MethodTestCase{
27-
Schema: schema,
28-
ExpectedPath: "/locks",
29-
ExpectedMethod: http.MethodPost,
30-
Resp: resp,
31-
Output: &api.LockResponse{
32-
Lock: api.Lock{
33-
Id: "some-lock-id",
34-
Path: "/path/to/file",
35-
Committer: api.Committer{
36-
Name: "Jane Doe",
37-
38-
},
39-
CommitSHA: "deadbeef",
40-
LockedAt: now,
41-
},
42-
},
43-
}
44-
45-
tc.Assert(t)
46-
47-
assert.Nil(t, resp.Err)
48-
assert.Equal(t, "", resp.CommitNeeded)
49-
assert.Equal(t, "some-lock-id", resp.Lock.Id)
50-
assert.Equal(t, "/path/to/file", resp.Lock.Path)
51-
assert.Equal(t, api.Committer{"Jane Doe", "[email protected]"}, resp.Lock.Committer)
52-
assert.Equal(t, now, resp.Lock.LockedAt)
53-
assert.True(t, resp.Lock.Active())
13+
got, body := LockService.Lock(new(api.LockRequest))
14+
15+
AssertSchema(t, &api.RequestSchema{
16+
Method: http.MethodPost,
17+
Path: "/locks",
18+
Body: new(api.LockRequest),
19+
Into: body,
20+
}, got)
5421
}

api/method_test.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

api/request_schema_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package api_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/github/git-lfs/api"
7+
"github.com/github/git-lfs/vendor/_nuts/github.com/stretchr/testify/assert"
8+
)
9+
10+
// SchemaTestCase encapsulates a single assertion of equality against two
11+
// generated RequestSchema instances.
12+
//
13+
// SchemaTestCases are meant only to test that the request schema generated by
14+
// an API service matches what we expect it to be. A SchemaTestCase does not
15+
// make use of the *api.Client, any particular lifecycle, or spin up a test
16+
// server. All of that behavior is tested at a higher strata in the
17+
// client/lifecycle tests.
18+
//
19+
// For examples, open any `*_api_test.go` file in the `api_test` package.
20+
//
21+
// - Expected is the *api.RequestSchema that we expected to be generated.
22+
// - Got is the *api.RequestSchema that was generated by a service.
23+
func AssertSchema(t *testing.T, expected, got *api.RequestSchema) {
24+
assert.Equal(t, expected, got)
25+
}

0 commit comments

Comments
 (0)