Skip to content

Commit

Permalink
fix(mock): update payloads and test for struct changes (#1060)
Browse files Browse the repository at this point in the history
* fix(mock): update payloads and test for struct changes

* fix json unmarshal error message
  • Loading branch information
ecrupper committed Feb 9, 2024
1 parent b5207c4 commit b0c6f13
Show file tree
Hide file tree
Showing 21 changed files with 429 additions and 84 deletions.
9 changes: 8 additions & 1 deletion mock/server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,20 @@ const (
BuildResp = `{
"id": 1,
"repo_id": 1,
"pipeline_id": 1,
"number": 1,
"parent": 1,
"event": "push",
"event_action": "",
"status": "created",
"error": "",
"enqueued": 1563474077,
"created": 1563474076,
"started": 1563474077,
"finished": 0,
"deploy": "",
"deploy_number": 1,
"deploy_payload": {},
"clone": "https://github.com/github/octocat.git",
"source": "https://github.com/github/octocat/commit/48afb5bdc41ad69bf22588491333f7cf71135163",
"title": "push received from https://github.com/github/octocat",
Expand All @@ -39,10 +43,13 @@ const (
"link": "https://vela.example.company.com/github/octocat/1",
"branch": "main",
"ref": "refs/heads/main",
"head_ref": "",
"base_ref": "",
"host": "example.company.com",
"runtime": "docker",
"distribution": "linux"
"distribution": "linux",
"approved_at": 0,
"approved_by": ""
}`

// BuildsResp represents a JSON return for one to many builds.
Expand Down
28 changes: 28 additions & 0 deletions mock/server/build_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: Apache-2.0

package server

import (
"encoding/json"
"reflect"
"testing"

"github.com/go-vela/types/library"
)

func TestBuild_ActiveBuildResp(t *testing.T) {
testBuild := library.Build{}

err := json.Unmarshal([]byte(BuildResp), &testBuild)
if err != nil {
t.Errorf("error unmarshaling build: %v", err)
}

tBuild := reflect.TypeOf(testBuild)

for i := 0; i < tBuild.NumField(); i++ {
if reflect.ValueOf(testBuild).Field(i).IsNil() {
t.Errorf("BuildResp missing field %s", tBuild.Field(i).Name)
}
}
}
8 changes: 6 additions & 2 deletions mock/server/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ const (
// DeploymentResp represents a JSON return for a single build.
DeploymentResp = `{
"id": 1,
"number": 1234,
"repo_id": 1,
"url": "https://api.github.com/repos/github/octocat/deployments/1",
"user": "octocat",
"commit": "48afb5bdc41ad69bf22588491333f7cf71135163",
"ref": "main",
"task": "deploy:vela",
"target": "production",
"description": "Deployment request from Vela"
"description": "Deployment request from Vela",
"payload": {},
"created_at": 1,
"created_by": "octocat",
"builds": []
}`

// DeploymentsResp represents a JSON return for one to many builds.
Expand Down
28 changes: 28 additions & 0 deletions mock/server/deployment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: Apache-2.0

package server

import (
"encoding/json"
"reflect"
"testing"

"github.com/go-vela/types/library"
)

func TestDeployment_ActiveDeploymentResp(t *testing.T) {
testDeployment := library.Deployment{}

err := json.Unmarshal([]byte(DeploymentResp), &testDeployment)
if err != nil {
t.Errorf("error unmarshaling deployment: %v", err)
}

tDeployment := reflect.TypeOf(testDeployment)

for i := 0; i < tDeployment.NumField(); i++ {
if reflect.ValueOf(testDeployment).Field(i).IsNil() {
t.Errorf("DeploymentResp missing field %s", tDeployment.Field(i).Name)
}
}
}
2 changes: 2 additions & 0 deletions mock/server/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const (
"created": 1563475419,
"host": "github.com",
"event": "push",
"event_action": "",
"webhook_id": 1234,
"branch": "main",
"error": "",
"status": "success",
Expand Down
28 changes: 28 additions & 0 deletions mock/server/hook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: Apache-2.0

package server

import (
"encoding/json"
"reflect"
"testing"

"github.com/go-vela/types/library"
)

func TestHook_ActiveHookResp(t *testing.T) {
testHook := library.Hook{}

err := json.Unmarshal([]byte(HookResp), &testHook)
if err != nil {
t.Errorf("error unmarshaling hook: %v", err)
}

tHook := reflect.TypeOf(testHook)

for i := 0; i < tHook.NumField(); i++ {
if reflect.ValueOf(testHook).Field(i).IsNil() {
t.Errorf("HookResp missing field %s", tHook.Field(i).Name)
}
}
}
28 changes: 28 additions & 0 deletions mock/server/log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: Apache-2.0

package server

import (
"encoding/json"
"reflect"
"testing"

"github.com/go-vela/types/library"
)

func TestLog_ActiveLogResp(t *testing.T) {
testLog := library.Log{}

err := json.Unmarshal([]byte(LogResp), &testLog)
if err != nil {
t.Errorf("error unmarshaling log: %v", err)
}

tLog := reflect.TypeOf(testLog)

for i := 0; i < tLog.NumField(); i++ {
if reflect.ValueOf(testLog).Field(i).IsNil() {
t.Errorf("LogResp missing field %s", tLog.Field(i).Name)
}
}
}
28 changes: 28 additions & 0 deletions mock/server/pipeline_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: Apache-2.0

package server

import (
"encoding/json"
"reflect"
"testing"

"github.com/go-vela/types/library"
)

func TestPipeline_ActivePipelineResp(t *testing.T) {
testPipeline := library.Pipeline{}

err := json.Unmarshal([]byte(PipelineResp), &testPipeline)
if err != nil {
t.Errorf("error unmarshaling pipeline: %v", err)
}

tPipeline := reflect.TypeOf(testPipeline)

for i := 0; i < tPipeline.NumField(); i++ {
if reflect.ValueOf(testPipeline).Field(i).IsNil() {
t.Errorf("PipelineResp missing field %s", tPipeline.Field(i).Name)
}
}
}
29 changes: 27 additions & 2 deletions mock/server/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
"id": 1,
"user_id": 1,
"org": "github",
"counter": 10,
"name": "octocat",
"full_name": "github/octocat",
"link": "https://github.com/github/octocat",
Expand All @@ -29,11 +30,35 @@ const (
"visibility": "public",
"private": false,
"trusted": true,
"pipeline_type": "yaml",
"topics": [],
"active": true,
"allow_pr": false,
"allow_pull": false,
"allow_push": true,
"allow_deploy": false,
"allow_tag": false
"allow_tag": false,
"allow_comment": false,
"allow_events": {
"push": {
"branch": true,
"tag": true
},
"pull_request": {
"opened": true,
"synchronize": true,
"reopened": true,
"edited": false
},
"deployment": {
"created": true
},
"comment": {
"created": false,
"edited": false
}
},
"approve_build": "fork-always",
"previous_name": ""
}`

// ReposResp represents a JSON return for one to many repos.
Expand Down
31 changes: 31 additions & 0 deletions mock/server/repo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: Apache-2.0

package server

import (
"encoding/json"
"reflect"
"testing"

"github.com/go-vela/types/library"
)

func TestRepo_ActiveRepoResp(t *testing.T) {
testRepo := library.Repo{}

err := json.Unmarshal([]byte(RepoResp), &testRepo)
if err != nil {
t.Errorf("error unmarshaling repo: %v", err)
}

tRepo := reflect.TypeOf(testRepo)

for i := 0; i < tRepo.NumField(); i++ {
if tRepo.Field(i).Name == "Hash" {
continue
}
if reflect.ValueOf(testRepo).Field(i).IsNil() {
t.Errorf("RepoResp missing field %s", tRepo.Field(i).Name)
}
}
}
81 changes: 6 additions & 75 deletions mock/server/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
// ScheduleResp represents a JSON return for a single schedule.
ScheduleResp = `{
"id": 2,
"repo_id": 1,
"active": true,
"name": "foo",
"entry": "@weekly",
Expand All @@ -25,31 +26,7 @@ const (
"updated_at": 1683154980,
"updated_by": "octocat",
"scheduled_at": 0,
"repo": {
"id": 1,
"user_id": 1,
"org": "github",
"name": "octocat",
"full_name": "github/octocat",
"link": "https://github.com/github/octocat",
"clone": "https://github.com/github/octocat.git",
"branch": "main",
"topics": [],
"build_limit": 10,
"timeout": 30,
"counter": 0,
"visibility": "public",
"private": false,
"trusted": false,
"active": true,
"allow_pull": false,
"allow_push": true,
"allow_deploy": false,
"allow_tag": false,
"allow_comment": false,
"pipeline_type": "yaml",
"previous_name": ""
}
"branch": "main"
}`
SchedulesResp = `[
{
Expand All @@ -62,31 +39,8 @@ const (
"updated_at": 1683154980,
"updated_by": "octocat",
"scheduled_at": 0,
"repo": {
"id": 1,
"user_id": 1,
"org": "github",
"name": "octokitty",
"full_name": "github/octokitty",
"link": "https://github.com/github/octokitty",
"clone": "https://github.com/github/octokitty.git",
"branch": "main",
"topics": [],
"build_limit": 10,
"timeout": 30,
"counter": 0,
"visibility": "public",
"private": false,
"trusted": false,
"active": true,
"allow_pull": false,
"allow_push": true,
"allow_deploy": false,
"allow_tag": false,
"allow_comment": false,
"pipeline_type": "yaml",
"previous_name": ""
}
"repo_id": 1,
"branch": "main"
},
{
"id": 1,
Expand All @@ -98,31 +52,8 @@ const (
"updated_at": 1683154974,
"updated_by": "octocat",
"scheduled_at": 0,
"repo": {
"id": 1,
"user_id": 1,
"org": "github",
"name": "octokitty",
"full_name": "github/octokitty",
"link": "https://github.com/github/octokitty",
"clone": "https://github.com/github/octokitty.git",
"branch": "main",
"topics": [],
"build_limit": 10,
"timeout": 30,
"counter": 0,
"visibility": "public",
"private": false,
"trusted": false,
"active": true,
"allow_pull": false,
"allow_push": true,
"allow_deploy": false,
"allow_tag": false,
"allow_comment": false,
"pipeline_type": "yaml",
"previous_name": ""
}
"repo_id": 1,
"branch": "main
}
]`
)
Expand Down
Loading

0 comments on commit b0c6f13

Please sign in to comment.