Skip to content

Commit

Permalink
add unit tests, readme file with task details
Browse files Browse the repository at this point in the history
  • Loading branch information
mohitsethiaDH committed Apr 27, 2024
1 parent 7e3dd9b commit b35297c
Show file tree
Hide file tree
Showing 14 changed files with 444 additions and 71 deletions.
16 changes: 10 additions & 6 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@

name: Go

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:

build:
runs-on: ubuntu-latest
env:
"database.name":"link_identity"
"database.host":"root"
"database.username":"root"
"database.pass":"root"
"database.port":"5432"
"server.port":"8000"
steps:
- uses: actions/checkout@v4

Expand All @@ -26,3 +27,6 @@ jobs:

- name: Lint
run: make lint-full

- name: Run unittests
run: go test ./...
File renamed without changes.
2 changes: 1 addition & 1 deletion app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var Values config
func init() {
// Load .env file
if err := godotenv.Load(); err != nil {
log.Fatal(err, "Error loading .env file")
log.Fatalf("Error loading .env file %s", err.Error())
}
if Values.Server.Port = os.Getenv("server.port"); Values.Server.Port == "" {
panic("server port cannot be empty")
Expand Down
File renamed without changes.
106 changes: 106 additions & 0 deletions app/http/link_identity_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package http_test

import (
"bytes"
"context"
"database/sql"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/link-identity/app/domain"
httpHandler "github.com/link-identity/app/http"
mockObject "github.com/link-identity/app/mock"

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

type testStruct struct {
IsCalled bool
Response interface{}
Error error
}

// TestLinkIdentityHandler_Identify ...
func TestLinkIdentityHandler_Identify(t *testing.T) {
tests := []struct {
Name string
RequestPayload *httpHandler.RequestDTO
ExpectedResponse string
ExpectedStatusCode int
Service testStruct
}{
{
Name: "Happy path",
RequestPayload: &httpHandler.RequestDTO{
Email: "test1@gmail.com",
Phone: "+4917611111111",
},
ExpectedResponse: `{
"status_code": 200,
"data": {
"contact": {
"PrimaryContactID": 1,
"emails": [
"test1@gmail.com"
],
"phoneNumbers": [
"+4917611111111"
],
"secondaryContactIds": null
}
}}`,
Service: testStruct{
IsCalled: true,
Response: []*domain.Contact{
{
ContactID: 1,
Email: sql.NullString{String: "test1@gmail.com", Valid: true},
Phone: sql.NullString{String: "+4917611111111", Valid: true},
LinkedPrecedence: "primary",
},
},
Error: nil,
},
ExpectedStatusCode: http.StatusOK,
},
}
for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
ctx := context.Background()

serviceMock := new(mockObject.LinkIdentityServiceMock)
if tt.Service.IsCalled {
if tt.Service.Response == nil {
tt.Service.Response = ([]*domain.Contact)(nil)
}
serviceMock.On("Identify", ctx, mock.Anything, mock.Anything).
Return(tt.Service.Response, tt.Service.Error)
}

handler := httpHandler.NewLinkIdentityHandler(serviceMock)

jsonPayload, _ := json.Marshal(tt.RequestPayload)
if tt.RequestPayload == nil {
jsonPayload, _ = json.Marshal("random")
}
req, err := http.NewRequest("POST", "/identify", bytes.NewBuffer(jsonPayload))
if err != nil {
t.Fatal(err)
}
req.Header.Add("Content-Type", "application/json")
req = req.WithContext(ctx)

rr := httptest.NewRecorder()

AuthorizedCustomerHandler := http.HandlerFunc(handler.Identify)
AuthorizedCustomerHandler.ServeHTTP(rr, req)

var body map[string]interface{}
json.NewDecoder(rr.Body).Decode(&body)
assert.Equal(t, tt.ExpectedStatusCode, rr.Code)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"

"github.com/link-identity/app/domain"
"github.com/link-identity/app/infrastructure/mysql"
"github.com/link-identity/app/infrastructure/sql"

"github.com/pkg/errors"
)
Expand All @@ -21,11 +21,11 @@ type ContactRepository interface {
}

type contactDBRepo struct {
db *mysql.DbConn
db *sql.DbConn
}

// NewContactRepository ...
func NewContactRepository(db *mysql.DbConn) ContactRepository {
func NewContactRepository(db *sql.DbConn) ContactRepository {
return &contactDBRepo{
db: db,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mysql
package sql

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mysql
package sql

import (
"log"
Expand Down
54 changes: 54 additions & 0 deletions app/mock/contact_repository_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package mock

import (
"context"
"github.com/link-identity/app/domain"
"github.com/stretchr/testify/mock"
)

// ContactRepositoryMock ...
type ContactRepositoryMock struct {
mock.Mock
}

// GetContactByEmail ...
func (m *ContactRepositoryMock) GetContactByEmail(ctx context.Context, email string) (*domain.Contact, error) {
args := m.Called(ctx, email)
return args.Get(0).(*domain.Contact), args.Error(1)
}

// GetContactByPhone ...
func (m *ContactRepositoryMock) GetContactByPhone(ctx context.Context, phone string) (*domain.Contact, error) {
args := m.Called(ctx, phone)
return args.Get(0).(*domain.Contact), args.Error(1)
}

// GetAllContacts ...
func (m *ContactRepositoryMock) GetAllContacts(ctx context.Context) ([]*domain.Contact, error) {
args := m.Called(ctx)
return args.Get(0).([]*domain.Contact), args.Error(1)
}

// GetAllSecondaryContacts ...
func (m *ContactRepositoryMock) GetAllSecondaryContacts(ctx context.Context, linkedID uint) ([]*domain.Contact, error) {
args := m.Called(ctx, linkedID)
return args.Get(0).([]*domain.Contact), args.Error(1)
}

// GetPrimaryContactFromLinkedID ...
func (m *ContactRepositoryMock) GetPrimaryContactFromLinkedID(ctx context.Context, linkedID uint) (*domain.Contact, error) {
args := m.Called(ctx, linkedID)
return args.Get(0).(*domain.Contact), args.Error(1)
}

// CreateContact ...
func (m *ContactRepositoryMock) CreateContact(ctx context.Context, contact *domain.Contact) (*domain.Contact, error) {
args := m.Called(ctx, contact)
return args.Get(0).(*domain.Contact), args.Error(1)
}

// UpdateContact ...
func (m *ContactRepositoryMock) UpdateContact(ctx context.Context, contact *domain.Contact) (*domain.Contact, error) {
args := m.Called(ctx, contact)
return args.Get(0).(*domain.Contact), args.Error(1)
}
20 changes: 20 additions & 0 deletions app/mock/link_identity_service_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package mock

import (
"context"

"github.com/link-identity/app/domain"

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

// LinkIdentityServiceMock ...
type LinkIdentityServiceMock struct {
mock.Mock
}

// Identify ...
func (m *LinkIdentityServiceMock) Identify(ctx context.Context, email, phone string) ([]*domain.Contact, error) {
args := m.Called(ctx, email, phone)
return args.Get(0).([]*domain.Contact), args.Error(1)
}
Loading

0 comments on commit b35297c

Please sign in to comment.