Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kk db tests #981

Merged
merged 5 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions models/test/board_model_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package test

import (
"testing"

"github.com/merico-dev/lake/models"
"github.com/merico-dev/lake/models/test/factory"
"github.com/stretchr/testify/assert"
)

func TestInsertBoard(t *testing.T) {
board, err := factory.CreateBoard()
assert.Nil(t, err)
tx := models.Db.Create(&board)
assert.Nil(t, tx.Error)
expected := int64(1)
assert.Equal(t, tx.RowsAffected, expected)
}
20 changes: 20 additions & 0 deletions models/test/build_model_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package test

import (
"testing"

"github.com/merico-dev/lake/models"
"github.com/merico-dev/lake/models/test/factory"
"github.com/stretchr/testify/assert"
)

func TestInsertBuild(t *testing.T) {
job, err := factory.CreateJob()
assert.Nil(t, err)
build, err := factory.CreateBuild(job.DomainEntity.Id)
assert.Nil(t, err)
tx := models.Db.Create(&build)
assert.Nil(t, tx.Error)
expected := int64(1)
assert.Equal(t, tx.RowsAffected, expected)
}
22 changes: 22 additions & 0 deletions models/test/changelog_model_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package test

import (
"testing"

"github.com/merico-dev/lake/models"
"github.com/merico-dev/lake/models/test/factory"
"github.com/stretchr/testify/assert"
)

func TestInsertChangelog(t *testing.T) {
board, err := factory.CreateBoard()
assert.Nil(t, err)
issue, err := factory.CreateIssue(board.DomainEntity.Id)
assert.Nil(t, err)
changelog, err := factory.CreateChangelog(issue.DomainEntity.Id)
assert.Nil(t, err)
tx := models.Db.Create(&changelog)
assert.Nil(t, tx.Error)
expected := int64(1)
assert.Equal(t, tx.RowsAffected, expected)
}
File renamed without changes.
17 changes: 17 additions & 0 deletions models/test/factory/board_factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package factory

import (
"github.com/merico-dev/lake/models/domainlayer"
"github.com/merico-dev/lake/models/domainlayer/ticket"
)

func CreateBoard() (*ticket.Board, error) {
board := &ticket.Board{
DomainEntity: domainlayer.DomainEntity{
Id: RandIntString(),
},
Name: "",
Url: "",
}
return board, nil
}
23 changes: 23 additions & 0 deletions models/test/factory/build_factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package factory

import (
"time"

"github.com/merico-dev/lake/models/domainlayer"
"github.com/merico-dev/lake/models/domainlayer/devops"
)

func CreateBuild(jobId string) (*devops.Build, error) {
build := &devops.Build{
DomainEntity: domainlayer.DomainEntity{
Id: RandIntString(),
},
JobId: jobId, // ref to job
Name: "",
CommitSha: "",
DurationSec: uint64(RandInt()),
Status: "",
StartedDate: time.Now(),
}
return build, nil
}
25 changes: 25 additions & 0 deletions models/test/factory/changelog_factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package factory

import (
"time"

"github.com/merico-dev/lake/models/domainlayer"
"github.com/merico-dev/lake/models/domainlayer/ticket"
)

