Added GetProject in db.go and corresponding test

This commit is contained in:
dDogge 2024-03-15 15:14:45 +01:00 committed by Imbus
parent 4920966388
commit 581209742a
2 changed files with 42 additions and 0 deletions

View file

@ -28,6 +28,7 @@ type Database interface {
GetAllUsersApplication() ([]string, error)
GetProjectsForUser(username string) ([]types.Project, error)
GetAllProjects() ([]types.Project, error)
GetProject(projectId int) (types.Project, error)
GetUserRole(username string, projectname string) (string, error)
}
@ -100,6 +101,12 @@ func (d *Db) GetAllProjects() ([]types.Project, error) {
return projects, err
}
func (d *Db) GetProject(projectId int) (types.Project, error) {
var project types.Project
err := d.Select(&project, "SELECT * FROM projects WHERE id = ?")
return project, err
}
func (d *Db) AddTimeReport(projectName string, userName string, start time.Time, end time.Time) error { // WIP
_, err := d.Exec(addTimeReport, userName, projectName, start, end)
return err

View file

@ -343,3 +343,38 @@ func TestGetProjectsForUser(t *testing.T) {
t.Error("GetProjectsForUser failed: expected 1, got", len(projects))
}
}
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")
}
}