diff --git a/.DS_Store b/.DS_Store index 60d43a90..cb98ecdc 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/panel.zip b/panel.zip new file mode 100644 index 00000000..86a1d48a Binary files /dev/null and b/panel.zip differ diff --git a/panel/http/http.go b/panel/http/http.go index 615ba05e..1d9136c5 100644 --- a/panel/http/http.go +++ b/panel/http/http.go @@ -46,7 +46,27 @@ func (p PanelHTTP) GetAllEmployee(c *gofr.Context) (interface{}, error) { return "Invalid CompanyID", customeerror.ErrInvalidCompanyID } - res, err := p.service.GetAllEmployee(c, companyID) + yoe, _ := strconv.Atoi(c.Param("yoe")) + designation := c.Param("designation") + page, err := strconv.Atoi(c.Param("page")) + if err != nil { + page = 1 + } + limit, err := strconv.Atoi(c.Param("limit")) + if err != nil { + limit = 20 + } + skills := []string{} + + queryParams := map[string]interface{}{ + "yoe": yoe, + "designation": designation, + "page": (page - 1) * limit, + "limit": limit, + "skills": skills, + } + + res, err := p.service.GetAllEmployee(c, companyID, queryParams) if err != nil { return nil, err } diff --git a/panel/service/coverage.txt b/panel/service/coverage.txt index f40eed11..98d8f90f 100644 --- a/panel/service/coverage.txt +++ b/panel/service/coverage.txt @@ -1,27 +1,29 @@ 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:18.140,19.20 1 1 +github.com/zopsmart/hiring-portal-api/panel/service/service.go:22.2,22.37 1 1 +github.com/zopsmart/hiring-portal-api/panel/service/service.go:25.2,25.72 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,61.18 3 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:64.96,65.20 1 1 -github.com/zopsmart/hiring-portal-api/panel/service/service.go:68.2,68.21 1 1 -github.com/zopsmart/hiring-portal-api/panel/service/service.go:71.2,71.57 1 1 -github.com/zopsmart/hiring-portal-api/panel/service/service.go:65.20,67.3 1 1 -github.com/zopsmart/hiring-portal-api/panel/service/service.go:68.21,70.3 1 1 +github.com/zopsmart/hiring-portal-api/panel/service/service.go:22.37,24.3 1 1 +github.com/zopsmart/hiring-portal-api/panel/service/service.go:28.116,29.20 1 1 +github.com/zopsmart/hiring-portal-api/panel/service/service.go:32.2,32.21 1 1 +github.com/zopsmart/hiring-portal-api/panel/service/service.go:35.2,35.58 1 1 +github.com/zopsmart/hiring-portal-api/panel/service/service.go:29.20,31.3 1 1 +github.com/zopsmart/hiring-portal-api/panel/service/service.go:32.21,34.3 1 1 +github.com/zopsmart/hiring-portal-api/panel/service/service.go:38.125,39.20 1 1 +github.com/zopsmart/hiring-portal-api/panel/service/service.go:42.2,43.16 2 1 +github.com/zopsmart/hiring-portal-api/panel/service/service.go:46.2,48.18 3 1 +github.com/zopsmart/hiring-portal-api/panel/service/service.go:39.20,41.3 1 1 +github.com/zopsmart/hiring-portal-api/panel/service/service.go:43.16,45.3 1 1 +github.com/zopsmart/hiring-portal-api/panel/service/service.go:51.135,52.20 1 1 +github.com/zopsmart/hiring-portal-api/panel/service/service.go:55.2,55.21 1 1 +github.com/zopsmart/hiring-portal-api/panel/service/service.go:58.2,59.16 2 1 +github.com/zopsmart/hiring-portal-api/panel/service/service.go:62.2,64.18 3 1 +github.com/zopsmart/hiring-portal-api/panel/service/service.go:52.20,54.3 1 1 +github.com/zopsmart/hiring-portal-api/panel/service/service.go:55.21,57.3 1 1 +github.com/zopsmart/hiring-portal-api/panel/service/service.go:59.16,61.3 1 1 +github.com/zopsmart/hiring-portal-api/panel/service/service.go:67.96,68.20 1 1 +github.com/zopsmart/hiring-portal-api/panel/service/service.go:71.2,71.21 1 1 +github.com/zopsmart/hiring-portal-api/panel/service/service.go:74.2,74.57 1 1 +github.com/zopsmart/hiring-portal-api/panel/service/service.go:68.20,70.3 1 1 +github.com/zopsmart/hiring-portal-api/panel/service/service.go:71.21,73.3 1 1 diff --git a/panel/service/interface.go b/panel/service/interface.go index 191cfc33..a9c88e09 100644 --- a/panel/service/interface.go +++ b/panel/service/interface.go @@ -6,7 +6,7 @@ import ( ) type PanelEmployee interface { - GetAllEmployee(*gofr.Context, int) ([]models.Employee, error) + GetAllEmployee(*gofr.Context, int, map[string]interface{}) ([]models.Employee, error) GetEmployeeByID(*gofr.Context, int, int) (models.Employee, error) CreateEmployee(*gofr.Context, int, *models.Employee) (models.Employee, error) EditEmployee(*gofr.Context, int, int, *models.Employee) (models.Employee, error) diff --git a/panel/service/service.go b/panel/service/service.go index ca8d230c..9c78059b 100644 --- a/panel/service/service.go +++ b/panel/service/service.go @@ -15,11 +15,14 @@ func New(store store.Panel) PanelEmployee { return PanelEmployeeService{store: store} } -func (p PanelEmployeeService) GetAllEmployee(c *gofr.Context, companyID int) ([]models.Employee, error) { +func (p PanelEmployeeService) GetAllEmployee(c *gofr.Context, companyID int, queryParam map[string]interface{}) ([]models.Employee, error) { if companyID <= 0 { return nil, customeerror.ErrInvalidCompanyID } - return p.store.GetAllEmployee(c, companyID) + if queryParam["designation"] == "" { + return p.store.GetAllEmployeeWithoutDesignation(c, companyID, queryParam) + } + return p.store.GetAllEmployeeWithDesignation(c, companyID, queryParam) } func (p PanelEmployeeService) GetEmployeeByID(c *gofr.Context, companyID, employeeID int) (models.Employee, error) { diff --git a/panel/service/service_test.go b/panel/service/service_test.go index 00a4f8be..d9e0c0d1 100644 --- a/panel/service/service_test.go +++ b/panel/service/service_test.go @@ -41,6 +41,7 @@ func testAllEmployeeService(t *testing.T, c *gofr.Context) { input int expectedOutput []models.Employee expectedErr error + queryParams map[string]interface{} }{ { input: 1, @@ -48,21 +49,53 @@ func testAllEmployeeService(t *testing.T, c *gofr.Context) { {2, "Bittu Ray", "bitturay@gmail.com", 7250073080, "sde2", 2, 1}, }, expectedErr: nil, + queryParams: map[string]interface{}{ + "yoe": 1, + "designation": "sde", + "page": 0, + "limit": 10, + "skills": []string{}, + }, }, { input: -1, expectedOutput: []models.Employee(nil), expectedErr: customeerror.ErrInvalidCompanyID, + queryParams: map[string]interface{}{ + "yoe": 1, + "designation": "sde", + "page": 0, + "limit": 10, + "skills": []string{}, + }, + }, + { + input: 2, + expectedErr: customeerror.ErrDBServer, + queryParams: map[string]interface{}{ + "yoe": 1, + "designation": "sde", + "page": 0, + "limit": 10, + "skills": []string{}, + }, }, { input: 2, expectedErr: customeerror.ErrDBServer, + queryParams: map[string]interface{}{ + "yoe": 1, + "designation": "", + "page": 0, + "limit": 10, + "skills": []string{}, + }, }, } for i := range testCases { dataService := New(mockStore{}) - output, err := dataService.GetAllEmployee(c, testCases[i].input) + output, err := dataService.GetAllEmployee(c, testCases[i].input, testCases[i].queryParams) CheckErrOutput(t, testCases[i].expectedErr, err, testCases[i].expectedOutput, output) } } @@ -235,7 +268,19 @@ func testDeleteEmployeeService(t *testing.T, c *gofr.Context) { type mockStore struct { } -func (m mockStore) GetAllEmployee(c *gofr.Context, companyID int) ([]models.Employee, error) { +func (m mockStore) GetAllEmployeeWithDesignation(c *gofr.Context, companyID int, queryParams map[string]interface{}) ([]models.Employee, error) { + if companyID == 2 { + return nil, customeerror.ErrDBServer + } + + res := []models.Employee{{1, "Bittu", "bittu@gmail.com", 7250073079, "sde1", 2, 1}, + {2, "Bittu Ray", "bitturay@gmail.com", 7250073080, "sde2", 2, 1}, + } + + return res, nil +} + +func (m mockStore) GetAllEmployeeWithoutDesignation(c *gofr.Context, companyID int, queryParams map[string]interface{}) ([]models.Employee, error) { if companyID == 2 { return nil, customeerror.ErrDBServer } diff --git a/panel/store/coverage.txt b/panel/store/coverage.txt index fc22581a..fc4fed50 100644 --- a/panel/store/coverage.txt +++ b/panel/store/coverage.txt @@ -1,6 +1,6 @@ mode: set github.com/zopsmart/hiring-portal-api/panel/store/store.go:11.18,13.2 1 1 -github.com/zopsmart/hiring-portal-api/panel/store/store.go:15.95,20.16 5 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:15.146,20.16 5 1 github.com/zopsmart/hiring-portal-api/panel/store/store.go:23.2,25.18 3 1 github.com/zopsmart/hiring-portal-api/panel/store/store.go:32.2,32.19 1 1 github.com/zopsmart/hiring-portal-api/panel/store/store.go:35.2,35.17 1 1 @@ -9,26 +9,35 @@ github.com/zopsmart/hiring-portal-api/panel/store/store.go:25.18,27.109 2 1 github.com/zopsmart/hiring-portal-api/panel/store/store.go:30.3,30.23 1 1 github.com/zopsmart/hiring-portal-api/panel/store/store.go:27.109,29.4 1 1 github.com/zopsmart/hiring-portal-api/panel/store/store.go:32.19,34.3 1 1 -github.com/zopsmart/hiring-portal-api/panel/store/store.go:38.106,43.16 5 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:38.149,43.16 5 1 github.com/zopsmart/hiring-portal-api/panel/store/store.go:46.2,48.18 3 1 -github.com/zopsmart/hiring-portal-api/panel/store/store.go:53.2,53.17 1 1 -github.com/zopsmart/hiring-portal-api/panel/store/store.go:57.2,57.17 1 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:55.2,55.19 1 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:58.2,58.17 1 1 github.com/zopsmart/hiring-portal-api/panel/store/store.go:43.16,45.3 1 1 -github.com/zopsmart/hiring-portal-api/panel/store/store.go:48.18,49.123 1 1 -github.com/zopsmart/hiring-portal-api/panel/store/store.go:49.123,51.4 1 1 -github.com/zopsmart/hiring-portal-api/panel/store/store.go:53.17,56.3 1 1 -github.com/zopsmart/hiring-portal-api/panel/store/store.go:60.103,65.16 5 1 -github.com/zopsmart/hiring-portal-api/panel/store/store.go:68.2,69.14 2 1 -github.com/zopsmart/hiring-portal-api/panel/store/store.go:72.2,72.21 1 1 -github.com/zopsmart/hiring-portal-api/panel/store/store.go:65.16,67.3 1 1 -github.com/zopsmart/hiring-portal-api/panel/store/store.go:69.14,71.3 1 1 -github.com/zopsmart/hiring-portal-api/panel/store/store.go:75.113,80.16 5 1 -github.com/zopsmart/hiring-portal-api/panel/store/store.go:83.2,84.13 2 1 -github.com/zopsmart/hiring-portal-api/panel/store/store.go:87.2,87.21 1 1 -github.com/zopsmart/hiring-portal-api/panel/store/store.go:80.16,82.3 1 1 -github.com/zopsmart/hiring-portal-api/panel/store/store.go:84.13,86.3 1 1 -github.com/zopsmart/hiring-portal-api/panel/store/store.go:90.86,94.16 4 1 -github.com/zopsmart/hiring-portal-api/panel/store/store.go:98.2,99.12 2 1 -github.com/zopsmart/hiring-portal-api/panel/store/store.go:103.2,103.12 1 1 -github.com/zopsmart/hiring-portal-api/panel/store/store.go:94.16,97.3 1 1 -github.com/zopsmart/hiring-portal-api/panel/store/store.go:99.12,102.3 1 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:48.18,50.109 2 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:53.3,53.23 1 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:50.109,52.4 1 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:55.19,57.3 1 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:61.106,66.16 5 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:69.2,71.18 3 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:76.2,76.17 1 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:80.2,80.17 1 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:66.16,68.3 1 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:71.18,72.123 1 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:72.123,74.4 1 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:76.17,79.3 1 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:83.103,88.16 5 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:91.2,92.14 2 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:95.2,95.21 1 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:88.16,90.3 1 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:92.14,94.3 1 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:98.113,103.16 5 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:106.2,107.13 2 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:110.2,110.21 1 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:103.16,105.3 1 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:107.13,109.3 1 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:113.86,117.16 4 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:121.2,122.12 2 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:126.2,126.12 1 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:117.16,120.3 1 1 +github.com/zopsmart/hiring-portal-api/panel/store/store.go:122.12,125.3 1 1 diff --git a/panel/store/interface.go b/panel/store/interface.go index 8e2ba0be..124d7404 100644 --- a/panel/store/interface.go +++ b/panel/store/interface.go @@ -6,7 +6,8 @@ import ( ) type Panel interface { - GetAllEmployee(*gofr.Context, int) ([]models.Employee, error) + GetAllEmployeeWithDesignation(*gofr.Context, int, map[string]interface{}) ([]models.Employee, error) + GetAllEmployeeWithoutDesignation(*gofr.Context, int, map[string]interface{}) ([]models.Employee, error) 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) diff --git a/panel/store/store.go b/panel/store/store.go index 9a87672c..45a1fa5b 100644 --- a/panel/store/store.go +++ b/panel/store/store.go @@ -12,10 +12,33 @@ func New() Panel { return PanelStore{} } -func (p PanelStore) GetAllEmployee(c *gofr.Context, companyID int) ([]models.Employee, error) { - const query = "select * from employee where company_id=? order by id, company_id asc" +func (p PanelStore) GetAllEmployeeWithDesignation(c *gofr.Context, companyID int, queryParams map[string]interface{}) ([]models.Employee, error) { + const query = "select * from employee where company_id=? and designation=? and yoe>=? order by id desc limit ?,?" var inputInterface []interface{} - inputInterface = append(inputInterface, companyID) + inputInterface = append(inputInterface, companyID, queryParams["designation"], queryParams["yoe"], queryParams["page"], queryParams["limit"]) + rows, err := c.DB.Query(query, inputInterface...) + if err != nil { + return nil, customeerror.ErrDBServer + } + defer rows.Close() + var res []models.Employee + for rows.Next() { + var e models.Employee + if err := rows.Scan(&e.ID, &e.Name, &e.Email, &e.Phone, &e.Designation, &e.YOE, &e.CompanyID); err != nil { + return nil, customeerror.ErrDBServer + } + res = append(res, e) + } + if len(res) == 0 { + return res, customeerror.ErrRecordNotFound + } + return res, nil +} + +func (p PanelStore) GetAllEmployeeWithoutDesignation(c *gofr.Context, companyID int, queryParams map[string]interface{}) ([]models.Employee, error) { + const query = "select * from employee where company_id=? and yoe>=? order by id desc limit ?,?" + var inputInterface []interface{} + inputInterface = append(inputInterface, companyID, queryParams["yoe"], queryParams["page"], queryParams["limit"]) rows, err := c.DB.Query(query, inputInterface...) if err != nil { return nil, customeerror.ErrDBServer diff --git a/panel/store/store_test.go b/panel/store/store_test.go index f33c8649..6a66efcb 100644 --- a/panel/store/store_test.go +++ b/panel/store/store_test.go @@ -26,7 +26,7 @@ func CheckErrOutput(t *testing.T, expectedErr, actualErr, expectedOutput, actual } } -func TestGetAllEmployeeMock(t *testing.T) { +func TestGetAllEmployeeWithDesignationMock(t *testing.T) { db, mock, err := sqlmock.New() if err != nil { fmt.Println(err) @@ -41,6 +41,7 @@ func TestGetAllEmployeeMock(t *testing.T) { input int expectedOutput []models.Employee expectedErr error + queryParams map[string]interface{} }{ { input: 1, @@ -49,23 +50,170 @@ func TestGetAllEmployeeMock(t *testing.T) { {2, "Bittu Ray", "bitturay@gmail.com", 7250073080, "sde2", 2, 1}, }, expectedErr: nil, + queryParams: map[string]interface{}{ + "yoe": 1, + "designation": "sde", + "page": 0, + "limit": 10, + "skills": []string{}, + }, + }, + + { + input: 2, + expectedOutput: []models.Employee(nil), + expectedErr: customeerror.ErrDBServer, + queryParams: map[string]interface{}{ + "yoe": 1, + "designation": "sde", + "page": 0, + "limit": 10, + "skills": []string{}, + }, + }, + { + input: 3, + expectedOutput: []models.Employee(nil), + expectedErr: customeerror.ErrRecordNotFound, + queryParams: map[string]interface{}{ + "yoe": 1, + "designation": "sde", + "page": 0, + "limit": 10, + "skills": []string{}, + }, + }, + + { + input: 4, + expectedOutput: []models.Employee(nil), + expectedErr: customeerror.ErrDBServer, + queryParams: map[string]interface{}{ + "yoe": 1, + "designation": "sde", + "page": 0, + "limit": 10, + "skills": []string{}, + }, + }, + } + + for i := range testCases { + var output, err interface{} + + if i == 3 { + table := []string{"id", "name", "email", "phone", "designation", "yoe"} + query := "select * from employee where companyid=? and designation=? 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) + } + + mock.ExpectQuery(regexp.QuoteMeta(query)).WithArgs(testCases[i].input, testCases[i].queryParams["designation"], testCases[i].queryParams["yoe"], testCases[i].queryParams["page"], testCases[i].queryParams["limit"]).WillReturnRows(rows) + output, err = dataStore.GetAllEmployeeWithDesignation(c, testCases[i].input, testCases[i].queryParams) + } else if i == 1 { + table := []string{"id", "name", "email", "phone", "designation", "company_id"} + query := "select * from employee where company_id=? and designation=? and yoe>=? order by id desc limit ?,?" + rows := sqlmock.NewRows(table) + + rows.AddRow(2, "Bittu Ray", "bitturay@gmail.com", 7250073080, "sde2", 2) + + mock.ExpectQuery(regexp.QuoteMeta(query)).WithArgs(testCases[i].input, testCases[i].queryParams["designation"], testCases[i].queryParams["yoe"], testCases[i].queryParams["page"], testCases[i].queryParams["limit"]).WillReturnRows(rows) + output, err = dataStore.GetAllEmployeeWithDesignation(c, testCases[i].input, testCases[i].queryParams) + } else { + table := []string{"id", "name", "email", "phone", "designation", "yoe", "company_id"} + query := "select * from employee where company_id=? and designation=? 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) + } + + mock.ExpectQuery(regexp.QuoteMeta(query)).WithArgs(testCases[i].input, testCases[i].queryParams["designation"], testCases[i].queryParams["yoe"], testCases[i].queryParams["page"], testCases[i].queryParams["limit"]).WillReturnRows(rows) + output, err = dataStore.GetAllEmployeeWithDesignation(c, testCases[i].input, testCases[i].queryParams) + + if err := mock.ExpectationsWereMet(); err != nil { + t.Errorf("there were unfulfilled expectations: %s", err) + } + } + CheckErrOutput(t, testCases[i].expectedErr, err, testCases[i].expectedOutput, output) + } +} + +func TestGetAllEmployeeWithoutDesignationMock(t *testing.T) { + db, mock, err := sqlmock.New() + if err != nil { + fmt.Println(err) + } + c := &gofr.Context{Container: &gofr.Container{ + Logger: logging.NewLogger(logging.DEBUG), + DB: &gofr.DB{db}, + }} + + dataStore := New() + testCases := []struct { + input int + expectedOutput []models.Employee + expectedErr error + queryParams map[string]interface{} + }{ + { + input: 1, + expectedOutput: []models.Employee{ + {1, "Bittu", "bittu@gmail.com", 7250073079, "sde1", 2, 1}, + {2, "Bittu Ray", "bitturay@gmail.com", 7250073080, "sde2", 2, 1}, + }, + expectedErr: nil, + queryParams: map[string]interface{}{ + "yoe": 1, + "designation": "sde", + "page": 0, + "limit": 10, + "skills": []string{}, + }, }, { input: 2, expectedOutput: []models.Employee(nil), expectedErr: customeerror.ErrDBServer, + queryParams: map[string]interface{}{ + "yoe": 1, + "designation": "sde", + "page": 0, + "limit": 10, + "skills": []string{}, + }, }, { input: 3, expectedOutput: []models.Employee(nil), expectedErr: customeerror.ErrRecordNotFound, + queryParams: map[string]interface{}{ + "yoe": 1, + "designation": "sde", + "page": 0, + "limit": 10, + "skills": []string{}, + }, }, { input: 4, expectedOutput: []models.Employee(nil), expectedErr: customeerror.ErrDBServer, + queryParams: map[string]interface{}{ + "yoe": 1, + "designation": "sde", + "page": 0, + "limit": 10, + "skills": []string{}, + }, }, } @@ -74,7 +222,7 @@ func TestGetAllEmployeeMock(t *testing.T) { if i == 3 { table := []string{"id", "name", "email", "phone", "designation", "yoe"} - query := "select * from employee where companyd=? order by id, company_id asc" + query := "select * from employee where companyid=? 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, @@ -83,20 +231,20 @@ func TestGetAllEmployeeMock(t *testing.T) { testCases[i].expectedOutput[j].CompanyID) } - mock.ExpectQuery(regexp.QuoteMeta(query)).WithArgs(testCases[i].input).WillReturnRows(rows) - output, err = dataStore.GetAllEmployee(c, testCases[i].input) + mock.ExpectQuery(regexp.QuoteMeta(query)).WithArgs(testCases[i].input, testCases[i].queryParams["yoe"], testCases[i].queryParams["page"], testCases[i].queryParams["limit"]).WillReturnRows(rows) + output, err = dataStore.GetAllEmployeeWithoutDesignation(c, testCases[i].input, testCases[i].queryParams) } else if i == 1 { table := []string{"id", "name", "email", "phone", "designation", "company_id"} - query := "select * from employee where company_id=? order by id, company_id asc" + query := "select * from employee where company_id=? and yoe>=? order by id desc limit ?,?" rows := sqlmock.NewRows(table) rows.AddRow(2, "Bittu Ray", "bitturay@gmail.com", 7250073080, "sde2", 2) - mock.ExpectQuery(regexp.QuoteMeta(query)).WithArgs(testCases[i].input).WillReturnRows(rows) - output, err = dataStore.GetAllEmployee(c, testCases[i].input) + mock.ExpectQuery(regexp.QuoteMeta(query)).WithArgs(testCases[i].input, testCases[i].queryParams["yoe"], testCases[i].queryParams["page"], testCases[i].queryParams["limit"]).WillReturnRows(rows) + output, err = dataStore.GetAllEmployeeWithoutDesignation(c, testCases[i].input, testCases[i].queryParams) } else { table := []string{"id", "name", "email", "phone", "designation", "yoe", "company_id"} - query := "select * from employee where company_id=? order by id, company_id asc" + query := "select * from employee 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, @@ -105,8 +253,8 @@ func TestGetAllEmployeeMock(t *testing.T) { testCases[i].expectedOutput[j].CompanyID) } - mock.ExpectQuery(regexp.QuoteMeta(query)).WithArgs(testCases[i].input).WillReturnRows(rows) - output, err = dataStore.GetAllEmployee(c, testCases[i].input) + mock.ExpectQuery(regexp.QuoteMeta(query)).WithArgs(testCases[i].input, testCases[i].queryParams["yoe"], testCases[i].queryParams["page"], testCases[i].queryParams["limit"]).WillReturnRows(rows) + output, err = dataStore.GetAllEmployeeWithoutDesignation(c, testCases[i].input, testCases[i].queryParams) if err := mock.ExpectationsWereMet(); err != nil { t.Errorf("there were unfulfilled expectations: %s", err)