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,6 +10,19 @@ 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"`
@ -26,26 +39,8 @@ var test1props = db.ObjectProperties{
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",
@ -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",
@ -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",
@ -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",

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,12 +7,7 @@ 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",
@ -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,13 +7,7 @@ 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",
@ -56,14 +50,9 @@ 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",
@ -89,12 +78,7 @@ 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",

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(),