GetTotalTimePerActivity och ett halvt test för den
This commit is contained in:
parent
45761dadcb
commit
bef8a6af85
2 changed files with 50 additions and 24 deletions
|
@ -39,6 +39,9 @@ type Database interface {
|
||||||
SignWeeklyReport(reportId int, projectManagerId int) error
|
SignWeeklyReport(reportId int, projectManagerId int) error
|
||||||
IsSiteAdmin(username string) (bool, error)
|
IsSiteAdmin(username string) (bool, error)
|
||||||
IsProjectManager(username string, projectname string) (bool, error)
|
IsProjectManager(username string, projectname string) (bool, error)
|
||||||
|
GetTotalTimePerActivity(projectName string) (map[string]int, error)
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This struct is a wrapper type that holds the database connection
|
// This struct is a wrapper type that holds the database connection
|
||||||
|
@ -519,3 +522,41 @@ func (d *Db) MigrateSampleData() error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Db) GetTotalTimePerActivity(projectName string) (map[string]int, error) {
|
||||||
|
|
||||||
|
query := `
|
||||||
|
SELECT development_time, meeting_time, admin_time, own_work_time, study_time, testing_time
|
||||||
|
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()
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
return totalTime, nil
|
||||||
|
}
|
||||||
|
|
|
@ -676,38 +676,23 @@ func TestIsProjectManager(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestChangeUserName(t *testing.T) {
|
func TestGetTotalTimePerActivity(t *testing.T) {
|
||||||
|
// Initialize your test database connection
|
||||||
db, err := setupState()
|
db, err := setupState()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("setupState failed:", err)
|
t.Error("setupState failed:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a user
|
// Run the query to get total time per activity
|
||||||
err = db.AddUser("testuser", "password")
|
totalTime, err := db.GetTotalTimePerActivity("projecttest")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("AddUser failed:", err)
|
t.Error("GetTotalTimePerActivity failed:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Change the user's name
|
// Check if the totalTime map is not nil
|
||||||
err = db.ChangeUserName("testuser", "newname")
|
if totalTime == nil {
|
||||||
if err != nil {
|
t.Error("Expected non-nil totalTime map, got nil")
|
||||||
t.Error("ChangeUserName failed:", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve the user's ID
|
// ska lägga till fler assertions
|
||||||
userID, err := db.GetUserId("newname")
|
|
||||||
if err != nil {
|
|
||||||
t.Error("GetUserId failed:", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure the user's ID matches the expected value
|
|
||||||
if userID != 1 {
|
|
||||||
t.Errorf("Expected user ID to be 1, got %d", userID)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Attempt to retrieve the user by the old name
|
|
||||||
_, err = db.GetUserId("testuser")
|
|
||||||
if err == nil {
|
|
||||||
t.Error("Expected GetUserId to fail for the old name, but it didn't")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue