test(backend): add UpdateCompanyStatus tests for admin service
- Added 3 test cases: updates active status, updates verified status, and error when company not found - All tests passing
This commit is contained in:
parent
39d1eff80f
commit
151d1f4347
1 changed files with 99 additions and 0 deletions
|
|
@ -181,3 +181,102 @@ func TestAdminService_GetUser(t *testing.T) {
|
|||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestAdminService_UpdateCompanyStatus(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create mock db: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
service := NewAdminService(db)
|
||||
ctx := context.Background()
|
||||
|
||||
companyColumns := []string{
|
||||
"id", "name", "slug", "type", "document", "address", "region_id", "city_id",
|
||||
"phone", "email", "website", "logo_url", "description", "active", "verified",
|
||||
"created_at", "updated_at",
|
||||
}
|
||||
|
||||
t.Run("updates active status successfully", func(t *testing.T) {
|
||||
companyID := "019b5290-9680-7c06-9ee3-c9e0e117251c"
|
||||
now := time.Now()
|
||||
active := false
|
||||
|
||||
// First: getCompanyByID query
|
||||
mock.ExpectQuery("SELECT id, name, slug").
|
||||
WithArgs(companyID).
|
||||
WillReturnRows(sqlmock.NewRows(companyColumns).
|
||||
AddRow(companyID, "Test Company", "test-company", "employer", nil, nil, nil, nil,
|
||||
nil, "test@company.com", nil, nil, nil, true, false, now, now))
|
||||
|
||||
// Then: UPDATE query
|
||||
mock.ExpectExec("UPDATE companies").
|
||||
WithArgs(false, false, sqlmock.AnyArg(), companyID).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
company, err := service.UpdateCompanyStatus(ctx, companyID, &active, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
if company == nil {
|
||||
t.Fatal("Expected company, got nil")
|
||||
}
|
||||
if company.Active != false {
|
||||
t.Errorf("Expected active=false, got %v", company.Active)
|
||||
}
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("Unmet expectations: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("updates verified status successfully", func(t *testing.T) {
|
||||
companyID := "019b5290-9680-7c06-9ee3-c9e0e117251d"
|
||||
now := time.Now()
|
||||
verified := true
|
||||
|
||||
mock.ExpectQuery("SELECT id, name, slug").
|
||||
WithArgs(companyID).
|
||||
WillReturnRows(sqlmock.NewRows(companyColumns).
|
||||
AddRow(companyID, "Pending Company", "pending-company", "employer", nil, nil, nil, nil,
|
||||
nil, "pending@company.com", nil, nil, nil, true, false, now, now))
|
||||
|
||||
mock.ExpectExec("UPDATE companies").
|
||||
WithArgs(true, true, sqlmock.AnyArg(), companyID).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
company, err := service.UpdateCompanyStatus(ctx, companyID, nil, &verified)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
if company == nil {
|
||||
t.Fatal("Expected company, got nil")
|
||||
}
|
||||
if company.Verified != true {
|
||||
t.Errorf("Expected verified=true, got %v", company.Verified)
|
||||
}
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("Unmet expectations: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("returns error when company not found", func(t *testing.T) {
|
||||
companyID := "non-existent-id"
|
||||
active := false
|
||||
|
||||
mock.ExpectQuery("SELECT id, name, slug").
|
||||
WithArgs(companyID).
|
||||
WillReturnError(err)
|
||||
|
||||
_, updateErr := service.UpdateCompanyStatus(ctx, companyID, &active, nil)
|
||||
if updateErr == nil {
|
||||
t.Error("Expected error for non-existent company")
|
||||
}
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("Unmet expectations: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue