Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use autopilot to generate test data, will replace most tpl files #298

Merged
merged 2 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions clientGQL_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Templated(input string) string {
if err != nil {
panic(err)
}
return response
return strings.TrimSpace(response)
}

func TemplatedResponse(response string) autopilot.ResponseWriter {
Expand Down Expand Up @@ -80,11 +80,10 @@ func ATestClient(t *testing.T, endpoint string) *ol.Client {
}

func NewTestRequest(request string, variables string, response string) TestRequest {
templater := NewTestDataTemplater()
testRequest := TestRequest{
Request: templater.ParseTemplatedString(request),
Variables: templater.ParseTemplatedString(variables),
Response: templater.ParseTemplatedString(response),
Request: Templated(request),
Variables: Templated(variables),
Response: Templated(response),
}
if !strings.HasPrefix(testRequest.Request, "\"") || !strings.HasSuffix(testRequest.Request, "\"") {
panic(fmt.Errorf("testRequest Request should be wrapped in quotes: '%s'", testRequest.Request))
Expand Down Expand Up @@ -137,22 +136,18 @@ func (t *TestDataTemplater) ParseValue(value string) string {
return t.ParseTemplatedString(`{{ template "` + value + `" }}`)
}

func (t *TestDataTemplater) ParseTemplatedStringWith(contents string, givenData any) string {
func (t *TestDataTemplater) ParseTemplatedString(contents string) string {
target, err := t.rootTemplate.Parse(contents)
if err != nil {
panic(fmt.Errorf("error parsing template: %s", err))
}
data := bytes.NewBuffer([]byte{})
if err = target.Execute(data, givenData); err != nil {
if err = target.Execute(data, nil); err != nil {
panic(err)
}
return strings.TrimSpace(data.String())
}

func (t *TestDataTemplater) ParseTemplatedString(contents string) string {
return t.ParseTemplatedStringWith(contents, nil)
}

type TestRequest struct {
Request string
Variables string
Expand Down
33 changes: 17 additions & 16 deletions domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import (

func TestDomainCreate(t *testing.T) {
// Arrange
input := ol.DomainInput{
Name: ol.NewString("platform-test"),
Description: ol.NewString("Domain created for testing."),
Owner: &id1,
Note: ol.NewString("additional note about platform-test domain"),
}
inputVars := dataTemplater.ParseTemplatedStringWith(`{{ template "domain_input" . }}`, input)
input := autopilot.Register[ol.DomainInput]("domain_create_input",
ol.DomainInput{
Name: ol.NewString("platform-test"),
Description: ol.NewString("Domain created for testing."),
Owner: &id1,
Note: ol.NewString("additional note about platform-test domain"),
})

testRequest := NewTestRequest(
`"mutation DomainCreate($input:DomainInput!){domainCreate(input:$input){domain{id,aliases,name,description,htmlUrl,owner{... on Team{teamAlias:alias,id}},note},errors{message,path}}}"`,
`{"input": `+inputVars+`}`,
`{"input": {{ template "domain_create_input" }} }`,
`{"data": {"domainCreate": {"domain": {{ template "domain1_response" }} }}}`,
)

Expand Down Expand Up @@ -173,16 +173,17 @@ func TestDomainList(t *testing.T) {

func TestDomainUpdate(t *testing.T) {
// Arrange
input := ol.DomainInput{
Name: ol.NewString("platform-test-4"),
Description: ol.NewString("Domain created for testing."),
Owner: &id3,
Note: ol.NewString("Please delete me"),
}
inputVars := dataTemplater.ParseTemplatedStringWith(`{{ template "domain_input" . }}`, input)
input := autopilot.Register[ol.DomainInput]("domain_update_input",
ol.DomainInput{
Name: ol.NewString("platform-test-4"),
Description: ol.NewString("Domain created for testing."),
Owner: &id3,
Note: ol.NewString("Please delete me"),
})

testRequest := NewTestRequest(
`"mutation DomainUpdate($domain:IdentifierInput!$input:DomainInput!){domainUpdate(domain:$domain,input:$input){domain{id,aliases,name,description,htmlUrl,owner{... on Team{teamAlias:alias,id}},note},errors{message,path}}}"`,
`{"domain": { {{ template "id1" }} }, "input": `+inputVars+`}`,
`{"domain": { {{ template "id1" }} }, "input": {{ template "domain_update_input" }} }`,
`{"data": {"domainUpdate": {"domain": {{ template "domain1_response" }} }}}`,
)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/hashicorp/go-retryablehttp v0.7.4
github.com/hasura/go-graphql-client v0.10.0
github.com/relvacode/iso8601 v1.3.0
github.com/rocktavious/autopilot/v2023 v2023.11.2
github.com/rocktavious/autopilot/v2023 v2023.11.3-0.20231109195949-b29afb0d3daa
github.com/rs/zerolog v1.31.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ github.com/relvacode/iso8601 v1.3.0 h1:HguUjsGpIMh/zsTczGN3DVJFxTU/GX+MMmzcKoMO7
github.com/relvacode/iso8601 v1.3.0/go.mod h1:FlNp+jz+TXpyRqgmM7tnzHHzBnz776kmAH2h3sZCn0I=
github.com/rocktavious/autopilot/v2023 v2023.11.2 h1:Iger3ThydAaAC7vFDbD3JKoFuru5x0+GmwEqkQo6dG4=
github.com/rocktavious/autopilot/v2023 v2023.11.2/go.mod h1:BxaQ/N7Y6Rpcy04W1ovQVSeNYPHKhUVyKMdQ3a6kbK4=
github.com/rocktavious/autopilot/v2023 v2023.11.3-0.20231109195949-b29afb0d3daa h1:pFr32wQahU5u7D4gakXHP3h0LjIwryEt6BVPwzPjrOY=
github.com/rocktavious/autopilot/v2023 v2023.11.3-0.20231109195949-b29afb0d3daa/go.mod h1:BxaQ/N7Y6Rpcy04W1ovQVSeNYPHKhUVyKMdQ3a6kbK4=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
Expand Down
109 changes: 62 additions & 47 deletions team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (

// TODO: not sure if there is a better way to handle reusing a client
// Probably should be a feature of autopilot
var testRequestWithAlias = NewTestRequest(
`"query TeamGet($alias:String!){account{team(alias: $alias){alias,id,aliases,contacts{address,displayName,id,type},group{alias,id},htmlUrl,manager{id,email,htmlUrl,name,role},members{nodes{id,email,htmlUrl,name,role},{{ template "pagination_request" }},totalCount},memberships{nodes{team{alias,id},role,user{id,email}},pageInfo{hasNextPage,hasPreviousPage,startCursor,endCursor},totalCount},name,parentTeam{alias,id},responsibilities,tags{nodes{id,key,value},{{ template "pagination_request" }},totalCount}}}}"`,
`{"alias":"example"}`,
`{ "data": {
func getTestRequestWithAlias() TestRequest {
return NewTestRequest(
`"query TeamGet($alias:String!){account{team(alias: $alias){alias,id,aliases,contacts{address,displayName,id,type},group{alias,id},htmlUrl,manager{id,email,htmlUrl,name,role},members{nodes{id,email,htmlUrl,name,role},{{ template "pagination_request" }},totalCount},memberships{nodes{team{alias,id},role,user{id,email}},pageInfo{hasNextPage,hasPreviousPage,startCursor,endCursor},totalCount},name,parentTeam{alias,id},responsibilities,tags{nodes{id,key,value},{{ template "pagination_request" }},totalCount}}}}"`,
`{"alias":"example"}`,
`{ "data": {
"account": {
"team": {
"alias": "example",
Expand Down Expand Up @@ -84,13 +85,29 @@ var testRequestWithAlias = NewTestRequest(
"name": "Example",
"responsibilities": "Foo & bar"
}}}}`,
)
)
}

func TestCreateTeam(t *testing.T) {
// Arrange
contacts := autopilot.Register[[]ol.ContactInput]("contact_input_slice",
[]ol.ContactInput{
ol.CreateContactSlackHandle("@mozzie", ol.NullString()),
ol.CreateContactSlack("#general", ol.NewString("")),
ol.CreateContactWeb("https://example.com", ol.NewString("Homepage")),
},
)
input := autopilot.Register[ol.TeamCreateInput]("team_create_input",
ol.TeamCreateInput{
Name: "Example",
ManagerEmail: "[email protected]",
Responsibilities: "Foo & bar",
Contacts: &contacts,
ParentTeam: ol.NewIdentifier("parent_team"),
})
testRequest := NewTestRequest(
`"mutation TeamCreate($input:TeamCreateInput!){teamCreate(input: $input){team{alias,id,aliases,contacts{address,displayName,id,type},group{alias,id},htmlUrl,manager{id,email,htmlUrl,name,role},members{nodes{id,email,htmlUrl,name,role},{{ template "pagination_request" }},totalCount},memberships{nodes{team{alias,id},role,user{id,email}},pageInfo{hasNextPage,hasPreviousPage,startCursor,endCursor},totalCount},name,parentTeam{alias,id},responsibilities,tags{nodes{id,key,value},{{ template "pagination_request" }},totalCount}},errors{message,path}}}"`,
`{"input": {"name": "Example", "managerEmail": "[email protected]", "parentTeam": {"alias": "parent_team"}, "responsibilities": "Foo & bar", "contacts": [ {"type": "slack_handle", "address": "@mozzie"}, {"type": "slack", "displayName": "", "address": "#general"}, {"type": "web", "displayName": "Homepage", "address": "https://example.com"} ] }}`,
`{"input": {{ template "team_create_input" }} }`,
`{ "data": {
"teamCreate": {
"team": {
Expand All @@ -99,20 +116,7 @@ func TestCreateTeam(t *testing.T) {
"aliases": [
"example"
],
"contacts": [
{
"address": "#general",
"displayName": "",
{{ template "id2" }},
"type": "slack"
},
{
"address": "https://example.com",
"displayName": "Homepage",
{{ template "id3" }},
"type": "web"
}
],
"contacts": {{ template "contact_input_slice" }},
"htmlUrl": "https://app.opslevel-staging.com/teams/example",
"manager": {
"email": "[email protected]",
Expand Down Expand Up @@ -143,18 +147,7 @@ func TestCreateTeam(t *testing.T) {

client := BestTestClient(t, "team/create", testRequest)
// Act
contacts := []ol.ContactInput{
ol.CreateContactSlackHandle("@mozzie", ol.NullString()),
ol.CreateContactSlack("#general", ol.NewString("")),
ol.CreateContactWeb("https://example.com", ol.NewString("Homepage")),
}
result, err := client.CreateTeam(ol.TeamCreateInput{
Name: "Example",
ManagerEmail: "[email protected]",
Responsibilities: "Foo & bar",
Contacts: &contacts,
ParentTeam: ol.NewIdentifier("parent_team"),
})
result, err := client.CreateTeam(input)
// Assert
autopilot.Ok(t, err)
autopilot.Equals(t, "Example", result.Name)
Expand Down Expand Up @@ -339,7 +332,7 @@ func TestTeamTags(t *testing.T) {

func TestGetTeamWithAlias(t *testing.T) {
// Arrange
client := BestTestClient(t, "team/get_with_alias", testRequestWithAlias)
client := BestTestClient(t, "team/get_with_alias", getTestRequestWithAlias())
// Act
result, err := client.GetTeamWithAlias("example")
// Assert
Expand Down Expand Up @@ -686,9 +679,17 @@ func TestListTeamsWithManager(t *testing.T) {

func TestUpdateTeam(t *testing.T) {
// Arrange
input := autopilot.Register[ol.TeamUpdateInput]("team_update_input",
ol.TeamUpdateInput{
Id: id1,
ManagerEmail: "[email protected]",
Responsibilities: "Foo & bar",
ParentTeam: ol.NewIdentifier("parent_team"),
},
)
testRequest := NewTestRequest(
`"mutation TeamUpdate($input:TeamUpdateInput!){teamUpdate(input: $input){team{alias,id,aliases,contacts{address,displayName,id,type},group{alias,id},htmlUrl,manager{id,email,htmlUrl,name,role},members{nodes{id,email,htmlUrl,name,role},{{ template "pagination_request" }},totalCount},memberships{nodes{team{alias,id},role,user{id,email}},pageInfo{hasNextPage,hasPreviousPage,startCursor,endCursor},totalCount},name,parentTeam{alias,id},responsibilities,tags{nodes{id,key,value},{{ template "pagination_request" }},totalCount}},errors{message,path}}}"`,
`{"input": { {{ template "id1" }}, "managerEmail": "[email protected]", "responsibilities": "Foo & bar", "parentTeam": {"alias": "parent_team"} }}`,
`{"input": {{ template "team_update_input" }} }`,
`{ "data": {
"teamUpdate": {
"team": {
Expand Down Expand Up @@ -744,12 +745,7 @@ func TestUpdateTeam(t *testing.T) {
)
client := BestTestClient(t, "team/update", testRequest)
// Act
result, err := client.UpdateTeam(ol.TeamUpdateInput{
Id: id1,
ManagerEmail: "[email protected]",
Responsibilities: "Foo & bar",
ParentTeam: ol.NewIdentifier("parent_team"),
})
result, err := client.UpdateTeam(input)
// Assert
autopilot.Ok(t, err)
autopilot.Equals(t, "Example", result.Name)
Expand Down Expand Up @@ -795,7 +791,7 @@ func TestTeamAddMemberhip(t *testing.T) {
)

clientWithTeamId := BestTestClient(t, "team/add_member", testRequestWithTeamId)
clientWithAlias := BestTestClient(t, "team/get_with_alias_add_member", testRequestWithAlias)
clientWithAlias := BestTestClient(t, "team/get_with_alias_add_member", getTestRequestWithAlias())

// Act
team, _ := clientWithAlias.GetTeamWithAlias("example")
Expand Down Expand Up @@ -827,7 +823,7 @@ func TestTeamRemoveMemberhip(t *testing.T) {
"errors": []
}}}`,
)
client1 := BestTestClient(t, "team/get_with_alias_rm_member", testRequestWithAlias)
client1 := BestTestClient(t, "team/get_with_alias_rm_member", getTestRequestWithAlias())
client2 := BestTestClient(t, "team/remove_member", testRequest)
// Act
team, _ := client1.GetTeamWithAlias("example")
Expand All @@ -849,7 +845,7 @@ func TestTeamAddContact(t *testing.T) {
`{"input": {"type":"slack", "address":"#general", "teamId": "{{ template "id1_string" }}" }}`,
`{"data": {"contactCreate": {"contact": {"address": "#general", "displayName": "Slack", {{ template "id2" }}, "type": "slack"}, "errors": [] } }}`,
)
client1 := BestTestClient(t, "team/get_with_alias_add_contact", testRequestWithAlias)
client1 := BestTestClient(t, "team/get_with_alias_add_contact", getTestRequestWithAlias())
client2 := BestTestClient(t, "team/alias_add_contact", testRequest)
// Act
team, _ := client1.GetTeamWithAlias("example")
Expand All @@ -861,29 +857,48 @@ func TestTeamAddContact(t *testing.T) {

func TestTeamUpdateContact(t *testing.T) {
// Arrange
input := autopilot.Register[ol.ContactInput]("contact_input_slack",
ol.CreateContactSlack("#general", ol.NewString("Main Channel")),
)
autopilot.Register[ol.ContactUpdateInput]("contact_update_input_slack",
ol.ContactUpdateInput{
Id: id1,
DisplayName: *input.DisplayName,
Address: input.Address,
Type: &input.Type,
})
testRequest := NewTestRequest(
`"mutation ContactUpdate($input:ContactUpdateInput!){contactUpdate(input: $input){contact{address,displayName,id,type},errors{message,path}}}"`,
`{"input": { {{ template "id1" }}, "type":"slack", "displayName":"Main Channel", "address":"#general" }}`,
`{"input": {{ template "contact_update_input_slack" }} }`,
`{"data": {"contactUpdate": {"contact": {"address": "#general", "displayName": "Main Channel", {{ template "id2" }}, "type": "slack" }, "errors": [] }}}`,
)
client := BestTestClient(t, "team/update_contact", testRequest)
// Act
result, err := client.UpdateContact(id1, ol.CreateContactSlack("#general", ol.NewString("Main Channel")))
result, err := client.UpdateContact(id1, input)
// Assert
autopilot.Ok(t, err)
autopilot.Equals(t, "Main Channel", result.DisplayName)
}

func TestTeamUpdateContactWithTypeNil(t *testing.T) {
// Arrange
input := autopilot.Register[ol.ContactInput]("contact_input",
ol.ContactInput{Address: "#general", DisplayName: ol.NewString("Main Channel")},
)
autopilot.Register[ol.ContactUpdateInput]("contact_update_input",
ol.ContactUpdateInput{
Id: id2,
DisplayName: *input.DisplayName,
Address: input.Address,
})
testRequest := NewTestRequest(
`"mutation ContactUpdate($input:ContactUpdateInput!){contactUpdate(input: $input){contact{address,displayName,id,type},errors{message,path}}}"`,
`{ "input": { {{ template "id2" }}, "displayName": "Main Channel", "address": "#general" }}`,
`{ "input": {{ template "contact_update_input" }} }`,
`{"data": {"contactUpdate": {"contact": {"address": "#general", "displayName": "Main Channel", {{ template "id2" }}, "type": "slack" }, "errors": [] }}}`,
)
client := BestTestClient(t, "team/update_contact_nil_type", testRequest)
// Act
result, err := client.UpdateContact(id2, ol.ContactInput{Address: "#general", DisplayName: ol.NewString("Main Channel")})
result, err := client.UpdateContact(id2, input)
// Assert
autopilot.Ok(t, err)
autopilot.Equals(t, "Main Channel", result.DisplayName)
Expand Down
9 changes: 0 additions & 9 deletions testdata/templates/domains.tpl
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
{{- define "domain_input" -}}
{
"name": "{{ .Name }}",
"description": "{{ .Description }}",
"note": "{{ .Note }}",
"ownerId": "{{ .Owner }}"
}
{{- end}}

{{- define "domain1_response" }}
{
{{ template "id1" }},
Expand Down