fixed unit testing

This commit is contained in:
2025-12-16 13:26:07 +08:00
parent 7e42d04fde
commit 5828a2ff21
7 changed files with 296 additions and 221 deletions
+33 -30
View File
@@ -298,21 +298,20 @@ func TestGetAllPolicyAttributes_Empty(t *testing.T) {
// Additional comprehensive test cases
func TestGetPermissionByResourceAndAction_EmptyResource(t *testing.T) {
t.Skip("Skipping - actual SQL query differs from mock expectation")
mock, cleanup := setupMockDB(t)
defer cleanup()
rows := sqlmock.NewRows([]string{"id", "permission_name", "description", "resource", "action"})
mock.ExpectQuery("SELECT id, permission_name, description, resource, action FROM permissions WHERE resource = \\? AND action = \\? LIMIT 1").
// Match the exact query format with whitespace handling
mock.ExpectQuery(`SELECT id, permission_name, description, resource, action\s+FROM permissions\s+WHERE resource = \? AND action = \?\s+LIMIT 1`).
WithArgs("", "read").
WillReturnRows(rows)
perm, err := GetPermissionByResourceAndAction("", "read")
if err != nil && err != sql.ErrNoRows {
t.Errorf("Expected sql.ErrNoRows or no error, got %v", err)
if err == nil {
t.Error("Expected error for empty resource")
}
if perm != nil {
t.Error("Expected nil permission for empty resource")
@@ -320,21 +319,20 @@ func TestGetPermissionByResourceAndAction_EmptyResource(t *testing.T) {
}
func TestGetPermissionByResourceAndAction_EmptyAction(t *testing.T) {
t.Skip("Skipping - actual SQL query differs from mock expectation")
mock, cleanup := setupMockDB(t)
defer cleanup()
rows := sqlmock.NewRows([]string{"id", "permission_name", "description", "resource", "action"})
mock.ExpectQuery("SELECT id, permission_name, description, resource, action FROM permissions WHERE resource = \\? AND action = \\? LIMIT 1").
// Match the exact query format with whitespace handling
mock.ExpectQuery(`SELECT id, permission_name, description, resource, action\s+FROM permissions\s+WHERE resource = \? AND action = \?\s+LIMIT 1`).
WithArgs("document", "").
WillReturnRows(rows)
perm, err := GetPermissionByResourceAndAction("document", "")
if err != nil && err != sql.ErrNoRows {
t.Errorf("Expected sql.ErrNoRows or no error, got %v", err)
if err == nil {
t.Error("Expected error for empty action")
}
if perm != nil {
t.Error("Expected nil permission for empty action")
@@ -366,9 +364,10 @@ func TestGetPolicyAttributesByPermission_InvalidID(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
rows := sqlmock.NewRows([]string{"id", "attribute_name", "attribute_type", "comparison", "attribute_value"})
rows := sqlmock.NewRows([]string{"id", "attribute_name", "attribute_type", "comparison", "attribute_value", "permission_id"})
mock.ExpectQuery("SELECT id, attribute_name, attribute_type, comparison, attribute_value FROM policy_attributes WHERE permission_id = \\?").
// Match the actual query with proper whitespace handling
mock.ExpectQuery(`SELECT id, attribute_name, attribute_type, comparison, attribute_value, permission_id\s+FROM policy_attributes\s+WHERE permission_id = \?`).
WithArgs(-1).
WillReturnRows(rows)
@@ -386,7 +385,7 @@ func TestGetPolicyAttributesByPermission_DatabaseError(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
mock.ExpectQuery("SELECT id, attribute_name, attribute_type, comparison, attribute_value FROM policy_attributes WHERE permission_id = \\?").
mock.ExpectQuery(`SELECT id, attribute_name, attribute_type, comparison, attribute_value, permission_id\s+FROM policy_attributes\s+WHERE permission_id = \?`).
WithArgs(1).
WillReturnError(errors.New("database error"))
@@ -397,8 +396,6 @@ func TestGetPolicyAttributesByPermission_DatabaseError(t *testing.T) {
}
if attrs != nil {
t.Error("Expected nil attributes on error")
t.Skip("Skipping - actual SQL query differs from mock expectation")
}
}
@@ -406,9 +403,10 @@ func TestGetUserAttributes_EmptyUserID(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
rows := sqlmock.NewRows([]string{"attribute_name", "attribute_value", "attribute_type"})
rows := sqlmock.NewRows([]string{"attribute_name", "attribute_value"})
mock.ExpectQuery("SELECT attribute_name, attribute_value, attribute_type FROM user_attributes WHERE user_id = \\?").
// Match the actual query format
mock.ExpectQuery(`SELECT attribute_name, attribute_value\s+FROM user_attributes\s+WHERE user_id = \?`).
WithArgs("").
WillReturnRows(rows)
@@ -419,8 +417,6 @@ func TestGetUserAttributes_EmptyUserID(t *testing.T) {
}
if len(attrs) != 0 {
t.Errorf("Expected 0 attributes for empty user ID, got %d", len(attrs))
t.Skip("Skipping - actual SQL query differs from mock expectation")
}
}
@@ -428,13 +424,14 @@ func TestGetUserAttributes_MultipleAttributes(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
rows := sqlmock.NewRows([]string{"attribute_name", "attribute_value", "attribute_type"}).
AddRow("department", "IT", "string").
AddRow("level", "5", "number").
AddRow("location", "US", "string").
AddRow("clearance", "high", "string")
rows := sqlmock.NewRows([]string{"attribute_name", "attribute_value"}).
AddRow("department", "IT").
AddRow("level", "5").
AddRow("location", "US").
AddRow("clearance", "high")
mock.ExpectQuery("SELECT attribute_name, attribute_value, attribute_type FROM user_attributes WHERE user_id = \\?").
// Match the actual query
mock.ExpectQuery(`SELECT attribute_name, attribute_value\s+FROM user_attributes\s+WHERE user_id = \?`).
WithArgs("user123").
WillReturnRows(rows)
@@ -443,7 +440,6 @@ func TestGetUserAttributes_MultipleAttributes(t *testing.T) {
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
t.Skip("Skipping - actual SQL query differs from mock expectation")
if len(attrs) != 4 {
t.Errorf("Expected 4 attributes, got %d", len(attrs))
@@ -454,16 +450,23 @@ func TestGetUserByID_EmptyID(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
rows := sqlmock.NewRows([]string{"id", "username", "role", "email", "created_at", "updated_at"})
rows := sqlmock.NewRows([]string{
"user_id", "first_name", "middle_name", "last_name", "suffix", "email_address",
"account_type", "emp_id", "reg", "prov", "aProv", "mun", "bgy", "is_logged_in",
"first_logged_in", "address", "contact_number", "device_id", "role_id",
"role_dps", "is_deleted", "secret_key", "is_activated", "created_at", "updated_at",
})
mock.ExpectQuery("SELECT id, username, role, email, created_at, updated_at FROM users WHERE id = \\?").
// Match the actual query format with all the fields
mock.ExpectQuery(`SELECT user_id, first_name, middle_name, last_name, suffix, email_address`).
WithArgs("").
WillReturnRows(rows)
user, err := GetUserByID("")
if err != nil && err != sql.ErrNoRows {
t.Errorf("Expected sql.ErrNoRows or no error, got %v", err)
// Should get an error (empty ID returns error from function logic)
if err == nil {
t.Error("Expected error for empty ID, got nil")
}
if user != nil {
t.Error("Expected nil user for empty ID")