115 lines
2.8 KiB
Go
115 lines
2.8 KiB
Go
package simpleorm_test
|
|
|
|
import (
|
|
"os"
|
|
"simpleorm"
|
|
"testing"
|
|
|
|
log "gitlab.com/gdulai/simpleloglvl"
|
|
)
|
|
|
|
func TestSelectEmpty(t *testing.T) {
|
|
// GIVEN
|
|
conn := simpleorm.OpenConnection("sqlite3", "file:test.db")
|
|
defer cleanUp("test.db", conn)
|
|
|
|
orm := simpleorm.NewORM(Test{}, TestWithFk{}, TestWithFkAndFkId{},
|
|
TestWithCompositePk{}, TestWithCompositeFk{})
|
|
|
|
simpleorm.InitDB(conn, orm)
|
|
|
|
exec := simpleorm.CreateExecution[Test](orm, conn)
|
|
// WHEN
|
|
res, err := exec.Select(Test{}, "")
|
|
// THEN
|
|
if err != nil {
|
|
log.LogError("Select failure. %s", err)
|
|
t.Fail()
|
|
}
|
|
if len(res) > 0 {
|
|
log.LogError("Expected empty result.")
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
func TestInsertAndSelectSingle(t *testing.T) {
|
|
// GIVEN
|
|
conn := simpleorm.OpenConnection("sqlite3", "file:test.db")
|
|
defer cleanUp("test.db", conn)
|
|
|
|
orm := simpleorm.NewORM(Test{}, TestWithFk{}, TestWithFkAndFkId{},
|
|
TestWithCompositePk{}, TestWithCompositeFk{})
|
|
|
|
simpleorm.InitDB(conn, orm)
|
|
|
|
exec := simpleorm.CreateExecution[Test](orm, conn)
|
|
|
|
testObj := Test{Int64Field: 54, IntField: 12, StringField: "asdf"}
|
|
err := exec.Insert(testObj)
|
|
if err != nil {
|
|
log.LogError("Insert failure. %s", err)
|
|
t.Fail()
|
|
}
|
|
// WHEN
|
|
res, err := exec.Select(Test{}, "")
|
|
// THEN
|
|
if err != nil {
|
|
log.LogError("Select failure. %s", err)
|
|
t.Fail()
|
|
}
|
|
if len(res) != 1 {
|
|
log.LogError("Selec test failed. Expected: 1, Actual: %s", len(res))
|
|
t.Fail()
|
|
}
|
|
|
|
singleRes := res[0]
|
|
if singleRes.ID != 1 || singleRes.Int64Field != 54 || singleRes.IntField != 12 || singleRes.StringField != "asdf" {
|
|
log.LogError("Invalid result. Expected: %s, Actual: %s", testObj, singleRes)
|
|
t.Fail()
|
|
}
|
|
|
|
}
|
|
|
|
func TestInsertMultipleAndSelectWithParam(t *testing.T) {
|
|
// GIVEN
|
|
conn := simpleorm.OpenConnection("sqlite3", "file:test.db")
|
|
defer cleanUp("test.db", conn)
|
|
|
|
orm := simpleorm.NewORM(Test{}, TestWithFk{}, TestWithFkAndFkId{},
|
|
TestWithCompositePk{}, TestWithCompositeFk{})
|
|
|
|
simpleorm.InitDB(conn, orm)
|
|
|
|
exec := simpleorm.CreateExecution[Test](orm, conn)
|
|
|
|
testObj := Test{Int64Field: 54, IntField: 12, StringField: "asdf"}
|
|
testObjSecond := Test{Int64Field: 12, IntField: 54, StringField: "fdsa"}
|
|
err := exec.Insert(testObj, testObjSecond)
|
|
if err != nil {
|
|
log.LogError("Insert failure. %s", err)
|
|
t.Fail()
|
|
}
|
|
// WHEN
|
|
res, err := exec.Select(Test{}, "string_field = ?", "fdsa")
|
|
// THEN
|
|
if err != nil {
|
|
log.LogError("Select failure. %s", err)
|
|
t.Fail()
|
|
}
|
|
if len(res) != 1 {
|
|
log.LogError("Select test failed. Expected: 1, Actual: %s", len(res))
|
|
t.Fail()
|
|
}
|
|
|
|
singleRes := res[0]
|
|
if singleRes.ID != 2 || singleRes.Int64Field != 12 || singleRes.IntField != 54 || singleRes.StringField != "fdsa" {
|
|
log.LogError("Invalid result. Expected: %s, Actual: %s", testObjSecond, singleRes)
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
func cleanUp(dbFile string, conn *simpleorm.DBConnection) {
|
|
conn.Close()
|
|
os.Remove(dbFile)
|
|
}
|