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)
}
err := store.Migrate()
err := db.Migrate(store)
if err != nil {
panic(err)

View File

@ -51,7 +51,7 @@ func doSetup() int {
}
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())
os.Exit(1)
}

View File

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

View File

@ -2,33 +2,13 @@ package bolt
import (
"github.com/ansible-semaphore/semaphore/db"
"math/rand"
"strconv"
"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) {
VERSION := "1.54.48"
store := createTestStore()
store := CreateTestStore()
build, err := store.CreateTemplate(db.Template{
ProjectID: 0,
@ -89,7 +69,7 @@ func TestTask_GetVersion(t *testing.T) {
t.Fatal(err)
}
version := deployTask.GetIncomingVersion(store)
version := deployTask.GetIncomingVersion(&store)
if version == nil {
t.Fatal()
return
@ -99,7 +79,7 @@ func TestTask_GetVersion(t *testing.T) {
return
}
version = deploy2Task.GetIncomingVersion(store)
version = deploy2Task.GetIncomingVersion(&store)
if version == nil {
t.Fatal()
return

View File

@ -10,7 +10,7 @@ type Migration_2_8_28 struct {
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 {
b := tx.Bucket([]byte("project__repository_" + projectID))
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
}
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 {
b := tx.Bucket([]byte("project__repository_" + projectID))
j, err := json.Marshal(repo)
@ -51,7 +51,7 @@ func (d Migration_2_8_28) Apply() (err error) {
for _, projectID := range projectIDs {
var err2 error
projectsRepositories[projectID], err2 = d.getProjectRepositories_2_8_26(projectID)
projectsRepositories[projectID], err2 = d.getProjectRepositories(projectID)
if err2 != nil {
return err2
}
@ -67,7 +67,7 @@ func (d Migration_2_8_28) Apply() (err error) {
}
repo["git_url"] = url
repo["git_branch"] = branch
err = d.setProjectRepository_2_8_26(projectID, repoID, repo)
err = d.setProjectRepository(projectID, repoID, repo)
if err != nil {
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) {
store := createStore()
err := store.Connect()
if err != nil {
t.Fatal(err.Error())
}
store := CreateTestStore()
usr, err := store.CreateUser(db.UserWithPwd{
Pwd: "123456",
User: db.User{
Email: "denguk@example.com",
Name: "Denis Gukov",
Email: "denguk@example.com",
Name: "Denis Gukov",
Username: "fiftin",
},
})
@ -29,7 +24,7 @@ func TestGetProjects(t *testing.T) {
proj1, err := store.CreateProject(db.Project{
Created: time.Now(),
Name: "Test1",
Name: "Test1",
})
if err != nil {
@ -38,8 +33,8 @@ func TestGetProjects(t *testing.T) {
_, err = store.CreateProjectUser(db.ProjectUser{
ProjectID: proj1.ID,
UserID: usr.ID,
Admin: true,
UserID: usr.ID,
Admin: true,
})
if err != nil {
@ -59,19 +54,13 @@ func TestGetProjects(t *testing.T) {
}
func TestGetProject(t *testing.T) {
store := createStore()
err := store.Connect()
if err != nil {
t.Fatal(err.Error())
}
store := CreateTestStore()
proj, err := store.CreateProject(db.Project{
Created: time.Now(),
Name: "Test1",
Name: "Test1",
})
if err != nil {
t.Fatal(err.Error())
}

View File

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

View File

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