From d942cce1bc449c22285e2f4c745033ec91cce3cb Mon Sep 17 00:00:00 2001 From: Denis Gukov Date: Sun, 23 Jan 2022 18:21:27 +0500 Subject: [PATCH] test(be): fix tests after migration refactoring --- cli/cmd/root.go | 2 +- cli/cmd/setup.go | 2 +- db/bolt/BoltDb_test.go | 109 ++++++++------------ db/bolt/Task_test.go | 26 +---- db/bolt/migrations/Migration_2_8_28.go | 8 +- db/bolt/migrations/Migration_2_8_28_test.go | 7 ++ db/bolt/project_test.go | 27 ++--- db/bolt/user_test.go | 42 +++----- db/bolt/view_test.go | 14 +-- 9 files changed, 83 insertions(+), 154 deletions(-) create mode 100644 db/bolt/migrations/Migration_2_8_28_test.go diff --git a/cli/cmd/root.go b/cli/cmd/root.go index 730d393f..3efe9161 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -109,7 +109,7 @@ func createStore() db.Store { panic(err) } - err := store.Migrate() + err := db.Migrate(store) if err != nil { panic(err) diff --git a/cli/cmd/setup.go b/cli/cmd/setup.go index acca2808..e75705c5 100644 --- a/cli/cmd/setup.go +++ b/cli/cmd/setup.go @@ -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) } diff --git a/db/bolt/BoltDb_test.go b/db/bolt/BoltDb_test.go index 6393ea71..d3ced57c 100644 --- a/db/bolt/BoltDb_test.go +++ b/db/bolt/BoltDb_test.go @@ -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, }) diff --git a/db/bolt/Task_test.go b/db/bolt/Task_test.go index 24bba0aa..20bc7986 100644 --- a/db/bolt/Task_test.go +++ b/db/bolt/Task_test.go @@ -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 diff --git a/db/bolt/migrations/Migration_2_8_28.go b/db/bolt/migrations/Migration_2_8_28.go index 9fab5637..cff5822e 100644 --- a/db/bolt/migrations/Migration_2_8_28.go +++ b/db/bolt/migrations/Migration_2_8_28.go @@ -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 } diff --git a/db/bolt/migrations/Migration_2_8_28_test.go b/db/bolt/migrations/Migration_2_8_28_test.go new file mode 100644 index 00000000..25157ed4 --- /dev/null +++ b/db/bolt/migrations/Migration_2_8_28_test.go @@ -0,0 +1,7 @@ +package migrations + +import "testing" + +func TestMigration_2_8_28_Apply(t *testing.T) { + +} diff --git a/db/bolt/project_test.go b/db/bolt/project_test.go index de646f1d..b67c4463 100644 --- a/db/bolt/project_test.go +++ b/db/bolt/project_test.go @@ -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()) } diff --git a/db/bolt/user_test.go b/db/bolt/user_test.go index 21f54e29..e9cefc37 100644 --- a/db/bolt/user_test.go +++ b/db/bolt/user_test.go @@ -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", }, }) diff --git a/db/bolt/view_test.go b/db/bolt/view_test.go index 2af7294f..eeb2e520 100644 --- a/db/bolt/view_test.go +++ b/db/bolt/view_test.go @@ -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(),