graphql_test.go

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