graphql_test.go

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