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.
- Loading branch information
Showing
2 changed files
with
143 additions
and
22 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 |
---|---|---|
@@ -1,9 +1,151 @@ | ||
package service | ||
|
||
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" | ||
"github.com/zopsmart/hiring-portal-api/panel/models" | ||
) | ||
|
||
func TestCreateEmployeeService(t *testing.T) { | ||
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) | ||
testEditEmployeeService(t, c) | ||
testDeleteEmployee(t, c) | ||
} | ||
|
||
type mockStore struct { | ||
} | ||
|
||
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}, | ||
{2, "Bittu Ray", "[email protected]", 7250073080, "sde2", 2, 1}, | ||
}, | ||
expectedErr: nil, | ||
}, | ||
{ | ||
input: 2, | ||
expectedOutput: []models.Employee(nil), | ||
expectedErr: customeerror.ErrDBServer, | ||
}, | ||
{ | ||
input: 3, | ||
expectedOutput: []models.Employee(nil), | ||
expectedErr: customeerror.ErrRecordNotFound, | ||
}, | ||
{ | ||
input: 4, | ||
expectedOutput: []models.Employee(nil), | ||
expectedErr: customeerror.ErrDBServer, | ||
}, | ||
} | ||
|
||
for i, v := range testCases { | ||
dataService := New(mockStore{}) | ||
actualOutput, actualError := dataService.GetAllEmployee(c, 1) | ||
|
||
} | ||
|
||
} | ||
func testEditEmployeeService(t *testing.T, c *gofr.Context) { | ||
|
||
type InputData struct { | ||
companyID int | ||
employeeID int | ||
body models.Employee | ||
} | ||
|
||
testCases := []struct { | ||
input InputData | ||
|
||
expectedOutput models.Employee | ||
expectedErr error | ||
}{ | ||
{ | ||
input: InputData{1, 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, 2, models.Employee{1, "Bittu", "[email protected]", 7250073079, "sde1", 2, 1}}, | ||
expectedOutput: models.Employee{}, | ||
expectedErr: customeerror.ErrInvalidCompanyID, | ||
}, | ||
{ | ||
input: InputData{1, -1, models.Employee{1, "Bittu", "[email protected]", 7250073079, "sde1", 2, 2}}, | ||
expectedOutput: models.Employee{}, | ||
expectedErr: customeerror.ErrInvalidEmployeeID, | ||
}, | ||
|
||
{ | ||
input: InputData{1, 2, models.Employee{1, "Bittu", "[email protected]", 7250073079, "sde1", 2, 2}}, | ||
expectedOutput: models.Employee{}, | ||
expectedErr: customeerror.ErrDBServer, | ||
}, | ||
} | ||
|
||
for i := range testCases { | ||
dataService := New(mockStore{}) | ||
output, err := dataService.EditEmployee(c, testCases[i].input.companyID, testCases[i].input.employeeID, | ||
&testCases[i].input.body) | ||
CheckErrOutput(t, testCases[i].expectedErr, err, testCases[i].expectedOutput, output) | ||
} | ||
|
||
} | ||
|
||
func testGetEmployeeByID(t *testing.T, c *gofr.Context) { | ||
|
||
} | ||
|
||
func (m mockStore) GetAllEmployee(c *gofr.Context, companyID int) ([]models.Employee, error) { | ||
|
||
} | ||
|
||
func (m mockStore) GetEmployeeByID(c *gofr.Context, companyID, employeeID int) (models.Employee, error) { | ||
return models.Employee{}, nil | ||
} | ||
|
||
func (m mockStore) CreateEmployee(c *gofr.Context, companyID int, emp *models.Employee) (int, error) { | ||
return 0, nil | ||
} | ||
|
||
func (m mockStore) EditEmployee(c *gofr.Context, companyID, employeeID int, emp *models.Employee) (int, error) { | ||
if companyID != emp.CompanyID || employeeID != emp.ID { | ||
return -1, customeerror.ErrDBServer | ||
} | ||
return companyID, nil | ||
} | ||
|
||
func (m mockStore) DeleteEmployee(c *gofr.Context, companyID, employeeID int) error { | ||
return nil | ||
} |
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