Repository impl & tests, move tests, comp. pk insert support
Go Tests / test (push) Failing after 8s
Go Tests / test (push) Failing after 8s
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
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()
|
||||
defer cleanUp("test.db", conn)
|
||||
|
||||
testObj := Test{Int64Field: 54, IntField: 12, StringField: "asdf"}
|
||||
insertExec, err := exec.NewInsert(orm, testObj)
|
||||
if err != nil {
|
||||
log.LogError("TestRepoSelectAll setup failed: %s", err)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
_, err = insertExec.Execute(conn)
|
||||
repo := repository.NewRepository[Test](conn, orm)
|
||||
|
||||
// WHEN
|
||||
res := repo.SelectAll()
|
||||
|
||||
// THEN
|
||||
if len(res) != 1 {
|
||||
log.LogError("TestRepoSelectAll test failed. Expected: 1, Actual: %s", len(res))
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepoSelectByPk(t *testing.T) {
|
||||
// GIVEN
|
||||
orm, conn := repoTestSetup()
|
||||
defer cleanUp("test.db", conn)
|
||||
|
||||
testObj := Test{Int64Field: 54, IntField: 12, StringField: "asdf"}
|
||||
insertExec, err := exec.NewInsert(orm, testObj)
|
||||
if err != nil {
|
||||
log.LogError("TestRepoSelectByPk setup failed: %s", err)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
_, err = insertExec.Execute(conn)
|
||||
repo := repository.NewRepository[Test](conn, orm)
|
||||
|
||||
// WHEN
|
||||
res := repo.SelectByPk(1)
|
||||
|
||||
// THEN
|
||||
if res == nil {
|
||||
log.LogError("TestRepoSelectByPk test failed. Result is nil!")
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepoSelectByCompundPk(t *testing.T) {
|
||||
// GIVEN
|
||||
orm, conn := repoTestSetup()
|
||||
defer cleanUp("test.db", conn)
|
||||
|
||||
testObj := TestWithCompositePk{ID: 42069, Name: "Test"}
|
||||
insertExec, err := exec.NewInsert(orm, testObj)
|
||||
if err != nil {
|
||||
log.LogError("TestRepoSelectByCompundPk setup failed: %s", err)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
_, err = insertExec.Execute(conn)
|
||||
repo := repository.NewRepository[TestWithCompositePk](conn, orm)
|
||||
|
||||
// WHEN
|
||||
res := repo.SelectByPk(42069, "Test")
|
||||
|
||||
// THEN
|
||||
if res == nil {
|
||||
log.LogError("TestRepoSelectByCompundPk test failed. Result is nil!")
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepoSave(t *testing.T) {
|
||||
// GIVEN
|
||||
orm, conn := repoTestSetup()
|
||||
defer cleanUp("test.db", conn)
|
||||
|
||||
testObj := Test{Int64Field: 54, IntField: 12, StringField: "asdf"}
|
||||
repo := repository.NewRepository[Test](conn, orm)
|
||||
|
||||
// WHEN
|
||||
repo.Save(testObj)
|
||||
|
||||
// THEN
|
||||
res := repo.SelectByPk(1)
|
||||
if res == nil {
|
||||
log.LogError("TestRepoSave test failed. Result is nil!")
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepoDelete(t *testing.T) {
|
||||
// GIVEN
|
||||
orm, conn := repoTestSetup()
|
||||
defer cleanUp("test.db", conn)
|
||||
|
||||
testObj := Test{Int64Field: 54, IntField: 12, StringField: "asdf"}
|
||||
repo := repository.NewRepository[Test](conn, orm)
|
||||
|
||||
repo.Save(testObj)
|
||||
|
||||
res := repo.SelectByPk(1)
|
||||
if res == nil {
|
||||
log.LogError("TestRepoDelete setup failed! Test data not saved!")
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
// WHEN
|
||||
repo.Delete(*res)
|
||||
|
||||
// THEN
|
||||
res = repo.SelectByPk(1)
|
||||
if res != nil {
|
||||
log.LogError("TestRepoDelete test failed. Result is not nil!")
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user