586 lines
14 KiB
Go
586 lines
14 KiB
Go
package database
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
// Tests are not guaranteed to be sequential
|
|
|
|
// setupState initializes a database instance with necessary setup for testing
|
|
func setupState() (Database, error) {
|
|
db := DbConnect(":memory:")
|
|
err := db.Migrate()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return db, nil
|
|
}
|
|
|
|
// TestDbConnect tests the connection to the database
|
|
func TestDbConnect(t *testing.T) {
|
|
db := DbConnect(":memory:")
|
|
_ = db
|
|
}
|
|
|
|
// TestDbAddUser tests the AddUser function of the database
|
|
func TestDbAddUser(t *testing.T) {
|
|
db, err := setupState()
|
|
if err != nil {
|
|
t.Error("setupState failed:", err)
|
|
}
|
|
err = db.AddUser("test", "password")
|
|
if err != nil {
|
|
t.Error("AddUser failed:", err)
|
|
}
|
|
}
|
|
|
|
// TestDbGetUserId tests the GetUserID function of the database
|
|
func TestDbGetUserId(t *testing.T) {
|
|
db, err := setupState()
|
|
if err != nil {
|
|
t.Error("setupState failed:", err)
|
|
}
|
|
if db.AddUser("test", "password") != nil {
|
|
t.Error("AddUser failed")
|
|
}
|
|
|
|
var id int
|
|
|
|
id, err = db.GetUserId("test")
|
|
if err != nil {
|
|
t.Error("GetUserId failed:", err)
|
|
}
|
|
if id != 1 {
|
|
t.Error("GetUserId failed: expected 1, got", id)
|
|
}
|
|
}
|
|
|
|
// TestDbAddProject tests the AddProject function of the database
|
|
func TestDbAddProject(t *testing.T) {
|
|
db, err := setupState()
|
|
if err != nil {
|
|
t.Error("setupState failed:", err)
|
|
}
|
|
|
|
err = db.AddProject("test", "description", "test")
|
|
if err != nil {
|
|
t.Error("AddProject failed:", err)
|
|
}
|
|
}
|
|
|
|
// TestDbRemoveUser tests the RemoveUser function of the database
|
|
func TestDbRemoveUser(t *testing.T) {
|
|
db, err := setupState()
|
|
if err != nil {
|
|
t.Error("setupState failed:", err)
|
|
}
|
|
|
|
err = db.RemoveUser("test")
|
|
if err != nil {
|
|
t.Error("RemoveUser failed:", err)
|
|
}
|
|
}
|
|
|
|
// TestPromoteToAdmin tests the PromoteToAdmin function of the database
|
|
func TestPromoteToAdmin(t *testing.T) {
|
|
db, err := setupState()
|
|
if err != nil {
|
|
t.Error("setupState failed:", err)
|
|
}
|
|
|
|
err = db.AddUser("test", "password")
|
|
if err != nil {
|
|
t.Error("AddUser failed:", err)
|
|
}
|
|
|
|
err = db.PromoteToAdmin("test")
|
|
if err != nil {
|
|
t.Error("PromoteToAdmin failed:", err)
|
|
}
|
|
}
|
|
|
|
// TestAddWeeklyReport tests the AddWeeklyReport function of the database
|
|
func TestAddWeeklyReport(t *testing.T) {
|
|
db, err := setupState()
|
|
if err != nil {
|
|
t.Error("setupState failed:", err)
|
|
}
|
|
|
|
err = db.AddUser("testuser", "password")
|
|
if err != nil {
|
|
t.Error("AddUser failed:", err)
|
|
}
|
|
|
|
err = db.AddProject("testproject", "description", "testuser")
|
|
if err != nil {
|
|
t.Error("AddProject failed:", err)
|
|
}
|
|
|
|
err = db.AddWeeklyReport("testproject", "testuser", 1, 1, 1, 1, 1, 1, 1)
|
|
if err != nil {
|
|
t.Error("AddWeeklyReport failed:", err)
|
|
}
|
|
}
|
|
|
|
// TestAddUserToProject tests the AddUseToProject function of the database
|
|
func TestAddUserToProject(t *testing.T) {
|
|
db, err := setupState()
|
|
if err != nil {
|
|
t.Error("setupState failed:", err)
|
|
}
|
|
|
|
err = db.AddUser("testuser", "password")
|
|
if err != nil {
|
|
t.Error("AddUser failed:", err)
|
|
}
|
|
|
|
err = db.AddProject("testproject", "description", "testuser")
|
|
if err != nil {
|
|
t.Error("AddProject failed:", err)
|
|
}
|
|
|
|
err = db.AddWeeklyReport("testproject", "testuser", 1, 1, 1, 1, 1, 1, 1)
|
|
if err != nil {
|
|
t.Error("AddWeeklyReport failed:", err)
|
|
}
|
|
|
|
err = db.AddUserToProject("testuser", "testproject", "user")
|
|
if err != nil {
|
|
t.Error("AddUserToProject failed:", err)
|
|
}
|
|
}
|
|
|
|
// TestChangeUserRole tests the ChangeUserRole function of the database
|
|
func TestChangeUserRole(t *testing.T) {
|
|
db, err := setupState()
|
|
if err != nil {
|
|
t.Error("setupState failed:", err)
|
|
}
|
|
|
|
err = db.AddUser("testuser", "password")
|
|
if err != nil {
|
|
t.Error("AddUser failed:", err)
|
|
}
|
|
|
|
err = db.AddProject("testproject", "description", "testuser")
|
|
if err != nil {
|
|
t.Error("AddProject failed:", err)
|
|
}
|
|
|
|
err = db.AddUserToProject("testuser", "testproject", "user")
|
|
if err != nil {
|
|
t.Error("AddUserToProject failed:", err)
|
|
}
|
|
|
|
role, err := db.GetUserRole("testuser", "testproject")
|
|
if err != nil {
|
|
t.Error("GetUserRole failed:", err)
|
|
}
|
|
if role != "user" {
|
|
t.Error("GetUserRole failed: expected user, got", role)
|
|
}
|
|
|
|
err = db.ChangeUserRole("testuser", "testproject", "admin")
|
|
if err != nil {
|
|
t.Error("ChangeUserRole failed:", err)
|
|
}
|
|
|
|
role, err = db.GetUserRole("testuser", "testproject")
|
|
if err != nil {
|
|
t.Error("GetUserRole failed:", err)
|
|
}
|
|
if role != "admin" {
|
|
t.Error("GetUserRole failed: expected admin, got", role)
|
|
}
|
|
|
|
}
|
|
|
|
// TestGetAllUsersProject tests the GetAllUsersProject function of the database
|
|
func TestGetAllUsersProject(t *testing.T) {
|
|
db, err := setupState()
|
|
if err != nil {
|
|
t.Error("setupState failed:", err)
|
|
}
|
|
|
|
err = db.AddUser("testuser1", "password")
|
|
if err != nil {
|
|
t.Error("AddUser failed:", err)
|
|
}
|
|
|
|
err = db.AddUser("testuser2", "password")
|
|
if err != nil {
|
|
t.Error("AddUser failed:", err)
|
|
}
|
|
|
|
err = db.AddProject("testproject", "description", "testuser1")
|
|
if err != nil {
|
|
t.Error("AddProject failed:", err)
|
|
}
|
|
|
|
err = db.AddUserToProject("testuser1", "testproject", "project_manager")
|
|
if err != nil {
|
|
t.Error("AddUserToProject failed:", err)
|
|
}
|
|
|
|
err = db.AddUserToProject("testuser2", "testproject", "user")
|
|
if err != nil {
|
|
t.Error("AddUserToProject failed:", err)
|
|
}
|
|
|
|
users, err := db.GetAllUsersProject("testproject")
|
|
if err != nil {
|
|
t.Error("GetAllUsersProject failed:", err)
|
|
}
|
|
|
|
// Check if both users are returned with their roles
|
|
if len(users) != 2 {
|
|
t.Errorf("Expected 2 users, got %d", len(users))
|
|
}
|
|
|
|
// Check if testuser1 has project manager role
|
|
foundProjectManager := false
|
|
for _, user := range users {
|
|
if user.Username == "testuser1" && user.UserRole == "project_manager" {
|
|
foundProjectManager = true
|
|
break
|
|
}
|
|
}
|
|
if !foundProjectManager {
|
|
t.Error("Project Manager user not found")
|
|
}
|
|
|
|
// Check if testuser2 has user role
|
|
foundUser := false
|
|
for _, user := range users {
|
|
if user.Username == "testuser2" && user.UserRole == "user" {
|
|
foundUser = true
|
|
break
|
|
}
|
|
}
|
|
if !foundUser {
|
|
t.Error("User user not found")
|
|
}
|
|
}
|
|
|
|
// TestGetAllUsersApplication tests the GetAllUsersApplicsation function of the database
|
|
func TestGetAllUsersApplication(t *testing.T) {
|
|
db, err := setupState()
|
|
if err != nil {
|
|
t.Error("setupState failed:", err)
|
|
}
|
|
|
|
err = db.AddUser("testuser1", "password")
|
|
if err != nil {
|
|
t.Error("AddUser failed:", err)
|
|
}
|
|
|
|
err = db.AddUser("testuser2", "password")
|
|
if err != nil {
|
|
t.Error("AddUser failed:", err)
|
|
}
|
|
|
|
users, err := db.GetAllUsersApplication()
|
|
if err != nil {
|
|
t.Error("GetAllUsersApplication failed:", err)
|
|
}
|
|
|
|
// Check if both users are returned
|
|
if len(users) != 2 {
|
|
t.Errorf("Expected 2 users, got %d", len(users))
|
|
}
|
|
|
|
// Check if the test users are included in the list
|
|
foundTestUser1 := false
|
|
foundTestUser2 := false
|
|
for _, user := range users {
|
|
if user == "testuser1" {
|
|
foundTestUser1 = true
|
|
}
|
|
if user == "testuser2" {
|
|
foundTestUser2 = true
|
|
}
|
|
}
|
|
|
|
if !foundTestUser1 {
|
|
t.Error("testuser1 not found")
|
|
}
|
|
if !foundTestUser2 {
|
|
t.Error("testuser2 not found")
|
|
}
|
|
}
|
|
|
|
// TestGetProjectsForUser tests the GetProjectsForUser function of the database
|
|
func TestGetProjectsForUser(t *testing.T) {
|
|
db, err := setupState()
|
|
if err != nil {
|
|
t.Error("setupState failed:", err)
|
|
}
|
|
|
|
err = db.AddUser("testuser", "password")
|
|
if err != nil {
|
|
t.Error("AddUser failed:", err)
|
|
}
|
|
|
|
err = db.AddProject("testproject", "description", "testuser")
|
|
if err != nil {
|
|
t.Error("AddProject failed:", err)
|
|
}
|
|
|
|
err = db.AddUserToProject("testuser", "testproject", "user")
|
|
if err != nil {
|
|
t.Error("AddUserToProject failed:", err)
|
|
}
|
|
|
|
projects1, err := db.GetAllProjects()
|
|
if err != nil {
|
|
t.Error("GetAllProjects failed:", err)
|
|
}
|
|
|
|
if len(projects1) != 1 {
|
|
t.Error("GetAllProjects failed: expected 1, got", len(projects1))
|
|
}
|
|
|
|
projects, err := db.GetProjectsForUser("testuser")
|
|
if err != nil {
|
|
t.Error("GetProjectsForUser failed:", err)
|
|
}
|
|
|
|
if len(projects) != 1 {
|
|
t.Error("GetProjectsForUser failed: expected 1, got", len(projects))
|
|
}
|
|
}
|
|
|
|
// TestAddProject tests AddProject function of the database
|
|
func TestAddProject(t *testing.T) {
|
|
db, err := setupState()
|
|
if err != nil {
|
|
t.Error("setupState failed:", err)
|
|
}
|
|
|
|
err = db.AddUser("testuser", "password")
|
|
if err != nil {
|
|
t.Error("AddUser failed:", err)
|
|
}
|
|
|
|
err = db.AddProject("testproject", "description", "testuser")
|
|
if err != nil {
|
|
t.Error("AddProject failed:", err)
|
|
}
|
|
|
|
// Retrieve the added project to verify its existence
|
|
projects, err := db.GetAllProjects()
|
|
if err != nil {
|
|
t.Error("GetAllProjects failed:", err)
|
|
}
|
|
|
|
// Check if the project was added successfully
|
|
found := false
|
|
for _, project := range projects {
|
|
if project.Name == "testproject" {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
t.Error("Added project not found")
|
|
}
|
|
}
|
|
|
|
// TestGetWeeklyReport tests GetWeeklyReport function of the database
|
|
func TestGetWeeklyReport(t *testing.T) {
|
|
db, err := setupState()
|
|
if err != nil {
|
|
t.Error("setupState failed:", err)
|
|
}
|
|
|
|
err = db.AddUser("testuser", "password")
|
|
if err != nil {
|
|
t.Error("AddUser failed:", err)
|
|
}
|
|
|
|
err = db.AddProject("testproject", "description", "testuser")
|
|
if err != nil {
|
|
t.Error("AddProject failed:", err)
|
|
}
|
|
|
|
err = db.AddWeeklyReport("testproject", "testuser", 1, 1, 1, 1, 1, 1, 1)
|
|
if err != nil {
|
|
t.Error("AddWeeklyReport failed:", err)
|
|
}
|
|
|
|
report, err := db.GetWeeklyReport("testuser", "testproject", 1)
|
|
if err != nil {
|
|
t.Error("GetWeeklyReport failed:", err)
|
|
}
|
|
|
|
// Check if the retrieved report matches the expected values
|
|
if report.UserId != 1 {
|
|
t.Errorf("Expected UserId to be 1, got %d", report.UserId)
|
|
}
|
|
if report.ProjectId != 1 {
|
|
t.Errorf("Expected ProjectId to be 1, got %d", report.ProjectId)
|
|
}
|
|
if report.Week != 1 {
|
|
t.Errorf("Expected Week to be 1, got %d", report.Week)
|
|
}
|
|
// Check other fields similarly
|
|
}
|
|
|
|
// TestSignWeeklyReport tests SignWeeklyReport function of the database
|
|
func TestSignWeeklyReport(t *testing.T) {
|
|
db, err := setupState()
|
|
if err != nil {
|
|
t.Error("setupState failed:", err)
|
|
}
|
|
|
|
// Add project manager
|
|
err = db.AddUser("projectManager", "password")
|
|
if err != nil {
|
|
t.Error("AddUser failed:", err)
|
|
}
|
|
|
|
// Add a regular user
|
|
err = db.AddUser("testuser", "password")
|
|
if err != nil {
|
|
t.Error("AddUser failed:", err)
|
|
}
|
|
|
|
// Add project
|
|
err = db.AddProject("testproject", "description", "projectManager")
|
|
if err != nil {
|
|
t.Error("AddProject failed:", err)
|
|
}
|
|
|
|
// Add both regular users as members to the project
|
|
err = db.AddUserToProject("testuser", "testproject", "member")
|
|
if err != nil {
|
|
t.Error("AddUserToProject failed:", err)
|
|
}
|
|
|
|
err = db.AddUserToProject("projectManager", "testproject", "project_manager")
|
|
if err != nil {
|
|
t.Error("AddUserToProject failed:", err)
|
|
}
|
|
|
|
// Add a weekly report for one of the regular users
|
|
err = db.AddWeeklyReport("testproject", "testuser", 1, 1, 1, 1, 1, 1, 1)
|
|
if err != nil {
|
|
t.Error("AddWeeklyReport failed:", err)
|
|
}
|
|
|
|
// Retrieve the added report
|
|
report, err := db.GetWeeklyReport("testuser", "testproject", 1)
|
|
if err != nil {
|
|
t.Error("GetWeeklyReport failed:", err)
|
|
}
|
|
|
|
// Print project manager's ID
|
|
projectManagerID, err := db.GetUserId("projectManager")
|
|
if err != nil {
|
|
t.Error("GetUserId failed:", err)
|
|
}
|
|
fmt.Println("Project Manager's ID:", projectManagerID)
|
|
|
|
// Sign the report with the project manager
|
|
err = db.SignWeeklyReport(report.ReportId, projectManagerID)
|
|
if err != nil {
|
|
t.Error("SignWeeklyReport failed:", err)
|
|
}
|
|
|
|
// Retrieve the report again to check if it's signed
|
|
signedReport, err := db.GetWeeklyReport("testuser", "testproject", 1)
|
|
if err != nil {
|
|
t.Error("GetWeeklyReport failed:", err)
|
|
}
|
|
|
|
// Ensure the report is signed by the project manager
|
|
if *signedReport.SignedBy != projectManagerID {
|
|
t.Errorf("Expected SignedBy to be %d, got %d", projectManagerID, *signedReport.SignedBy)
|
|
}
|
|
}
|
|
|
|
// TestSignWeeklyReportByAnotherProjectManager tests the scenario where a project manager attempts to sign a weekly report for a user who is not assigned to their project
|
|
func TestSignWeeklyReportByAnotherProjectManager(t *testing.T) {
|
|
db, err := setupState()
|
|
if err != nil {
|
|
t.Error("setupState failed:", err)
|
|
}
|
|
|
|
// Add project manager
|
|
err = db.AddUser("projectManager", "password")
|
|
if err != nil {
|
|
t.Error("AddUser failed:", err)
|
|
}
|
|
|
|
// Add a regular user
|
|
err = db.AddUser("testuser", "password")
|
|
if err != nil {
|
|
t.Error("AddUser failed:", err)
|
|
}
|
|
|
|
// Add project
|
|
err = db.AddProject("testproject", "description", "projectManager")
|
|
if err != nil {
|
|
t.Error("AddProject failed:", err)
|
|
}
|
|
|
|
// Add the regular user as a member to the project
|
|
err = db.AddUserToProject("testuser", "testproject", "member")
|
|
if err != nil {
|
|
t.Error("AddUserToProject failed:", err)
|
|
}
|
|
|
|
// Add a weekly report for the regular user
|
|
err = db.AddWeeklyReport("testproject", "testuser", 1, 1, 1, 1, 1, 1, 1)
|
|
if err != nil {
|
|
t.Error("AddWeeklyReport failed:", err)
|
|
}
|
|
|
|
// Retrieve the added report
|
|
report, err := db.GetWeeklyReport("testuser", "testproject", 1)
|
|
if err != nil {
|
|
t.Error("GetWeeklyReport failed:", err)
|
|
}
|
|
|
|
anotherManagerID, err := db.GetUserId("projectManager")
|
|
if err != nil {
|
|
t.Error("GetUserId failed:", err)
|
|
}
|
|
|
|
err = db.SignWeeklyReport(report.ReportId, anotherManagerID)
|
|
if err == nil {
|
|
t.Error("Expected SignWeeklyReport to fail with a project manager who is not in the project, but it didn't")
|
|
}
|
|
}
|
|
|
|
// TestGetProject tests GetProject function of the database
|
|
func TestGetProject(t *testing.T) {
|
|
db, err := setupState()
|
|
if err != nil {
|
|
t.Error("setupState failed:", err)
|
|
}
|
|
|
|
// Add a user
|
|
err = db.AddUser("testuser", "password")
|
|
if err != nil {
|
|
t.Error("AddUser failed:", err)
|
|
}
|
|
|
|
// Add a project
|
|
err = db.AddProject("testproject", "description", "testuser")
|
|
if err != nil {
|
|
t.Error("AddProject failed:", err)
|
|
}
|
|
|
|
// Retrieve the added project
|
|
project, err := db.GetProject(1)
|
|
if err != nil {
|
|
t.Error("GetProject failed:", err)
|
|
}
|
|
|
|
// Check if the retrieved project matches the expected values
|
|
if project.Name != "testproject" {
|
|
t.Errorf("Expected Name to be testproject, got %s", project.Name)
|
|
}
|
|
}
|