forked from zopsmart/ezgo
-
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.
zopsmart#3 | service layer for CRUD on EmployeePanel completed with 1…
…00% coverage
- Loading branch information
Showing
3 changed files
with
191 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
mode: set | ||
github.com/zopsmart/hiring-portal-api/panel/service/service.go:14.43,16.2 1 1 | ||
github.com/zopsmart/hiring-portal-api/panel/service/service.go:18.105,19.20 1 1 | ||
github.com/zopsmart/hiring-portal-api/panel/service/service.go:22.2,22.45 1 1 | ||
github.com/zopsmart/hiring-portal-api/panel/service/service.go:19.20,21.3 1 1 | ||
github.com/zopsmart/hiring-portal-api/panel/service/service.go:25.116,26.20 1 1 | ||
github.com/zopsmart/hiring-portal-api/panel/service/service.go:29.2,29.21 1 1 | ||
github.com/zopsmart/hiring-portal-api/panel/service/service.go:32.2,32.58 1 1 | ||
github.com/zopsmart/hiring-portal-api/panel/service/service.go:26.20,28.3 1 1 | ||
github.com/zopsmart/hiring-portal-api/panel/service/service.go:29.21,31.3 1 1 | ||
github.com/zopsmart/hiring-portal-api/panel/service/service.go:35.125,36.20 1 1 | ||
github.com/zopsmart/hiring-portal-api/panel/service/service.go:39.2,40.16 2 1 | ||
github.com/zopsmart/hiring-portal-api/panel/service/service.go:43.2,45.18 3 1 | ||
github.com/zopsmart/hiring-portal-api/panel/service/service.go:36.20,38.3 1 1 | ||
github.com/zopsmart/hiring-portal-api/panel/service/service.go:40.16,42.3 1 1 | ||
github.com/zopsmart/hiring-portal-api/panel/service/service.go:48.135,49.20 1 1 | ||
github.com/zopsmart/hiring-portal-api/panel/service/service.go:52.2,52.21 1 1 | ||
github.com/zopsmart/hiring-portal-api/panel/service/service.go:55.2,56.16 2 1 | ||
github.com/zopsmart/hiring-portal-api/panel/service/service.go:59.2,59.18 1 1 | ||
github.com/zopsmart/hiring-portal-api/panel/service/service.go:49.20,51.3 1 1 | ||
github.com/zopsmart/hiring-portal-api/panel/service/service.go:52.21,54.3 1 1 | ||
github.com/zopsmart/hiring-portal-api/panel/service/service.go:56.16,58.3 1 1 | ||
github.com/zopsmart/hiring-portal-api/panel/service/service.go:62.96,63.20 1 1 | ||
github.com/zopsmart/hiring-portal-api/panel/service/service.go:66.2,66.21 1 1 | ||
github.com/zopsmart/hiring-portal-api/panel/service/service.go:69.2,69.57 1 1 | ||
github.com/zopsmart/hiring-portal-api/panel/service/service.go:63.20,65.3 1 1 | ||
github.com/zopsmart/hiring-portal-api/panel/service/service.go:66.21,68.3 1 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
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 |
---|---|---|
|
@@ -4,86 +4,155 @@ import ( | |
"reflect" | ||
"testing" | ||
|
||
customeerror "github.com/zopsmart/hiring-portal-api/panel/error" | ||
|
||
"github.com/DATA-DOG/go-sqlmock" | ||
"github.com/vikash/gofr/pkg/gofr" | ||
"github.com/vikash/gofr/pkg/gofr/logging" | ||
customeerror "github.com/zopsmart/hiring-portal-api/panel/error" | ||
"github.com/zopsmart/hiring-portal-api/panel/models" | ||
) | ||
|
||
func CheckErrOutput(t *testing.T, expectedErr, actualErr, expectedOutput, actualOutPut interface{}) { | ||
if expectedErr != actualErr { | ||
t.Errorf("FAILED!! Expected Error %v Got Error %v\n", expectedErr, actualErr) | ||
} | ||
|
||
if !reflect.DeepEqual(expectedOutput, actualOutPut) { | ||
t.Errorf("FAILED!! Expected %v Got %v\n", expectedOutput, actualOutPut) | ||
} | ||
} | ||
|
||
func TestServiceLayer(t *testing.T) { | ||
db, _, _ := sqlmock.New() | ||
c := &gofr.Context{Container: &gofr.Container{ | ||
Logger: logging.NewLogger(logging.DEBUG), | ||
DB: &gofr.DB{db}, | ||
}} | ||
|
||
testGetAllEmployee(t, c) | ||
testGetEmployeeByID(t, c) | ||
testCreateEmployeeService(t, c) | ||
testEditEmployeeService(t, c) | ||
testDeleteEmployee(t, c) | ||
testDeleteEmployeeService(t, c) | ||
testAllEmployeeService(t, c) | ||
testEmployeeByIDService(t, c) | ||
} | ||
|
||
type mockStore struct { | ||
} | ||
func testAllEmployeeService(t *testing.T, c *gofr.Context) { | ||
|
||
func CheckErrOutput(t *testing.T, expectedErr, actualErr, expectedOutput, actualOutPut interface{}) { | ||
if expectedErr != actualErr { | ||
t.Errorf("FAILED!! Expected Error %v Got Error %v\n", expectedErr, actualErr) | ||
} | ||
|
||
if !reflect.DeepEqual(expectedOutput, actualOutPut) { | ||
t.Errorf("FAILED!! Expected Value %v Got Value %v\n", expectedOutput, actualOutPut) | ||
} | ||
} | ||
|
||
func testGetAllEmployee(t *testing.T, c *gofr.Context) { | ||
testCases := []struct { | ||
input int | ||
expectedOutput []models.Employee | ||
expectedErr error | ||
}{ | ||
{ | ||
input: 1, | ||
expectedOutput: []models.Employee{ | ||
{1, "Bittu", "[email protected]", 7250073079, "sde1", 2, 1}, | ||
expectedOutput: []models.Employee{{1, "Bittu", "[email protected]", 7250073079, "sde1", 2, 1}, | ||
{2, "Bittu Ray", "[email protected]", 7250073080, "sde2", 2, 1}, | ||
}, | ||
expectedErr: nil, | ||
}, | ||
{ | ||
input: 2, | ||
input: -1, | ||
expectedOutput: []models.Employee(nil), | ||
expectedErr: customeerror.ErrDBServer, | ||
expectedErr: customeerror.ErrInvalidCompanyID, | ||
}, | ||
{ | ||
input: 3, | ||
expectedOutput: []models.Employee(nil), | ||
input: 2, | ||
expectedErr: customeerror.ErrDBServer, | ||
}, | ||
} | ||
|
||
for i := range testCases { | ||
dataService := New(mockStore{}) | ||
output, err := dataService.GetAllEmployee(c, testCases[i].input) | ||
CheckErrOutput(t, testCases[i].expectedErr, err, testCases[i].expectedOutput, output) | ||
} | ||
} | ||
|
||
func testEmployeeByIDService(t *testing.T, c *gofr.Context) { | ||
type inputData struct { | ||
empID, companyID int | ||
} | ||
testCases := []struct { | ||
input inputData | ||
expectedOutput models.Employee | ||
expectedErr error | ||
}{ | ||
{ | ||
input: inputData{1, 1}, | ||
expectedOutput: models.Employee{1, "Bittu", "[email protected]", 7250073079, "sde1", 2, 1}, | ||
expectedErr: nil, | ||
}, | ||
|
||
{ | ||
input: inputData{1, -1}, | ||
expectedOutput: models.Employee{}, | ||
expectedErr: customeerror.ErrInvalidCompanyID, | ||
}, | ||
{ | ||
input: inputData{-2, 2}, | ||
expectedOutput: models.Employee{}, | ||
expectedErr: customeerror.ErrInvalidEmployeeID, | ||
}, | ||
|
||
{ | ||
input: inputData{4, 3}, | ||
expectedOutput: models.Employee{}, | ||
expectedErr: customeerror.ErrRecordNotFound, | ||
}, | ||
} | ||
|
||
for i := range testCases { | ||
dataService := New(mockStore{}) | ||
output, err := dataService.GetEmployeeByID(c, testCases[i].input.companyID, testCases[i].input.empID) | ||
CheckErrOutput(t, testCases[i].expectedErr, err, testCases[i].expectedOutput, output) | ||
} | ||
} | ||
|
||
func testCreateEmployeeService(t *testing.T, c *gofr.Context) { | ||
|
||
type InputData struct { | ||
companyID int | ||
body models.Employee | ||
} | ||
|
||
testCases := []struct { | ||
input InputData | ||
|
||
expectedOutput models.Employee | ||
expectedErr error | ||
}{ | ||
{ | ||
input: 4, | ||
expectedOutput: []models.Employee(nil), | ||
input: InputData{1, models.Employee{1, "Bittu", "[email protected]", 7250073079, "sde1", 2, 1}}, | ||
expectedOutput: models.Employee{1, "Bittu", "[email protected]", 7250073079, "sde1", 2, 1}, | ||
expectedErr: nil, | ||
}, | ||
|
||
{ | ||
input: InputData{-1, models.Employee{1, "Bittu", "[email protected]", 7250073079, "sde1", 2, 1}}, | ||
expectedOutput: models.Employee{}, | ||
expectedErr: customeerror.ErrInvalidCompanyID, | ||
}, | ||
|
||
{ | ||
input: InputData{1, models.Employee{1, "Bittu", "[email protected]", 7250073079, "sde1", 2, 2}}, | ||
expectedOutput: models.Employee{}, | ||
expectedErr: customeerror.ErrDBServer, | ||
}, | ||
} | ||
|
||
for i, v := range testCases { | ||
for i := range testCases { | ||
dataService := New(mockStore{}) | ||
actualOutput, actualError := dataService.GetAllEmployee(c, 1) | ||
|
||
output, err := dataService.CreateEmployee(c, testCases[i].input.companyID, &testCases[i].input.body) | ||
CheckErrOutput(t, testCases[i].expectedErr, err, testCases[i].expectedOutput, output) | ||
} | ||
|
||
} | ||
func testEditEmployeeService(t *testing.T, c *gofr.Context) { | ||
|
||
func testEditEmployeeService(t *testing.T, c *gofr.Context) { | ||
type InputData struct { | ||
companyID int | ||
employeeID int | ||
body models.Employee | ||
} | ||
|
||
testCases := []struct { | ||
input InputData | ||
|
||
|
@@ -123,20 +192,78 @@ func testEditEmployeeService(t *testing.T, c *gofr.Context) { | |
|
||
} | ||
|
||
func testGetEmployeeByID(t *testing.T, c *gofr.Context) { | ||
func testDeleteEmployeeService(t *testing.T, c *gofr.Context) { | ||
|
||
type InputData struct { | ||
companyID int | ||
employeeID int | ||
} | ||
|
||
testCases := []struct { | ||
input InputData | ||
expectedErr error | ||
}{ | ||
{ | ||
input: InputData{1, 1}, | ||
expectedErr: nil, | ||
}, | ||
|
||
{ | ||
input: InputData{-1, 2}, | ||
expectedErr: customeerror.ErrInvalidCompanyID, | ||
}, | ||
{ | ||
input: InputData{1, -1}, | ||
expectedErr: customeerror.ErrInvalidEmployeeID, | ||
}, | ||
|
||
{ | ||
input: InputData{1, 2}, | ||
expectedErr: customeerror.ErrDBServer, | ||
}, | ||
} | ||
|
||
for i := range testCases { | ||
dataService := New(mockStore{}) | ||
err := dataService.DeleteEmployee(c, testCases[i].input.employeeID, testCases[i].input.companyID) | ||
if err != testCases[i].expectedErr { | ||
t.Errorf("FAILED!! Expected Error %v Got Error %v\n", testCases[i].expectedErr, err) | ||
} | ||
} | ||
} | ||
|
||
type mockStore struct { | ||
} | ||
|
||
func (m mockStore) GetAllEmployee(c *gofr.Context, companyID int) ([]models.Employee, error) { | ||
if companyID == 2 { | ||
return nil, customeerror.ErrDBServer | ||
} | ||
|
||
res := []models.Employee{{1, "Bittu", "[email protected]", 7250073079, "sde1", 2, 1}, | ||
{2, "Bittu Ray", "[email protected]", 7250073080, "sde2", 2, 1}, | ||
} | ||
|
||
return res, nil | ||
} | ||
|
||
func (m mockStore) GetEmployeeByID(c *gofr.Context, companyID, employeeID int) (models.Employee, error) { | ||
if companyID == employeeID { | ||
return models.Employee{1, "Bittu", "[email protected]", 7250073079, "sde1", 2, 1}, nil | ||
} | ||
if companyID != employeeID { | ||
return models.Employee{}, customeerror.ErrRecordNotFound | ||
} | ||
|
||
return models.Employee{}, nil | ||
|
||
} | ||
|
||
func (m mockStore) CreateEmployee(c *gofr.Context, companyID int, emp *models.Employee) (int, error) { | ||
return 0, nil | ||
if companyID != emp.CompanyID { | ||
return -1, customeerror.ErrDBServer | ||
} | ||
return companyID, nil | ||
} | ||
|
||
func (m mockStore) EditEmployee(c *gofr.Context, companyID, employeeID int, emp *models.Employee) (int, error) { | ||
|
@@ -147,5 +274,8 @@ func (m mockStore) EditEmployee(c *gofr.Context, companyID, employeeID int, emp | |
} | ||
|
||
func (m mockStore) DeleteEmployee(c *gofr.Context, companyID, employeeID int) error { | ||
if companyID != employeeID { | ||
return customeerror.ErrDBServer | ||
} | ||
return nil | ||
} |