Skip to content

Commit

Permalink
Merge pull request #296 from OpsLevel/db/test-data-consolidate
Browse files Browse the repository at this point in the history
consolidate test data in tpl files, DomainInput creates test data
  • Loading branch information
davidbloss authored Nov 9, 2023
2 parents 0543d45 + 56fda3c commit 146302d
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 58 deletions.
2 changes: 1 addition & 1 deletion actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestCreateWebhookAction(t *testing.T) {
// Arrange
testRequest := NewTestRequest(
`"mutation WebhookActionCreate($input:CustomActionsWebhookActionCreateInput!){customActionsWebhookActionCreate(input: $input){webhookAction{{ template "custom_actions_request" }},errors{message,path}}}"`,
`{"input":{"headers":"{\"Content-Type\":\"application/json\"}","httpMethod":"POST","liquidTemplate":"{\"token\": \"XXX\", \"ref\":\"main\", \"action\": \"rollback\"}","name":"Deploy Rollback","webhookUrl":"https://gitlab.com/api/v4/projects/1/trigger/pipeline"}}`,
`{"input":{"headers":"{\"Content-Type\":\"application/json\"}","httpMethod":"POST",{{ template "liquid_template_rollback" }},"name":"Deploy Rollback","webhookUrl":"https://gitlab.com/api/v4/projects/1/trigger/pipeline"}}`,
`{"data": {"customActionsWebhookActionCreate": { "webhookAction": {{ template "custom_action1" }}, "errors": [] }}}`,
)

Expand Down
8 changes: 6 additions & 2 deletions clientGQL_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,22 @@ func (t *TestDataTemplater) ParseValue(value string) string {
return t.ParseTemplatedString(`{{ template "` + value + `" }}`)
}

func (t *TestDataTemplater) ParseTemplatedString(contents string) string {
func (t *TestDataTemplater) ParseTemplatedStringWith(contents string, givenData any) 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, nil); err != nil {
if err = target.Execute(data, givenData); 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
31 changes: 17 additions & 14 deletions domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ 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)

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": { "name": "platform-test", "description": "Domain created for testing.", "ownerId": "{{ template "id1_string" }}", "note": "additional note about platform-test domain" }}`,
`{"input": `+inputVars+`}`,
`{"data": {"domainCreate": {"domain": {{ template "domain1_response" }} }}}`,
)

client := BestTestClient(t, "domain/create", testRequest)
// Act
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"),
}
result, err := client.CreateDomain(input)
// Assert
autopilot.Ok(t, err)
Expand Down Expand Up @@ -171,19 +173,20 @@ func TestDomainList(t *testing.T) {

func TestDomainUpdate(t *testing.T) {
// Arrange
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": {"name": "platform-test-4", "description":"Domain created for testing.", "ownerId":"{{ template "id3_string" }}", "note": "Please delete me" }}`,
`{"data": {"domainUpdate": {"domain": {{ template "domain1_response" }} }}}`,
)

client := BestTestClient(t, "domain/update", testRequest)
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)
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+`}`,
`{"data": {"domainUpdate": {"domain": {{ template "domain1_response" }} }}}`,
)

