test(be): fix tests after migration refactoring

This commit is contained in:
Denis Gukov 2022-01-23 18:21:27 +05:00
parent 9520c0cd1b
commit d942cce1bc
9 changed files with 83 additions and 154 deletions

View File

@ -109,7 +109,7 @@ func createStore() db.Store {
panic(err) panic(err)
} }
err := store.Migrate() err := db.Migrate(store)
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -51,7 +51,7 @@ func doSetup() int {
} }
fmt.Println("Running db Migrations..") fmt.Println("Running db Migrations..")
if err := store.Migrate(); err != nil { if err := db.Migrate(store); err != nil {
fmt.Printf("Database migrations failed!\n %v\n", err.Error()) fmt.Printf("Database migrations failed!\n %v\n", err.Error())
os.Exit(1) os.Exit(1)
} }

View File

@ -10,46 +10,41 @@ import (
"time" "time"
) )
func CreateTestStore() BoltDb {
r := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
fn := "/tmp/test_semaphore_db_" + strconv.Itoa(r.Int())
store := BoltDb{
Filename: fn,
}
err := store.Connect()
if err != nil {
panic(err)
}
return store
}
type test1 struct { type test1 struct {
ID int `db:"ID"` ID int `db:"ID"`
FirstName string `db:"first_name" json:"firstName"` FirstName string `db:"first_name" json:"firstName"`
LastName string `db:"last_name" json:"lastName"` LastName string `db:"last_name" json:"lastName"`
Password string `db:"-" json:"password"` Password string `db:"-" json:"password"`
PasswordRepeat string `db:"-" json:"passwordRepeat"` PasswordRepeat string `db:"-" json:"passwordRepeat"`
PasswordHash string `db:"password" json:"-"` PasswordHash string `db:"password" json:"-"`
Removed bool `db:"removed"` Removed bool `db:"removed"`
} }
var test1props = db.ObjectProperties{ var test1props = db.ObjectProperties{
IsGlobal: true, IsGlobal: true,
TableName: "test1", TableName: "test1",
PrimaryColumnName: "ID", PrimaryColumnName: "ID",
} }
func createBoltDb() BoltDb {
r := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
fn := "/tmp/test_semaphore_db_" + strconv.Itoa(r.Int())
return BoltDb{
Filename: fn,
}
}
func createStore() db.Store {
store := createBoltDb()
return &store
}
func TestDeleteObjectSoft(t *testing.T) { func TestDeleteObjectSoft(t *testing.T) {
store := createBoltDb() store := CreateTestStore()
err := store.Connect()
if err != nil {
t.Fatal(err.Error())
}
obj := test1{ obj := test1{
FirstName: "Denis", FirstName: "Denis",
LastName: "Gukov", LastName: "Gukov",
} }
newObj, err := store.createObject(0, test1props, obj) newObj, err := store.createObject(0, test1props, obj)
@ -107,11 +102,11 @@ func TestMarshalObject_UserWithPwd(t *testing.T) {
func TestMarshalObject(t *testing.T) { func TestMarshalObject(t *testing.T) {
test1 := test1{ test1 := test1{
FirstName: "Denis", FirstName: "Denis",
LastName: "Gukov", LastName: "Gukov",
Password: "1234556", Password: "1234556",
PasswordRepeat: "123456", PasswordRepeat: "123456",
PasswordHash: "9347502348723", PasswordHash: "9347502348723",
} }
bytes, err := marshalObject(test1) bytes, err := marshalObject(test1)
@ -151,23 +146,23 @@ func TestUnmarshalObject(t *testing.T) {
func TestSortObjects(t *testing.T) { func TestSortObjects(t *testing.T) {
objects := []db.Inventory{ objects := []db.Inventory{
{ {
ID: 1, ID: 1,
Name: "x", Name: "x",
}, },
{ {
ID: 2, ID: 2,
Name: "a", Name: "a",
}, },
{ {
ID: 3, ID: 3,
Name: "d", Name: "d",
}, },
{ {
ID: 4, ID: 4,
Name: "b", Name: "b",
}, },
{ {
ID: 5, ID: 5,
Name: "r", Name: "r",
}, },
} }
@ -183,7 +178,6 @@ func TestSortObjects(t *testing.T) {
objects[3].Name == "r" && objects[3].Name == "r" &&
objects[4].Name == "x" objects[4].Name == "x"
if !expected { if !expected {
t.Fatal(fmt.Errorf("objects not sorted")) t.Fatal(fmt.Errorf("objects not sorted"))
} }
@ -211,12 +205,7 @@ func TestGetFieldNameByTag2(t *testing.T) {
} }
func TestIsObjectInUse(t *testing.T) { func TestIsObjectInUse(t *testing.T) {
store := createBoltDb() store := CreateTestStore()
err := store.Connect()
if err != nil {
t.Fatal(err.Error())
}
proj, err := store.CreateProject(db.Project{ proj, err := store.CreateProject(db.Project{
Name: "test", Name: "test",
@ -227,9 +216,9 @@ func TestIsObjectInUse(t *testing.T) {
} }
_, err = store.CreateTemplate(db.Template{ _, err = store.CreateTemplate(db.Template{
Alias: "Test", Alias: "Test",
Playbook: "test.yml", Playbook: "test.yml",
ProjectID: proj.ID, ProjectID: proj.ID,
InventoryID: 10, InventoryID: 10,
}) })
@ -250,12 +239,7 @@ func TestIsObjectInUse(t *testing.T) {
} }
func TestIsObjectInUse_Environment(t *testing.T) { func TestIsObjectInUse_Environment(t *testing.T) {
store := createBoltDb() store := CreateTestStore()
err := store.Connect()
if err != nil {
t.Fatal(err.Error())
}
proj, err := store.CreateProject(db.Project{ proj, err := store.CreateProject(db.Project{
Name: "test", Name: "test",
@ -268,9 +252,9 @@ func TestIsObjectInUse_Environment(t *testing.T) {
envID := 10 envID := 10
_, err = store.CreateTemplate(db.Template{ _, err = store.CreateTemplate(db.Template{
Alias: "Test", Alias: "Test",
Playbook: "test.yml", Playbook: "test.yml",
ProjectID: proj.ID, ProjectID: proj.ID,
EnvironmentID: &envID, EnvironmentID: &envID,
}) })
@ -291,12 +275,7 @@ func TestIsObjectInUse_Environment(t *testing.T) {
} }
func TestIsObjectInUse_EnvironmentNil(t *testing.T) { func TestIsObjectInUse_EnvironmentNil(t *testing.T) {
store := createBoltDb() store := CreateTestStore()
err := store.Connect()
if err != nil {
t.Fatal(err.Error())
}
proj, err := store.CreateProject(db.Project{ proj, err := store.CreateProject(db.Project{
Name: "test", Name: "test",
@ -307,9 +286,9 @@ func TestIsObjectInUse_EnvironmentNil(t *testing.T) {
} }
_, err = store.CreateTemplate(db.Template{ _, err = store.CreateTemplate(db.Template{
Alias: "Test", Alias: "Test",
Playbook: "test.yml", Playbook: "test.yml",
ProjectID: proj.ID, ProjectID: proj.ID,
EnvironmentID: nil, EnvironmentID: nil,
}) })

View File

@ -2,33 +2,13 @@ package bolt
import ( import (
"github.com/ansible-semaphore/semaphore/db" "github.com/ansible-semaphore/semaphore/db"
"math/rand"
"strconv"
"testing" "testing"
"time"
) )
func createTestBoltDb() BoltDb {
r := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
fn := "/tmp/test_semaphore_db_" + strconv.Itoa(r.Int())
return BoltDb{
Filename: fn,
}
}
func createTestStore() db.Store {
store := createTestBoltDb()
err := store.Connect()
if err != nil {
panic(err)
}
return &store
}
func TestTask_GetVersion(t *testing.T) { func TestTask_GetVersion(t *testing.T) {
VERSION := "1.54.48" VERSION := "1.54.48"
store := createTestStore() store := CreateTestStore()
build, err := store.CreateTemplate(db.Template{ build, err := store.CreateTemplate(db.Template{
ProjectID: 0, ProjectID: 0,
@ -89,7 +69,7 @@ func TestTask_GetVersion(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
version := deployTask.GetIncomingVersion(store) version := deployTask.GetIncomingVersion(&store)
if version == nil { if version == nil {
t.Fatal() t.Fatal()
return return
@ -99,7 +79,7 @@ func TestTask_GetVersion(t *testing.T) {
return return
} }
version = deploy2Task.GetIncomingVersion(store) version = deploy2Task.GetIncomingVersion(&store)
if version == nil { if version == nil {
t.Fatal() t.Fatal()
return return

View File

@ -10,7 +10,7 @@ type Migration_2_8_28 struct {
DB *bbolt.DB DB *bbolt.DB
} }
func (d Migration_2_8_28) getProjectRepositories_2_8_26(projectID string) (repos map[string]map[string]interface{}, err error) { func (d Migration_2_8_28) getProjectRepositories(projectID string) (repos map[string]map[string]interface{}, err error) {
err = d.DB.View(func(tx *bbolt.Tx) error { err = d.DB.View(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte("project__repository_" + projectID)) b := tx.Bucket([]byte("project__repository_" + projectID))
return b.ForEach(func(id, body []byte) error { return b.ForEach(func(id, body []byte) error {
@ -22,7 +22,7 @@ func (d Migration_2_8_28) getProjectRepositories_2_8_26(projectID string) (repos
return return
} }
func (d Migration_2_8_28) setProjectRepository_2_8_26(projectID string, repoID string, repo map[string]interface{}) error { func (d Migration_2_8_28) setProjectRepository(projectID string, repoID string, repo map[string]interface{}) error {
return d.DB.Update(func(tx *bbolt.Tx) error { return d.DB.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte("project__repository_" + projectID)) b := tx.Bucket([]byte("project__repository_" + projectID))
j, err := json.Marshal(repo) j, err := json.Marshal(repo)
@ -51,7 +51,7 @@ func (d Migration_2_8_28) Apply() (err error) {
for _, projectID := range projectIDs { for _, projectID := range projectIDs {
var err2 error var err2 error
projectsRepositories[projectID], err2 = d.getProjectRepositories_2_8_26(projectID) projectsRepositories[projectID], err2 = d.getProjectRepositories(projectID)
if err2 != nil { if err2 != nil {
return err2 return err2
} }
@ -67,7 +67,7 @@ func (d Migration_2_8_28) Apply() (err error) {
} }
repo["git_url"] = url repo["git_url"] = url
repo["git_branch"] = branch repo["git_branch"] = branch
err = d.setProjectRepository_2_8_26(projectID, repoID, repo) err = d.setProjectRepository(projectID, repoID, repo)
if err != nil { if err != nil {
return err return err
} }

View File

@ -0,0 +1,7 @@
package migrations
import "testing"
func TestMigration_2_8_28_Apply(t *testing.T) {
}

View File

@ -7,18 +7,13 @@ import (
) )
func TestGetProjects(t *testing.T) { func TestGetProjects(t *testing.T) {
store := createStore() store := CreateTestStore()
err := store.Connect()
if err != nil {
t.Fatal(err.Error())
}
usr, err := store.CreateUser(db.UserWithPwd{ usr, err := store.CreateUser(db.UserWithPwd{
Pwd: "123456", Pwd: "123456",
User: db.User{ User: db.User{
Email: "denguk@example.com", Email: "denguk@example.com",
Name: "Denis Gukov", Name: "Denis Gukov",
Username: "fiftin", Username: "fiftin",
}, },
}) })
@ -29,7 +24,7 @@ func TestGetProjects(t *testing.T) {
proj1, err := store.CreateProject(db.Project{ proj1, err := store.CreateProject(db.Project{
Created: time.Now(), Created: time.Now(),
Name: "Test1", Name: "Test1",
}) })
if err != nil { if err != nil {
@ -38,8 +33,8 @@ func TestGetProjects(t *testing.T) {
_, err = store.CreateProjectUser(db.ProjectUser{ _, err = store.CreateProjectUser(db.ProjectUser{
ProjectID: proj1.ID, ProjectID: proj1.ID,
UserID: usr.ID, UserID: usr.ID,
Admin: true, Admin: true,
}) })
if err != nil { if err != nil {
@ -59,19 +54,13 @@ func TestGetProjects(t *testing.T) {
} }
func TestGetProject(t *testing.T) { func TestGetProject(t *testing.T) {
store := createStore() store := CreateTestStore()
err := store.Connect()
if err != nil {
t.Fatal(err.Error())
}
proj, err := store.CreateProject(db.Project{ proj, err := store.CreateProject(db.Project{
Created: time.Now(), Created: time.Now(),
Name: "Test1", Name: "Test1",
}) })
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }

View File

@ -7,19 +7,13 @@ import (
) )
func TestBoltDb_UpdateProjectUser(t *testing.T) { func TestBoltDb_UpdateProjectUser(t *testing.T) {
store := createStore() store := CreateTestStore()
err := store.Connect()
if err != nil {
t.Fatal(err.Error())
}
usr, err := store.CreateUser(db.UserWithPwd{ usr, err := store.CreateUser(db.UserWithPwd{
Pwd: "123456", Pwd: "123456",
User: db.User{ User: db.User{
Email: "denguk@example.com", Email: "denguk@example.com",
Name: "Denis Gukov", Name: "Denis Gukov",
Username: "fiftin", Username: "fiftin",
}, },
}) })
@ -30,7 +24,7 @@ func TestBoltDb_UpdateProjectUser(t *testing.T) {
proj1, err := store.CreateProject(db.Project{ proj1, err := store.CreateProject(db.Project{
Created: time.Now(), Created: time.Now(),
Name: "Test1", Name: "Test1",
}) })
if err != nil { if err != nil {
@ -39,8 +33,8 @@ func TestBoltDb_UpdateProjectUser(t *testing.T) {
projUser, err := store.CreateProjectUser(db.ProjectUser{ projUser, err := store.CreateProjectUser(db.ProjectUser{
ProjectID: proj1.ID, ProjectID: proj1.ID,
UserID: usr.ID, UserID: usr.ID,
Admin: true, Admin: true,
}) })
if err != nil { if err != nil {
@ -56,18 +50,13 @@ func TestBoltDb_UpdateProjectUser(t *testing.T) {
} }
func TestGetUsers(t *testing.T) { func TestGetUsers(t *testing.T) {
store := createStore() store := CreateTestStore()
err := store.Connect()
if err != nil { _, err := store.CreateUser(db.UserWithPwd{
t.Fatal(err.Error())
}
_, err = store.CreateUser(db.UserWithPwd{
Pwd: "123456", Pwd: "123456",
User: db.User{ User: db.User{
Email: "denguk@example.com", Email: "denguk@example.com",
Name: "Denis Gukov", Name: "Denis Gukov",
Username: "fiftin", Username: "fiftin",
}, },
}) })
@ -89,18 +78,13 @@ func TestGetUsers(t *testing.T) {
} }
func TestGetUser(t *testing.T) { func TestGetUser(t *testing.T) {
store := createStore() store := CreateTestStore()
err := store.Connect()
if err != nil {
t.Fatal(err.Error())
}
usr, err := store.CreateUser(db.UserWithPwd{ usr, err := store.CreateUser(db.UserWithPwd{
Pwd: "123456", Pwd: "123456",
User: db.User{ User: db.User{
Email: "denguk@example.com", Email: "denguk@example.com",
Name: "Denis Gukov", Name: "Denis Gukov",
Username: "fiftin", Username: "fiftin",
}, },
}) })

View File

@ -8,12 +8,7 @@ import (
) )
func TestGetViews(t *testing.T) { func TestGetViews(t *testing.T) {
store := createStore() store := CreateTestStore()
err := store.Connect()
if err != nil {
t.Fatal(err.Error())
}
proj1, err := store.CreateProject(db.Project{ proj1, err := store.CreateProject(db.Project{
Created: time.Now(), Created: time.Now(),
@ -56,12 +51,7 @@ func TestGetViews(t *testing.T) {
} }
func TestSetViewPositions(t *testing.T) { func TestSetViewPositions(t *testing.T) {
store := createStore() store := CreateTestStore()
err := store.Connect()
if err != nil {
t.Fatal(err.Error())
}
proj1, err := store.CreateProject(db.Project{ proj1, err := store.CreateProject(db.Project{
Created: time.Now(), Created: time.Now(),