From e7acfb37b1eadc2dc6ab889ba42c307876a159f7 Mon Sep 17 00:00:00 2001 From: Melker Date: Wed, 20 Mar 2024 23:49:55 +0100 Subject: [PATCH] GetProjectTimes och tester till det --- backend/internal/database/db.go | 46 ++++++------- backend/internal/database/db_test.go | 98 +++++++++++++++++++++++----- 2 files changed, 105 insertions(+), 39 deletions(-) diff --git a/backend/internal/database/db.go b/backend/internal/database/db.go index fd0a083..fd8297f 100644 --- a/backend/internal/database/db.go +++ b/backend/internal/database/db.go @@ -39,7 +39,9 @@ type Database interface { SignWeeklyReport(reportId int, projectManagerId int) error IsSiteAdmin(username string) (bool, error) IsProjectManager(username string, projectname string) (bool, error) - GetTotalTimePerActivity(projectName string) (map[string]int, error) + GetProjectTimes(projectName string) (map[string]int, error) + + } // This struct is a wrapper type that holds the database connection @@ -491,36 +493,36 @@ func (d *Db) MigrateSampleData() error { return nil } -func (d *Db) GetTotalTimePerActivity(projectName string) (map[string]int, error) { - - query := ` +// GetProjectTimes retrieves a map with times per "Activity" for a given project +func (d *Db) GetProjectTimes(projectName string) (map[string]int, error) { + query := ` SELECT development_time, meeting_time, admin_time, own_work_time, study_time, testing_time - FROM weekly_reports + FROM weekly_reports JOIN projects ON weekly_reports.project_id = projects.id WHERE projects.name = ? ` - rows, err := d.DB.Query(query, projectName) - if err != nil { - return nil, err - } - defer rows.Close() + rows, err := d.DB.Query(query, projectName) + if err != nil { + return nil, err + } + defer rows.Close() totalTime := make(map[string]int) - for rows.Next() { - var developmentTime, meetingTime, adminTime, ownWorkTime, studyTime, testingTime int - if err := rows.Scan(&developmentTime, &meetingTime, &adminTime, &ownWorkTime, &studyTime, &testingTime); err != nil { - return nil, err - } + for rows.Next() { + var developmentTime, meetingTime, adminTime, ownWorkTime, studyTime, testingTime int + if err := rows.Scan(&developmentTime, &meetingTime, &adminTime, &ownWorkTime, &studyTime, &testingTime); err != nil { + return nil, err + } - totalTime["development"] += developmentTime - totalTime["meeting"] += meetingTime - totalTime["admin"] += adminTime - totalTime["own_work"] += ownWorkTime - totalTime["study"] += studyTime - totalTime["testing"] += testingTime - } + totalTime["development"] += developmentTime + totalTime["meeting"] += meetingTime + totalTime["admin"] += adminTime + totalTime["own_work"] += ownWorkTime + totalTime["study"] += studyTime + totalTime["testing"] += testingTime + } if err := rows.Err(); err != nil { return nil, err diff --git a/backend/internal/database/db_test.go b/backend/internal/database/db_test.go index 139fba9..442bf91 100644 --- a/backend/internal/database/db_test.go +++ b/backend/internal/database/db_test.go @@ -729,27 +729,91 @@ func TestIsProjectManager(t *testing.T) { } } -func TestGetTotalTimePerActivity(t *testing.T) { - // Initialize your test database connection - db, err := setupState() - if err != nil { - t.Error("setupState failed:", err) - } - // Run the query to get total time per activity - totalTime, err := db.GetTotalTimePerActivity("projecttest") - if err != nil { - t.Error("GetTotalTimePerActivity failed:", err) - } +func TestGetProjectTimes(t *testing.T) { + // Initialize + db, err := setupState() + if err != nil { + t.Error("setupState failed:", err) + return + } - // Check if the totalTime map is not nil - if totalTime == nil { - t.Error("Expected non-nil totalTime map, got nil") - } + // Create a user + user := "TeaUser" + password := "Vanilla" + err = db.AddUser(user, password) + if err != nil { + t.Error("AddUser failed:", err) + return + } - // ska lägga till fler assertions + // Create a project + projectName := "ProjectVanilla" + projectDescription := "When tea tastes its best" + err = db.AddProject(projectName, projectDescription, user) // Fix the variable name here + if err != nil { + t.Error("AddProject failed:", err) + return + } + + // Tests the func in db.go + totalTime, err := db.GetProjectTimes(projectName) + if err != nil { + t.Error("GetTotalTimePerActivity failed:", err) + return + } + + // Check if the totalTime map is not nil + if totalTime == nil { + t.Error("Expected non-nil totalTime map, got nil") + return + } + + // Define the expected valeus + expectedTotalTime := map[string]int{ + "development": 0, + "meeting": 0, + "admin": 0, + "own_work": 0, + "study": 0, + "testing": 0, + } + + // Compare the expectedTotalTime with the totalTime retrieved from the database + for activity, expectedTime := range expectedTotalTime { + if totalTime[activity] != expectedTime { + t.Errorf("Expected %s time to be %d, got %d", activity, expectedTime, totalTime[activity]) + } + } + + // Insert some data into the database for different activities + err = db.AddWeeklyReport(projectName, user, 1, 1, 3, 2, 1, 4, 5) + if err != nil { + t.Error("Failed to insert data into the database:", err) + return + } + + newTotalTime, err := db.GetProjectTimes(projectName) + if err != nil { + t.Error("GetTotalTimePerActivity failed:", err) + return + } + + newExpectedTotalTime := map[string]int{ + "development": 1, + "meeting": 3, + "admin": 2, + "own_work": 1, + "study": 4, + "testing": 5, + } + + for activity, newExpectedTime := range newExpectedTotalTime { + if newTotalTime[activity] != newExpectedTime { + t.Errorf("Expected %s time to be %d, got %d", activity, newExpectedTime, newTotalTime[activity]) + } + } } - func TestEnsureManagerOfCreatedProject(t *testing.T) { db, err := setupState() if err != nil {