func CreateChangelog(issueId string) (*ticket.Changelog, error) {
changelog := &ticket.Changelog{
DomainEntity: domainlayer.DomainEntity{
Id: RandIntString(),
},
IssueId: issueId, // ref to issue
AuthorId: "",
AuthorName: "",
FieldId: "",
FieldName: "",
From: "",
To: "",
CreatedDate: time.Now(),
}
return changelog, nil
}
8 changes: 4 additions & 4 deletions models/test/factory/commit_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
func CreateCommit(repoId uint64) (*code.Commit, error) {
commit := &code.Commit{
DomainEntity: domainlayer.DomainEntity{
Id: "something",
Id: RandIntString(),
},
RepoId: repoId,
Sha: "dosifj9302hf80h23f",
Additions: 33,
Deletions: 33,
DevEq: 33,
Additions: RandInt(),
Deletions: RandInt(),
DevEq: RandInt(),
Message: "",
AuthorName: "",
AuthorEmail: "",
Expand Down
39 changes: 39 additions & 0 deletions models/test/factory/issue_factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package factory

import (
"time"

"github.com/merico-dev/lake/models/domainlayer"
"github.com/merico-dev/lake/models/domainlayer/ticket"
)

func CreateIssue(boardId string) (*ticket.Issue, error) {
issue := &ticket.Issue{
DomainEntity: domainlayer.DomainEntity{
Id: RandIntString(),
},
BoardId: boardId, // ref to board
Url: "",
Key: "",
Title: "",
Summary: "",
EpicKey: "",
Type: "",
Status: "",
StoryPoint: 1,
OriginalEstimateMinutes: 1, // user input?
AggregateEstimateMinutes: 1, // sum up of all subtasks?
RemainingEstimateMinutes: 1, // could it be negative value?
CreatorId: "",
AssigneeId: "",
ResolutionDate: nil,
Priority: "", // not sure how to deal with it yet, copy the name for now
ParentId: "",
SprintId: "",
CreatedDate: time.Now(),
UpdatedDate: time.Now(),
SpentMinutes: 1,
LeadTimeMinutes: 1,
}
return issue, nil
}
16 changes: 16 additions & 0 deletions models/test/factory/job_factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package factory

import (
"github.com/merico-dev/lake/models/domainlayer"
"github.com/merico-dev/lake/models/domainlayer/devops"
)

func CreateJob() (*devops.Job, error) {
job := &devops.Job{
DomainEntity: domainlayer.DomainEntity{
Id: RandIntString(),
},
Name: "",
}
return job, nil
}
2 changes: 1 addition & 1 deletion models/test/factory/note_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func CreateNote(prId uint64) (*code.Note, error) {
note := &code.Note{
DomainEntity: domainlayer.DomainEntity{
Id: "something",
Id: RandIntString(),
},
PrId: prId,
Author: "note.AuthorUsername",
Expand Down
4 changes: 2 additions & 2 deletions models/test/factory/pipeline_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
func CreatePipeline() (*models.Pipeline, error) {
pipeline := &models.Pipeline{
Name: "My Pipeline",
FinishedTasks: 0,
FinishedTasks: RandInt(),
Status: "MY_STATUS",
Message: "",
SpentSeconds: 0,
SpentSeconds: RandInt(),
}
return pipeline, nil
}
2 changes: 1 addition & 1 deletion models/test/factory/pr_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func CreatePr(repoId uint64) (*code.Pr, error) {
pr := &code.Pr{
DomainEntity: domainlayer.DomainEntity{
Id: "something",
Id: RandIntString(),
},
RepoId: repoId,
State: "",
Expand Down
2 changes: 1 addition & 1 deletion models/test/factory/repo_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
func CreateRepo() (*code.Repo, error) {
repo := &code.Repo{
DomainEntity: domainlayer.DomainEntity{
Id: "something",
Id: RandIntString(),
},
}
return repo, nil
Expand Down
22 changes: 22 additions & 0 deletions models/test/factory/sprint_factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package factory

import (
"github.com/merico-dev/lake/models/domainlayer"
"github.com/merico-dev/lake/models/domainlayer/ticket"
)

func CreateSprint(boardId string) (*ticket.Sprint, error) {
sprint := &ticket.Sprint{
DomainEntity: domainlayer.DomainEntity{
Id: RandIntString(),
},
BoardId: boardId, // ref to board
Url: "",
State: "",
Name: "",
StartDate: nil,
EndDate: nil,
CompleteDate: nil,
}
return sprint, nil
}
2 changes: 1 addition & 1 deletion models/test/factory/task_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func CreateTask(pipelineId uint64) (*models.Task, error) {
Options: nil,
Status: "MY_STATUS",
Message: "my message",
PipelineId: pipelineId,
PipelineId: pipelineId, // ref to pipeline
}
return task, nil
}
19 changes: 19 additions & 0 deletions models/test/factory/user_factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package factory

import (
"github.com/merico-dev/lake/models/domainlayer"
"github.com/merico-dev/lake/models/domainlayer/user"
)

func CreateUser() (*user.User, error) {
user := &user.User{
DomainEntity: domainlayer.DomainEntity{
Id: RandIntString(),
},
Name: "",
Email: "",
AvatarUrl: "",
Timezone: "",
}
return user, nil
}
17 changes: 17 additions & 0 deletions models/test/factory/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package factory

import (
"fmt"
"math"
"math/rand"
)

func RandInt() int {
seed := 1000000
r := int(math.Floor(rand.Float64() * float64(seed)))
return r
}

func RandIntString() string {
return fmt.Sprintf("something%v", RandInt())
}
25 changes: 25 additions & 0 deletions models/test/factory/worklog_factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package factory

import (
"time"

"github.com/merico-dev/lake/models/domainlayer"
"github.com/merico-dev/lake/models/domainlayer/ticket"
)

func CreateWorklog(boardId string, issueId string) (*ticket.Worklog, error) {
worklog := &ticket.Worklog{
DomainEntity: domainlayer.DomainEntity{
Id: RandIntString(),
},
IssueId: issueId, // ref to issue
BoardId: boardId, // ref to board
AuthorId: "",
UpdateAuthorId: "",
TimeSpent: "",
TimeSpentSeconds: RandInt(),
Updated: time.Now(),
Started: time.Now(),
}
return worklog, nil
}
20 changes: 20 additions & 0 deletions models/test/issue_model_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package test

import (
"testing"

"github.com/merico-dev/lake/models"
"github.com/merico-dev/lake/models/test/factory"
"github.com/stretchr/testify/assert"
)

func TestInsertIssue(t *testing.T) {
board, err := factory.CreateBoard()
assert.Nil(t, err)
issue, err := factory.CreateIssue(board.DomainEntity.Id)
assert.Nil(t, err)
tx := models.Db.Create(&issue)
assert.Nil(t, tx.Error)
expected := int64(1)
assert.Equal(t, tx.RowsAffected, expected)
}
18 changes: 18 additions & 0 deletions models/test/job_model_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package test

import (
"testing"

"github.com/merico-dev/lake/models"
"github.com/merico-dev/lake/models/test/factory"
"github.com/stretchr/testify/assert"
)

func TestInsertJob(t *testing.T) {
job, err := factory.CreateJob()
assert.Nil(t, err)
tx := models.Db.Create(&job)
assert.Nil(t, tx.Error)
expected := int64(1)
assert.Equal(t, tx.RowsAffected, expected)
}
Loading