Skip to content

Commit

Permalink
zopsmart#3 | Add deletion with test on panel-employee, 100% coverage,…
Browse files Browse the repository at this point in the history
… CRUD operation completed
  • Loading branch information
Ishan Pandey authored and Ishan Pandey committed Feb 11, 2021
1 parent 7cef971 commit 4fc0bb4
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 4 deletions.
6 changes: 5 additions & 1 deletion panel/store/coverage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ github.com/zopsmart/hiring-portal-api/panel/store/store.go:101.2,103.14 2 1
github.com/zopsmart/hiring-portal-api/panel/store/store.go:107.2,107.21 1 1
github.com/zopsmart/hiring-portal-api/panel/store/store.go:97.16,99.3 1 1
github.com/zopsmart/hiring-portal-api/panel/store/store.go:103.14,105.3 1 1
github.com/zopsmart/hiring-portal-api/panel/store/store.go:110.105,112.2 1 0
github.com/zopsmart/hiring-portal-api/panel/store/store.go:110.78,114.16 4 1
github.com/zopsmart/hiring-portal-api/panel/store/store.go:118.2,119.12 2 1
github.com/zopsmart/hiring-portal-api/panel/store/store.go:123.2,123.12 1 1
github.com/zopsmart/hiring-portal-api/panel/store/store.go:114.16,117.3 1 1
github.com/zopsmart/hiring-portal-api/panel/store/store.go:119.12,122.3 1 1
2 changes: 1 addition & 1 deletion panel/store/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ type Panel interface {
GetEmployeeByID(*gofr.Context, int, int) (models.Employee, error)
CreateEmployee(*gofr.Context, int, *models.Employee) (int, error)
EditEmployee(*gofr.Context, int, int, *models.Employee) (int, error)
DeleteEmployee(*gofr.Context, int, int) (models.Employee, error)
DeleteEmployee(c *gofr.Context, id, companyId int) error
}
16 changes: 14 additions & 2 deletions panel/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ func (p PanelStore) EditEmployee(c *gofr.Context, companyID, employeeID int, emp
return int(id), nil
}

func (p PanelStore) DeleteEmployee(c *gofr.Context, companyID, employeeID int) (models.Employee, error) {
return models.Employee{}, nil
func (p PanelStore) DeleteEmployee(c *gofr.Context, id, companyId int) error {
var inputInterface []interface{}
inputInterface = append(inputInterface, id, companyId)
rows, err := c.DB.Exec("DELETE FROM employee WHERE id=? AND companyId=?", inputInterface...)
if err != nil {
// Happens in connection loose or if the query is wrong.
return customeerror.ErrDBServer
}
r, _ := rows.RowsAffected()
if r == 0 {
// Some issue while deleting record.
return customeerror.ErrRecordNotFound
}
return err
}
70 changes: 70 additions & 0 deletions panel/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,73 @@ func TestEditEmployee(t *testing.T) {
CheckErrOutput(t, testCases[i].expectedErr, err, testCases[i].expectedOutput, output)
}
}

func TestDeleteEmployeeMock(t *testing.T) {
fdb, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("Technical issue: %s, connecting with the stub database", err)
}
defer fdb.Close()
c := &gofr.Context{
Container: &gofr.Container{
Logger: logging.NewLogger(logging.DEBUG),
DB: &gofr.DB{DB: fdb},
},
}
dataStore := New()

type inputType struct {
id int
companyId int
}
var testCases = []struct {
input inputType
rowsAffected int64
expectedErr error
}{
// Success
{
input: inputType{1, 1},
rowsAffected: 1,
expectedErr: nil,
},
// Failure
{
input: inputType{2, 3},
rowsAffected: 0,
expectedErr: customeerror.ErrRecordNotFound,
},
// DBQuery cover fail: i = 2
{
input: inputType{1, 1},
rowsAffected: 0,
expectedErr: customeerror.ErrDBServer,
},
}

for i, val := range testCases {
if i == 2 {
mock.ExpectExec(regexp.QuoteMeta("DELETE FROM employee WHERE id=? AND companyd=?")).
WithArgs(val.input.id, val.input.companyId).
WillReturnResult(sqlmock.NewResult(0, val.rowsAffected))
actualError := dataStore.DeleteEmployee(c, val.input.id, val.input.companyId)
if !reflect.DeepEqual(actualError, val.expectedErr) {
t.Errorf("Failed for testDeleteEmployeeMock, testcase: %v, errors don't match \n", i)
t.Errorf("FAILED!! Expected value: %v Actual Output: %v\n", val.expectedErr, actualError)
}
} else {
mock.ExpectExec(regexp.QuoteMeta("DELETE FROM employee WHERE id=? AND companyId=?")).
WithArgs(val.input.id, val.input.companyId).
WillReturnResult(sqlmock.NewResult(0, val.rowsAffected))
actualError := dataStore.DeleteEmployee(c, val.input.id, val.input.companyId)
if !reflect.DeepEqual(actualError, val.expectedErr) {
t.Errorf("Failed for testDeleteEmployeeMock, testcase: %v, errors don't match \n", i)
t.Errorf("FAILED!! Expected value: %v Actual Output: %v\n", val.expectedErr, actualError)
}

if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("Expectations unfulfilled: %s", err)
}
}
}
}

0 comments on commit 4fc0bb4

Please sign in to comment.