client := BestTestClient(t, "domain/update", testRequest)
// Act
result, err := client.UpdateDomain(string(id1), input)
// Assert
Expand Down
41 changes: 5 additions & 36 deletions testdata/templates/check.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@
"enabled": true,
"filter": null,
{{ template "id1" }},
"level": {
"alias": "bronze",
"description": "Services in this level satisfy critical checks. This is the minimum standard to ship to production.",
"id": "Z2lkOi8vb3BzbGV2ZWwvTGV2ZWwvMzE3",
"index": 1,
"name": "Bronze"
},
"level": {{ template "level_1" }},
"name": "Repository Integrated",
"notes": null
{{ end }}
Expand All @@ -34,44 +28,19 @@
"category": null,
"description": "The service has a metrics tool.",
"enabled": true,
"filter": {
"connective": null,
"htmlUrl": "https://app.opslevel.com/filters/401",
"id": "Z2lkOi8vb3BzbGV2ZWwvQ2hlY2tsaXN0LzQwMQ",
"name": "Tier 1 Services",
"predicates": [
{
"key": "tier_index",
"keyData": null,
"type": "equals",
"value": "1"
}
]
},
"filter": { {{ template "filter_tier1service_response" }} },
"id": "Z2lkOi8vb3BzbGV2ZWwvQ2hlY2tzOjpUb29sVXNhZ2UvMTMyNw",
"level": {
"alias": "bronze",
"description": "Services in this level satisfy critical checks. This is the minimum standard to ship to production.",
"id": "Z2lkOi8vb3BzbGV2ZWwvTGV2ZWwvMzE3",
"index": 1,
"name": "Bronze"
},
{{ template "level_bronze" }},
"name": "Metrics Tool",
"notes": null
{{ end }}
{{- define "owner_defined_check" }}
"category": null,
"description": "Verifies that the service has an owner defined.",
"description": "Verifies that the service has an owner defined.",
"enabled": true,
"filter": null,
"id": "Z2lkOi8vb3BzbGV2ZWwvQ2hlY2tzOjpIYXNPd25lci8xMzI9",
"level": {
"alias": "bronze",
"description": "Services in this level satisfy critical checks. This is the minimum standard to ship to production.",
"id": "Z2lkOi8vb3BzbGV2ZWwvTGV2ZWwvMzE3",
"index": 1,
"name": "Bronze"
},
{{ template "level_bronze" }},
"name": "Owner Defined",
"notes": null
{{ end }}
26 changes: 21 additions & 5 deletions testdata/templates/custom_actions.tpl
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
{{- define "liquid_template_freeze_string" -}}
{\"token\": \"XXX\", \"ref\":\"main\", \"action\": \"freeze\"}
{{- end }}
{{- define "liquid_template_rollback_string" -}}
{\"token\": \"XXX\", \"ref\":\"main\", \"action\": \"rollback\"}
{{- end }}
{{- define "liquid_template_freeze" -}}
"liquidTemplate": "{{- template "liquid_template_freeze_string" -}}"
{{ end }}

{{- define "liquid_template_rollback" }}
"liquidTemplate": "{{- template "liquid_template_rollback_string" -}}"
{{ end }}

{{- define "custom_action1_response" }}
"aliases": [],
"description": null,
"id": "123456789",
"liquidTemplate": "{\"token\": \"XXX\", \"ref\":\"main\", \"action\": \"rollback\"}",
{{ template "liquid_template_rollback" }},
"name": "Deploy Rollback",
"headers": {
"Content-Type": "application/json"
Expand All @@ -14,7 +30,7 @@
"aliases": [],
"description": "Trigger a deploy freeze",
"id": "987654322",
"liquidTemplate": "{\"token\": \"XXX\", \"ref\":\"main\", \"action\": \"freeze\"}",
{{ template "liquid_template_freeze" }},
"name": "Deploy Freeze",
"headers": {
"Accept": "application/vnd.github+json",
Expand All @@ -27,7 +43,7 @@
"aliases": [],
"description": "Page the On-Call Engineer",
"id": "987654323",
"liquidTemplate": "{\"token\": \"XXX\", \"ref\":\"main\", \"action\": \"freeze\"}",
{{ template "liquid_template_freeze" }},
"name": "Page On-Call",
"headers": {
"Accept": "application/vnd.github+json",
Expand Down Expand Up @@ -108,7 +124,7 @@
"aliases": [],
"description": null,
"id": "123456789",
"liquidTemplate": "{\"token\": \"XXX\", \"ref\":\"main\", \"action\": \"rollback\"}",
{{ template "liquid_template_rollback" }},
"name": "Deploy Rollback",
"headers": {
"Content-Type": "application/json"
Expand All @@ -120,7 +136,7 @@
"aliases": [],
"description": "Trigger a deploy freeze",
"id": "987654321",
"liquidTemplate": "{\"token\": \"XXX\", \"ref\":\"main\", \"action\": \"freeze\"}",
{{ template "liquid_template_freeze" }},
"name": "Deploy Freeze",
"headers": {
"Accept": "application/vnd.github+json",
Expand Down
9 changes: 9 additions & 0 deletions testdata/templates/domains.tpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
{{- define "domain_input" -}}
{
"name": "{{ .Name }}",
"description": "{{ .Description }}",
"note": "{{ .Note }}",
"ownerId": "{{ .Owner }}"
}
{{- end}}

{{- define "domain1_response" }}
{
{{ template "id1" }},
Expand Down
4 changes: 4 additions & 0 deletions testdata/templates/filters.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"name": "Kubernetes",
"predicates": []
{{ end }}

{{- define "filter_tier1service_response" }}
"connective": null,
"htmlUrl": "https://app.opslevel.com/filters/401",
Expand All @@ -56,6 +57,7 @@
}
]
{{ end }}

{{- define "filter_complex_kubernetes_response" }}
"connective": null,
"htmlUrl": "https://app.opslevel.com/filters/452",
Expand Down Expand Up @@ -87,6 +89,7 @@
],
"connective": "or"
{{ end }}

{{- define "create_filter_nested_response" }}
"connective": "or",
"htmlUrl": "https://app.opslevel.com/filters/2346",
Expand Down Expand Up @@ -124,6 +127,7 @@
],
"connective": "and"
{{ end }}

{{- define "update_filter_nested_response" }}
"connective": "and",
"htmlUrl": "https://app.opslevel.com/filters/2346",
Expand Down
10 changes: 10 additions & 0 deletions testdata/templates/levels.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,13 @@
"name": "{{ template "name3" }}"
}
{{ end }}

{{- define "level_bronze" }}
"level": {
"alias": "bronze",
"description": "Services in this level satisfy critical checks. This is the minimum standard to ship to production.",
"id": "Z2lkOi8vb3BzbGV2ZWwvTGV2ZWwvMzE3",
"index": 1,
"name": "Bronze"
}
{{ end }}

0 comments on commit 146302d

Please sign in to comment.