graphql_test.go

  1package graphql
  2
  3import (
  4	"bytes"
  5	"testing"
  6
  7	"github.com/99designs/gqlgen/client"
  8	"github.com/stretchr/testify/assert"
  9	"github.com/stretchr/testify/require"
 10
 11	"github.com/MichaelMure/git-bug/api/graphql/models"
 12	"github.com/MichaelMure/git-bug/cache"
 13	"github.com/MichaelMure/git-bug/misc/random_bugs"
 14	"github.com/MichaelMure/git-bug/repository"
 15)
 16
 17func TestQueries(t *testing.T) {
 18	repo := repository.CreateGoGitTestRepo(t, false)
 19
 20	random_bugs.FillRepoWithSeed(repo, 10, 42)
 21
 22	stderr := &bytes.Buffer{}
 23
 24	mrc := cache.NewMultiRepoCache()
 25	_, err := mrc.RegisterDefaultRepository(repo, stderr)
 26	require.NoError(t, err)
 27
 28	handler := NewHandler(mrc, nil)
 29
 30	c := client.New(handler)
 31
 32	query := `
 33     query {
 34        repository {
 35          allBugs(first: 2) {
 36            pageInfo {
 37              endCursor
 38              hasNextPage
 39              startCursor
 40              hasPreviousPage
 41            }
 42            nodes{
 43              author {
 44                name
 45                email
 46                avatarUrl
 47              }
 48      
 49              createdAt
 50              humanId
 51              id
 52              lastEdit
 53              status
 54              title
 55
 56              actors(first: 10) {
 57                pageInfo {
 58                  endCursor
 59                  hasNextPage
 60                  startCursor
 61                  hasPreviousPage
 62                }
 63                nodes {
 64                  id
 65                  humanId
 66                  name
 67                  displayName
 68                }
 69              }
 70
 71              participants(first: 10) {
 72                pageInfo {
 73                  endCursor
 74                  hasNextPage
 75                  startCursor
 76                  hasPreviousPage
 77                }
 78                nodes {
 79                  id
 80                  humanId
 81                  name
 82                  displayName
 83                }
 84              }
 85      
 86              comments(first: 2) {
 87                pageInfo {
 88                  endCursor
 89                  hasNextPage
 90                  startCursor
 91                  hasPreviousPage
 92                }
 93                nodes {
 94                  files
 95                  message
 96                }
 97              }
 98      
 99              operations(first: 20) {
100                pageInfo {
101                  endCursor
102                  hasNextPage
103                  startCursor
104                  hasPreviousPage
105                }
106                nodes {
107                  author {
108                    name
109                    email
110                    avatarUrl
111                  }
112                  date
113                  ... on CreateOperation {
114                    title
115                    message
116                    files
117                  }
118                  ... on SetTitleOperation {
119                    title
120                    was
121                  }
122                  ... on AddCommentOperation {
123                    files
124                    message
125                  }
126                  ... on SetStatusOperation {
127                    status
128                  }
129                  ... on LabelChangeOperation {
130                    added {
131                      name
132                      color {
133                        R
134                        G
135                        B
136                      }
137                    }
138                    removed {
139                      name
140                      color {
141                        R
142                        G
143                        B
144                      }
145                    }
146                  }
147                }
148              }
149            }
150          }
151        }
152      }`
153
154	type Identity struct {
155		Id          string `json:"id"`
156		HumanId     string `json:"humanId"`
157		Name        string `json:"name"`
158		Email       string `json:"email"`
159		AvatarUrl   string `json:"avatarUrl"`
160		DisplayName string `json:"displayName"`
161	}
162
163	type Label struct {
164		Name  string
165		Color struct {
166			R, G, B int
167		}
168	}
169
170	var resp struct {
171		Repository struct {
172			AllBugs struct {
173				PageInfo models.PageInfo
174				Nodes    []struct {
175					Author    Identity
176					CreatedAt string `json:"createdAt"`
177					HumanId   string `json:"humanId"`
178					Id        string
179					LastEdit  string `json:"lastEdit"`
180					Status    string
181					Title     string
182
183					Actors struct {
184						PageInfo models.PageInfo
185						Nodes    []Identity
186					}
187
188					Participants struct {
189						PageInfo models.PageInfo
190						Nodes    []Identity
191					}
192
193					Comments struct {
194						PageInfo models.PageInfo
195						Nodes    []struct {
196							Files   []string
197							Message string
198						}
199					}
200
201					Operations struct {
202						PageInfo models.PageInfo
203						Nodes    []struct {
204							Author  Identity
205							Date    string
206							Title   string
207							Files   []string
208							Message string
209							Was     string
210							Status  string
211							Added   []Label
212							Removed []Label
213						}
214					}
215				}
216			}
217		}
218	}
219
220	err = c.Post(query, &resp)
221	assert.NoError(t, err)
222
223	require.Equal(t, cache.ExpectedCacheInitializationMessage, stderr.String())
224}