Skip to content

Commit

Permalink
zopsmart#3 | Fixed all linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
raybittu committed Feb 16, 2021
1 parent 10a81bc commit 483e25e
Showing 1 changed file with 26 additions and 30 deletions.
56 changes: 26 additions & 30 deletions panel/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package store

import (
"database/sql"
"errors"
"fmt"
"reflect"
"regexp"
Expand All @@ -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 {
Expand All @@ -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()
Expand Down Expand Up @@ -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"],
Expand All @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 483e25e

Please sign in to comment.