stufflog graphql server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

165 lines
4.3 KiB

package mysqldriver
import (
"context"
"git.aiterp.net/stufflog/server/internal/xlerrors"
"git.aiterp.net/stufflog/server/models"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
var project1 = models.Project{
ID: "STUFF",
Name: "Stuff and things",
Description: "Items and artifacts",
DailyPoints: 200,
}
var project2 = models.Project{
ID: "MODELING",
Name: "3D Modelling",
Description: "Making stuff.",
DailyPoints: 250,
}
var project3 = models.Project{
ID: "DINNER",
Name: "Dinner",
Description: "Shopping lists and meals.",
DailyPoints: 0,
}
var projectSearch1 = "stuff"
func TestProjectRepository(t *testing.T) {
projects := testDB.projects
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
assert.NoError(t, clearTable("project"))
// INSERT
result, err := projects.Insert(ctx, project1)
assert.NoError(t, err)
assert.Equal(t, project1, *result)
result, err = projects.Insert(ctx, project2)
assert.NoError(t, err)
assert.Equal(t, project2, *result)
result, err = projects.Insert(ctx, project3)
assert.NoError(t, err)
assert.Equal(t, project3, *result)
if t.Failed() {
return
}
// FIND
result, err = projects.Find(ctx, "MODELING")
assert.NoError(t, err)
assert.Equal(t, &project2, result)
// FIND't
result, err = projects.Find(ctx, "BLARGH")
assert.Error(t, err)
assert.True(t, xlerrors.IsNotFound(err))
assert.Nil(t, result)
// LIST
results, err := projects.List(ctx, models.ProjectFilter{})
assert.NoError(t, err)
assert.Equal(t, []*models.Project{&project3, &project2, &project1}, results)
results, err = projects.List(ctx, models.ProjectFilter{
Search: &projectSearch1,
})
assert.NoError(t, err)
assert.Equal(t, []*models.Project{&project2, &project1}, results)
results, err = projects.List(ctx, models.ProjectFilter{
ProjectIDs: []string{"DINNER", "MODELING"},
})
assert.NoError(t, err)
assert.Equal(t, []*models.Project{&project3, &project2}, results)
if t.Failed() {
return
}
// UPDATE
project2fix := project2
project2fix.Name = "3D Modeling"
project2fix.Description = "Modeling 3D stuff."
project2fix.DailyPoints = 150
err = projects.Save(ctx, project2fix)
assert.NoError(t, err)
if t.Failed() {
return
}
// FIND after UPDATE
result, err = projects.Find(ctx, "MODELING")
assert.NoError(t, err)
assert.Equal(t, &project2fix, result)
// DELETE
err = projects.Delete(ctx, project3)
assert.NoError(t, err)
if t.Failed() {
return
}
// LIST after DELETE + UPDATE
results, err = projects.List(ctx, models.ProjectFilter{})
assert.NoError(t, err)
assert.Equal(t, []*models.Project{&project2fix, &project1}, results)
// SET PERMISSION
err = projects.SetPermission(ctx, models.ProjectPermission{
ProjectID: project1.ID,
UserID: user1.ID,
Level: models.ProjectPermissionLevelOwner,
})
assert.NoError(t, err)
err = projects.SetPermission(ctx, models.ProjectPermission{
ProjectID: project2fix.ID,
UserID: user1.ID,
Level: models.ProjectPermissionLevelMember,
})
assert.NoError(t, err)
// GET PERMISSIONS
permission, err := projects.GetPermission(ctx, project1, user1)
assert.NoError(t, err)
assert.Equal(t, project1.ID, permission.ProjectID)
assert.Equal(t, user1.ID, permission.UserID)
assert.Equal(t, models.ProjectPermissionLevelOwner, permission.Level)
permission, err = projects.GetPermission(ctx, project2fix, user1)
assert.NoError(t, err)
assert.Equal(t, models.ProjectPermissionLevelMember, permission.Level)
// GET PERMISSION (default)
permission, err = projects.GetPermission(ctx, project1, user2)
assert.NoError(t, err)
assert.Equal(t, project1.ID, permission.ProjectID)
assert.Equal(t, user2.ID, permission.UserID)
assert.Equal(t, models.ProjectPermissionLevelNoAccess, permission.Level)
// LIST after SET PERMISSION
results, err = projects.List(ctx, models.ProjectFilter{
Permission: &models.ProjectFilterPermission{
UserID: user1.ID,
MinLevel: models.ProjectPermissionLevelMember,
},
})
assert.NoError(t, err)
assert.Equal(t, []*models.Project{&project2fix, &project1}, results)
results, err = projects.List(ctx, models.ProjectFilter{
Permission: &models.ProjectFilterPermission{
UserID: user2.ID,
MinLevel: models.ProjectPermissionLevelMember,
},
})
assert.NoError(t, err)
assert.Equal(t, []*models.Project{}, results)
}