You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
3.6 KiB
136 lines
3.6 KiB
package common
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"git.devices.local/mawas/golang-api-skeleton/lib/cache"
|
|
"github.com/oklog/ulid/v2"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
const (
|
|
username = "unittest"
|
|
userID = "01F5Z7CFER2D9QH2VY2V7PVQHF"
|
|
)
|
|
|
|
func mock(t *testing.T) (*gorm.DB, cache.Cache) {
|
|
db, err := gorm.Open(
|
|
sqlite.Open(":memory:"),
|
|
&gorm.Config{DisableForeignKeyConstraintWhenMigrating: true},
|
|
)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
appCache, err := cache.Bootstrap()
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if err := appCache.Set("user:"+userID, username); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if err := appCache.Set("user:"+username, userID); err != nil {
|
|
t.Error(err)
|
|
}
|
|
return db, appCache
|
|
}
|
|
|
|
func basicfieldsMock(t *testing.T) (*gorm.DB, cache.Cache, BasicFields) {
|
|
db, appCache := mock(t)
|
|
return db, appCache, BasicFields{}
|
|
}
|
|
|
|
func modelGUIDPKMock(t *testing.T) (*gorm.DB, cache.Cache, ModelGUIDPK) {
|
|
db, appCache := mock(t)
|
|
return db, appCache, ModelGUIDPK{}
|
|
}
|
|
|
|
func modelHiddenGUIDPKMock(t *testing.T) (*gorm.DB, cache.Cache, ModelHiddenGUIDPK) {
|
|
db, appCache := mock(t)
|
|
return db, appCache, ModelHiddenGUIDPK{}
|
|
}
|
|
|
|
func TestBasicFieldsBeforeCreate(t *testing.T) {
|
|
db, _, basicFields := basicfieldsMock(t)
|
|
err := basicFields.BeforeCreate(db.Set("userID", userID))
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if basicFields.CreatedByDB.String() != userID {
|
|
t.Error("BeforeCreate doesn't set CreatedByDB")
|
|
}
|
|
if basicFields.UpdatedByDB.String() != userID {
|
|
t.Error("BeforeCreate doesn't set UpdatedByDB")
|
|
}
|
|
}
|
|
|
|
func TestBasicFieldsAfterCreate(t *testing.T) {
|
|
db, _, basicFields := basicfieldsMock(t)
|
|
err := basicFields.AfterCreate(db.Set("username", username))
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if basicFields.CreatedByJSON != username {
|
|
t.Error("BasicFields AfterCreate doesn't set CreatedByJSON")
|
|
}
|
|
if basicFields.UpdatedByJSON != username {
|
|
t.Error("BasicFields AfterCreate doesn't set UpdatedByJSON")
|
|
}
|
|
}
|
|
|
|
func TestModelGUIDPKBeforeCreate(t *testing.T) {
|
|
db, _, modelGUIDPK := modelGUIDPKMock(t)
|
|
err := modelGUIDPK.BeforeCreate(db.Set("userID", userID))
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if modelGUIDPK.CreatedByDB.String() != userID {
|
|
t.Error("ModelGUIDPK BeforeCreate doesn't set CreatedByDB")
|
|
}
|
|
if modelGUIDPK.UpdatedByDB.String() != userID {
|
|
t.Error("ModelGUIDPK BeforeCreate doesn't set UpdatedByDB")
|
|
}
|
|
if _, err := ulid.Parse(modelGUIDPK.ID.String()); err != nil {
|
|
t.Error("ModelGUIDPK BeforeCreate doesn't set proper ID ->", err.Error())
|
|
}
|
|
jsonString, err := json.Marshal(modelGUIDPK)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
var j map[string]interface{}
|
|
if err := json.Unmarshal([]byte(jsonString), &j); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if _, exists := j["id"]; !exists {
|
|
t.Error("ModelGUIDPK ID must be visible in json")
|
|
}
|
|
}
|
|
|
|
func TestModelHiddenGUIDPKBeforeCreate(t *testing.T) {
|
|
db, _, modelHiddenGUIDPK := modelHiddenGUIDPKMock(t)
|
|
err := modelHiddenGUIDPK.BeforeCreate(db.Set("userID", userID))
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if modelHiddenGUIDPK.CreatedByDB.String() != userID {
|
|
t.Error("ModelHiddenGUIDPK BeforeCreate doesn't set CreatedByDB")
|
|
}
|
|
if modelHiddenGUIDPK.UpdatedByDB.String() != userID {
|
|
t.Error("ModelHiddenGUIDPK BeforeCreate doesn't set UpdatedByDB")
|
|
}
|
|
if _, err := ulid.Parse(modelHiddenGUIDPK.ID.String()); err != nil {
|
|
t.Error("ModelHiddenGUIDPK BeforeCreate doesn't set proper ID ->", err.Error())
|
|
}
|
|
jsonString, err := json.Marshal(modelHiddenGUIDPK)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
var j map[string]interface{}
|
|
if err := json.Unmarshal([]byte(jsonString), &j); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if _, exists := j["id"]; exists {
|
|
t.Error("ModelGUIDPK ID must be hidden in json")
|
|
}
|
|
}
|