+20
-33
@@ -3,26 +3,14 @@ package simpleorm_test
|
||||
import (
|
||||
"testing"
|
||||
|
||||
simpleorm "git.gdulai.com/gdulai/simpleorm"
|
||||
"git.gdulai.com/gdulai/simpleorm/exec"
|
||||
"git.gdulai.com/gdulai/simpleorm/repository"
|
||||
log "gitlab.com/gdulai/simpleloglvl"
|
||||
)
|
||||
|
||||
func repoTestSetup() (*simpleorm.ORM, *simpleorm.DBConnection) {
|
||||
conn := simpleorm.OpenConnection("sqlite3", "file:test.db")
|
||||
|
||||
orm := simpleorm.NewORM(Test{}, TestWithFk{}, TestWithFkAndFkId{},
|
||||
TestWithCompositePk{}, TestWithCompositeFk{})
|
||||
|
||||
exec.ExecuteDDL(conn, orm)
|
||||
|
||||
return orm, conn
|
||||
}
|
||||
|
||||
func TestRepoSelectAll(t *testing.T) {
|
||||
// GIVEN
|
||||
orm, conn := repoTestSetup()
|
||||
orm, conn := testSetup()
|
||||
defer cleanUp("test.db", conn)
|
||||
|
||||
testObj := Test{Int64Field: 54, IntField: 12, StringField: "asdf"}
|
||||
@@ -32,7 +20,7 @@ func TestRepoSelectAll(t *testing.T) {
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
_, err = insertExec.Execute(conn)
|
||||
err = insertExec.Execute(conn)
|
||||
repo := repository.NewRepository[Test](conn, orm)
|
||||
|
||||
// WHEN
|
||||
@@ -48,7 +36,7 @@ func TestRepoSelectAll(t *testing.T) {
|
||||
|
||||
func TestRepoSelectByPk(t *testing.T) {
|
||||
// GIVEN
|
||||
orm, conn := repoTestSetup()
|
||||
orm, conn := testSetup()
|
||||
defer cleanUp("test.db", conn)
|
||||
|
||||
testObj := Test{Int64Field: 54, IntField: 12, StringField: "asdf"}
|
||||
@@ -58,7 +46,7 @@ func TestRepoSelectByPk(t *testing.T) {
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
_, err = insertExec.Execute(conn)
|
||||
err = insertExec.Execute(conn)
|
||||
repo := repository.NewRepository[Test](conn, orm)
|
||||
|
||||
// WHEN
|
||||
@@ -74,7 +62,7 @@ func TestRepoSelectByPk(t *testing.T) {
|
||||
|
||||
func TestRepoSelectByCompundPk(t *testing.T) {
|
||||
// GIVEN
|
||||
orm, conn := repoTestSetup()
|
||||
orm, conn := testSetup()
|
||||
defer cleanUp("test.db", conn)
|
||||
|
||||
testObj := TestWithCompositePk{ID: 42069, Name: "Test"}
|
||||
@@ -84,7 +72,7 @@ func TestRepoSelectByCompundPk(t *testing.T) {
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
_, err = insertExec.Execute(conn)
|
||||
err = insertExec.Execute(conn)
|
||||
repo := repository.NewRepository[TestWithCompositePk](conn, orm)
|
||||
|
||||
// WHEN
|
||||
@@ -100,46 +88,45 @@ func TestRepoSelectByCompundPk(t *testing.T) {
|
||||
|
||||
func TestRepoSave(t *testing.T) {
|
||||
// GIVEN
|
||||
orm, conn := repoTestSetup()
|
||||
orm, conn := testSetup()
|
||||
defer cleanUp("test.db", conn)
|
||||
|
||||
testObj := Test{Int64Field: 54, IntField: 12, StringField: "asdf"}
|
||||
testObj := &Test{Int64Field: 54, IntField: 12, StringField: "asdf"}
|
||||
repo := repository.NewRepository[Test](conn, orm)
|
||||
|
||||
// WHEN
|
||||
repo.Save(testObj)
|
||||
err := repo.Save(testObj)
|
||||
|
||||
// THEN
|
||||
res := repo.SelectByPk(1)
|
||||
if res == nil {
|
||||
log.LogError("TestRepoSave test failed. Result is nil!")
|
||||
if err != nil {
|
||||
log.LogError("TestRepoSave failed: %s", err)
|
||||
}
|
||||
if testObj.ID == 0 {
|
||||
log.LogError("TestRepoSave failed! testObj.ID is 0!")
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepoDelete(t *testing.T) {
|
||||
// GIVEN
|
||||
orm, conn := repoTestSetup()
|
||||
orm, conn := testSetup()
|
||||
defer cleanUp("test.db", conn)
|
||||
|
||||
testObj := Test{Int64Field: 54, IntField: 12, StringField: "asdf"}
|
||||
testObj := &Test{Int64Field: 54, IntField: 12, StringField: "asdf"}
|
||||
repo := repository.NewRepository[Test](conn, orm)
|
||||
|
||||
repo.Save(testObj)
|
||||
|
||||
res := repo.SelectByPk(1)
|
||||
if res == nil {
|
||||
err := repo.Save(testObj)
|
||||
if err != nil {
|
||||
log.LogError("TestRepoDelete setup failed! Test data not saved!")
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
// WHEN
|
||||
repo.Delete(*res)
|
||||
repo.Delete(testObj)
|
||||
|
||||
// THEN
|
||||
res = repo.SelectByPk(1)
|
||||
res := repo.SelectByPk(testObj.ID)
|
||||
if res != nil {
|
||||
log.LogError("TestRepoDelete test failed. Result is not nil!")
|
||||
t.Fail()
|
||||
|
||||
Reference in New Issue
Block a user