storj/pkg/satellite/satellitedb/projects_test.go

256 lines
6.0 KiB
Go
Raw Normal View History

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package satellitedb
import (
"testing"
"github.com/skyrings/skyring-common/tools/uuid"
"storj.io/storj/pkg/satellite/satellitedb/dbx"
"github.com/stretchr/testify/assert"
"storj.io/storj/internal/testcontext"
"storj.io/storj/pkg/satellite"
)
func TestProjectsRepository(t *testing.T) {
//testing constants
const (
// for user
lastName = "lastName"
email = "email@ukr.net"
pass = "123456"
userName = "name"
// for project
2018-11-14 12:45:49 +00:00
name = "Project"
description = "some description"
// updated project values
2018-11-14 12:45:49 +00:00
newName = "NewProject"
newDescription = "some new description"
)
ctx := testcontext.New(t)
defer ctx.Cleanup()
// creating in-memory db and opening connection
// to test with real db3 file use this connection string - "../db/accountdb.db3"
db, err := New("sqlite3", "file::memory:?mode=memory&cache=shared")
if err != nil {
assert.NoError(t, err)
}
defer ctx.Check(db.Close)
// creating tables
err = db.CreateTables()
if err != nil {
assert.NoError(t, err)
}
// repositories
users := db.Users()
projects := db.Projects()
2018-11-14 12:45:49 +00:00
var owner *satellite.User
2018-11-14 12:45:49 +00:00
t.Run("Can insert project without owner", func(t *testing.T) {
project := &satellite.Project{
2018-11-14 12:45:49 +00:00
OwnerID: nil,
Name: name,
Description: description,
IsAgreedWithTerms: false,
}
2018-11-14 12:45:49 +00:00
createdProject, err := projects.Insert(ctx, project)
2018-11-14 12:45:49 +00:00
assert.NotNil(t, createdProject)
assert.Nil(t, err)
assert.NoError(t, err)
})
t.Run("Insert project successfully", func(t *testing.T) {
2018-11-14 12:45:49 +00:00
owner, err = users.Insert(ctx, &satellite.User{
FirstName: userName,
LastName: lastName,
Email: email,
PasswordHash: []byte(pass),
})
assert.NoError(t, err)
2018-11-14 12:45:49 +00:00
assert.NotNil(t, owner)
project := &satellite.Project{
2018-11-14 12:45:49 +00:00
OwnerID: &owner.ID,
Name: name,
Description: description,
IsAgreedWithTerms: false,
}
createdProject, err := projects.Insert(ctx, project)
assert.NotNil(t, createdProject)
assert.Nil(t, err)
assert.NoError(t, err)
})
t.Run("Get project success", func(t *testing.T) {
2018-11-14 12:45:49 +00:00
projectsByOwnerID, err := projects.GetByOwnerID(ctx, owner.ID)
assert.Nil(t, err)
assert.NoError(t, err)
2018-11-14 12:45:49 +00:00
assert.Equal(t, len(projectsByOwnerID), 1)
2018-11-14 12:45:49 +00:00
assert.Equal(t, projectsByOwnerID[0].OwnerID, &owner.ID)
assert.Equal(t, projectsByOwnerID[0].Name, name)
assert.Equal(t, projectsByOwnerID[0].Description, description)
assert.Equal(t, projectsByOwnerID[0].IsAgreedWithTerms, false)
2018-11-14 12:45:49 +00:00
projectByID, err := projects.Get(ctx, projectsByOwnerID[0].ID)
assert.Nil(t, err)
assert.NoError(t, err)
2018-11-14 12:45:49 +00:00
assert.Equal(t, projectByID.ID, projectsByOwnerID[0].ID)
assert.Equal(t, projectByID.OwnerID, &owner.ID)
assert.Equal(t, projectByID.Name, name)
assert.Equal(t, projectByID.Description, description)
assert.Equal(t, projectByID.IsAgreedWithTerms, false)
})
t.Run("Update project success", func(t *testing.T) {
2018-11-14 12:45:49 +00:00
oldProjects, err := projects.GetByOwnerID(ctx, owner.ID)
assert.NoError(t, err)
2018-11-14 12:45:49 +00:00
assert.NotNil(t, oldProjects)
assert.Equal(t, len(oldProjects), 1)
2018-11-14 12:45:49 +00:00
// creating new project with updated values
newProject := &satellite.Project{
2018-11-14 12:45:49 +00:00
ID: oldProjects[0].ID,
OwnerID: &owner.ID,
Name: newName,
Description: newDescription,
IsAgreedWithTerms: true,
}
err = projects.Update(ctx, newProject)
assert.Nil(t, err)
assert.NoError(t, err)
// fetching updated project from db
2018-11-14 12:45:49 +00:00
newProject, err = projects.Get(ctx, oldProjects[0].ID)
assert.NoError(t, err)
2018-11-14 12:45:49 +00:00
assert.Equal(t, newProject.ID, oldProjects[0].ID)
assert.Equal(t, newProject.OwnerID, &owner.ID)
assert.Equal(t, newProject.Name, newName)
assert.Equal(t, newProject.Description, newDescription)
assert.Equal(t, newProject.IsAgreedWithTerms, true)
})
t.Run("Delete project success", func(t *testing.T) {
2018-11-14 12:45:49 +00:00
oldProjects, err := projects.GetByOwnerID(ctx, owner.ID)
assert.NoError(t, err)
2018-11-14 12:45:49 +00:00
assert.NotNil(t, oldProjects)
2018-11-14 12:45:49 +00:00
err = projects.Delete(ctx, oldProjects[0].ID)
assert.Nil(t, err)
assert.NoError(t, err)
2018-11-14 12:45:49 +00:00
_, err = projects.Get(ctx, oldProjects[0].ID)
assert.NotNil(t, err)
assert.Error(t, err)
})
t.Run("GetAll success", func(t *testing.T) {
allProjects, err := projects.GetAll(ctx)
assert.Nil(t, err)
assert.NoError(t, err)
2018-11-14 12:45:49 +00:00
assert.Equal(t, len(allProjects), 1)
newProject := &satellite.Project{
2018-11-14 12:45:49 +00:00
OwnerID: &owner.ID,
Description: description,
Name: name,
IsAgreedWithTerms: true,
}
_, err = projects.Insert(ctx, newProject)
assert.Nil(t, err)
assert.NoError(t, err)
allProjects, err = projects.GetAll(ctx)
assert.Nil(t, err)
assert.NoError(t, err)
2018-11-14 12:45:49 +00:00
assert.Equal(t, len(allProjects), 2)
newProject2 := &satellite.Project{
2018-11-14 12:45:49 +00:00
OwnerID: &owner.ID,
Description: description,
Name: name,
IsAgreedWithTerms: true,
}
_, err = projects.Insert(ctx, newProject2)
assert.Nil(t, err)
assert.NoError(t, err)
allProjects, err = projects.GetAll(ctx)
assert.Nil(t, err)
assert.NoError(t, err)
2018-11-14 12:45:49 +00:00
assert.Equal(t, len(allProjects), 3)
})
}
func TestProjectFromDbx(t *testing.T) {
t.Run("can't create dbo from nil dbx model", func(t *testing.T) {
2018-11-14 12:45:49 +00:00
project, err := projectFromDBX(nil)
2018-11-14 12:45:49 +00:00
assert.Nil(t, project)
assert.NotNil(t, err)
assert.Error(t, err)
})
t.Run("can't create dbo from dbx model with invalid ID", func(t *testing.T) {
dbxProject := dbx.Project{
Id: []byte("qweqwe"),
}
project, err := projectFromDBX(&dbxProject)
assert.Nil(t, project)
assert.NotNil(t, err)
assert.Error(t, err)
})
2018-11-14 12:45:49 +00:00
t.Run("can't create dbo from dbx model with invalid OwnerID", func(t *testing.T) {
projectID, err := uuid.New()
assert.NoError(t, err)
assert.Nil(t, err)
dbxProject := dbx.Project{
2018-11-14 12:45:49 +00:00
Id: projectID[:],
OwnerId: []byte("qweqwe"),
}
project, err := projectFromDBX(&dbxProject)
assert.Nil(t, project)
assert.NotNil(t, err)
assert.Error(t, err)
})
}