Skip to content

Commit

Permalink
tests: Include UT tests to pkg/api/graphql
Browse files Browse the repository at this point in the history
Add unit tests for `pkg/api/graphql` coverage as 86%

Signed-off-by: Kairo de Araujo <[email protected]>
  • Loading branch information
kairoaraujo committed Dec 21, 2023
1 parent 477fe51 commit 8d28176
Showing 1 changed file with 161 additions and 0 deletions.
161 changes: 161 additions & 0 deletions pkg/api/graphql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// Copyright 2023 The Archivista Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package api_test

import (
"context"
"net/http"
"net/http/httptest"
"testing"

"github.com/in-toto/archivista/pkg/api"
"github.com/stretchr/testify/suite"
)

// Test Suite: UT APIStore
type UTAPIGraphQLSuite struct {
suite.Suite
}

func TestAPIGraphQLSuite(t *testing.T) {
suite.Run(t, new(UTAPIGraphQLSuite))
}

func (ut *UTAPIGraphQLSuite) Test_Store() {

testServer := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(`{"data": {"data": "test"}}`))
if err != nil {
ut.FailNow(err.Error())
}
},
),
)
defer testServer.Close()
ctx := context.TODO()

type testSubjectVar struct {
Gitoid string `json:"gitoid"`
}

type testSubjectResult struct {
Data string `json:"data"`
}
result, err := api.GraphQlQuery[testSubjectResult](ctx, testServer.URL, `query`, testSubjectVar{Gitoid: "test_Gitoid"})
ut.NoError(err)
ut.Equal(testSubjectResult{Data: "test"}, result)
}

func (ut *UTAPIGraphQLSuite) Test_Store_NoServer() {

ctx := context.TODO()

type testSubjectVar struct {
Gitoid string `json:"gitoid"`
}

type testSubjectResult struct {
Data string `json:"data"`
}
result, err := api.GraphQlQuery[testSubjectResult](ctx, "http://invalid-archivista", `query`, testSubjectVar{Gitoid: "test_Gitoid"})
ut.Error(err)
ut.Equal(testSubjectResult{Data: ""}, result)
}

func (ut *UTAPIGraphQLSuite) Test_Store_BadStatusCode_NoMsg() {

testServer := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
},
),
)
defer testServer.Close()
ctx := context.TODO()

type testSubjectVar struct {
Gitoid string `json:"gitoid"`
}

type testSubjectResult struct {
Data string `json:"data"`
}
result, err := api.GraphQlQuery[testSubjectResult](ctx, testServer.URL, `query`, testSubjectVar{Gitoid: "test_Gitoid"})
ut.Error(err)
ut.Equal(testSubjectResult{Data: ""}, result)
}

func (ut *UTAPIGraphQLSuite) Test_Store_InvalidData() {

testServer := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(``))
if err != nil {
ut.FailNow(err.Error())
}
},
),
)
defer testServer.Close()
ctx := context.TODO()

type testSubjectVar struct {
Gitoid string `json:"gitoid"`
}

type testSubjectResult struct {
Data string `json:"data"`
}
result, err := api.GraphQlQuery[testSubjectResult](ctx, testServer.URL, `query`, testSubjectVar{Gitoid: "test_Gitoid"})
ut.Error(err)
ut.Equal(testSubjectResult{Data: ""}, result)
}

func (ut *UTAPIGraphQLSuite) Test_Store_QLReponseWithErrors() {

testServer := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(`{"data": {"data": "test"}, "errors": [{"message": "test_error"}]}}`))
if err != nil {
ut.FailNow(err.Error())
}
},
),
)
defer testServer.Close()
ctx := context.TODO()

type testSubjectVar struct {
Gitoid string `json:"gitoid"`
}

type testSubjectResult struct {
Data string `json:"data"`
Errors string `json:"errors"`
}

result, err := api.GraphQlQuery[testSubjectResult](ctx, testServer.URL, `query`, testSubjectVar{Gitoid: "test_Gitoid"})
ut.Error(err)
ut.EqualError(err, "graph ql query failed: [{test_error}]")
ut.Equal(testSubjectResult{Data: ""}, result)
}

0 comments on commit 8d28176

Please sign in to comment.