mirror of
https://github.com/astaxie/beego.git
synced 2024-11-13 06:10:54 +00:00
Fix orm test when using Driver = mysql
This commit is contained in:
parent
9d936c58bf
commit
192a278a2a
@ -1,7 +1,7 @@
|
||||
language: go
|
||||
|
||||
go:
|
||||
- "1.13.x"
|
||||
- "1.14.x"
|
||||
services:
|
||||
- redis-server
|
||||
- mysql
|
||||
@ -63,7 +63,7 @@ after_script:
|
||||
- killall -w ssdb-server
|
||||
- rm -rf ./res/var/*
|
||||
script:
|
||||
- go test -v ./...
|
||||
- go test ./...
|
||||
- staticcheck -show-ignored -checks "-ST1017,-U1000,-ST1005,-S1034,-S1012,-SA4006,-SA6005,-SA1019,-SA1024"
|
||||
- unconvert $(go list ./... | grep -v /vendor/)
|
||||
- ineffassign .
|
||||
|
6
cache/redis/redis_test.go
vendored
6
cache/redis/redis_test.go
vendored
@ -21,6 +21,7 @@ import (
|
||||
|
||||
"github.com/astaxie/beego/cache"
|
||||
"github.com/gomodule/redigo/redis"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRedisCache(t *testing.T) {
|
||||
@ -124,9 +125,8 @@ func TestCache_Scan(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Error("scan Error", err)
|
||||
}
|
||||
if len(keys) != 10000 {
|
||||
t.Error("scan all err")
|
||||
}
|
||||
|
||||
assert.Equal(t, 10000, len(keys), "scan all error")
|
||||
|
||||
// clear all
|
||||
if err = bm.ClearAll(); err != nil {
|
||||
|
2
go.mod
2
go.mod
@ -37,4 +37,4 @@ replace golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85 => github.com/gol
|
||||
|
||||
replace gopkg.in/yaml.v2 v2.2.1 => github.com/go-yaml/yaml v0.0.0-20180328195020-5420a8b6744d
|
||||
|
||||
go 1.13
|
||||
go 1.14
|
||||
|
@ -178,9 +178,9 @@ func getDbCreateSQL(al *alias) (sqls []string, tableIndexes map[string][]dbIndex
|
||||
column += " " + "NOT NULL"
|
||||
}
|
||||
|
||||
//if fi.initial.String() != "" {
|
||||
// if fi.initial.String() != "" {
|
||||
// column += " DEFAULT " + fi.initial.String()
|
||||
//}
|
||||
// }
|
||||
|
||||
// Append attribute DEFAULT
|
||||
column += getColumnDefault(fi)
|
||||
@ -197,9 +197,9 @@ func getDbCreateSQL(al *alias) (sqls []string, tableIndexes map[string][]dbIndex
|
||||
if strings.Contains(column, "%COL%") {
|
||||
column = strings.Replace(column, "%COL%", fi.column, -1)
|
||||
}
|
||||
|
||||
if fi.description != "" && al.Driver!=DRSqlite {
|
||||
column += " " + fmt.Sprintf("COMMENT '%s'",fi.description)
|
||||
|
||||
if fi.description != "" && al.Driver != DRSqlite {
|
||||
column += " " + fmt.Sprintf("COMMENT '%s'", fi.description)
|
||||
}
|
||||
|
||||
columns = append(columns, column)
|
||||
|
@ -1,497 +0,0 @@
|
||||
// Copyright 2014 beego Author. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package orm
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/lib/pq"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
// As tidb can't use go get, so disable the tidb testing now
|
||||
// _ "github.com/pingcap/tidb"
|
||||
)
|
||||
|
||||
// A slice string field.
|
||||
type SliceStringField []string
|
||||
|
||||
func (e SliceStringField) Value() []string {
|
||||
return []string(e)
|
||||
}
|
||||
|
||||
func (e *SliceStringField) Set(d []string) {
|
||||
*e = SliceStringField(d)
|
||||
}
|
||||
|
||||
func (e *SliceStringField) Add(v string) {
|
||||
*e = append(*e, v)
|
||||
}
|
||||
|
||||
func (e *SliceStringField) String() string {
|
||||
return strings.Join(e.Value(), ",")
|
||||
}
|
||||
|
||||
func (e *SliceStringField) FieldType() int {
|
||||
return TypeVarCharField
|
||||
}
|
||||
|
||||
func (e *SliceStringField) SetRaw(value interface{}) error {
|
||||
switch d := value.(type) {
|
||||
case []string:
|
||||
e.Set(d)
|
||||
case string:
|
||||
if len(d) > 0 {
|
||||
parts := strings.Split(d, ",")
|
||||
v := make([]string, 0, len(parts))
|
||||
for _, p := range parts {
|
||||
v = append(v, strings.TrimSpace(p))
|
||||
}
|
||||
e.Set(v)
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("<SliceStringField.SetRaw> unknown value `%v`", value)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *SliceStringField) RawValue() interface{} {
|
||||
return e.String()
|
||||
}
|
||||
|
||||
var _ Fielder = new(SliceStringField)
|
||||
|
||||
// A json field.
|
||||
type JSONFieldTest struct {
|
||||
Name string
|
||||
Data string
|
||||
}
|
||||
|
||||
func (e *JSONFieldTest) String() string {
|
||||
data, _ := json.Marshal(e)
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func (e *JSONFieldTest) FieldType() int {
|
||||
return TypeTextField
|
||||
}
|
||||
|
||||
func (e *JSONFieldTest) SetRaw(value interface{}) error {
|
||||
switch d := value.(type) {
|
||||
case string:
|
||||
return json.Unmarshal([]byte(d), e)
|
||||
default:
|
||||
return fmt.Errorf("<JSONField.SetRaw> unknown value `%v`", value)
|
||||
}
|
||||
}
|
||||
|
||||
func (e *JSONFieldTest) RawValue() interface{} {
|
||||
return e.String()
|
||||
}
|
||||
|
||||
var _ Fielder = new(JSONFieldTest)
|
||||
|
||||
type Data struct {
|
||||
ID int `orm:"column(id)"`
|
||||
Boolean bool
|
||||
Char string `orm:"size(50)"`
|
||||
Text string `orm:"type(text)"`
|
||||
JSON string `orm:"type(json);default({\"name\":\"json\"})"`
|
||||
Jsonb string `orm:"type(jsonb)"`
|
||||
Time time.Time `orm:"type(time)"`
|
||||
Date time.Time `orm:"type(date)"`
|
||||
DateTime time.Time `orm:"column(datetime)"`
|
||||
Byte byte
|
||||
Rune rune
|
||||
Int int
|
||||
Int8 int8
|
||||
Int16 int16
|
||||
Int32 int32
|
||||
Int64 int64
|
||||
Uint uint
|
||||
Uint8 uint8
|
||||
Uint16 uint16
|
||||
Uint32 uint32
|
||||
Uint64 uint64
|
||||
Float32 float32
|
||||
Float64 float64
|
||||
Decimal float64 `orm:"digits(8);decimals(4)"`
|
||||
}
|
||||
|
||||
type DataNull struct {
|
||||
ID int `orm:"column(id)"`
|
||||
Boolean bool `orm:"null"`
|
||||
Char string `orm:"null;size(50)"`
|
||||
Text string `orm:"null;type(text)"`
|
||||
JSON string `orm:"type(json);null"`
|
||||
Jsonb string `orm:"type(jsonb);null"`
|
||||
Time time.Time `orm:"null;type(time)"`
|
||||
Date time.Time `orm:"null;type(date)"`
|
||||
DateTime time.Time `orm:"null;column(datetime)"`
|
||||
Byte byte `orm:"null"`
|
||||
Rune rune `orm:"null"`
|
||||
Int int `orm:"null"`
|
||||
Int8 int8 `orm:"null"`
|
||||
Int16 int16 `orm:"null"`
|
||||
Int32 int32 `orm:"null"`
|
||||
Int64 int64 `orm:"null"`
|
||||
Uint uint `orm:"null"`
|
||||
Uint8 uint8 `orm:"null"`
|
||||
Uint16 uint16 `orm:"null"`
|
||||
Uint32 uint32 `orm:"null"`
|
||||
Uint64 uint64 `orm:"null"`
|
||||
Float32 float32 `orm:"null"`
|
||||
Float64 float64 `orm:"null"`
|
||||
Decimal float64 `orm:"digits(8);decimals(4);null"`
|
||||
NullString sql.NullString `orm:"null"`
|
||||
NullBool sql.NullBool `orm:"null"`
|
||||
NullFloat64 sql.NullFloat64 `orm:"null"`
|
||||
NullInt64 sql.NullInt64 `orm:"null"`
|
||||
BooleanPtr *bool `orm:"null"`
|
||||
CharPtr *string `orm:"null;size(50)"`
|
||||
TextPtr *string `orm:"null;type(text)"`
|
||||
BytePtr *byte `orm:"null"`
|
||||
RunePtr *rune `orm:"null"`
|
||||
IntPtr *int `orm:"null"`
|
||||
Int8Ptr *int8 `orm:"null"`
|
||||
Int16Ptr *int16 `orm:"null"`
|
||||
Int32Ptr *int32 `orm:"null"`
|
||||
Int64Ptr *int64 `orm:"null"`
|
||||
UintPtr *uint `orm:"null"`
|
||||
Uint8Ptr *uint8 `orm:"null"`
|
||||
Uint16Ptr *uint16 `orm:"null"`
|
||||
Uint32Ptr *uint32 `orm:"null"`
|
||||
Uint64Ptr *uint64 `orm:"null"`
|
||||
Float32Ptr *float32 `orm:"null"`
|
||||
Float64Ptr *float64 `orm:"null"`
|
||||
DecimalPtr *float64 `orm:"digits(8);decimals(4);null"`
|
||||
TimePtr *time.Time `orm:"null;type(time)"`
|
||||
DatePtr *time.Time `orm:"null;type(date)"`
|
||||
DateTimePtr *time.Time `orm:"null"`
|
||||
}
|
||||
|
||||
type String string
|
||||
type Boolean bool
|
||||
type Byte byte
|
||||
type Rune rune
|
||||
type Int int
|
||||
type Int8 int8
|
||||
type Int16 int16
|
||||
type Int32 int32
|
||||
type Int64 int64
|
||||
type Uint uint
|
||||
type Uint8 uint8
|
||||
type Uint16 uint16
|
||||
type Uint32 uint32
|
||||
type Uint64 uint64
|
||||
type Float32 float64
|
||||
type Float64 float64
|
||||
|
||||
type DataCustom struct {
|
||||
ID int `orm:"column(id)"`
|
||||
Boolean Boolean
|
||||
Char string `orm:"size(50)"`
|
||||
Text string `orm:"type(text)"`
|
||||
Byte Byte
|
||||
Rune Rune
|
||||
Int Int
|
||||
Int8 Int8
|
||||
Int16 Int16
|
||||
Int32 Int32
|
||||
Int64 Int64
|
||||
Uint Uint
|
||||
Uint8 Uint8
|
||||
Uint16 Uint16
|
||||
Uint32 Uint32
|
||||
Uint64 Uint64
|
||||
Float32 Float32
|
||||
Float64 Float64
|
||||
Decimal Float64 `orm:"digits(8);decimals(4)"`
|
||||
}
|
||||
|
||||
// only for mysql
|
||||
type UserBig struct {
|
||||
ID uint64 `orm:"column(id)"`
|
||||
Name string
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID int `orm:"column(id)"`
|
||||
UserName string `orm:"size(30);unique"`
|
||||
Email string `orm:"size(100)"`
|
||||
Password string `orm:"size(100)"`
|
||||
Status int16 `orm:"column(Status)"`
|
||||
IsStaff bool
|
||||
IsActive bool `orm:"default(true)"`
|
||||
Created time.Time `orm:"auto_now_add;type(date)"`
|
||||
Updated time.Time `orm:"auto_now"`
|
||||
Profile *Profile `orm:"null;rel(one);on_delete(set_null)"`
|
||||
Posts []*Post `orm:"reverse(many)" json:"-"`
|
||||
ShouldSkip string `orm:"-"`
|
||||
Nums int
|
||||
Langs SliceStringField `orm:"size(100)"`
|
||||
Extra JSONFieldTest `orm:"type(text)"`
|
||||
unexport bool `orm:"-"`
|
||||
unexportBool bool
|
||||
}
|
||||
|
||||
func (u *User) TableIndex() [][]string {
|
||||
return [][]string{
|
||||
{"Id", "UserName"},
|
||||
{"Id", "Created"},
|
||||
}
|
||||
}
|
||||
|
||||
func (u *User) TableUnique() [][]string {
|
||||
return [][]string{
|
||||
{"UserName", "Email"},
|
||||
}
|
||||
}
|
||||
|
||||
func NewUser() *User {
|
||||
obj := new(User)
|
||||
return obj
|
||||
}
|
||||
|
||||
type Profile struct {
|
||||
ID int `orm:"column(id)"`
|
||||
Age int16
|
||||
Money float64
|
||||
User *User `orm:"reverse(one)" json:"-"`
|
||||
BestPost *Post `orm:"rel(one);null"`
|
||||
}
|
||||
|
||||
func (u *Profile) TableName() string {
|
||||
return "user_profile"
|
||||
}
|
||||
|
||||
func NewProfile() *Profile {
|
||||
obj := new(Profile)
|
||||
return obj
|
||||
}
|
||||
|
||||
type Post struct {
|
||||
ID int `orm:"column(id)"`
|
||||
User *User `orm:"rel(fk)"`
|
||||
Title string `orm:"size(60)"`
|
||||
Content string `orm:"type(text)"`
|
||||
Created time.Time `orm:"auto_now_add"`
|
||||
Updated time.Time `orm:"auto_now"`
|
||||
Tags []*Tag `orm:"rel(m2m);rel_through(github.com/astaxie/beego/orm.PostTags)"`
|
||||
}
|
||||
|
||||
func (u *Post) TableIndex() [][]string {
|
||||
return [][]string{
|
||||
{"Id", "Created"},
|
||||
}
|
||||
}
|
||||
|
||||
func NewPost() *Post {
|
||||
obj := new(Post)
|
||||
return obj
|
||||
}
|
||||
|
||||
type Tag struct {
|
||||
ID int `orm:"column(id)"`
|
||||
Name string `orm:"size(30)"`
|
||||
BestPost *Post `orm:"rel(one);null"`
|
||||
Posts []*Post `orm:"reverse(many)" json:"-"`
|
||||
}
|
||||
|
||||
func NewTag() *Tag {
|
||||
obj := new(Tag)
|
||||
return obj
|
||||
}
|
||||
|
||||
type PostTags struct {
|
||||
ID int `orm:"column(id)"`
|
||||
Post *Post `orm:"rel(fk)"`
|
||||
Tag *Tag `orm:"rel(fk)"`
|
||||
}
|
||||
|
||||
func (m *PostTags) TableName() string {
|
||||
return "prefix_post_tags"
|
||||
}
|
||||
|
||||
type Comment struct {
|
||||
ID int `orm:"column(id)"`
|
||||
Post *Post `orm:"rel(fk);column(post)"`
|
||||
Content string `orm:"type(text)"`
|
||||
Parent *Comment `orm:"null;rel(fk)"`
|
||||
Created time.Time `orm:"auto_now_add"`
|
||||
}
|
||||
|
||||
func NewComment() *Comment {
|
||||
obj := new(Comment)
|
||||
return obj
|
||||
}
|
||||
|
||||
type Group struct {
|
||||
ID int `orm:"column(gid);size(32)"`
|
||||
Name string
|
||||
Permissions []*Permission `orm:"reverse(many)" json:"-"`
|
||||
}
|
||||
|
||||
type Permission struct {
|
||||
ID int `orm:"column(id)"`
|
||||
Name string
|
||||
Groups []*Group `orm:"rel(m2m);rel_through(github.com/astaxie/beego/orm.GroupPermissions)"`
|
||||
}
|
||||
|
||||
type GroupPermissions struct {
|
||||
ID int `orm:"column(id)"`
|
||||
Group *Group `orm:"rel(fk)"`
|
||||
Permission *Permission `orm:"rel(fk)"`
|
||||
}
|
||||
|
||||
type ModelID struct {
|
||||
ID int64
|
||||
}
|
||||
|
||||
type ModelBase struct {
|
||||
ModelID
|
||||
|
||||
Created time.Time `orm:"auto_now_add;type(datetime)"`
|
||||
Updated time.Time `orm:"auto_now;type(datetime)"`
|
||||
}
|
||||
|
||||
type InLine struct {
|
||||
// Common Fields
|
||||
ModelBase
|
||||
|
||||
// Other Fields
|
||||
Name string `orm:"unique"`
|
||||
Email string
|
||||
}
|
||||
|
||||
func NewInLine() *InLine {
|
||||
return new(InLine)
|
||||
}
|
||||
|
||||
type InLineOneToOne struct {
|
||||
// Common Fields
|
||||
ModelBase
|
||||
|
||||
Note string
|
||||
InLine *InLine `orm:"rel(fk);column(inline)"`
|
||||
}
|
||||
|
||||
func NewInLineOneToOne() *InLineOneToOne {
|
||||
return new(InLineOneToOne)
|
||||
}
|
||||
|
||||
type IntegerPk struct {
|
||||
ID int64 `orm:"pk"`
|
||||
Value string
|
||||
}
|
||||
|
||||
type UintPk struct {
|
||||
ID uint32 `orm:"pk"`
|
||||
Name string
|
||||
}
|
||||
|
||||
type PtrPk struct {
|
||||
ID *IntegerPk `orm:"pk;rel(one)"`
|
||||
Positive bool
|
||||
}
|
||||
|
||||
var DBARGS = struct {
|
||||
Driver string
|
||||
Source string
|
||||
Debug string
|
||||
}{
|
||||
os.Getenv("ORM_DRIVER"),
|
||||
os.Getenv("ORM_SOURCE"),
|
||||
os.Getenv("ORM_DEBUG"),
|
||||
}
|
||||
|
||||
var (
|
||||
IsMysql = DBARGS.Driver == "mysql"
|
||||
IsSqlite = DBARGS.Driver == "sqlite3"
|
||||
IsPostgres = DBARGS.Driver == "postgres"
|
||||
IsTidb = DBARGS.Driver == "tidb"
|
||||
)
|
||||
|
||||
var (
|
||||
dORM Ormer
|
||||
dDbBaser dbBaser
|
||||
)
|
||||
|
||||
var (
|
||||
helpinfo = `need driver and source!
|
||||
|
||||
Default DB Drivers.
|
||||
|
||||
driver: url
|
||||
mysql: https://github.com/go-sql-driver/mysql
|
||||
sqlite3: https://github.com/mattn/go-sqlite3
|
||||
postgres: https://github.com/lib/pq
|
||||
tidb: https://github.com/pingcap/tidb
|
||||
|
||||
usage:
|
||||
|
||||
go get -u github.com/astaxie/beego/orm
|
||||
go get -u github.com/go-sql-driver/mysql
|
||||
go get -u github.com/mattn/go-sqlite3
|
||||
go get -u github.com/lib/pq
|
||||
go get -u github.com/pingcap/tidb
|
||||
|
||||
#### MySQL
|
||||
mysql -u root -e 'create database orm_test;'
|
||||
export ORM_DRIVER=mysql
|
||||
export ORM_SOURCE="root:@/orm_test?charset=utf8"
|
||||
go test -v github.com/astaxie/beego/orm
|
||||
|
||||
|
||||
#### Sqlite3
|
||||
export ORM_DRIVER=sqlite3
|
||||
export ORM_SOURCE='file:memory_test?mode=memory'
|
||||
go test -v github.com/astaxie/beego/orm
|
||||
|
||||
|
||||
#### PostgreSQL
|
||||
psql -c 'create database orm_test;' -U postgres
|
||||
export ORM_DRIVER=postgres
|
||||
export ORM_SOURCE="user=postgres dbname=orm_test sslmode=disable"
|
||||
go test -v github.com/astaxie/beego/orm
|
||||
|
||||
#### TiDB
|
||||
export ORM_DRIVER=tidb
|
||||
export ORM_SOURCE='memory://test/test'
|
||||
go test -v github.com/astaxie/beego/orm
|
||||
|
||||
`
|
||||
)
|
||||
|
||||
func init() {
|
||||
Debug, _ = StrTo(DBARGS.Debug).Bool()
|
||||
|
||||
if DBARGS.Driver == "" || DBARGS.Source == "" {
|
||||
fmt.Println(helpinfo)
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
RegisterDataBase("default", DBARGS.Driver, DBARGS.Source, 20)
|
||||
|
||||
alias := getDbAlias("default")
|
||||
if alias.Driver == DRMySQL {
|
||||
alias.Engine = "INNODB"
|
||||
}
|
||||
|
||||
}
|
2494
orm/orm_test.go
2494
orm/orm_test.go
File diff suppressed because it is too large
Load Diff
@ -1,70 +0,0 @@
|
||||
// Copyright 2014 beego Author. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package orm
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCamelString(t *testing.T) {
|
||||
snake := []string{"pic_url", "hello_world_", "hello__World", "_HelLO_Word", "pic_url_1", "pic_url__1"}
|
||||
camel := []string{"PicUrl", "HelloWorld", "HelloWorld", "HelLOWord", "PicUrl1", "PicUrl1"}
|
||||
|
||||
answer := make(map[string]string)
|
||||
for i, v := range snake {
|
||||
answer[v] = camel[i]
|
||||
}
|
||||
|
||||
for _, v := range snake {
|
||||
res := camelString(v)
|
||||
if res != answer[v] {
|
||||
t.Error("Unit Test Fail:", v, res, answer[v])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnakeString(t *testing.T) {
|
||||
camel := []string{"PicUrl", "HelloWorld", "HelloWorld", "HelLOWord", "PicUrl1", "XyXX"}
|
||||
snake := []string{"pic_url", "hello_world", "hello_world", "hel_l_o_word", "pic_url1", "xy_x_x"}
|
||||
|
||||
answer := make(map[string]string)
|
||||
for i, v := range camel {
|
||||
answer[v] = snake[i]
|
||||
}
|
||||
|
||||
for _, v := range camel {
|
||||
res := snakeString(v)
|
||||
if res != answer[v] {
|
||||
t.Error("Unit Test Fail:", v, res, answer[v])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnakeStringWithAcronym(t *testing.T) {
|
||||
camel := []string{"ID", "PicURL", "HelloWorld", "HelloWorld", "HelLOWord", "PicUrl1", "XyXX"}
|
||||
snake := []string{"id", "pic_url", "hello_world", "hello_world", "hel_lo_word", "pic_url1", "xy_xx"}
|
||||
|
||||
answer := make(map[string]string)
|
||||
for i, v := range camel {
|
||||
answer[v] = snake[i]
|
||||
}
|
||||
|
||||
for _, v := range camel {
|
||||
res := snakeStringWithAcronym(v)
|
||||
if res != answer[v] {
|
||||
t.Error("Unit Test Fail:", v, res, answer[v])
|
||||
}
|
||||
}
|
||||
}
|
@ -293,7 +293,7 @@ type Post struct {
|
||||
Content string `orm:"type(text)"`
|
||||
Created time.Time `orm:"auto_now_add"`
|
||||
Updated time.Time `orm:"auto_now"`
|
||||
Tags []*Tag `orm:"rel(m2m);rel_through(github.com/astaxie/beego/orm.PostTags)"`
|
||||
Tags []*Tag `orm:"rel(m2m);rel_through(github.com/astaxie/beego/pkg/orm.PostTags)"`
|
||||
}
|
||||
|
||||
func (u *Post) TableIndex() [][]string {
|
||||
@ -351,7 +351,7 @@ type Group struct {
|
||||
type Permission struct {
|
||||
ID int `orm:"column(id)"`
|
||||
Name string
|
||||
Groups []*Group `orm:"rel(m2m);rel_through(github.com/astaxie/beego/orm.GroupPermissions)"`
|
||||
Groups []*Group `orm:"rel(m2m);rel_through(github.com/astaxie/beego/pkg/orm.GroupPermissions)"`
|
||||
}
|
||||
|
||||
type GroupPermissions struct {
|
||||
@ -446,7 +446,7 @@ var (
|
||||
|
||||
usage:
|
||||
|
||||
go get -u github.com/astaxie/beego/orm
|
||||
go get -u github.com/astaxie/beego/pkg/orm
|
||||
go get -u github.com/go-sql-driver/mysql
|
||||
go get -u github.com/mattn/go-sqlite3
|
||||
go get -u github.com/lib/pq
|
||||
@ -456,25 +456,25 @@ var (
|
||||
mysql -u root -e 'create database orm_test;'
|
||||
export ORM_DRIVER=mysql
|
||||
export ORM_SOURCE="root:@/orm_test?charset=utf8"
|
||||
go test -v github.com/astaxie/beego/orm
|
||||
go test -v github.com/astaxie/beego/pkg/orm
|
||||
|
||||
|
||||
#### Sqlite3
|
||||
export ORM_DRIVER=sqlite3
|
||||
export ORM_SOURCE='file:memory_test?mode=memory'
|
||||
go test -v github.com/astaxie/beego/orm
|
||||
go test -v github.com/astaxie/beego/pkg/orm
|
||||
|
||||
|
||||
#### PostgreSQL
|
||||
psql -c 'create database orm_test;' -U postgres
|
||||
export ORM_DRIVER=postgres
|
||||
export ORM_SOURCE="user=postgres dbname=orm_test sslmode=disable"
|
||||
go test -v github.com/astaxie/beego/orm
|
||||
go test -v github.com/astaxie/beego/pkg/orm
|
||||
|
||||
#### TiDB
|
||||
export ORM_DRIVER=tidb
|
||||
export ORM_SOURCE='memory://test/test'
|
||||
go test -v github.com/astaxie/beego/orm
|
||||
go test -v github.com/astaxie/beego/pgk/orm
|
||||
|
||||
`
|
||||
)
|
||||
@ -487,7 +487,11 @@ func init() {
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
RegisterDataBase("default", DBARGS.Driver, DBARGS.Source, 20)
|
||||
err := RegisterDataBase("default", DBARGS.Driver, DBARGS.Source, 20)
|
||||
|
||||
if err != nil{
|
||||
panic(fmt.Sprintf("can not register database: %v", err))
|
||||
}
|
||||
|
||||
alias := getDbAlias("default")
|
||||
if alias.Driver == DRMySQL {
|
||||
|
@ -21,7 +21,7 @@
|
||||
//
|
||||
// import (
|
||||
// "fmt"
|
||||
// "github.com/astaxie/beego/orm"
|
||||
// "github.com/astaxie/beego/pkg/orm"
|
||||
// _ "github.com/go-sql-driver/mysql" // import your used driver
|
||||
// )
|
||||
//
|
||||
|
@ -30,6 +30,8 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var _ = os.PathSeparator
|
||||
@ -141,6 +143,7 @@ func getCaller(skip int) string {
|
||||
return fmt.Sprintf("%s:%s:%d: \n%s", fn, funName, line, strings.Join(codes, "\n"))
|
||||
}
|
||||
|
||||
// Deprecated: Using stretchr/testify/assert
|
||||
func throwFail(t *testing.T, err error, args ...interface{}) {
|
||||
if err != nil {
|
||||
con := fmt.Sprintf("\t\nError: %s\n%s\n", err.Error(), getCaller(2))
|
||||
@ -455,9 +458,11 @@ func TestNullDataTypes(t *testing.T) {
|
||||
throwFail(t, AssertIs(*d.Float32Ptr, float32Ptr))
|
||||
throwFail(t, AssertIs(*d.Float64Ptr, float64Ptr))
|
||||
throwFail(t, AssertIs(*d.DecimalPtr, decimalPtr))
|
||||
throwFail(t, AssertIs((*d.TimePtr).UTC().Format(testTime), timePtr.UTC().Format(testTime)))
|
||||
throwFail(t, AssertIs((*d.DatePtr).UTC().Format(testDate), datePtr.UTC().Format(testDate)))
|
||||
throwFail(t, AssertIs((*d.DateTimePtr).UTC().Format(testDateTime), dateTimePtr.UTC().Format(testDateTime)))
|
||||
|
||||
// in mysql, there are some precision problem, (*d.TimePtr).UTC() != timePtr.UTC()
|
||||
assert.True(t, (*d.TimePtr).UTC().Sub(timePtr.UTC()) <= time.Second)
|
||||
assert.True(t, (*d.DatePtr).UTC().Sub(datePtr.UTC()) <= time.Second)
|
||||
assert.True(t, (*d.DateTimePtr).UTC().Sub(dateTimePtr.UTC()) <= time.Second)
|
||||
|
||||
// test support for pointer fields using RawSeter.QueryRows()
|
||||
var dnList []*DataNull
|
||||
@ -532,8 +537,9 @@ func TestCRUD(t *testing.T) {
|
||||
throwFail(t, AssertIs(u.Status, 3))
|
||||
throwFail(t, AssertIs(u.IsStaff, true))
|
||||
throwFail(t, AssertIs(u.IsActive, true))
|
||||
throwFail(t, AssertIs(u.Created.In(DefaultTimeLoc), user.Created.In(DefaultTimeLoc), testDate))
|
||||
throwFail(t, AssertIs(u.Updated.In(DefaultTimeLoc), user.Updated.In(DefaultTimeLoc), testDateTime))
|
||||
|
||||
assert.True(t, u.Created.In(DefaultTimeLoc).Sub(user.Created.In(DefaultTimeLoc)) <= time.Second)
|
||||
assert.True(t, u.Updated.In(DefaultTimeLoc).Sub(user.Updated.In(DefaultTimeLoc)) <= time.Second)
|
||||
|
||||
user.UserName = "astaxie"
|
||||
user.Profile = profile
|
||||
@ -1793,7 +1799,7 @@ func TestQueryRows(t *testing.T) {
|
||||
throwFailNow(t, AssertIs(ids[2], 4))
|
||||
throwFailNow(t, AssertIs(usernames[2], "nobody"))
|
||||
|
||||
//test query rows by nested struct
|
||||
// test query rows by nested struct
|
||||
var l []userProfile
|
||||
query = fmt.Sprintf("SELECT * FROM %suser_profile%s LEFT JOIN %suser%s ON %suser_profile%s.%sid%s = %suser%s.%sid%s", Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q)
|
||||
num, err = dORM.Raw(query).QueryRows(&l)
|
||||
@ -2413,7 +2419,7 @@ func TestInsertOrUpdate(t *testing.T) {
|
||||
fmt.Println("sqlite3 is nonsupport")
|
||||
return
|
||||
}
|
||||
//test1
|
||||
// test1
|
||||
_, err := dORM.InsertOrUpdate(&user1, "user_name")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
@ -2425,7 +2431,7 @@ func TestInsertOrUpdate(t *testing.T) {
|
||||
dORM.Read(&test, "user_name")
|
||||
throwFailNow(t, AssertIs(user1.Status, test.Status))
|
||||
}
|
||||
//test2
|
||||
// test2
|
||||
_, err = dORM.InsertOrUpdate(&user2, "user_name")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
@ -2439,11 +2445,11 @@ func TestInsertOrUpdate(t *testing.T) {
|
||||
throwFailNow(t, AssertIs(user2.Password, strings.TrimSpace(test.Password)))
|
||||
}
|
||||
|
||||
//postgres ON CONFLICT DO UPDATE SET can`t use colu=colu+values
|
||||
// postgres ON CONFLICT DO UPDATE SET can`t use colu=colu+values
|
||||
if IsPostgres {
|
||||
return
|
||||
}
|
||||
//test3 +
|
||||
// test3 +
|
||||
_, err = dORM.InsertOrUpdate(&user2, "user_name", "status=status+1")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
@ -2455,7 +2461,7 @@ func TestInsertOrUpdate(t *testing.T) {
|
||||
dORM.Read(&test, "user_name")
|
||||
throwFailNow(t, AssertIs(user2.Status+1, test.Status))
|
||||
}
|
||||
//test4 -
|
||||
// test4 -
|
||||
_, err = dORM.InsertOrUpdate(&user2, "user_name", "status=status-1")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
@ -2467,7 +2473,7 @@ func TestInsertOrUpdate(t *testing.T) {
|
||||
dORM.Read(&test, "user_name")
|
||||
throwFailNow(t, AssertIs((user2.Status+1)-1, test.Status))
|
||||
}
|
||||
//test5 *
|
||||
// test5 *
|
||||
_, err = dORM.InsertOrUpdate(&user2, "user_name", "status=status*3")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
@ -2479,7 +2485,7 @@ func TestInsertOrUpdate(t *testing.T) {
|
||||
dORM.Read(&test, "user_name")
|
||||
throwFailNow(t, AssertIs(((user2.Status+1)-1)*3, test.Status))
|
||||
}
|
||||
//test6 /
|
||||
// test6 /
|
||||
_, err = dORM.InsertOrUpdate(&user2, "user_name", "Status=Status/3")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
Loading…
Reference in New Issue
Block a user