From ded9a09ed9362262c34c5fde3eb2c28f14434dcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Garc=C3=ADa=20Crespo?= Date: Sat, 11 May 2024 05:38:47 +0000 Subject: [PATCH] Add tests to ssclient --- .github/workflows/test.yml | 37 ++++++++++++++++++++++ ssclient_test.go | 64 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 .github/workflows/test.yml create mode 100644 ssclient_test.go diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..f8cc13e --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,37 @@ +name: Tests +on: + pull_request: + push: +permissions: + contents: read +jobs: + test: + name: Test + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest] + runs-on: ${{ matrix.os }} + steps: + - name: Check out repository + uses: actions/checkout@v4 + - name: Install Go + uses: actions/setup-go@v5 + - name: Run tests + run: go test -race ./... + mod: + name: Check that `go mod tidy` is clean + runs-on: ubuntu-latest + steps: + - name: Check out repository + uses: actions/checkout@v4 + - name: Install Go + uses: actions/setup-go@v5 + with: + go-version-file: hack/ccp/go.mod + cache-dependency-path: hack/ccp/go.sum + - name: Check + run: | + go mod tidy + git diff --name-only --exit-code || (echo "Please run 'go mod tidy'."; exit 1) + working-directory: hack/ccp diff --git a/ssclient_test.go b/ssclient_test.go new file mode 100644 index 0000000..4cbaf02 --- /dev/null +++ b/ssclient_test.go @@ -0,0 +1,64 @@ +package ssclient_test + +import ( + "context" + "net/http" + "net/http/httptest" + "reflect" + "testing" + + kiotahttpgo "github.com/microsoft/kiota-http-go" + + "go.artefactual.dev/ssclient" +) + +func assertEqual(t *testing.T, got, want interface{}) { + if !reflect.DeepEqual(got, want) { + t.Errorf("Mismatch found:\nGot: %#v\nWant: %#v", got, want) + } +} + +func TestClient(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assertEqual(t, r.URL.Path, "/api/v2/location") + assertEqual(t, r.Header, http.Header(map[string][]string{ + "Accept": {"application/json"}, + "Accept-Encoding": {"gzip"}, + "Authorization": {"ApiKey test:test"}, + "User-Agent": {"kiota-go/" + kiotahttpgo.NewUserAgentHandlerOptions().ProductVersion}, + })) + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte(`{ + "meta": { + "limit": 20, + "next": null, + "offset": 0, + "previous": null, + "total_count": 1 + }, + "objects": [ + { + "enabled": true, + "path": "/home", + "pipeline": ["/api/v2/pipeline/a64e061a-5688-49b5-95c1-0b6885c40c04/"], + "purpose": "DS", + "quota": null, + "relative_path": "home", + "resource_uri": "/api/v2/location/fff70864-a5d4-4ca6-ab29-b4ce67d8eeab/", + "space": "/api/v2/space/218caeb7-fd59-4b7b-99b1-f5771a2dd34f/", + "uuid": "fff70864-a5d4-4ca6-ab29-b4ce67d8eeab" + } + ] + }`)) + })) + t.Cleanup(func() { srv.Close() }) + + client, err := ssclient.New(&http.Client{}, srv.URL, "test", "test") + assertEqual(t, err, nil) + + locations, err := client.Api().V2().Location().Get(context.Background(), nil) + assertEqual(t, err, nil) + assertEqual(t, len(locations.GetObjects()), 1) +}