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 | Fixed all linter errors
- Loading branch information
Showing
1 changed file
with
26 additions
and
30 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 |
---|---|---|
|
@@ -2,6 +2,7 @@ package store | |
|
||
import ( | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
"reflect" | ||
"regexp" | ||
|
@@ -25,6 +26,12 @@ func CheckErrOutput(t *testing.T, expectedErr, actualErr, expectedOutput, actual | |
} | ||
} | ||
|
||
func DeleteCheckErr(t *testing.T, expectedErr, actualErr error) { | ||
if !errors.Is(expectedErr, actualErr) { | ||
t.Errorf("FAILED!! Expected Error %v Got Error %v\n", expectedErr, actualErr) | ||
} | ||
} | ||
|
||
func DBMockContextInit() (*sql.DB, sqlmock.Sqlmock, *gofr.Context) { | ||
db, mock, err := sqlmock.New() | ||
if err != nil { | ||
|
@@ -39,6 +46,12 @@ func DBMockContextInit() (*sql.DB, sqlmock.Sqlmock, *gofr.Context) { | |
return db, mock, c | ||
} | ||
|
||
func AddRowForGetAllEmployee(rows *sqlmock.Rows, expectedOutput []models.Employee) { | ||
if len(expectedOutput) > 0 { | ||
rows.AddRow(1, "Bittu", "[email protected]", 7250073079, "sde1", 2, 1) | ||
} | ||
} | ||
|
||
func TestGetAllEmployee(t *testing.T) { | ||
_, mock, c := DBMockContextInit() | ||
dataStore := New() | ||
|
@@ -96,7 +109,9 @@ func TestGetAllEmployee(t *testing.T) { | |
// This condition is to check errors thrown by db.query in store.go, here I have put companyid instead of company_id | ||
if i == 3 { | ||
table := []string{"id", "name", "email", "phone", "designation", "yoe"} | ||
query := "select * from employees where companyid=? and yoe>=? order by id desc limit ?,?" | ||
|
||
const query = "select * from employees where companyid=? and yoe>=? order by id desc limit ?,?" | ||
|
||
rows := sqlmock.NewRows(table) | ||
|
||
mock.ExpectQuery(regexp.QuoteMeta(query)).WithArgs(testCases[i].input, testCases[i].queryParams["yoe"], | ||
|
@@ -105,7 +120,8 @@ func TestGetAllEmployee(t *testing.T) { | |
output, err = dataStore.GetAllEmployee(c, testCases[i].input, testCases[i].queryParams) | ||
} else if i == 1 { // This condition to catch errors of row.scan in store.go, here I have removed column yoe to throw this errors | ||
table := []string{"id", "name", "email", "phone", "designation", "company_id"} | ||
query := "select * from employees where company_id=? and yoe>=? order by id desc limit ?,?" | ||
|
||
const query = "select * from employees where company_id=? and yoe>=? order by id desc limit ?,?" | ||
rows := sqlmock.NewRows(table) | ||
rows.AddRow(2, "Bittu Ray", "[email protected]", 7250073080, "sde2", 2) | ||
|
||
|
@@ -118,11 +134,7 @@ func TestGetAllEmployee(t *testing.T) { | |
query := "select * from employees where company_id=? and yoe>=? order by id desc limit ?,?" | ||
rows := sqlmock.NewRows(table) | ||
|
||
for j := range testCases[i].expectedOutput { | ||
rows.AddRow(testCases[i].expectedOutput[j].ID, testCases[i].expectedOutput[j].Name, | ||
testCases[i].expectedOutput[j].Email, testCases[i].expectedOutput[j].Phone, | ||
testCases[i].expectedOutput[j].Designation, testCases[i].expectedOutput[j].YOE, testCases[i].expectedOutput[j].CompanyID) | ||
} | ||
AddRowForGetAllEmployee(rows, testCases[i].expectedOutput) | ||
|
||
mock.ExpectQuery(regexp.QuoteMeta(query)).WithArgs(testCases[i].input, testCases[i].queryParams["yoe"], | ||
testCases[i].queryParams["page"], testCases[i].queryParams["limit"]).WillReturnRows(rows) | ||
|
@@ -410,18 +422,7 @@ func TestEditEmployee(t *testing.T) { | |
} | ||
|
||
func TestDeleteEmployee(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}, | ||
}, | ||
} | ||
_, mock, c := DBMockContextInit() | ||
dataStore := New() | ||
|
||
type inputType struct { | ||
|
@@ -455,30 +456,25 @@ func TestDeleteEmployee(t *testing.T) { | |
} | ||
|
||
for i, val := range testCases { | ||
var actualError error | ||
|
||
if i == 2 { | ||
mock.ExpectExec(regexp.QuoteMeta("DELETE FROM employees 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) | ||
} | ||
actualError = dataStore.DeleteEmployee(c, val.input.id, val.input.companyID) | ||
} else { | ||
mock.ExpectExec(regexp.QuoteMeta("DELETE FROM employees WHERE id=? AND company_id=?")). | ||
WithArgs(val.input.id, val.input.companyID). | ||
WillReturnResult(sqlmock.NewResult(0, val.rowsAffected)) | ||
actualError := dataStore.DeleteEmployee(c, val.input.companyID, val.input.id) | ||
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) | ||
} | ||
actualError = dataStore.DeleteEmployee(c, val.input.companyID, val.input.id) | ||
|
||
if err := mock.ExpectationsWereMet(); err != nil { | ||
t.Errorf("Expectations unfulfilled: %s", err) | ||
} | ||
} | ||
|
||
DeleteCheckErr(t, testCases[i].expectedErr, actualError) | ||
} | ||
} |