2024-02-12 12:40:49 +01:00
package database
import (
"testing"
)
2024-02-27 05:51:16 +01:00
// Tests are not guaranteed to be sequential
2024-02-28 03:21:13 +01:00
2024-03-20 16:13:53 +01:00
// setupState initializes a database instance with necessary setup for testing
2024-02-29 20:33:20 +01:00
func setupState ( ) ( Database , error ) {
2024-02-28 03:21:13 +01:00
db := DbConnect ( ":memory:" )
2024-03-17 14:38:20 +01:00
err := db . Migrate ( )
2024-02-28 03:21:13 +01:00
if err != nil {
return nil , err
}
return db , nil
}
2024-02-27 05:51:16 +01:00
2024-03-20 21:51:36 +01:00
// This is a more advanced setup that includes more data in the database.
// This is useful for more complex testing scenarios.
func setupAdvancedState ( ) ( Database , error ) {
db , err := setupState ( )
if err != nil {
return nil , err
}
// Add a user
if err = db . AddUser ( "demouser" , "password" ) ; err != nil {
return nil , err
}
// Add a project
if err = db . AddProject ( "projecttest" , "description" , "demouser" ) ; err != nil {
return nil , err
}
// Add a weekly report
if err = db . AddWeeklyReport ( "projecttest" , "demouser" , 1 , 1 , 1 , 1 , 1 , 1 , 1 ) ; err != nil {
return nil , err
}
return db , nil
}
2024-03-20 16:13:53 +01:00
// TestDbConnect tests the connection to the database
2024-02-12 12:40:49 +01:00
func TestDbConnect ( t * testing . T ) {
2024-02-28 03:21:13 +01:00
db := DbConnect ( ":memory:" )
2024-02-12 12:40:49 +01:00
_ = db
}
2024-02-27 05:00:04 +01:00
2024-03-20 21:51:36 +01:00
func TestSetupAdvancedState ( t * testing . T ) {
db , err := setupAdvancedState ( )
if err != nil {
t . Error ( "setupAdvancedState failed:" , err )
}
// Check if the user was added
if _ , err = db . GetUserId ( "demouser" ) ; err != nil {
t . Error ( "GetUserId failed:" , err )
}
// Check if the project was added
projects , err := db . GetAllProjects ( )
if err != nil {
t . Error ( "GetAllProjects failed:" , err )
}
if len ( projects ) != 1 {
t . Error ( "GetAllProjects failed: expected 1, got" , len ( projects ) )
}
// To be continued...
}
2024-03-20 16:13:53 +01:00
// TestDbAddUser tests the AddUser function of the database
2024-02-27 05:00:04 +01:00
func TestDbAddUser ( t * testing . T ) {
2024-02-28 03:21:13 +01:00
db , err := setupState ( )
if err != nil {
t . Error ( "setupState failed:" , err )
}
err = db . AddUser ( "test" , "password" )
2024-02-27 05:00:04 +01:00
if err != nil {
t . Error ( "AddUser failed:" , err )
}
}
2024-03-20 16:13:53 +01:00
// TestDbGetUserId tests the GetUserID function of the database
2024-02-27 05:51:16 +01:00
func TestDbGetUserId ( t * testing . T ) {
2024-02-28 03:21:13 +01:00
db , err := setupState ( )
if err != nil {
t . Error ( "setupState failed:" , err )
}
2024-03-02 04:29:50 +01:00
if db . AddUser ( "test" , "password" ) != nil {
t . Error ( "AddUser failed" )
}
2024-02-27 05:51:16 +01:00
var id int
2024-02-28 03:21:13 +01:00
id , err = db . GetUserId ( "test" )
2024-02-27 05:51:16 +01:00
if err != nil {
t . Error ( "GetUserId failed:" , err )
}
if id != 1 {
t . Error ( "GetUserId failed: expected 1, got" , id )
}
}
2024-03-20 16:13:53 +01:00
// TestDbAddProject tests the AddProject function of the database
2024-02-27 07:59:42 +01:00
func TestDbAddProject ( t * testing . T ) {
2024-03-20 21:51:36 +01:00
db , err := setupAdvancedState ( )
2024-02-28 03:21:13 +01:00
if err != nil {
t . Error ( "setupState failed:" , err )
}
2024-03-20 21:51:36 +01:00
err = db . AddProject ( "test" , "description" , "demouser" )
2024-02-27 07:59:42 +01:00
if err != nil {
t . Error ( "AddProject failed:" , err )
}
}
2024-03-20 16:13:53 +01:00
// TestDbRemoveUser tests the RemoveUser function of the database
2024-02-27 05:00:04 +01:00
func TestDbRemoveUser ( t * testing . T ) {
2024-02-28 03:21:13 +01:00
db , err := setupState ( )
if err != nil {
t . Error ( "setupState failed:" , err )
}
err = db . RemoveUser ( "test" )
2024-02-27 05:00:04 +01:00
if err != nil {
t . Error ( "RemoveUser failed:" , err )
}
}
2024-03-07 13:21:47 +01:00
2024-03-20 16:13:53 +01:00
// TestPromoteToAdmin tests the PromoteToAdmin function of the database
2024-03-07 13:21:47 +01:00
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 )
}
}
2024-03-07 14:25:28 +01:00
2024-03-20 16:13:53 +01:00
// TestAddWeeklyReport tests the AddWeeklyReport function of the database
2024-03-16 22:47:19 +01:00
func TestAddWeeklyReport ( t * testing . T ) {
2024-03-14 13:39:56 +01:00
db , err := setupState ( )
if err != nil {
t . Error ( "setupState failed:" , err )
}
2024-03-07 14:25:28 +01:00
2024-03-14 13:39:56 +01:00
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 )
}
2024-03-16 22:47:19 +01:00
err = db . AddWeeklyReport ( "testproject" , "testuser" , 1 , 1 , 1 , 1 , 1 , 1 , 1 )
2024-03-14 13:39:56 +01:00
if err != nil {
2024-03-16 22:47:19 +01:00
t . Error ( "AddWeeklyReport failed:" , err )
2024-03-14 13:39:56 +01:00
}
}
2024-03-20 16:13:53 +01:00
// TestAddUserToProject tests the AddUseToProject function of the database
2024-03-14 13:39:56 +01:00
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 )
}
2024-03-16 22:47:19 +01:00
err = db . AddWeeklyReport ( "testproject" , "testuser" , 1 , 1 , 1 , 1 , 1 , 1 , 1 )
2024-03-14 13:39:56 +01:00
if err != nil {
2024-03-16 22:47:19 +01:00
t . Error ( "AddWeeklyReport failed:" , err )
2024-03-14 13:39:56 +01:00
}
err = db . AddUserToProject ( "testuser" , "testproject" , "user" )
if err != nil {
t . Error ( "AddUserToProject failed:" , err )
}
}
2024-03-07 14:25:28 +01:00
2024-03-20 16:13:53 +01:00
// TestChangeUserRole tests the ChangeUserRole function of the database
2024-03-14 13:47:04 +01:00
func TestChangeUserRole ( t * testing . T ) {
db , err := setupState ( )
if err != nil {
t . Error ( "setupState failed:" , err )
}
2024-03-07 14:25:28 +01:00
2024-03-14 13:47:04 +01:00
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 )
}
2024-03-14 19:48:49 +01:00
role , err := db . GetUserRole ( "testuser" , "testproject" )
if err != nil {
t . Error ( "GetUserRole failed:" , err )
}
2024-03-20 21:51:36 +01:00
if role != "project_manager" {
t . Error ( "GetUserRole failed: expected project_manager, got" , role )
2024-03-14 19:48:49 +01:00
}
2024-03-20 21:51:36 +01:00
err = db . ChangeUserRole ( "testuser" , "testproject" , "member" )
2024-03-14 13:47:04 +01:00
if err != nil {
t . Error ( "ChangeUserRole failed:" , err )
}
2024-03-14 19:48:49 +01:00
role , err = db . GetUserRole ( "testuser" , "testproject" )
if err != nil {
t . Error ( "GetUserRole failed:" , err )
}
2024-03-20 21:51:36 +01:00
if role != "member" {
t . Error ( "GetUserRole failed: expected member, got" , role )
2024-03-14 19:48:49 +01:00
}
2024-03-14 13:47:04 +01:00
}
2024-03-14 16:01:56 +01:00
2024-03-20 16:13:53 +01:00
// TestGetAllUsersProject tests the GetAllUsersProject function of the database
2024-03-14 16:01:56 +01:00
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" )
}
}
2024-03-14 16:25:54 +01:00
2024-03-20 16:13:53 +01:00
// TestGetAllUsersApplication tests the GetAllUsersApplicsation function of the database
2024-03-14 16:25:54 +01:00
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" )
}
}
2024-03-14 19:48:49 +01:00
2024-03-20 16:13:53 +01:00
// TestGetProjectsForUser tests the GetProjectsForUser function of the database
2024-03-14 19:48:49 +01:00
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 ) )
}
}
2024-03-15 15:14:45 +01:00
2024-03-20 16:13:53 +01:00
// TestAddProject tests AddProject function of the database
2024-03-15 15:14:45 +01:00
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" )
}
}
2024-03-17 17:58:02 +01:00
2024-03-20 16:13:53 +01:00
// TestGetWeeklyReport tests GetWeeklyReport function of the database
2024-03-17 17:58:02 +01:00
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
}
2024-03-17 20:30:55 +01:00
2024-03-20 16:13:53 +01:00
// TestSignWeeklyReport tests SignWeeklyReport function of the database
2024-03-17 20:30:55 +01:00
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 )
}
// 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 )
}
}
2024-03-20 16:13:53 +01:00
// TestSignWeeklyReportByAnotherProjectManager tests the scenario where a project manager attempts to sign a weekly report for a user who is not assigned to their project
2024-03-17 20:35:48 +01:00
func TestSignWeeklyReportByAnotherProjectManager ( t * testing . T ) {
2024-03-17 20:30:55 +01:00
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 )
}
2024-03-20 21:51:36 +01:00
// Add project, projectManager is the owner
2024-03-17 20:30:55 +01:00
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 )
}
2024-03-20 21:51:36 +01:00
managerID , err := db . GetUserId ( "projectManager" )
2024-03-17 20:30:55 +01:00
if err != nil {
t . Error ( "GetUserId failed:" , err )
}
2024-03-20 21:51:36 +01:00
err = db . SignWeeklyReport ( report . ReportId , managerID )
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 != managerID {
t . Errorf ( "Expected SignedBy to be %d, got %d" , managerID , * signedReport . SignedBy )
2024-03-17 20:30:55 +01:00
}
}
2024-03-18 16:42:35 +01:00
2024-03-20 16:13:53 +01:00
// TestGetProject tests GetProject function of the database
2024-03-18 16:42:35 +01:00
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 )
}
}
2024-03-19 19:04:45 +01:00
func TestGetWeeklyReportsUser ( 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 . AddWeeklyReport ( "testproject" , "testuser" , 2 , 1 , 1 , 1 , 1 , 1 , 1 )
if err != nil {
t . Error ( "AddWeeklyReport failed:" , err )
}
reports , err := db . GetWeeklyReportsUser ( "testuser" , "testproject" )
if err != nil {
t . Error ( "GetWeeklyReportsUser failed:" , err )
}
// Check if the retrieved reports match the expected values
if len ( reports ) != 2 {
t . Errorf ( "Expected 1 report, got %d" , len ( reports ) )
}
2024-03-19 19:14:55 +01:00
}
func TestIsProjectManager ( t * testing . T ) {
db , err := setupState ( )
if err != nil {
t . Error ( "setupState failed:" , err )
}
// Add a project manager
err = db . AddUser ( "projectManager" , "password" )
if err != nil {
t . Error ( "AddUser failed:" , err )
}
2024-03-19 19:04:45 +01:00
2024-03-19 19:14:55 +01:00
// 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 )
}
// Check if the regular user is not a project manager
isManager , err := db . IsProjectManager ( "testuser" , "testproject" )
if err != nil {
t . Error ( "IsProjectManager failed:" , err )
}
if isManager {
t . Error ( "Expected testuser not to be a project manager, but it is." )
}
// Check if the project manager is indeed a project manager
isManager , err = db . IsProjectManager ( "projectManager" , "testproject" )
if err != nil {
t . Error ( "IsProjectManager failed:" , err )
}
if ! isManager {
t . Error ( "Expected projectManager to be a project manager, but it's not." )
}
2024-03-19 19:04:45 +01:00
}
2024-03-20 16:46:17 +01:00
2024-03-20 23:49:55 +01:00
func TestGetProjectTimes ( t * testing . T ) {
2024-03-21 02:48:26 +01:00
// Initialize
db , err := setupState ( )
if err != nil {
t . Error ( "setupState failed:" , err )
return
}
// Create a user
user := "TeaUser"
password := "Vanilla"
err = db . AddUser ( user , password )
if err != nil {
t . Error ( "AddUser failed:" , err )
return
}
// 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
}
2024-03-20 23:49:55 +01:00
newTotalTime , err := db . GetProjectTimes ( projectName )
2024-03-21 02:48:26 +01:00
if err != nil {
t . Error ( "GetTotalTimePerActivity failed:" , err )
return
}
2024-03-20 23:49:55 +01:00
newExpectedTotalTime := map [ string ] int {
2024-03-21 02:48:26 +01:00
"development" : 1 ,
"meeting" : 3 ,
"admin" : 2 ,
"own_work" : 1 ,
"study" : 4 ,
"testing" : 5 ,
}
2024-03-20 23:49:55 +01:00
for activity , newExpectedTime := range newExpectedTotalTime {
2024-03-21 02:48:26 +01:00
if newTotalTime [ activity ] != newExpectedTime {
t . Errorf ( "Expected %s time to be %d, got %d" , activity , newExpectedTime , newTotalTime [ activity ] )
}
}
2024-03-20 16:46:17 +01:00
}
2024-03-20 18:47:37 +01:00
func TestEnsureManagerOfCreatedProject ( 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 )
}
2024-03-20 21:51:36 +01:00
// Set user to a project manager
// err = db.AddUserToProject("testuser", "testproject", "project_manager")
// if err != nil {
// t.Error("AddUserToProject failed:", err)
// }
2024-03-20 18:47:37 +01:00
managerState , err := db . IsProjectManager ( "testuser" , "testproject" )
if err != nil {
t . Error ( "IsProjectManager failed:" , err )
}
if ! managerState {
t . Error ( "Expected testuser to be a project manager, but it's not." )
}
}
2024-03-21 02:48:26 +01:00
// TestUpdateWeeklyReport tests the UpdateWeeklyReport function of the database
func TestUpdateWeeklyReport ( 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 )
}
// Add a weekly report
err = db . AddWeeklyReport ( "testproject" , "testuser" , 1 , 1 , 1 , 1 , 1 , 1 , 1 )
if err != nil {
t . Error ( "AddWeeklyReport failed:" , err )
}
// Update the weekly report
err = db . UpdateWeeklyReport ( "testproject" , "testuser" , 1 , 2 , 2 , 2 , 2 , 2 , 2 )
if err != nil {
t . Error ( "UpdateWeeklyReport failed:" , err )
}
// Retrieve the updated report
updatedReport , err := db . GetWeeklyReport ( "testuser" , "testproject" , 1 )
if err != nil {
t . Error ( "GetWeeklyReport failed:" , err )
}
// Check if the report was updated correctly
if updatedReport . DevelopmentTime != 2 ||
updatedReport . MeetingTime != 2 ||
updatedReport . AdminTime != 2 ||
updatedReport . OwnWorkTime != 2 ||
updatedReport . StudyTime != 2 ||
updatedReport . TestingTime != 2 {
t . Error ( "UpdateWeeklyReport failed: report not updated correctly" )
}
}