Set db timezone

This commit is contained in:
kolaente 2020-06-27 00:26:54 +02:00
parent d55e9f5d9a
commit 60a51b1f37
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
8 changed files with 47 additions and 1 deletions

1
go.mod
View File

@ -46,6 +46,7 @@ require (
github.com/labstack/gommon v0.3.0
github.com/laurent22/ical-go v0.1.1-0.20181107184520-7e5d6ade8eef
github.com/lib/pq v1.7.0
github.com/magiconair/properties v1.8.1
github.com/mailru/easyjson v0.7.0 // indirect
github.com/mattn/go-sqlite3 v2.0.3+incompatible
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect

View File

@ -72,6 +72,11 @@ func CreateDBEngine() (engine *xorm.Engine, err error) {
log.Fatalf("Unknown database type %s", config.DatabaseType.GetString())
}
loc, err := time.LoadLocation(config.ServiceTimeZone.GetString())
if err != nil {
log.Fatalf("Could not parse timezone '%s': %s", config.ServiceTimeZone.GetString(), err)
}
engine.SetTZLocation(loc)
engine.SetMapper(core.GonicMapper{})
logger := log.NewXormLogger("")
engine.SetLogger(logger)

View File

@ -21,6 +21,7 @@ import (
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/log"
"os"
"time"
"xorm.io/core"
"xorm.io/xorm"
)
@ -49,6 +50,11 @@ func CreateTestEngine() (engine *xorm.Engine, err error) {
logger := log.NewXormLogger("DEBUG")
logger.ShowSQL(os.Getenv("UNIT_TESTS_VERBOSE") == "1")
engine.SetLogger(logger)
loc, err := time.LoadLocation(config.ServiceTimeZone.GetString())
if err != nil {
log.Fatalf("Could not parse timezone '%s': %s", config.ServiceTimeZone.GetString(), err)
}
engine.SetTZLocation(loc)
x = engine
return
}

View File

@ -48,11 +48,15 @@ func TestLabelTask_ReadAll(t *testing.T) {
Label: Label{
ID: 4,
Title: "Label #4 - visible via other task",
Created: testCreatedTime,
Updated: testUpdatedTime,
CreatedByID: 2,
CreatedBy: &user.User{
ID: 2,
Username: "user2",
Password: "$2a$14$dcadBoMBL9jQoOcZK8Fju.cy0Ptx2oZECkKLnaa8ekRoTFe1w7To.",
Created: testCreatedTime,
Updated: testUpdatedTime,
},
},
},

View File

@ -71,6 +71,8 @@ func TestLabel_ReadAll(t *testing.T) {
Title: "Label #1",
CreatedByID: 1,
CreatedBy: user1,
Created: testCreatedTime,
Updated: testUpdatedTime,
},
},
{
@ -79,17 +81,23 @@ func TestLabel_ReadAll(t *testing.T) {
Title: "Label #2",
CreatedByID: 1,
CreatedBy: user1,
Created: testCreatedTime,
Updated: testUpdatedTime,
},
},
{
Label: Label{
ID: 4,
Title: "Label #4 - visible via other task",
Created: testCreatedTime,
Updated: testUpdatedTime,
CreatedByID: 2,
CreatedBy: &user.User{
ID: 2,
Username: "user2",
Password: "$2a$14$dcadBoMBL9jQoOcZK8Fju.cy0Ptx2oZECkKLnaa8ekRoTFe1w7To.",
Created: testCreatedTime,
Updated: testUpdatedTime,
},
},
},

View File

@ -20,12 +20,28 @@ import (
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/files"
"code.vikunja.io/api/pkg/user"
"fmt"
"os"
"testing"
"time"
)
func TestMain(m *testing.M) {
var err error
testCreatedTime, err = time.ParseInLocation(time.RFC3339Nano, "2018-12-01T15:13:12.0+00:00", time.Local)
if err != nil {
fmt.Printf("Error setting up time: %s", err)
os.Exit(1)
}
testCreatedTime = testCreatedTime.Local()
testUpdatedTime, err = time.ParseInLocation(time.RFC3339Nano, "2018-12-02T15:13:12.0+00:00", time.Local)
if err != nil {
fmt.Printf("Error setting up time: %s", err)
os.Exit(1)
}
testUpdatedTime = testUpdatedTime.Local()
// Set default config
config.InitDefaultConfig()
// We need to set the root path even if we're not using the config, otherwise fixtures are not loaded correctly

View File

@ -22,6 +22,7 @@ import (
"code.vikunja.io/api/pkg/log"
_ "github.com/go-sql-driver/mysql" // Because.
_ "github.com/lib/pq" // Because.
"time"
"xorm.io/xorm"
_ "github.com/mattn/go-sqlite3" // Because.
@ -29,6 +30,9 @@ import (
var (
x *xorm.Engine
testCreatedTime time.Time
testUpdatedTime time.Time
)
// GetTables returns all structs which are also a table.

View File

@ -139,7 +139,9 @@ func getUser(user *User, withEmail bool) (userOut *User, err error) {
userOut = &User{} // To prevent a panic if user is nil
*userOut = *user
exists, err := x.Get(userOut)
if err != nil {
return nil, err
}
if !exists {
return &User{}, ErrUserDoesNotExist{UserID: user.ID}
}