diff --git a/avatar_test.go b/avatar_test.go index 219bd16e5ad9c6efe6d83d2bf0c9ac7aad70696d..c91533b4ff4ac21e46ab9224d30b49bda101f033 100644 --- a/avatar_test.go +++ b/avatar_test.go @@ -20,6 +20,9 @@ import ( "encoding/json" "net/http" "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestGetAvatar(t *testing.T) { @@ -28,27 +31,21 @@ func TestGetAvatar(t *testing.T) { const url = "https://www.gravatar.com/avatar/10e6bf7bcf22c2f00a3ef684b4ada178" - mux.HandleFunc("/api/v4/avatar", - func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, http.MethodGet) - w.WriteHeader(http.StatusAccepted) - avatar := Avatar{AvatarURL: url} - resp, _ := json.Marshal(avatar) - _, _ = w.Write(resp) - }, - ) + mux.HandleFunc("/api/v4/avatar", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodGet) + w.WriteHeader(http.StatusAccepted) + avatar := Avatar{AvatarURL: url} + resp, _ := json.Marshal(avatar) + _, _ = w.Write(resp) + }) opt := &GetAvatarOptions{Email: Ptr("sander@vanharmelen.nnl")} avatar, resp, err := client.Avatar.GetAvatar(opt) - if err != nil { - t.Fatalf("Avatar.GetAvatar returned error: %v", err) - } - if resp.Status != "202 Accepted" { - t.Fatalf("Avatar.GetAvatar returned wrong status code: %v", resp.Status) - } + require.NoError(t, err, "Avatar.GetAvatar should not return an error") + require.NotNil(t, resp, "Response should not be nil") + assert.Equal(t, "202 Accepted", resp.Status, "Expected HTTP status 202 Accepted") - if url != avatar.AvatarURL { - t.Errorf("Avatar.GetAvatar wrong result %s, want %s", avatar.AvatarURL, url) - } + require.NotNil(t, avatar, "Avatar should not be nil") + assert.Equal(t, url, avatar.AvatarURL, "Avatar.GetAvatar returned unexpected URL") } diff --git a/dora_metrics_test.go b/dora_metrics_test.go index 4b13b71bbee15899730629c4b1014e395aa7b15a..4af59ef04532479076fe5b2b6d764e2bf69120bb 100644 --- a/dora_metrics_test.go +++ b/dora_metrics_test.go @@ -6,6 +6,7 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -16,28 +17,25 @@ func TestDORAMetrics_GetProjectDORAMetrics(t *testing.T) { mux.HandleFunc("/api/v4/projects/1/dora/metrics", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodGet) query := r.URL.Query() + for k, v := range map[string]string{ "metric": "deployment_frequency", "start_date": "2021-03-01", "end_date": "2021-03-08", } { - if query.Get(k) != v { - t.Errorf("Query parameter %s: %s, want %s", k, query.Get(k), v) - } + assert.Equal(t, v, query.Get(k), "unexpected value for query param %s", k) } - fmt.Fprint(w, ` - [ - { "date": "2021-03-01", "value": 3 }, - { "date": "2021-03-02", "value": 6 }, - { "date": "2021-03-03", "value": 0 }, - { "date": "2021-03-04", "value": 0 }, - { "date": "2021-03-05", "value": 0 }, - { "date": "2021-03-06", "value": 0 }, - { "date": "2021-03-07", "value": 0 }, - { "date": "2021-03-08", "value": 4 } - ] - `) + fmt.Fprint(w, `[ + { "date": "2021-03-01", "value": 3 }, + { "date": "2021-03-02", "value": 6 }, + { "date": "2021-03-03", "value": 0 }, + { "date": "2021-03-04", "value": 0 }, + { "date": "2021-03-05", "value": 0 }, + { "date": "2021-03-06", "value": 0 }, + { "date": "2021-03-07", "value": 0 }, + { "date": "2021-03-08", "value": 4 } + ]`) }) want := []DORAMetric{ @@ -59,9 +57,10 @@ func TestDORAMetrics_GetProjectDORAMetrics(t *testing.T) { StartDate: &startDate, EndDate: &endDate, }) + require.NoError(t, err) require.NotNil(t, resp) - require.Equal(t, want, d) + assert.Equal(t, want, d) } func TestDORAMetrics_GetGroupDORAMetrics(t *testing.T) { @@ -71,28 +70,25 @@ func TestDORAMetrics_GetGroupDORAMetrics(t *testing.T) { mux.HandleFunc("/api/v4/groups/1/dora/metrics", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodGet) query := r.URL.Query() + for k, v := range map[string]string{ "metric": "deployment_frequency", "start_date": "2021-03-01", "end_date": "2021-03-08", } { - if query.Get(k) != v { - t.Errorf("Query parameter %s: %s, want %s", k, query.Get(k), v) - } + assert.Equal(t, v, query.Get(k), "unexpected value for query param %s", k) } - fmt.Fprint(w, ` - [ - { "date": "2021-03-01", "value": 3 }, - { "date": "2021-03-02", "value": 6 }, - { "date": "2021-03-03", "value": 0 }, - { "date": "2021-03-04", "value": 0 }, - { "date": "2021-03-05", "value": 0 }, - { "date": "2021-03-06", "value": 0 }, - { "date": "2021-03-07", "value": 0 }, - { "date": "2021-03-08", "value": 4 } - ] - `) + fmt.Fprint(w, `[ + { "date": "2021-03-01", "value": 3 }, + { "date": "2021-03-02", "value": 6 }, + { "date": "2021-03-03", "value": 0 }, + { "date": "2021-03-04", "value": 0 }, + { "date": "2021-03-05", "value": 0 }, + { "date": "2021-03-06", "value": 0 }, + { "date": "2021-03-07", "value": 0 }, + { "date": "2021-03-08", "value": 4 } + ]`) }) want := []DORAMetric{ @@ -114,7 +110,8 @@ func TestDORAMetrics_GetGroupDORAMetrics(t *testing.T) { StartDate: &startDate, EndDate: &endDate, }) + require.NoError(t, err) require.NotNil(t, resp) - require.Equal(t, want, d) + assert.Equal(t, want, d) } diff --git a/group_clusters_test.go b/group_clusters_test.go index a53266457fef04cb2729e137d4b251e83a10a6ff..8577a8719688953f349d63ea5b3a8d4c599ba7f3 100644 --- a/group_clusters_test.go +++ b/group_clusters_test.go @@ -19,8 +19,10 @@ package gitlab import ( "fmt" "net/http" - "reflect" "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestGroupListClusters(t *testing.T) { @@ -41,8 +43,7 @@ func TestGroupListClusters(t *testing.T) { "platform_type":"kubernetes", "environment_scope":"*", "cluster_type":"group_type", - "user": - { + "user": { "id":1, "name":"Administrator", "username":"root", @@ -50,16 +51,14 @@ func TestGroupListClusters(t *testing.T) { "avatar_url":"https://www.gravatar.com/avatar/4249f4df72b..", "web_url":"https://gitlab.example.com/root" }, - "platform_kubernetes": - { + "platform_kubernetes": { "api_url":"https://104.197.68.152", "authorization_type":"rbac", - "ca_cert":"-----BEGIN CERTIFICATE-----\r\nAAAAA\r\n-----END CERTIFICATE-----" + "ca_cert":"-----BEGIN CERTIFICATE-----\r\nAAAAA\r\n-----END CERTIFICATE-----" }, - "management_project": - { + "management_project": { "id":2, - "description": "sdhfgnbsdjfhg", + "description":"sdhfgnbsdjfhg", "name":"project2", "name_with_namespace":"John Doe8 / project2", "path":"project2", @@ -71,13 +70,11 @@ func TestGroupListClusters(t *testing.T) { "id":19, "name":"cluster-2" } - ]`) + ]`) }) clusters, _, err := client.GroupCluster.ListClusters(26) - if err != nil { - t.Errorf("GroupCluster.ListClusters returned error: %v", err) - } + require.NoError(t, err) want := []*GroupCluster{ { @@ -119,9 +116,8 @@ func TestGroupListClusters(t *testing.T) { Name: "cluster-2", }, } - if !reflect.DeepEqual(want, clusters) { - t.Errorf("GroupCluster.ListClusters returned %+v, want %+v", clusters, want) - } + + assert.Equal(t, want, clusters) } func TestGetGroupCluster(t *testing.T) { @@ -141,8 +137,7 @@ func TestGetGroupCluster(t *testing.T) { "platform_type":"kubernetes", "environment_scope":"*", "cluster_type":"group_type", - "user": - { + "user": { "id":1, "name":"Administrator", "username":"root", @@ -150,35 +145,30 @@ func TestGetGroupCluster(t *testing.T) { "avatar_url":"https://www.gravatar.com/avatar/4249f4df72b..", "web_url":"https://gitlab.example.com/root" }, - "platform_kubernetes": - { + "platform_kubernetes": { "api_url":"https://104.197.68.152", "authorization_type":"rbac", "ca_cert":"-----BEGIN CERTIFICATE-----\r\nAAAAA\r\n-----END CERTIFICATE-----" }, - "management_project": - { + "management_project": { "id":2, - "description": "skjdfgsdfg", + "description":"skjdfgsdfg", "name":"project2", "name_with_namespace":"John Doe8 / project2", "path":"project2", "path_with_namespace":"namespace2/project2", "created_at":"2019-10-11T02:55:54.138Z" }, - "group": - { + "group": { "id":26, "name":"group-with-clusters-api", "web_url":"https://gitlab.example.com/group-with-clusters-api" } - }`) + }`) }) cluster, _, err := client.GroupCluster.GetCluster(26, 18) - if err != nil { - t.Errorf("GroupCluster.GetCluster returned error: %v", err) - } + require.NoError(t, err) want := &GroupCluster{ ID: 18, @@ -219,9 +209,8 @@ func TestGetGroupCluster(t *testing.T) { WebURL: "https://gitlab.example.com/group-with-clusters-api", }, } - if !reflect.DeepEqual(want, cluster) { - t.Errorf("GroupCluster.GetCluster returned %+v, want %+v", cluster, want) - } + + assert.Equal(t, want, cluster) } func TestAddGroupCluster(t *testing.T) { @@ -240,8 +229,7 @@ func TestAddGroupCluster(t *testing.T) { "platform_type":"kubernetes", "environment_scope":"*", "cluster_type":"group_type", - "user": - { + "user": { "id":1, "name":"Administrator", "username":"root", @@ -249,26 +237,22 @@ func TestAddGroupCluster(t *testing.T) { "avatar_url":"https://www.gravatar.com/avatar/4249f4df72b..", "web_url":"https://gitlab.example.com/root" }, - "platform_kubernetes": - { + "platform_kubernetes": { "api_url":"https://35.111.51.20", "authorization_type":"rbac", "ca_cert":"-----BEGIN CERTIFICATE-----\r\nAAAAA\r\n-----END CERTIFICATE-----" }, - "management_project":null, - "group": - { + "management_project": null, + "group": { "id":26, "name":"group-with-clusters-api", "web_url":"https://gitlab.example.com/root/group-with-clusters-api" } - }`) + }`) }) cluster, _, err := client.GroupCluster.AddCluster(26, &AddGroupClusterOptions{}) - if err != nil { - t.Errorf("GroupCluster.AddCluster returned error: %v", err) - } + require.NoError(t, err) want := &GroupCluster{ ID: 24, @@ -300,9 +284,8 @@ func TestAddGroupCluster(t *testing.T) { WebURL: "https://gitlab.example.com/root/group-with-clusters-api", }, } - if !reflect.DeepEqual(want, cluster) { - t.Errorf("GroupCluster.AddCluster returned %+v, want %+v", cluster, want) - } + + assert.Equal(t, want, cluster) } func TestEditGroupCluster(t *testing.T) { @@ -322,43 +305,40 @@ func TestEditGroupCluster(t *testing.T) { "platform_type":"kubernetes", "environment_scope":"*", "cluster_type":"group_type", - "user": - { - "id":1, - "name":"Administrator", - "username":"root", - "state":"active", - "avatar_url":"https://www.gravatar.com/avatar/4249f4df72b..", - "web_url":"https://gitlab.example.com/root" + "user": { + "id":1, + "name":"Administrator", + "username":"root", + "state":"active", + "avatar_url":"https://www.gravatar.com/avatar/4249f4df72b..", + "web_url":"https://gitlab.example.com/root" }, - "platform_kubernetes": - { - "api_url":"https://new-api-url.com", - "authorization_type":"rbac" + "platform_kubernetes": { + "api_url":"https://new-api-url.com", + "authorization_type":"rbac" }, - "management_project": - { - "id":2, - "description":"sjdkfngjkdsfngdfgndfg", - "name":"project2", - "name_with_namespace":"John Doe8 / project2", - "path":"project2", - "path_with_namespace":"namespace2/project2", - "created_at":"2019-10-11T02:55:54.138Z" + "management_project": { + "id":2, + "description":"sjdkfngjkdsfngdfgndfg", + "name":"project2", + "name_with_namespace":"John Doe8 / project2", + "path":"project2", + "path_with_namespace":"namespace2/project2", + "created_at":"2019-10-11T02:55:54.138Z" }, - "group": - { - "id":26, - "name":"group-with-clusters-api", - "web_url":"https://gitlab.example.com/group-with-clusters-api" + "group": { + "id":26, + "name":"group-with-clusters-api", + "web_url":"https://gitlab.example.com/group-with-clusters-api" } - }`) + }`) }) name := "new-cluster-name" domain := "new-domain.com" environmentScope := "*" apiURL := "https://new-api-url.com" + opt := &EditGroupClusterOptions{ Name: &name, Domain: &domain, @@ -367,10 +347,10 @@ func TestEditGroupCluster(t *testing.T) { APIURL: &apiURL, }, } + cluster, _, err := client.GroupCluster.EditCluster(26, 24, opt) - if err != nil { - t.Errorf("GroupCluster.EditCluster returned error: %v", err) - } + require.NoError(t, err, "GroupCluster.EditCluster should not return an error") + require.NotNil(t, cluster, "Cluster response should not be nil") want := &GroupCluster{ ID: 24, @@ -410,9 +390,8 @@ func TestEditGroupCluster(t *testing.T) { WebURL: "https://gitlab.example.com/group-with-clusters-api", }, } - if !reflect.DeepEqual(want, cluster) { - t.Errorf("GroupCluster.EditCluster returned %+v, want %+v", cluster, want) - } + + assert.Equal(t, want, cluster, "GroupCluster.EditCluster returned unexpected result") } func TestDeleteGroupCluster(t *testing.T) { @@ -424,7 +403,5 @@ func TestDeleteGroupCluster(t *testing.T) { }) _, err := client.GroupCluster.DeleteCluster(26, 23) - if err != nil { - t.Errorf("GroupCluster.DeleteCluster returned error: %v", err) - } + require.NoError(t, err, "GroupCluster.DeleteCluster should not return an error") }