-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add unit tests, readme file with task details
- Loading branch information
1 parent
7e3dd9b
commit b35297c
Showing
14 changed files
with
444 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
app/infrastructure/mysql/db_conn.go → app/infrastructure/sql/db_conn.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package mysql | ||
package sql | ||
|
||
import ( | ||
"fmt" | ||
|
2 changes: 1 addition & 1 deletion
2
app/infrastructure/mysql/migrations.go → app/infrastructure/sql/migrations.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package mysql | ||
package sql | ||
|
||
import ( | ||
"log" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.