Skip to content

Commit

Permalink
[ADD] Mock tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stivenramireza committed Mar 15, 2024
1 parent b1dc256 commit 6655b61
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
54 changes: 54 additions & 0 deletions github-tracker/main_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package main

import (
"context"
"encoding/json"
"github-tracker/github-tracker/models"
"github-tracker/github-tracker/repository"
"github-tracker/github-tracker/repository/entity"
"testing"
"time"

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

Expand All @@ -12,4 +19,51 @@ func TestDummy(t *testing.T) {
result := 22

c.Equal(22, result)

}

func TestInsert(t *testing.T) {
c := require.New(t)

webhook := models.GitHubWebhook{
Repository: models.Repository{
FullName: "stivenramireza/secure-software-learning",
},
HeadCommit: models.Commit{
ID: "b1dc2566b219d3941f199b206c057e1ddeaabe43",
Message: "Add sample code for handle-github-webhook",
Author: models.CommitUser{
Email: "stivenramireza@gmail.com",
Username: "stivenramireza",
},
},
}

body, err := json.Marshal(webhook)
c.NoError(err)

createdTime := time.Now()

m := mock.Mock{}
mockCommit := repository.MockCommit{Mock: &m}

commit := entity.Commit{
RepoName: webhook.Repository.FullName,
CommitID: webhook.HeadCommit.ID,
CommitMessage: webhook.HeadCommit.Message,
AuthorUsername: webhook.HeadCommit.Author.Username,
AuthorEmail: webhook.HeadCommit.Author.Email,
Payload: string(body),
CreatedAt: createdTime,
UpdatedAt: createdTime,
}

ctx := context.Background()

mockCommit.On("Insert", ctx, &commit).Return(nil)

err = insertGitHubWebhook(ctx, mockCommit, webhook, string(body), createdTime)
c.NoError(err)

// m.AssertExpectations(t)
}
10 changes: 3 additions & 7 deletions github-tracker/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,12 @@ type Repository struct {
}

type Commit struct {
ID string `json:"id"`
Message string `json:"message"`
Author struct {
Username string `json:"username"`
Email string `json:"email"`
}
ID string `json:"id"`
Message string `json:"message"`
Author CommitUser `json:"author"`
}

type CommitUser struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
}

0 comments on commit 6655b61

Please sign in to comment.