orm_test.go 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package orm
  15. import (
  16. "bytes"
  17. "database/sql"
  18. "fmt"
  19. "io/ioutil"
  20. "math"
  21. "os"
  22. "path/filepath"
  23. "reflect"
  24. "runtime"
  25. "strings"
  26. "testing"
  27. "time"
  28. )
  29. var _ = os.PathSeparator
  30. var (
  31. testDate = formatDate + " -0700"
  32. testDateTime = formatDateTime + " -0700"
  33. testTime = formatTime + " -0700"
  34. )
  35. type argAny []interface{}
  36. // get interface by index from interface slice
  37. func (a argAny) Get(i int, args ...interface{}) (r interface{}) {
  38. if i >= 0 && i < len(a) {
  39. r = a[i]
  40. }
  41. if len(args) > 0 {
  42. r = args[0]
  43. }
  44. return
  45. }
  46. func ValuesCompare(is bool, a interface{}, args ...interface{}) (ok bool, err error) {
  47. if len(args) == 0 {
  48. return false, fmt.Errorf("miss args")
  49. }
  50. b := args[0]
  51. arg := argAny(args)
  52. switch v := a.(type) {
  53. case reflect.Kind:
  54. ok = reflect.ValueOf(b).Kind() == v
  55. case time.Time:
  56. if v2, vo := b.(time.Time); vo {
  57. if arg.Get(1) != nil {
  58. format := ToStr(arg.Get(1))
  59. a = v.Format(format)
  60. b = v2.Format(format)
  61. ok = a == b
  62. } else {
  63. err = fmt.Errorf("compare datetime miss format")
  64. goto wrongArg
  65. }
  66. }
  67. default:
  68. ok = ToStr(a) == ToStr(b)
  69. }
  70. ok = is && ok || !is && !ok
  71. if !ok {
  72. if is {
  73. err = fmt.Errorf("expected: `%v`, get `%v`", b, a)
  74. } else {
  75. err = fmt.Errorf("expected: `%v`, get `%v`", b, a)
  76. }
  77. }
  78. wrongArg:
  79. if err != nil {
  80. return false, err
  81. }
  82. return true, nil
  83. }
  84. func AssertIs(a interface{}, args ...interface{}) error {
  85. if ok, err := ValuesCompare(true, a, args...); ok == false {
  86. return err
  87. }
  88. return nil
  89. }
  90. func AssertNot(a interface{}, args ...interface{}) error {
  91. if ok, err := ValuesCompare(false, a, args...); ok == false {
  92. return err
  93. }
  94. return nil
  95. }
  96. func getCaller(skip int) string {
  97. pc, file, line, _ := runtime.Caller(skip)
  98. fun := runtime.FuncForPC(pc)
  99. _, fn := filepath.Split(file)
  100. data, err := ioutil.ReadFile(file)
  101. var codes []string
  102. if err == nil {
  103. lines := bytes.Split(data, []byte{'\n'})
  104. n := 10
  105. for i := 0; i < n; i++ {
  106. o := line - n
  107. if o < 0 {
  108. continue
  109. }
  110. cur := o + i + 1
  111. flag := " "
  112. if cur == line {
  113. flag = ">>"
  114. }
  115. code := fmt.Sprintf(" %s %5d: %s", flag, cur, strings.Replace(string(lines[o+i]), "\t", " ", -1))
  116. if code != "" {
  117. codes = append(codes, code)
  118. }
  119. }
  120. }
  121. funName := fun.Name()
  122. if i := strings.LastIndex(funName, "."); i > -1 {
  123. funName = funName[i+1:]
  124. }
  125. return fmt.Sprintf("%s:%d: \n%s", fn, line, strings.Join(codes, "\n"))
  126. }
  127. func throwFail(t *testing.T, err error, args ...interface{}) {
  128. if err != nil {
  129. con := fmt.Sprintf("\t\nError: %s\n%s\n", err.Error(), getCaller(2))
  130. if len(args) > 0 {
  131. parts := make([]string, 0, len(args))
  132. for _, arg := range args {
  133. parts = append(parts, fmt.Sprintf("%v", arg))
  134. }
  135. con += " " + strings.Join(parts, ", ")
  136. }
  137. t.Error(con)
  138. t.Fail()
  139. }
  140. }
  141. func throwFailNow(t *testing.T, err error, args ...interface{}) {
  142. if err != nil {
  143. con := fmt.Sprintf("\t\nError: %s\n%s\n", err.Error(), getCaller(2))
  144. if len(args) > 0 {
  145. parts := make([]string, 0, len(args))
  146. for _, arg := range args {
  147. parts = append(parts, fmt.Sprintf("%v", arg))
  148. }
  149. con += " " + strings.Join(parts, ", ")
  150. }
  151. t.Error(con)
  152. t.FailNow()
  153. }
  154. }
  155. func TestGetDB(t *testing.T) {
  156. if db, err := GetDB(); err != nil {
  157. throwFailNow(t, err)
  158. } else {
  159. err = db.Ping()
  160. throwFailNow(t, err)
  161. }
  162. }
  163. func TestSyncDb(t *testing.T) {
  164. RegisterModel(new(Data), new(DataNull), new(DataCustom))
  165. RegisterModel(new(User))
  166. RegisterModel(new(Profile))
  167. RegisterModel(new(Post))
  168. RegisterModel(new(Tag))
  169. RegisterModel(new(Comment))
  170. RegisterModel(new(UserBig))
  171. RegisterModel(new(PostTags))
  172. RegisterModel(new(Group))
  173. RegisterModel(new(Permission))
  174. RegisterModel(new(GroupPermissions))
  175. RegisterModel(new(InLine))
  176. RegisterModel(new(InLineOneToOne))
  177. RegisterModel(new(IntegerPk))
  178. RegisterModel(new(UintPk))
  179. RegisterModel(new(PtrPk))
  180. err := RunSyncdb("default", true, Debug)
  181. throwFail(t, err)
  182. modelCache.clean()
  183. }
  184. func TestRegisterModels(t *testing.T) {
  185. RegisterModel(new(Data), new(DataNull), new(DataCustom))
  186. RegisterModel(new(User))
  187. RegisterModel(new(Profile))
  188. RegisterModel(new(Post))
  189. RegisterModel(new(Tag))
  190. RegisterModel(new(Comment))
  191. RegisterModel(new(UserBig))
  192. RegisterModel(new(PostTags))
  193. RegisterModel(new(Group))
  194. RegisterModel(new(Permission))
  195. RegisterModel(new(GroupPermissions))
  196. RegisterModel(new(InLine))
  197. RegisterModel(new(InLineOneToOne))
  198. RegisterModel(new(IntegerPk))
  199. RegisterModel(new(UintPk))
  200. RegisterModel(new(PtrPk))
  201. BootStrap()
  202. dORM = NewOrm()
  203. dDbBaser = getDbAlias("default").DbBaser
  204. }
  205. func TestModelSyntax(t *testing.T) {
  206. user := &User{}
  207. ind := reflect.ValueOf(user).Elem()
  208. fn := getFullName(ind.Type())
  209. mi, ok := modelCache.getByFullName(fn)
  210. throwFail(t, AssertIs(ok, true))
  211. mi, ok = modelCache.get("user")
  212. throwFail(t, AssertIs(ok, true))
  213. if ok {
  214. throwFail(t, AssertIs(mi.fields.GetByName("ShouldSkip") == nil, true))
  215. }
  216. }
  217. var DataValues = map[string]interface{}{
  218. "Boolean": true,
  219. "Char": "char",
  220. "Text": "text",
  221. "JSON": `{"name":"json"}`,
  222. "Jsonb": `{"name": "jsonb"}`,
  223. "Time": time.Now(),
  224. "Date": time.Now(),
  225. "DateTime": time.Now(),
  226. "Byte": byte(1<<8 - 1),
  227. "Rune": rune(1<<31 - 1),
  228. "Int": int(1<<31 - 1),
  229. "Int8": int8(1<<7 - 1),
  230. "Int16": int16(1<<15 - 1),
  231. "Int32": int32(1<<31 - 1),
  232. "Int64": int64(1<<63 - 1),
  233. "Uint": uint(1<<32 - 1),
  234. "Uint8": uint8(1<<8 - 1),
  235. "Uint16": uint16(1<<16 - 1),
  236. "Uint32": uint32(1<<32 - 1),
  237. "Uint64": uint64(1<<63 - 1), // uint64 values with high bit set are not supported
  238. "Float32": float32(100.1234),
  239. "Float64": float64(100.1234),
  240. "Decimal": float64(100.1234),
  241. }
  242. func TestDataTypes(t *testing.T) {
  243. d := Data{}
  244. ind := reflect.Indirect(reflect.ValueOf(&d))
  245. for name, value := range DataValues {
  246. if name == "JSON" {
  247. continue
  248. }
  249. e := ind.FieldByName(name)
  250. e.Set(reflect.ValueOf(value))
  251. }
  252. id, err := dORM.Insert(&d)
  253. throwFail(t, err)
  254. throwFail(t, AssertIs(id, 1))
  255. d = Data{ID: 1}
  256. err = dORM.Read(&d)
  257. throwFail(t, err)
  258. ind = reflect.Indirect(reflect.ValueOf(&d))
  259. for name, value := range DataValues {
  260. e := ind.FieldByName(name)
  261. vu := e.Interface()
  262. switch name {
  263. case "Date":
  264. vu = vu.(time.Time).In(DefaultTimeLoc).Format(testDate)
  265. value = value.(time.Time).In(DefaultTimeLoc).Format(testDate)
  266. case "DateTime":
  267. vu = vu.(time.Time).In(DefaultTimeLoc).Format(testDateTime)
  268. value = value.(time.Time).In(DefaultTimeLoc).Format(testDateTime)
  269. case "Time":
  270. vu = vu.(time.Time).In(DefaultTimeLoc).Format(testTime)
  271. value = value.(time.Time).In(DefaultTimeLoc).Format(testTime)
  272. }
  273. throwFail(t, AssertIs(vu == value, true), value, vu)
  274. }
  275. }
  276. func TestNullDataTypes(t *testing.T) {
  277. d := DataNull{}
  278. if IsPostgres {
  279. // can removed when this fixed
  280. // https://github.com/lib/pq/pull/125
  281. d.DateTime = time.Now()
  282. }
  283. id, err := dORM.Insert(&d)
  284. throwFail(t, err)
  285. throwFail(t, AssertIs(id, 1))
  286. data := `{"ok":1,"data":{"arr":[1,2],"msg":"gopher"}}`
  287. d = DataNull{ID: 1, JSON: data}
  288. num, err := dORM.Update(&d)
  289. throwFail(t, err)
  290. throwFail(t, AssertIs(num, 1))
  291. d = DataNull{ID: 1}
  292. err = dORM.Read(&d)
  293. throwFail(t, err)
  294. throwFail(t, AssertIs(d.JSON, data))
  295. throwFail(t, AssertIs(d.NullBool.Valid, false))
  296. throwFail(t, AssertIs(d.NullString.Valid, false))
  297. throwFail(t, AssertIs(d.NullInt64.Valid, false))
  298. throwFail(t, AssertIs(d.NullFloat64.Valid, false))
  299. throwFail(t, AssertIs(d.BooleanPtr, nil))
  300. throwFail(t, AssertIs(d.CharPtr, nil))
  301. throwFail(t, AssertIs(d.TextPtr, nil))
  302. throwFail(t, AssertIs(d.BytePtr, nil))
  303. throwFail(t, AssertIs(d.RunePtr, nil))
  304. throwFail(t, AssertIs(d.IntPtr, nil))
  305. throwFail(t, AssertIs(d.Int8Ptr, nil))
  306. throwFail(t, AssertIs(d.Int16Ptr, nil))
  307. throwFail(t, AssertIs(d.Int32Ptr, nil))
  308. throwFail(t, AssertIs(d.Int64Ptr, nil))
  309. throwFail(t, AssertIs(d.UintPtr, nil))
  310. throwFail(t, AssertIs(d.Uint8Ptr, nil))
  311. throwFail(t, AssertIs(d.Uint16Ptr, nil))
  312. throwFail(t, AssertIs(d.Uint32Ptr, nil))
  313. throwFail(t, AssertIs(d.Uint64Ptr, nil))
  314. throwFail(t, AssertIs(d.Float32Ptr, nil))
  315. throwFail(t, AssertIs(d.Float64Ptr, nil))
  316. throwFail(t, AssertIs(d.DecimalPtr, nil))
  317. throwFail(t, AssertIs(d.TimePtr, nil))
  318. throwFail(t, AssertIs(d.DatePtr, nil))
  319. throwFail(t, AssertIs(d.DateTimePtr, nil))
  320. _, err = dORM.Raw(`INSERT INTO data_null (boolean) VALUES (?)`, nil).Exec()
  321. throwFail(t, err)
  322. d = DataNull{ID: 2}
  323. err = dORM.Read(&d)
  324. throwFail(t, err)
  325. booleanPtr := true
  326. charPtr := string("test")
  327. textPtr := string("test")
  328. bytePtr := byte('t')
  329. runePtr := rune('t')
  330. intPtr := int(42)
  331. int8Ptr := int8(42)
  332. int16Ptr := int16(42)
  333. int32Ptr := int32(42)
  334. int64Ptr := int64(42)
  335. uintPtr := uint(42)
  336. uint8Ptr := uint8(42)
  337. uint16Ptr := uint16(42)
  338. uint32Ptr := uint32(42)
  339. uint64Ptr := uint64(42)
  340. float32Ptr := float32(42.0)
  341. float64Ptr := float64(42.0)
  342. decimalPtr := float64(42.0)
  343. timePtr := time.Now()
  344. datePtr := time.Now()
  345. dateTimePtr := time.Now()
  346. d = DataNull{
  347. DateTime: time.Now(),
  348. NullString: sql.NullString{String: "test", Valid: true},
  349. NullBool: sql.NullBool{Bool: true, Valid: true},
  350. NullInt64: sql.NullInt64{Int64: 42, Valid: true},
  351. NullFloat64: sql.NullFloat64{Float64: 42.42, Valid: true},
  352. BooleanPtr: &booleanPtr,
  353. CharPtr: &charPtr,
  354. TextPtr: &textPtr,
  355. BytePtr: &bytePtr,
  356. RunePtr: &runePtr,
  357. IntPtr: &intPtr,
  358. Int8Ptr: &int8Ptr,
  359. Int16Ptr: &int16Ptr,
  360. Int32Ptr: &int32Ptr,
  361. Int64Ptr: &int64Ptr,
  362. UintPtr: &uintPtr,
  363. Uint8Ptr: &uint8Ptr,
  364. Uint16Ptr: &uint16Ptr,
  365. Uint32Ptr: &uint32Ptr,
  366. Uint64Ptr: &uint64Ptr,
  367. Float32Ptr: &float32Ptr,
  368. Float64Ptr: &float64Ptr,
  369. DecimalPtr: &decimalPtr,
  370. TimePtr: &timePtr,
  371. DatePtr: &datePtr,
  372. DateTimePtr: &dateTimePtr,
  373. }
  374. id, err = dORM.Insert(&d)
  375. throwFail(t, err)
  376. throwFail(t, AssertIs(id, 3))
  377. d = DataNull{ID: 3}
  378. err = dORM.Read(&d)
  379. throwFail(t, err)
  380. throwFail(t, AssertIs(d.NullBool.Valid, true))
  381. throwFail(t, AssertIs(d.NullBool.Bool, true))
  382. throwFail(t, AssertIs(d.NullString.Valid, true))
  383. throwFail(t, AssertIs(d.NullString.String, "test"))
  384. throwFail(t, AssertIs(d.NullInt64.Valid, true))
  385. throwFail(t, AssertIs(d.NullInt64.Int64, 42))
  386. throwFail(t, AssertIs(d.NullFloat64.Valid, true))
  387. throwFail(t, AssertIs(d.NullFloat64.Float64, 42.42))
  388. throwFail(t, AssertIs(*d.BooleanPtr, booleanPtr))
  389. throwFail(t, AssertIs(*d.CharPtr, charPtr))
  390. throwFail(t, AssertIs(*d.TextPtr, textPtr))
  391. throwFail(t, AssertIs(*d.BytePtr, bytePtr))
  392. throwFail(t, AssertIs(*d.RunePtr, runePtr))
  393. throwFail(t, AssertIs(*d.IntPtr, intPtr))
  394. throwFail(t, AssertIs(*d.Int8Ptr, int8Ptr))
  395. throwFail(t, AssertIs(*d.Int16Ptr, int16Ptr))
  396. throwFail(t, AssertIs(*d.Int32Ptr, int32Ptr))
  397. throwFail(t, AssertIs(*d.Int64Ptr, int64Ptr))
  398. throwFail(t, AssertIs(*d.UintPtr, uintPtr))
  399. throwFail(t, AssertIs(*d.Uint8Ptr, uint8Ptr))
  400. throwFail(t, AssertIs(*d.Uint16Ptr, uint16Ptr))
  401. throwFail(t, AssertIs(*d.Uint32Ptr, uint32Ptr))
  402. throwFail(t, AssertIs(*d.Uint64Ptr, uint64Ptr))
  403. throwFail(t, AssertIs(*d.Float32Ptr, float32Ptr))
  404. throwFail(t, AssertIs(*d.Float64Ptr, float64Ptr))
  405. throwFail(t, AssertIs(*d.DecimalPtr, decimalPtr))
  406. throwFail(t, AssertIs((*d.TimePtr).Format(testTime), timePtr.Format(testTime)))
  407. throwFail(t, AssertIs((*d.DatePtr).Format(testDate), datePtr.Format(testDate)))
  408. throwFail(t, AssertIs((*d.DateTimePtr).Format(testDateTime), dateTimePtr.Format(testDateTime)))
  409. }
  410. func TestDataCustomTypes(t *testing.T) {
  411. d := DataCustom{}
  412. ind := reflect.Indirect(reflect.ValueOf(&d))
  413. for name, value := range DataValues {
  414. e := ind.FieldByName(name)
  415. if !e.IsValid() {
  416. continue
  417. }
  418. e.Set(reflect.ValueOf(value).Convert(e.Type()))
  419. }
  420. id, err := dORM.Insert(&d)
  421. throwFail(t, err)
  422. throwFail(t, AssertIs(id, 1))
  423. d = DataCustom{ID: 1}
  424. err = dORM.Read(&d)
  425. throwFail(t, err)
  426. ind = reflect.Indirect(reflect.ValueOf(&d))
  427. for name, value := range DataValues {
  428. e := ind.FieldByName(name)
  429. if !e.IsValid() {
  430. continue
  431. }
  432. vu := e.Interface()
  433. value = reflect.ValueOf(value).Convert(e.Type()).Interface()
  434. throwFail(t, AssertIs(vu == value, true), value, vu)
  435. }
  436. }
  437. func TestCRUD(t *testing.T) {
  438. profile := NewProfile()
  439. profile.Age = 30
  440. profile.Money = 1234.12
  441. id, err := dORM.Insert(profile)
  442. throwFail(t, err)
  443. throwFail(t, AssertIs(id, 1))
  444. user := NewUser()
  445. user.UserName = "slene"
  446. user.Email = "vslene@gmail.com"
  447. user.Password = "pass"
  448. user.Status = 3
  449. user.IsStaff = true
  450. user.IsActive = true
  451. id, err = dORM.Insert(user)
  452. throwFail(t, err)
  453. throwFail(t, AssertIs(id, 1))
  454. u := &User{ID: user.ID}
  455. err = dORM.Read(u)
  456. throwFail(t, err)
  457. throwFail(t, AssertIs(u.UserName, "slene"))
  458. throwFail(t, AssertIs(u.Email, "vslene@gmail.com"))
  459. throwFail(t, AssertIs(u.Password, "pass"))
  460. throwFail(t, AssertIs(u.Status, 3))
  461. throwFail(t, AssertIs(u.IsStaff, true))
  462. throwFail(t, AssertIs(u.IsActive, true))
  463. throwFail(t, AssertIs(u.Created.In(DefaultTimeLoc), user.Created.In(DefaultTimeLoc), testDate))
  464. throwFail(t, AssertIs(u.Updated.In(DefaultTimeLoc), user.Updated.In(DefaultTimeLoc), testDateTime))
  465. user.UserName = "astaxie"
  466. user.Profile = profile
  467. num, err := dORM.Update(user)
  468. throwFail(t, err)
  469. throwFail(t, AssertIs(num, 1))
  470. u = &User{ID: user.ID}
  471. err = dORM.Read(u)
  472. throwFailNow(t, err)
  473. throwFail(t, AssertIs(u.UserName, "astaxie"))
  474. throwFail(t, AssertIs(u.Profile.ID, profile.ID))
  475. u = &User{UserName: "astaxie", Password: "pass"}
  476. err = dORM.Read(u, "UserName")
  477. throwFailNow(t, err)
  478. throwFailNow(t, AssertIs(id, 1))
  479. u.UserName = "QQ"
  480. u.Password = "111"
  481. num, err = dORM.Update(u, "UserName")
  482. throwFail(t, err)
  483. throwFail(t, AssertIs(num, 1))
  484. u = &User{ID: user.ID}
  485. err = dORM.Read(u)
  486. throwFailNow(t, err)
  487. throwFail(t, AssertIs(u.UserName, "QQ"))
  488. throwFail(t, AssertIs(u.Password, "pass"))
  489. num, err = dORM.Delete(profile)
  490. throwFail(t, err)
  491. throwFail(t, AssertIs(num, 1))
  492. u = &User{ID: user.ID}
  493. err = dORM.Read(u)
  494. throwFail(t, err)
  495. throwFail(t, AssertIs(true, u.Profile == nil))
  496. num, err = dORM.Delete(user)
  497. throwFail(t, err)
  498. throwFail(t, AssertIs(num, 1))
  499. u = &User{ID: 100}
  500. err = dORM.Read(u)
  501. throwFail(t, AssertIs(err, ErrNoRows))
  502. ub := UserBig{}
  503. ub.Name = "name"
  504. id, err = dORM.Insert(&ub)
  505. throwFail(t, err)
  506. throwFail(t, AssertIs(id, 1))
  507. ub = UserBig{ID: 1}
  508. err = dORM.Read(&ub)
  509. throwFail(t, err)
  510. throwFail(t, AssertIs(ub.Name, "name"))
  511. num, err = dORM.Delete(&ub, "name")
  512. throwFail(t, err)
  513. throwFail(t, AssertIs(num, 1))
  514. }
  515. func TestInsertTestData(t *testing.T) {
  516. var users []*User
  517. profile := NewProfile()
  518. profile.Age = 28
  519. profile.Money = 1234.12
  520. id, err := dORM.Insert(profile)
  521. throwFail(t, err)
  522. throwFail(t, AssertIs(id, 2))
  523. user := NewUser()
  524. user.UserName = "slene"
  525. user.Email = "vslene@gmail.com"
  526. user.Password = "pass"
  527. user.Status = 1
  528. user.IsStaff = false
  529. user.IsActive = true
  530. user.Profile = profile
  531. users = append(users, user)
  532. id, err = dORM.Insert(user)
  533. throwFail(t, err)
  534. throwFail(t, AssertIs(id, 2))
  535. profile = NewProfile()
  536. profile.Age = 30
  537. profile.Money = 4321.09
  538. id, err = dORM.Insert(profile)
  539. throwFail(t, err)
  540. throwFail(t, AssertIs(id, 3))
  541. user = NewUser()
  542. user.UserName = "astaxie"
  543. user.Email = "astaxie@gmail.com"
  544. user.Password = "password"
  545. user.Status = 2
  546. user.IsStaff = true
  547. user.IsActive = false
  548. user.Profile = profile
  549. users = append(users, user)
  550. id, err = dORM.Insert(user)
  551. throwFail(t, err)
  552. throwFail(t, AssertIs(id, 3))
  553. user = NewUser()
  554. user.UserName = "nobody"
  555. user.Email = "nobody@gmail.com"
  556. user.Password = "nobody"
  557. user.Status = 3
  558. user.IsStaff = false
  559. user.IsActive = false
  560. users = append(users, user)
  561. id, err = dORM.Insert(user)
  562. throwFail(t, err)
  563. throwFail(t, AssertIs(id, 4))
  564. tags := []*Tag{
  565. {Name: "golang", BestPost: &Post{ID: 2}},
  566. {Name: "example"},
  567. {Name: "format"},
  568. {Name: "c++"},
  569. }
  570. posts := []*Post{
  571. {User: users[0], Tags: []*Tag{tags[0]}, Title: "Introduction", Content: `Go is a new language. Although it borrows ideas from existing languages, it has unusual properties that make effective Go programs different in character from programs written in its relatives. A straightforward translation of a C++ or Java program into Go is unlikely to produce a satisfactory result—Java programs are written in Java, not Go. On the other hand, thinking about the problem from a Go perspective could produce a successful but quite different program. In other words, to write Go well, it's important to understand its properties and idioms. It's also important to know the established conventions for programming in Go, such as naming, formatting, program construction, and so on, so that programs you write will be easy for other Go programmers to understand.
  572. This document gives tips for writing clear, idiomatic Go code. It augments the language specification, the Tour of Go, and How to Write Go Code, all of which you should read first.`},
  573. {User: users[1], Tags: []*Tag{tags[0], tags[1]}, Title: "Examples", Content: `The Go package sources are intended to serve not only as the core library but also as examples of how to use the language. Moreover, many of the packages contain working, self-contained executable examples you can run directly from the golang.org web site, such as this one (click on the word "Example" to open it up). If you have a question about how to approach a problem or how something might be implemented, the documentation, code and examples in the library can provide answers, ideas and background.`},
  574. {User: users[1], Tags: []*Tag{tags[0], tags[2]}, Title: "Formatting", Content: `Formatting issues are the most contentious but the least consequential. People can adapt to different formatting styles but it's better if they don't have to, and less time is devoted to the topic if everyone adheres to the same style. The problem is how to approach this Utopia without a long prescriptive style guide.
  575. With Go we take an unusual approach and let the machine take care of most formatting issues. The gofmt program (also available as go fmt, which operates at the package level rather than source file level) reads a Go program and emits the source in a standard style of indentation and vertical alignment, retaining and if necessary reformatting comments. If you want to know how to handle some new layout situation, run gofmt; if the answer doesn't seem right, rearrange your program (or file a bug about gofmt), don't work around it.`},
  576. {User: users[2], Tags: []*Tag{tags[3]}, Title: "Commentary", Content: `Go provides C-style /* */ block comments and C++-style // line comments. Line comments are the norm; block comments appear mostly as package comments, but are useful within an expression or to disable large swaths of code.
  577. The program—and web server—godoc processes Go source files to extract documentation about the contents of the package. Comments that appear before top-level declarations, with no intervening newlines, are extracted along with the declaration to serve as explanatory text for the item. The nature and style of these comments determines the quality of the documentation godoc produces.`},
  578. }
  579. comments := []*Comment{
  580. {Post: posts[0], Content: "a comment"},
  581. {Post: posts[1], Content: "yes"},
  582. {Post: posts[1]},
  583. {Post: posts[1]},
  584. {Post: posts[2]},
  585. {Post: posts[2]},
  586. }
  587. for _, tag := range tags {
  588. id, err := dORM.Insert(tag)
  589. throwFail(t, err)
  590. throwFail(t, AssertIs(id > 0, true))
  591. }
  592. for _, post := range posts {
  593. id, err := dORM.Insert(post)
  594. throwFail(t, err)
  595. throwFail(t, AssertIs(id > 0, true))
  596. num := len(post.Tags)
  597. if num > 0 {
  598. nums, err := dORM.QueryM2M(post, "tags").Add(post.Tags)
  599. throwFailNow(t, err)
  600. throwFailNow(t, AssertIs(nums, num))
  601. }
  602. }
  603. for _, comment := range comments {
  604. id, err := dORM.Insert(comment)
  605. throwFail(t, err)
  606. throwFail(t, AssertIs(id > 0, true))
  607. }
  608. permissions := []*Permission{
  609. {Name: "writePosts"},
  610. {Name: "readComments"},
  611. {Name: "readPosts"},
  612. }
  613. groups := []*Group{
  614. {
  615. Name: "admins",
  616. Permissions: []*Permission{permissions[0], permissions[1], permissions[2]},
  617. },
  618. {
  619. Name: "users",
  620. Permissions: []*Permission{permissions[1], permissions[2]},
  621. },
  622. }
  623. for _, permission := range permissions {
  624. id, err := dORM.Insert(permission)
  625. throwFail(t, err)
  626. throwFail(t, AssertIs(id > 0, true))
  627. }
  628. for _, group := range groups {
  629. _, err := dORM.Insert(group)
  630. throwFail(t, err)
  631. throwFail(t, AssertIs(id > 0, true))
  632. num := len(group.Permissions)
  633. if num > 0 {
  634. nums, err := dORM.QueryM2M(group, "permissions").Add(group.Permissions)
  635. throwFailNow(t, err)
  636. throwFailNow(t, AssertIs(nums, num))
  637. }
  638. }
  639. }
  640. func TestCustomField(t *testing.T) {
  641. user := User{ID: 2}
  642. err := dORM.Read(&user)
  643. throwFailNow(t, err)
  644. user.Langs = append(user.Langs, "zh-CN", "en-US")
  645. user.Extra.Name = "beego"
  646. user.Extra.Data = "orm"
  647. _, err = dORM.Update(&user, "Langs", "Extra")
  648. throwFailNow(t, err)
  649. user = User{ID: 2}
  650. err = dORM.Read(&user)
  651. throwFailNow(t, err)
  652. throwFailNow(t, AssertIs(len(user.Langs), 2))
  653. throwFailNow(t, AssertIs(user.Langs[0], "zh-CN"))
  654. throwFailNow(t, AssertIs(user.Langs[1], "en-US"))
  655. throwFailNow(t, AssertIs(user.Extra.Name, "beego"))
  656. throwFailNow(t, AssertIs(user.Extra.Data, "orm"))
  657. }
  658. func TestExpr(t *testing.T) {
  659. user := &User{}
  660. qs := dORM.QueryTable(user)
  661. qs = dORM.QueryTable((*User)(nil))
  662. qs = dORM.QueryTable("User")
  663. qs = dORM.QueryTable("user")
  664. num, err := qs.Filter("UserName", "slene").Filter("user_name", "slene").Filter("profile__Age", 28).Count()
  665. throwFail(t, err)
  666. throwFail(t, AssertIs(num, 1))
  667. num, err = qs.Filter("created", time.Now()).Count()
  668. throwFail(t, err)
  669. throwFail(t, AssertIs(num, 3))
  670. // num, err = qs.Filter("created", time.Now().Format(format_Date)).Count()
  671. // throwFail(t, err)
  672. // throwFail(t, AssertIs(num, 3))
  673. }
  674. func TestOperators(t *testing.T) {
  675. qs := dORM.QueryTable("user")
  676. num, err := qs.Filter("user_name", "slene").Count()
  677. throwFail(t, err)
  678. throwFail(t, AssertIs(num, 1))
  679. num, err = qs.Filter("user_name__exact", String("slene")).Count()
  680. throwFail(t, err)
  681. throwFail(t, AssertIs(num, 1))
  682. num, err = qs.Filter("user_name__exact", "slene").Count()
  683. throwFail(t, err)
  684. throwFail(t, AssertIs(num, 1))
  685. num, err = qs.Filter("user_name__iexact", "Slene").Count()
  686. throwFail(t, err)
  687. throwFail(t, AssertIs(num, 1))
  688. num, err = qs.Filter("user_name__contains", "e").Count()
  689. throwFail(t, err)
  690. throwFail(t, AssertIs(num, 2))
  691. var shouldNum int
  692. if IsSqlite || IsTidb {
  693. shouldNum = 2
  694. } else {
  695. shouldNum = 0
  696. }
  697. num, err = qs.Filter("user_name__contains", "E").Count()
  698. throwFail(t, err)
  699. throwFail(t, AssertIs(num, shouldNum))
  700. num, err = qs.Filter("user_name__icontains", "E").Count()
  701. throwFail(t, err)
  702. throwFail(t, AssertIs(num, 2))
  703. num, err = qs.Filter("user_name__icontains", "E").Count()
  704. throwFail(t, err)
  705. throwFail(t, AssertIs(num, 2))
  706. num, err = qs.Filter("status__gt", 1).Count()
  707. throwFail(t, err)
  708. throwFail(t, AssertIs(num, 2))
  709. num, err = qs.Filter("status__gte", 1).Count()
  710. throwFail(t, err)
  711. throwFail(t, AssertIs(num, 3))
  712. num, err = qs.Filter("status__lt", Uint(3)).Count()
  713. throwFail(t, err)
  714. throwFail(t, AssertIs(num, 2))
  715. num, err = qs.Filter("status__lte", Int(3)).Count()
  716. throwFail(t, err)
  717. throwFail(t, AssertIs(num, 3))
  718. num, err = qs.Filter("user_name__startswith", "s").Count()
  719. throwFail(t, err)
  720. throwFail(t, AssertIs(num, 1))
  721. if IsSqlite || IsTidb {
  722. shouldNum = 1
  723. } else {
  724. shouldNum = 0
  725. }
  726. num, err = qs.Filter("user_name__startswith", "S").Count()
  727. throwFail(t, err)
  728. throwFail(t, AssertIs(num, shouldNum))
  729. num, err = qs.Filter("user_name__istartswith", "S").Count()
  730. throwFail(t, err)
  731. throwFail(t, AssertIs(num, 1))
  732. num, err = qs.Filter("user_name__endswith", "e").Count()
  733. throwFail(t, err)
  734. throwFail(t, AssertIs(num, 2))
  735. if IsSqlite || IsTidb {
  736. shouldNum = 2
  737. } else {
  738. shouldNum = 0
  739. }
  740. num, err = qs.Filter("user_name__endswith", "E").Count()
  741. throwFail(t, err)
  742. throwFail(t, AssertIs(num, shouldNum))
  743. num, err = qs.Filter("user_name__iendswith", "E").Count()
  744. throwFail(t, err)
  745. throwFail(t, AssertIs(num, 2))
  746. num, err = qs.Filter("profile__isnull", true).Count()
  747. throwFail(t, err)
  748. throwFail(t, AssertIs(num, 1))
  749. num, err = qs.Filter("status__in", 1, 2).Count()
  750. throwFail(t, err)
  751. throwFail(t, AssertIs(num, 2))
  752. num, err = qs.Filter("status__in", []int{1, 2}).Count()
  753. throwFail(t, err)
  754. throwFail(t, AssertIs(num, 2))
  755. n1, n2 := 1, 2
  756. num, err = qs.Filter("status__in", []*int{&n1}, &n2).Count()
  757. throwFail(t, err)
  758. throwFail(t, AssertIs(num, 2))
  759. num, err = qs.Filter("id__between", 2, 3).Count()
  760. throwFail(t, err)
  761. throwFail(t, AssertIs(num, 2))
  762. num, err = qs.Filter("id__between", []int{2, 3}).Count()
  763. throwFail(t, err)
  764. throwFail(t, AssertIs(num, 2))
  765. }
  766. func TestSetCond(t *testing.T) {
  767. cond := NewCondition()
  768. cond1 := cond.And("profile__isnull", false).AndNot("status__in", 1).Or("profile__age__gt", 2000)
  769. qs := dORM.QueryTable("user")
  770. num, err := qs.SetCond(cond1).Count()
  771. throwFail(t, err)
  772. throwFail(t, AssertIs(num, 1))
  773. cond2 := cond.AndCond(cond1).OrCond(cond.And("user_name", "slene"))
  774. num, err = qs.SetCond(cond2).Count()
  775. throwFail(t, err)
  776. throwFail(t, AssertIs(num, 2))
  777. cond3 := cond.AndNotCond(cond.And("status__in", 1))
  778. num, err = qs.SetCond(cond3).Count()
  779. throwFail(t, err)
  780. throwFail(t, AssertIs(num, 2))
  781. cond4 := cond.And("user_name", "slene").OrNotCond(cond.And("user_name", "slene"))
  782. num, err = qs.SetCond(cond4).Count()
  783. throwFail(t, err)
  784. throwFail(t, AssertIs(num, 3))
  785. }
  786. func TestLimit(t *testing.T) {
  787. var posts []*Post
  788. qs := dORM.QueryTable("post")
  789. num, err := qs.Limit(1).All(&posts)
  790. throwFail(t, err)
  791. throwFail(t, AssertIs(num, 1))
  792. num, err = qs.Limit(-1).All(&posts)
  793. throwFail(t, err)
  794. throwFail(t, AssertIs(num, 4))
  795. num, err = qs.Limit(-1, 2).All(&posts)
  796. throwFail(t, err)
  797. throwFail(t, AssertIs(num, 2))
  798. num, err = qs.Limit(0, 2).All(&posts)
  799. throwFail(t, err)
  800. throwFail(t, AssertIs(num, 2))
  801. }
  802. func TestOffset(t *testing.T) {
  803. var posts []*Post
  804. qs := dORM.QueryTable("post")
  805. num, err := qs.Limit(1).Offset(2).All(&posts)
  806. throwFail(t, err)
  807. throwFail(t, AssertIs(num, 1))
  808. num, err = qs.Offset(2).All(&posts)
  809. throwFail(t, err)
  810. throwFail(t, AssertIs(num, 2))
  811. }
  812. func TestOrderBy(t *testing.T) {
  813. qs := dORM.QueryTable("user")
  814. num, err := qs.OrderBy("-status").Filter("user_name", "nobody").Count()
  815. throwFail(t, err)
  816. throwFail(t, AssertIs(num, 1))
  817. num, err = qs.OrderBy("status").Filter("user_name", "slene").Count()
  818. throwFail(t, err)
  819. throwFail(t, AssertIs(num, 1))
  820. num, err = qs.OrderBy("-profile__age").Filter("user_name", "astaxie").Count()
  821. throwFail(t, err)
  822. throwFail(t, AssertIs(num, 1))
  823. }
  824. func TestAll(t *testing.T) {
  825. var users []*User
  826. qs := dORM.QueryTable("user")
  827. num, err := qs.OrderBy("Id").All(&users)
  828. throwFail(t, err)
  829. throwFailNow(t, AssertIs(num, 3))
  830. throwFail(t, AssertIs(users[0].UserName, "slene"))
  831. throwFail(t, AssertIs(users[1].UserName, "astaxie"))
  832. throwFail(t, AssertIs(users[2].UserName, "nobody"))
  833. var users2 []User
  834. qs = dORM.QueryTable("user")
  835. num, err = qs.OrderBy("Id").All(&users2)
  836. throwFail(t, err)
  837. throwFailNow(t, AssertIs(num, 3))
  838. throwFailNow(t, AssertIs(users2[0].UserName, "slene"))
  839. throwFailNow(t, AssertIs(users2[1].UserName, "astaxie"))
  840. throwFailNow(t, AssertIs(users2[2].UserName, "nobody"))
  841. qs = dORM.QueryTable("user")
  842. num, err = qs.OrderBy("Id").RelatedSel().All(&users2, "UserName")
  843. throwFail(t, err)
  844. throwFailNow(t, AssertIs(num, 3))
  845. throwFailNow(t, AssertIs(len(users2), 3))
  846. throwFailNow(t, AssertIs(users2[0].UserName, "slene"))
  847. throwFailNow(t, AssertIs(users2[1].UserName, "astaxie"))
  848. throwFailNow(t, AssertIs(users2[2].UserName, "nobody"))
  849. throwFailNow(t, AssertIs(users2[0].ID, 0))
  850. throwFailNow(t, AssertIs(users2[1].ID, 0))
  851. throwFailNow(t, AssertIs(users2[2].ID, 0))
  852. throwFailNow(t, AssertIs(users2[0].Profile == nil, false))
  853. throwFailNow(t, AssertIs(users2[1].Profile == nil, false))
  854. throwFailNow(t, AssertIs(users2[2].Profile == nil, true))
  855. qs = dORM.QueryTable("user")
  856. num, err = qs.Filter("user_name", "nothing").All(&users)
  857. throwFailNow(t, err)
  858. throwFailNow(t, AssertIs(num, 0))
  859. var users3 []*User
  860. qs = dORM.QueryTable("user")
  861. num, err = qs.Filter("user_name", "nothing").All(&users3)
  862. throwFailNow(t, AssertIs(users3 == nil, false))
  863. }
  864. func TestOne(t *testing.T) {
  865. var user User
  866. qs := dORM.QueryTable("user")
  867. err := qs.One(&user)
  868. throwFail(t, err)
  869. user = User{}
  870. err = qs.OrderBy("Id").Limit(1).One(&user)
  871. throwFailNow(t, err)
  872. throwFail(t, AssertIs(user.UserName, "slene"))
  873. throwFail(t, AssertNot(err, ErrMultiRows))
  874. user = User{}
  875. err = qs.OrderBy("-Id").Limit(100).One(&user)
  876. throwFailNow(t, err)
  877. throwFail(t, AssertIs(user.UserName, "nobody"))
  878. throwFail(t, AssertNot(err, ErrMultiRows))
  879. err = qs.Filter("user_name", "nothing").One(&user)
  880. throwFail(t, AssertIs(err, ErrNoRows))
  881. }
  882. func TestValues(t *testing.T) {
  883. var maps []Params
  884. qs := dORM.QueryTable("user")
  885. num, err := qs.OrderBy("Id").Values(&maps)
  886. throwFail(t, err)
  887. throwFail(t, AssertIs(num, 3))
  888. if num == 3 {
  889. throwFail(t, AssertIs(maps[0]["UserName"], "slene"))
  890. throwFail(t, AssertIs(maps[2]["Profile"], nil))
  891. }
  892. num, err = qs.OrderBy("Id").Values(&maps, "UserName", "Profile__Age")
  893. throwFail(t, err)
  894. throwFail(t, AssertIs(num, 3))
  895. if num == 3 {
  896. throwFail(t, AssertIs(maps[0]["UserName"], "slene"))
  897. throwFail(t, AssertIs(maps[0]["Profile__Age"], 28))
  898. throwFail(t, AssertIs(maps[2]["Profile__Age"], nil))
  899. }
  900. num, err = qs.Filter("UserName", "slene").Values(&maps)
  901. throwFail(t, err)
  902. throwFail(t, AssertIs(num, 1))
  903. }
  904. func TestValuesList(t *testing.T) {
  905. var list []ParamsList
  906. qs := dORM.QueryTable("user")
  907. num, err := qs.OrderBy("Id").ValuesList(&list)
  908. throwFail(t, err)
  909. throwFail(t, AssertIs(num, 3))
  910. if num == 3 {
  911. throwFail(t, AssertIs(list[0][1], "slene"))
  912. throwFail(t, AssertIs(list[2][9], nil))
  913. }
  914. num, err = qs.OrderBy("Id").ValuesList(&list, "UserName", "Profile__Age")
  915. throwFail(t, err)
  916. throwFail(t, AssertIs(num, 3))
  917. if num == 3 {
  918. throwFail(t, AssertIs(list[0][0], "slene"))
  919. throwFail(t, AssertIs(list[0][1], 28))
  920. throwFail(t, AssertIs(list[2][1], nil))
  921. }
  922. }
  923. func TestValuesFlat(t *testing.T) {
  924. var list ParamsList
  925. qs := dORM.QueryTable("user")
  926. num, err := qs.OrderBy("id").ValuesFlat(&list, "UserName")
  927. throwFail(t, err)
  928. throwFail(t, AssertIs(num, 3))
  929. if num == 3 {
  930. throwFail(t, AssertIs(list[0], "slene"))
  931. throwFail(t, AssertIs(list[1], "astaxie"))
  932. throwFail(t, AssertIs(list[2], "nobody"))
  933. }
  934. }
  935. func TestRelatedSel(t *testing.T) {
  936. if IsTidb {
  937. // Skip it. TiDB does not support relation now.
  938. return
  939. }
  940. qs := dORM.QueryTable("user")
  941. num, err := qs.Filter("profile__age", 28).Count()
  942. throwFail(t, err)
  943. throwFail(t, AssertIs(num, 1))
  944. num, err = qs.Filter("profile__age__gt", 28).Count()
  945. throwFail(t, err)
  946. throwFail(t, AssertIs(num, 1))
  947. num, err = qs.Filter("profile__user__profile__age__gt", 28).Count()
  948. throwFail(t, err)
  949. throwFail(t, AssertIs(num, 1))
  950. var user User
  951. err = qs.Filter("user_name", "slene").RelatedSel("profile").One(&user)
  952. throwFail(t, err)
  953. throwFail(t, AssertIs(num, 1))
  954. throwFail(t, AssertNot(user.Profile, nil))
  955. if user.Profile != nil {
  956. throwFail(t, AssertIs(user.Profile.Age, 28))
  957. }
  958. err = qs.Filter("user_name", "slene").RelatedSel().One(&user)
  959. throwFail(t, err)
  960. throwFail(t, AssertIs(num, 1))
  961. throwFail(t, AssertNot(user.Profile, nil))
  962. if user.Profile != nil {
  963. throwFail(t, AssertIs(user.Profile.Age, 28))
  964. }
  965. err = qs.Filter("user_name", "nobody").RelatedSel("profile").One(&user)
  966. throwFail(t, AssertIs(num, 1))
  967. throwFail(t, AssertIs(user.Profile, nil))
  968. qs = dORM.QueryTable("user_profile")
  969. num, err = qs.Filter("user__username", "slene").Count()
  970. throwFail(t, err)
  971. throwFail(t, AssertIs(num, 1))
  972. var posts []*Post
  973. qs = dORM.QueryTable("post")
  974. num, err = qs.RelatedSel().All(&posts)
  975. throwFail(t, err)
  976. throwFailNow(t, AssertIs(num, 4))
  977. throwFailNow(t, AssertIs(posts[0].User.UserName, "slene"))
  978. throwFailNow(t, AssertIs(posts[1].User.UserName, "astaxie"))
  979. throwFailNow(t, AssertIs(posts[2].User.UserName, "astaxie"))
  980. throwFailNow(t, AssertIs(posts[3].User.UserName, "nobody"))
  981. }
  982. func TestReverseQuery(t *testing.T) {
  983. var profile Profile
  984. err := dORM.QueryTable("user_profile").Filter("User", 3).One(&profile)
  985. throwFailNow(t, err)
  986. throwFailNow(t, AssertIs(profile.Age, 30))
  987. profile = Profile{}
  988. err = dORM.QueryTable("user_profile").Filter("User__UserName", "astaxie").One(&profile)
  989. throwFailNow(t, err)
  990. throwFailNow(t, AssertIs(profile.Age, 30))
  991. var user User
  992. err = dORM.QueryTable("user").Filter("Posts__Title", "Examples").One(&user)
  993. throwFailNow(t, err)
  994. throwFailNow(t, AssertIs(user.UserName, "astaxie"))
  995. user = User{}
  996. err = dORM.QueryTable("user").Filter("Posts__User__UserName", "astaxie").Limit(1).One(&user)
  997. throwFailNow(t, err)
  998. throwFailNow(t, AssertIs(user.UserName, "astaxie"))
  999. user = User{}
  1000. err = dORM.QueryTable("user").Filter("Posts__User__UserName", "astaxie").RelatedSel().Limit(1).One(&user)
  1001. throwFailNow(t, err)
  1002. throwFailNow(t, AssertIs(user.UserName, "astaxie"))
  1003. throwFailNow(t, AssertIs(user.Profile == nil, false))
  1004. throwFailNow(t, AssertIs(user.Profile.Age, 30))
  1005. var posts []*Post
  1006. num, err := dORM.QueryTable("post").Filter("Tags__Tag__Name", "golang").All(&posts)
  1007. throwFailNow(t, err)
  1008. throwFailNow(t, AssertIs(num, 3))
  1009. throwFailNow(t, AssertIs(posts[0].Title, "Introduction"))
  1010. posts = []*Post{}
  1011. num, err = dORM.QueryTable("post").Filter("Tags__Tag__Name", "golang").Filter("User__UserName", "slene").All(&posts)
  1012. throwFailNow(t, err)
  1013. throwFailNow(t, AssertIs(num, 1))
  1014. throwFailNow(t, AssertIs(posts[0].Title, "Introduction"))
  1015. posts = []*Post{}
  1016. num, err = dORM.QueryTable("post").Filter("Tags__Tag__Name", "golang").
  1017. Filter("User__UserName", "slene").RelatedSel().All(&posts)
  1018. throwFailNow(t, err)
  1019. throwFailNow(t, AssertIs(num, 1))
  1020. throwFailNow(t, AssertIs(posts[0].User == nil, false))
  1021. throwFailNow(t, AssertIs(posts[0].User.UserName, "slene"))
  1022. var tags []*Tag
  1023. num, err = dORM.QueryTable("tag").Filter("Posts__Post__Title", "Introduction").All(&tags)
  1024. throwFailNow(t, err)
  1025. throwFailNow(t, AssertIs(num, 1))
  1026. throwFailNow(t, AssertIs(tags[0].Name, "golang"))
  1027. tags = []*Tag{}
  1028. num, err = dORM.QueryTable("tag").Filter("Posts__Post__Title", "Introduction").
  1029. Filter("BestPost__User__UserName", "astaxie").All(&tags)
  1030. throwFailNow(t, err)
  1031. throwFailNow(t, AssertIs(num, 1))
  1032. throwFailNow(t, AssertIs(tags[0].Name, "golang"))
  1033. tags = []*Tag{}
  1034. num, err = dORM.QueryTable("tag").Filter("Posts__Post__Title", "Introduction").
  1035. Filter("BestPost__User__UserName", "astaxie").RelatedSel().All(&tags)
  1036. throwFailNow(t, err)
  1037. throwFailNow(t, AssertIs(num, 1))
  1038. throwFailNow(t, AssertIs(tags[0].Name, "golang"))
  1039. throwFailNow(t, AssertIs(tags[0].BestPost == nil, false))
  1040. throwFailNow(t, AssertIs(tags[0].BestPost.Title, "Examples"))
  1041. throwFailNow(t, AssertIs(tags[0].BestPost.User == nil, false))
  1042. throwFailNow(t, AssertIs(tags[0].BestPost.User.UserName, "astaxie"))
  1043. }
  1044. func TestLoadRelated(t *testing.T) {
  1045. // load reverse foreign key
  1046. user := User{ID: 3}
  1047. err := dORM.Read(&user)
  1048. throwFailNow(t, err)
  1049. num, err := dORM.LoadRelated(&user, "Posts")
  1050. throwFailNow(t, err)
  1051. throwFailNow(t, AssertIs(num, 2))
  1052. throwFailNow(t, AssertIs(len(user.Posts), 2))
  1053. throwFailNow(t, AssertIs(user.Posts[0].User.ID, 3))
  1054. num, err = dORM.LoadRelated(&user, "Posts", true)
  1055. throwFailNow(t, err)
  1056. throwFailNow(t, AssertIs(len(user.Posts), 2))
  1057. throwFailNow(t, AssertIs(user.Posts[0].User.UserName, "astaxie"))
  1058. num, err = dORM.LoadRelated(&user, "Posts", true, 1)
  1059. throwFailNow(t, err)
  1060. throwFailNow(t, AssertIs(len(user.Posts), 1))
  1061. num, err = dORM.LoadRelated(&user, "Posts", true, 0, 0, "-Id")
  1062. throwFailNow(t, err)
  1063. throwFailNow(t, AssertIs(len(user.Posts), 2))
  1064. throwFailNow(t, AssertIs(user.Posts[0].Title, "Formatting"))
  1065. num, err = dORM.LoadRelated(&user, "Posts", true, 1, 1, "Id")
  1066. throwFailNow(t, err)
  1067. throwFailNow(t, AssertIs(len(user.Posts), 1))
  1068. throwFailNow(t, AssertIs(user.Posts[0].Title, "Formatting"))
  1069. // load reverse one to one
  1070. profile := Profile{ID: 3}
  1071. profile.BestPost = &Post{ID: 2}
  1072. num, err = dORM.Update(&profile, "BestPost")
  1073. throwFailNow(t, err)
  1074. throwFailNow(t, AssertIs(num, 1))
  1075. err = dORM.Read(&profile)
  1076. throwFailNow(t, err)
  1077. num, err = dORM.LoadRelated(&profile, "User")
  1078. throwFailNow(t, err)
  1079. throwFailNow(t, AssertIs(num, 1))
  1080. throwFailNow(t, AssertIs(profile.User == nil, false))
  1081. throwFailNow(t, AssertIs(profile.User.UserName, "astaxie"))
  1082. num, err = dORM.LoadRelated(&profile, "User", true)
  1083. throwFailNow(t, err)
  1084. throwFailNow(t, AssertIs(num, 1))
  1085. throwFailNow(t, AssertIs(profile.User == nil, false))
  1086. throwFailNow(t, AssertIs(profile.User.UserName, "astaxie"))
  1087. throwFailNow(t, AssertIs(profile.User.Profile.Age, profile.Age))
  1088. // load rel one to one
  1089. err = dORM.Read(&user)
  1090. throwFailNow(t, err)
  1091. num, err = dORM.LoadRelated(&user, "Profile")
  1092. throwFailNow(t, err)
  1093. throwFailNow(t, AssertIs(num, 1))
  1094. throwFailNow(t, AssertIs(user.Profile == nil, false))
  1095. throwFailNow(t, AssertIs(user.Profile.Age, 30))
  1096. num, err = dORM.LoadRelated(&user, "Profile", true)
  1097. throwFailNow(t, err)
  1098. throwFailNow(t, AssertIs(num, 1))
  1099. throwFailNow(t, AssertIs(user.Profile == nil, false))
  1100. throwFailNow(t, AssertIs(user.Profile.Age, 30))
  1101. throwFailNow(t, AssertIs(user.Profile.BestPost == nil, false))
  1102. throwFailNow(t, AssertIs(user.Profile.BestPost.Title, "Examples"))
  1103. post := Post{ID: 2}
  1104. // load rel foreign key
  1105. err = dORM.Read(&post)
  1106. throwFailNow(t, err)
  1107. num, err = dORM.LoadRelated(&post, "User")
  1108. throwFailNow(t, err)
  1109. throwFailNow(t, AssertIs(num, 1))
  1110. throwFailNow(t, AssertIs(post.User == nil, false))
  1111. throwFailNow(t, AssertIs(post.User.UserName, "astaxie"))
  1112. num, err = dORM.LoadRelated(&post, "User", true)
  1113. throwFailNow(t, err)
  1114. throwFailNow(t, AssertIs(num, 1))
  1115. throwFailNow(t, AssertIs(post.User == nil, false))
  1116. throwFailNow(t, AssertIs(post.User.UserName, "astaxie"))
  1117. throwFailNow(t, AssertIs(post.User.Profile == nil, false))
  1118. throwFailNow(t, AssertIs(post.User.Profile.Age, 30))
  1119. // load rel m2m
  1120. post = Post{ID: 2}
  1121. err = dORM.Read(&post)
  1122. throwFailNow(t, err)
  1123. num, err = dORM.LoadRelated(&post, "Tags")
  1124. throwFailNow(t, err)
  1125. throwFailNow(t, AssertIs(num, 2))
  1126. throwFailNow(t, AssertIs(len(post.Tags), 2))
  1127. throwFailNow(t, AssertIs(post.Tags[0].Name, "golang"))
  1128. num, err = dORM.LoadRelated(&post, "Tags", true)
  1129. throwFailNow(t, err)
  1130. throwFailNow(t, AssertIs(num, 2))
  1131. throwFailNow(t, AssertIs(len(post.Tags), 2))
  1132. throwFailNow(t, AssertIs(post.Tags[0].Name, "golang"))
  1133. throwFailNow(t, AssertIs(post.Tags[0].BestPost == nil, false))
  1134. throwFailNow(t, AssertIs(post.Tags[0].BestPost.User.UserName, "astaxie"))
  1135. // load reverse m2m
  1136. tag := Tag{ID: 1}
  1137. err = dORM.Read(&tag)
  1138. throwFailNow(t, err)
  1139. num, err = dORM.LoadRelated(&tag, "Posts")
  1140. throwFailNow(t, err)
  1141. throwFailNow(t, AssertIs(num, 3))
  1142. throwFailNow(t, AssertIs(tag.Posts[0].Title, "Introduction"))
  1143. throwFailNow(t, AssertIs(tag.Posts[0].User.ID, 2))
  1144. throwFailNow(t, AssertIs(tag.Posts[0].User.Profile == nil, true))
  1145. num, err = dORM.LoadRelated(&tag, "Posts", true)
  1146. throwFailNow(t, err)
  1147. throwFailNow(t, AssertIs(num, 3))
  1148. throwFailNow(t, AssertIs(tag.Posts[0].Title, "Introduction"))
  1149. throwFailNow(t, AssertIs(tag.Posts[0].User.ID, 2))
  1150. throwFailNow(t, AssertIs(tag.Posts[0].User.UserName, "slene"))
  1151. }
  1152. func TestQueryM2M(t *testing.T) {
  1153. post := Post{ID: 4}
  1154. m2m := dORM.QueryM2M(&post, "Tags")
  1155. tag1 := []*Tag{{Name: "TestTag1"}, {Name: "TestTag2"}}
  1156. tag2 := &Tag{Name: "TestTag3"}
  1157. tag3 := []interface{}{&Tag{Name: "TestTag4"}}
  1158. tags := []interface{}{tag1[0], tag1[1], tag2, tag3[0]}
  1159. for _, tag := range tags {
  1160. _, err := dORM.Insert(tag)
  1161. throwFailNow(t, err)
  1162. }
  1163. num, err := m2m.Add(tag1)
  1164. throwFailNow(t, err)
  1165. throwFailNow(t, AssertIs(num, 2))
  1166. num, err = m2m.Add(tag2)
  1167. throwFailNow(t, err)
  1168. throwFailNow(t, AssertIs(num, 1))
  1169. num, err = m2m.Add(tag3)
  1170. throwFailNow(t, err)
  1171. throwFailNow(t, AssertIs(num, 1))
  1172. num, err = m2m.Count()
  1173. throwFailNow(t, err)
  1174. throwFailNow(t, AssertIs(num, 5))
  1175. num, err = m2m.Remove(tag3)
  1176. throwFailNow(t, err)
  1177. throwFailNow(t, AssertIs(num, 1))
  1178. num, err = m2m.Count()
  1179. throwFailNow(t, err)
  1180. throwFailNow(t, AssertIs(num, 4))
  1181. exist := m2m.Exist(tag2)
  1182. throwFailNow(t, AssertIs(exist, true))
  1183. num, err = m2m.Remove(tag2)
  1184. throwFailNow(t, err)
  1185. throwFailNow(t, AssertIs(num, 1))
  1186. exist = m2m.Exist(tag2)
  1187. throwFailNow(t, AssertIs(exist, false))
  1188. num, err = m2m.Count()
  1189. throwFailNow(t, err)
  1190. throwFailNow(t, AssertIs(num, 3))
  1191. num, err = m2m.Clear()
  1192. throwFailNow(t, err)
  1193. throwFailNow(t, AssertIs(num, 3))
  1194. num, err = m2m.Count()
  1195. throwFailNow(t, err)
  1196. throwFailNow(t, AssertIs(num, 0))
  1197. tag := Tag{Name: "test"}
  1198. _, err = dORM.Insert(&tag)
  1199. throwFailNow(t, err)
  1200. m2m = dORM.QueryM2M(&tag, "Posts")
  1201. post1 := []*Post{{Title: "TestPost1"}, {Title: "TestPost2"}}
  1202. post2 := &Post{Title: "TestPost3"}
  1203. post3 := []interface{}{&Post{Title: "TestPost4"}}
  1204. posts := []interface{}{post1[0], post1[1], post2, post3[0]}
  1205. for _, post := range posts {
  1206. p := post.(*Post)
  1207. p.User = &User{ID: 1}
  1208. _, err := dORM.Insert(post)
  1209. throwFailNow(t, err)
  1210. }
  1211. num, err = m2m.Add(post1)
  1212. throwFailNow(t, err)
  1213. throwFailNow(t, AssertIs(num, 2))
  1214. num, err = m2m.Add(post2)
  1215. throwFailNow(t, err)
  1216. throwFailNow(t, AssertIs(num, 1))
  1217. num, err = m2m.Add(post3)
  1218. throwFailNow(t, err)
  1219. throwFailNow(t, AssertIs(num, 1))
  1220. num, err = m2m.Count()
  1221. throwFailNow(t, err)
  1222. throwFailNow(t, AssertIs(num, 4))
  1223. num, err = m2m.Remove(post3)
  1224. throwFailNow(t, err)
  1225. throwFailNow(t, AssertIs(num, 1))
  1226. num, err = m2m.Count()
  1227. throwFailNow(t, err)
  1228. throwFailNow(t, AssertIs(num, 3))
  1229. exist = m2m.Exist(post2)
  1230. throwFailNow(t, AssertIs(exist, true))
  1231. num, err = m2m.Remove(post2)
  1232. throwFailNow(t, err)
  1233. throwFailNow(t, AssertIs(num, 1))
  1234. exist = m2m.Exist(post2)
  1235. throwFailNow(t, AssertIs(exist, false))
  1236. num, err = m2m.Count()
  1237. throwFailNow(t, err)
  1238. throwFailNow(t, AssertIs(num, 2))
  1239. num, err = m2m.Clear()
  1240. throwFailNow(t, err)
  1241. throwFailNow(t, AssertIs(num, 2))
  1242. num, err = m2m.Count()
  1243. throwFailNow(t, err)
  1244. throwFailNow(t, AssertIs(num, 0))
  1245. num, err = dORM.Delete(&tag)
  1246. throwFailNow(t, err)
  1247. throwFailNow(t, AssertIs(num, 1))
  1248. }
  1249. func TestQueryRelate(t *testing.T) {
  1250. // post := &Post{Id: 2}
  1251. // qs := dORM.QueryRelate(post, "Tags")
  1252. // num, err := qs.Count()
  1253. // throwFailNow(t, err)
  1254. // throwFailNow(t, AssertIs(num, 2))
  1255. // var tags []*Tag
  1256. // num, err = qs.All(&tags)
  1257. // throwFailNow(t, err)
  1258. // throwFailNow(t, AssertIs(num, 2))
  1259. // throwFailNow(t, AssertIs(tags[0].Name, "golang"))
  1260. // num, err = dORM.QueryTable("Tag").Filter("Posts__Post", 2).Count()
  1261. // throwFailNow(t, err)
  1262. // throwFailNow(t, AssertIs(num, 2))
  1263. }
  1264. func TestPkManyRelated(t *testing.T) {
  1265. permission := &Permission{Name: "readPosts"}
  1266. err := dORM.Read(permission, "Name")
  1267. throwFailNow(t, err)
  1268. var groups []*Group
  1269. qs := dORM.QueryTable("Group")
  1270. num, err := qs.Filter("Permissions__Permission", permission.ID).All(&groups)
  1271. throwFailNow(t, err)
  1272. throwFailNow(t, AssertIs(num, 2))
  1273. }
  1274. func TestPrepareInsert(t *testing.T) {
  1275. qs := dORM.QueryTable("user")
  1276. i, err := qs.PrepareInsert()
  1277. throwFailNow(t, err)
  1278. var user User
  1279. user.UserName = "testing1"
  1280. num, err := i.Insert(&user)
  1281. throwFail(t, err)
  1282. throwFail(t, AssertIs(num > 0, true))
  1283. user.UserName = "testing2"
  1284. num, err = i.Insert(&user)
  1285. throwFail(t, err)
  1286. throwFail(t, AssertIs(num > 0, true))
  1287. num, err = qs.Filter("user_name__in", "testing1", "testing2").Delete()
  1288. throwFail(t, err)
  1289. throwFail(t, AssertIs(num, 2))
  1290. err = i.Close()
  1291. throwFail(t, err)
  1292. err = i.Close()
  1293. throwFail(t, AssertIs(err, ErrStmtClosed))
  1294. }
  1295. func TestRawExec(t *testing.T) {
  1296. Q := dDbBaser.TableQuote()
  1297. query := fmt.Sprintf("UPDATE %suser%s SET %suser_name%s = ? WHERE %suser_name%s = ?", Q, Q, Q, Q, Q, Q)
  1298. res, err := dORM.Raw(query, "testing", "slene").Exec()
  1299. throwFail(t, err)
  1300. num, err := res.RowsAffected()
  1301. throwFail(t, AssertIs(num, 1), err)
  1302. res, err = dORM.Raw(query, "slene", "testing").Exec()
  1303. throwFail(t, err)
  1304. num, err = res.RowsAffected()
  1305. throwFail(t, AssertIs(num, 1), err)
  1306. }
  1307. func TestRawQueryRow(t *testing.T) {
  1308. var (
  1309. Boolean bool
  1310. Char string
  1311. Text string
  1312. Time time.Time
  1313. Date time.Time
  1314. DateTime time.Time
  1315. Byte byte
  1316. Rune rune
  1317. Int int
  1318. Int8 int
  1319. Int16 int16
  1320. Int32 int32
  1321. Int64 int64
  1322. Uint uint
  1323. Uint8 uint8
  1324. Uint16 uint16
  1325. Uint32 uint32
  1326. Uint64 uint64
  1327. Float32 float32
  1328. Float64 float64
  1329. Decimal float64
  1330. )
  1331. dataValues := make(map[string]interface{}, len(DataValues))
  1332. for k, v := range DataValues {
  1333. dataValues[strings.ToLower(k)] = v
  1334. }
  1335. Q := dDbBaser.TableQuote()
  1336. cols := []string{
  1337. "id", "boolean", "char", "text", "time", "date", "datetime", "byte", "rune", "int", "int8", "int16", "int32",
  1338. "int64", "uint", "uint8", "uint16", "uint32", "uint64", "float32", "float64", "decimal",
  1339. }
  1340. sep := fmt.Sprintf("%s, %s", Q, Q)
  1341. query := fmt.Sprintf("SELECT %s%s%s FROM data WHERE id = ?", Q, strings.Join(cols, sep), Q)
  1342. var id int
  1343. values := []interface{}{
  1344. &id, &Boolean, &Char, &Text, &Time, &Date, &DateTime, &Byte, &Rune, &Int, &Int8, &Int16, &Int32,
  1345. &Int64, &Uint, &Uint8, &Uint16, &Uint32, &Uint64, &Float32, &Float64, &Decimal,
  1346. }
  1347. err := dORM.Raw(query, 1).QueryRow(values...)
  1348. throwFailNow(t, err)
  1349. for i, col := range cols {
  1350. vu := values[i]
  1351. v := reflect.ValueOf(vu).Elem().Interface()
  1352. switch col {
  1353. case "id":
  1354. throwFail(t, AssertIs(id, 1))
  1355. case "time":
  1356. v = v.(time.Time).In(DefaultTimeLoc)
  1357. value := dataValues[col].(time.Time).In(DefaultTimeLoc)
  1358. throwFail(t, AssertIs(v, value, testTime))
  1359. case "date":
  1360. v = v.(time.Time).In(DefaultTimeLoc)
  1361. value := dataValues[col].(time.Time).In(DefaultTimeLoc)
  1362. throwFail(t, AssertIs(v, value, testDate))
  1363. case "datetime":
  1364. v = v.(time.Time).In(DefaultTimeLoc)
  1365. value := dataValues[col].(time.Time).In(DefaultTimeLoc)
  1366. throwFail(t, AssertIs(v, value, testDateTime))
  1367. default:
  1368. throwFail(t, AssertIs(v, dataValues[col]))
  1369. }
  1370. }
  1371. var (
  1372. uid int
  1373. status *int
  1374. pid *int
  1375. )
  1376. cols = []string{
  1377. "id", "Status", "profile_id",
  1378. }
  1379. query = fmt.Sprintf("SELECT %s%s%s FROM %suser%s WHERE id = ?", Q, strings.Join(cols, sep), Q, Q, Q)
  1380. err = dORM.Raw(query, 4).QueryRow(&uid, &status, &pid)
  1381. throwFail(t, err)
  1382. throwFail(t, AssertIs(uid, 4))
  1383. throwFail(t, AssertIs(*status, 3))
  1384. throwFail(t, AssertIs(pid, nil))
  1385. }
  1386. func TestQueryRows(t *testing.T) {
  1387. Q := dDbBaser.TableQuote()
  1388. var datas []*Data
  1389. query := fmt.Sprintf("SELECT * FROM %sdata%s", Q, Q)
  1390. num, err := dORM.Raw(query).QueryRows(&datas)
  1391. throwFailNow(t, err)
  1392. throwFailNow(t, AssertIs(num, 1))
  1393. throwFailNow(t, AssertIs(len(datas), 1))
  1394. ind := reflect.Indirect(reflect.ValueOf(datas[0]))
  1395. for name, value := range DataValues {
  1396. e := ind.FieldByName(name)
  1397. vu := e.Interface()
  1398. switch name {
  1399. case "Time":
  1400. vu = vu.(time.Time).In(DefaultTimeLoc).Format(testTime)
  1401. value = value.(time.Time).In(DefaultTimeLoc).Format(testTime)
  1402. case "Date":
  1403. vu = vu.(time.Time).In(DefaultTimeLoc).Format(testDate)
  1404. value = value.(time.Time).In(DefaultTimeLoc).Format(testDate)
  1405. case "DateTime":
  1406. vu = vu.(time.Time).In(DefaultTimeLoc).Format(testDateTime)
  1407. value = value.(time.Time).In(DefaultTimeLoc).Format(testDateTime)
  1408. }
  1409. throwFail(t, AssertIs(vu == value, true), value, vu)
  1410. }
  1411. var datas2 []Data
  1412. query = fmt.Sprintf("SELECT * FROM %sdata%s", Q, Q)
  1413. num, err = dORM.Raw(query).QueryRows(&datas2)
  1414. throwFailNow(t, err)
  1415. throwFailNow(t, AssertIs(num, 1))
  1416. throwFailNow(t, AssertIs(len(datas2), 1))
  1417. ind = reflect.Indirect(reflect.ValueOf(datas2[0]))
  1418. for name, value := range DataValues {
  1419. e := ind.FieldByName(name)
  1420. vu := e.Interface()
  1421. switch name {
  1422. case "Time":
  1423. vu = vu.(time.Time).In(DefaultTimeLoc).Format(testTime)
  1424. value = value.(time.Time).In(DefaultTimeLoc).Format(testTime)
  1425. case "Date":
  1426. vu = vu.(time.Time).In(DefaultTimeLoc).Format(testDate)
  1427. value = value.(time.Time).In(DefaultTimeLoc).Format(testDate)
  1428. case "DateTime":
  1429. vu = vu.(time.Time).In(DefaultTimeLoc).Format(testDateTime)
  1430. value = value.(time.Time).In(DefaultTimeLoc).Format(testDateTime)
  1431. }
  1432. throwFail(t, AssertIs(vu == value, true), value, vu)
  1433. }
  1434. var ids []int
  1435. var usernames []string
  1436. query = fmt.Sprintf("SELECT %sid%s, %suser_name%s FROM %suser%s ORDER BY %sid%s ASC", Q, Q, Q, Q, Q, Q, Q, Q)
  1437. num, err = dORM.Raw(query).QueryRows(&ids, &usernames)
  1438. throwFailNow(t, err)
  1439. throwFailNow(t, AssertIs(num, 3))
  1440. throwFailNow(t, AssertIs(len(ids), 3))
  1441. throwFailNow(t, AssertIs(ids[0], 2))
  1442. throwFailNow(t, AssertIs(usernames[0], "slene"))
  1443. throwFailNow(t, AssertIs(ids[1], 3))
  1444. throwFailNow(t, AssertIs(usernames[1], "astaxie"))
  1445. throwFailNow(t, AssertIs(ids[2], 4))
  1446. throwFailNow(t, AssertIs(usernames[2], "nobody"))
  1447. }
  1448. func TestRawValues(t *testing.T) {
  1449. Q := dDbBaser.TableQuote()
  1450. var maps []Params
  1451. query := fmt.Sprintf("SELECT %suser_name%s FROM %suser%s WHERE %sStatus%s = ?", Q, Q, Q, Q, Q, Q)
  1452. num, err := dORM.Raw(query, 1).Values(&maps)
  1453. throwFail(t, err)
  1454. throwFail(t, AssertIs(num, 1))
  1455. if num == 1 {
  1456. throwFail(t, AssertIs(maps[0]["user_name"], "slene"))
  1457. }
  1458. var lists []ParamsList
  1459. num, err = dORM.Raw(query, 1).ValuesList(&lists)
  1460. throwFail(t, err)
  1461. throwFail(t, AssertIs(num, 1))
  1462. if num == 1 {
  1463. throwFail(t, AssertIs(lists[0][0], "slene"))
  1464. }
  1465. query = fmt.Sprintf("SELECT %sprofile_id%s FROM %suser%s ORDER BY %sid%s ASC", Q, Q, Q, Q, Q, Q)
  1466. var list ParamsList
  1467. num, err = dORM.Raw(query).ValuesFlat(&list)
  1468. throwFail(t, err)
  1469. throwFail(t, AssertIs(num, 3))
  1470. if num == 3 {
  1471. throwFail(t, AssertIs(list[0], "2"))
  1472. throwFail(t, AssertIs(list[1], "3"))
  1473. throwFail(t, AssertIs(list[2], nil))
  1474. }
  1475. }
  1476. func TestRawPrepare(t *testing.T) {
  1477. switch {
  1478. case IsMysql || IsSqlite:
  1479. pre, err := dORM.Raw("INSERT INTO tag (name) VALUES (?)").Prepare()
  1480. throwFail(t, err)
  1481. if pre != nil {
  1482. r, err := pre.Exec("name1")
  1483. throwFail(t, err)
  1484. tid, err := r.LastInsertId()
  1485. throwFail(t, err)
  1486. throwFail(t, AssertIs(tid > 0, true))
  1487. r, err = pre.Exec("name2")
  1488. throwFail(t, err)
  1489. id, err := r.LastInsertId()
  1490. throwFail(t, err)
  1491. throwFail(t, AssertIs(id, tid+1))
  1492. r, err = pre.Exec("name3")
  1493. throwFail(t, err)
  1494. id, err = r.LastInsertId()
  1495. throwFail(t, err)
  1496. throwFail(t, AssertIs(id, tid+2))
  1497. err = pre.Close()
  1498. throwFail(t, err)
  1499. res, err := dORM.Raw("DELETE FROM tag WHERE name IN (?, ?, ?)", []string{"name1", "name2", "name3"}).Exec()
  1500. throwFail(t, err)
  1501. num, err := res.RowsAffected()
  1502. throwFail(t, err)
  1503. throwFail(t, AssertIs(num, 3))
  1504. }
  1505. case IsPostgres:
  1506. pre, err := dORM.Raw(`INSERT INTO "tag" ("name") VALUES (?) RETURNING "id"`).Prepare()
  1507. throwFail(t, err)
  1508. if pre != nil {
  1509. _, err := pre.Exec("name1")
  1510. throwFail(t, err)
  1511. _, err = pre.Exec("name2")
  1512. throwFail(t, err)
  1513. _, err = pre.Exec("name3")
  1514. throwFail(t, err)
  1515. err = pre.Close()
  1516. throwFail(t, err)
  1517. res, err := dORM.Raw(`DELETE FROM "tag" WHERE "name" IN (?, ?, ?)`, []string{"name1", "name2", "name3"}).Exec()
  1518. throwFail(t, err)
  1519. if err == nil {
  1520. num, err := res.RowsAffected()
  1521. throwFail(t, err)
  1522. throwFail(t, AssertIs(num, 3))
  1523. }
  1524. }
  1525. }
  1526. }
  1527. func TestUpdate(t *testing.T) {
  1528. qs := dORM.QueryTable("user")
  1529. num, err := qs.Filter("user_name", "slene").Filter("is_staff", false).Update(Params{
  1530. "is_staff": true,
  1531. "is_active": true,
  1532. })
  1533. throwFail(t, err)
  1534. throwFail(t, AssertIs(num, 1))
  1535. // with join
  1536. num, err = qs.Filter("user_name", "slene").Filter("profile__age", 28).Filter("is_staff", true).Update(Params{
  1537. "is_staff": false,
  1538. })
  1539. throwFail(t, err)
  1540. throwFail(t, AssertIs(num, 1))
  1541. num, err = qs.Filter("user_name", "slene").Update(Params{
  1542. "Nums": ColValue(ColAdd, 100),
  1543. })
  1544. throwFail(t, err)
  1545. throwFail(t, AssertIs(num, 1))
  1546. num, err = qs.Filter("user_name", "slene").Update(Params{
  1547. "Nums": ColValue(ColMinus, 50),
  1548. })
  1549. throwFail(t, err)
  1550. throwFail(t, AssertIs(num, 1))
  1551. num, err = qs.Filter("user_name", "slene").Update(Params{
  1552. "Nums": ColValue(ColMultiply, 3),
  1553. })
  1554. throwFail(t, err)
  1555. throwFail(t, AssertIs(num, 1))
  1556. num, err = qs.Filter("user_name", "slene").Update(Params{
  1557. "Nums": ColValue(ColExcept, 5),
  1558. })
  1559. throwFail(t, err)
  1560. throwFail(t, AssertIs(num, 1))
  1561. user := User{UserName: "slene"}
  1562. err = dORM.Read(&user, "UserName")
  1563. throwFail(t, err)
  1564. throwFail(t, AssertIs(user.Nums, 30))
  1565. }
  1566. func TestDelete(t *testing.T) {
  1567. qs := dORM.QueryTable("user_profile")
  1568. num, err := qs.Filter("user__user_name", "slene").Delete()
  1569. throwFail(t, err)
  1570. throwFail(t, AssertIs(num, 1))
  1571. qs = dORM.QueryTable("user")
  1572. num, err = qs.Filter("user_name", "slene").Filter("profile__isnull", true).Count()
  1573. throwFail(t, err)
  1574. throwFail(t, AssertIs(num, 1))
  1575. qs = dORM.QueryTable("comment")
  1576. num, err = qs.Count()
  1577. throwFail(t, err)
  1578. throwFail(t, AssertIs(num, 6))
  1579. qs = dORM.QueryTable("post")
  1580. num, err = qs.Filter("Id", 3).Delete()
  1581. throwFail(t, err)
  1582. throwFail(t, AssertIs(num, 1))
  1583. qs = dORM.QueryTable("comment")
  1584. num, err = qs.Count()
  1585. throwFail(t, err)
  1586. throwFail(t, AssertIs(num, 4))
  1587. qs = dORM.QueryTable("comment")
  1588. num, err = qs.Filter("Post__User", 3).Delete()
  1589. throwFail(t, err)
  1590. throwFail(t, AssertIs(num, 3))
  1591. qs = dORM.QueryTable("comment")
  1592. num, err = qs.Count()
  1593. throwFail(t, err)
  1594. throwFail(t, AssertIs(num, 1))
  1595. }
  1596. func TestTransaction(t *testing.T) {
  1597. // this test worked when database support transaction
  1598. o := NewOrm()
  1599. err := o.Begin()
  1600. throwFail(t, err)
  1601. var names = []string{"1", "2", "3"}
  1602. var tag Tag
  1603. tag.Name = names[0]
  1604. id, err := o.Insert(&tag)
  1605. throwFail(t, err)
  1606. throwFail(t, AssertIs(id > 0, true))
  1607. num, err := o.QueryTable("tag").Filter("name", "golang").Update(Params{"name": names[1]})
  1608. throwFail(t, err)
  1609. throwFail(t, AssertIs(num, 1))
  1610. switch {
  1611. case IsMysql || IsSqlite:
  1612. res, err := o.Raw("INSERT INTO tag (name) VALUES (?)", names[2]).Exec()
  1613. throwFail(t, err)
  1614. if err == nil {
  1615. id, err = res.LastInsertId()
  1616. throwFail(t, err)
  1617. throwFail(t, AssertIs(id > 0, true))
  1618. }
  1619. }
  1620. err = o.Rollback()
  1621. throwFail(t, err)
  1622. num, err = o.QueryTable("tag").Filter("name__in", names).Count()
  1623. throwFail(t, err)
  1624. throwFail(t, AssertIs(num, 0))
  1625. err = o.Begin()
  1626. throwFail(t, err)
  1627. tag.Name = "commit"
  1628. id, err = o.Insert(&tag)
  1629. throwFail(t, err)
  1630. throwFail(t, AssertIs(id > 0, true))
  1631. o.Commit()
  1632. throwFail(t, err)
  1633. num, err = o.QueryTable("tag").Filter("name", "commit").Delete()
  1634. throwFail(t, err)
  1635. throwFail(t, AssertIs(num, 1))
  1636. }
  1637. func TestReadOrCreate(t *testing.T) {
  1638. u := &User{
  1639. UserName: "Kyle",
  1640. Email: "kylemcc@gmail.com",
  1641. Password: "other_pass",
  1642. Status: 7,
  1643. IsStaff: false,
  1644. IsActive: true,
  1645. }
  1646. created, pk, err := dORM.ReadOrCreate(u, "UserName")
  1647. throwFail(t, err)
  1648. throwFail(t, AssertIs(created, true))
  1649. throwFail(t, AssertIs(u.UserName, "Kyle"))
  1650. throwFail(t, AssertIs(u.Email, "kylemcc@gmail.com"))
  1651. throwFail(t, AssertIs(u.Password, "other_pass"))
  1652. throwFail(t, AssertIs(u.Status, 7))
  1653. throwFail(t, AssertIs(u.IsStaff, false))
  1654. throwFail(t, AssertIs(u.IsActive, true))
  1655. throwFail(t, AssertIs(u.Created.In(DefaultTimeLoc), u.Created.In(DefaultTimeLoc), testDate))
  1656. throwFail(t, AssertIs(u.Updated.In(DefaultTimeLoc), u.Updated.In(DefaultTimeLoc), testDateTime))
  1657. nu := &User{UserName: u.UserName, Email: "someotheremail@gmail.com"}
  1658. created, pk, err = dORM.ReadOrCreate(nu, "UserName")
  1659. throwFail(t, err)
  1660. throwFail(t, AssertIs(created, false))
  1661. throwFail(t, AssertIs(nu.ID, u.ID))
  1662. throwFail(t, AssertIs(pk, u.ID))
  1663. throwFail(t, AssertIs(nu.UserName, u.UserName))
  1664. throwFail(t, AssertIs(nu.Email, u.Email)) // should contain the value in the table, not the one specified above
  1665. throwFail(t, AssertIs(nu.Password, u.Password))
  1666. throwFail(t, AssertIs(nu.Status, u.Status))
  1667. throwFail(t, AssertIs(nu.IsStaff, u.IsStaff))
  1668. throwFail(t, AssertIs(nu.IsActive, u.IsActive))
  1669. dORM.Delete(u)
  1670. }
  1671. func TestInLine(t *testing.T) {
  1672. name := "inline"
  1673. email := "hello@go.com"
  1674. inline := NewInLine()
  1675. inline.Name = name
  1676. inline.Email = email
  1677. id, err := dORM.Insert(inline)
  1678. throwFail(t, err)
  1679. throwFail(t, AssertIs(id, 1))
  1680. il := NewInLine()
  1681. il.ID = 1
  1682. err = dORM.Read(il)
  1683. throwFail(t, err)
  1684. throwFail(t, AssertIs(il.Name, name))
  1685. throwFail(t, AssertIs(il.Email, email))
  1686. throwFail(t, AssertIs(il.Created.In(DefaultTimeLoc), inline.Created.In(DefaultTimeLoc), testDate))
  1687. throwFail(t, AssertIs(il.Updated.In(DefaultTimeLoc), inline.Updated.In(DefaultTimeLoc), testDateTime))
  1688. }
  1689. func TestInLineOneToOne(t *testing.T) {
  1690. name := "121"
  1691. email := "121@go.com"
  1692. inline := NewInLine()
  1693. inline.Name = name
  1694. inline.Email = email
  1695. id, err := dORM.Insert(inline)
  1696. throwFail(t, err)
  1697. throwFail(t, AssertIs(id, 2))
  1698. note := "one2one"
  1699. il121 := NewInLineOneToOne()
  1700. il121.Note = note
  1701. il121.InLine = inline
  1702. _, err = dORM.Insert(il121)
  1703. throwFail(t, err)
  1704. throwFail(t, AssertIs(il121.ID, 1))
  1705. il := NewInLineOneToOne()
  1706. err = dORM.QueryTable(il).Filter("Id", 1).RelatedSel().One(il)
  1707. throwFail(t, err)
  1708. throwFail(t, AssertIs(il.Note, note))
  1709. throwFail(t, AssertIs(il.InLine.ID, id))
  1710. throwFail(t, AssertIs(il.InLine.Name, name))
  1711. throwFail(t, AssertIs(il.InLine.Email, email))
  1712. rinline := NewInLine()
  1713. err = dORM.QueryTable(rinline).Filter("InLineOneToOne__Id", 1).One(rinline)
  1714. throwFail(t, err)
  1715. throwFail(t, AssertIs(rinline.ID, id))
  1716. throwFail(t, AssertIs(rinline.Name, name))
  1717. throwFail(t, AssertIs(rinline.Email, email))
  1718. }
  1719. func TestIntegerPk(t *testing.T) {
  1720. its := []IntegerPk{
  1721. {ID: math.MinInt64, Value: "-"},
  1722. {ID: 0, Value: "0"},
  1723. {ID: math.MaxInt64, Value: "+"},
  1724. }
  1725. num, err := dORM.InsertMulti(len(its), its)
  1726. throwFail(t, err)
  1727. throwFail(t, AssertIs(num, len(its)))
  1728. for _, intPk := range its {
  1729. out := IntegerPk{ID: intPk.ID}
  1730. err = dORM.Read(&out)
  1731. throwFail(t, err)
  1732. throwFail(t, AssertIs(out.Value, intPk.Value))
  1733. }
  1734. num, err = dORM.InsertMulti(1, []*IntegerPk{{
  1735. ID: 1, Value: "ok",
  1736. }})
  1737. throwFail(t, err)
  1738. throwFail(t, AssertIs(num, 1))
  1739. }
  1740. func TestInsertAuto(t *testing.T) {
  1741. u := &User{
  1742. UserName: "autoPre",
  1743. Email: "autoPre@gmail.com",
  1744. }
  1745. id, err := dORM.Insert(u)
  1746. throwFail(t, err)
  1747. id += 100
  1748. su := &User{
  1749. ID: int(id),
  1750. UserName: "auto",
  1751. Email: "auto@gmail.com",
  1752. }
  1753. nid, err := dORM.Insert(su)
  1754. throwFail(t, err)
  1755. throwFail(t, AssertIs(nid, id))
  1756. users := []User{
  1757. {ID: int(id + 100), UserName: "auto_100"},
  1758. {ID: int(id + 110), UserName: "auto_110"},
  1759. {ID: int(id + 120), UserName: "auto_120"},
  1760. }
  1761. num, err := dORM.InsertMulti(100, users)
  1762. throwFail(t, err)
  1763. throwFail(t, AssertIs(num, 3))
  1764. u = &User{
  1765. UserName: "auto_121",
  1766. }
  1767. nid, err = dORM.Insert(u)
  1768. throwFail(t, err)
  1769. throwFail(t, AssertIs(nid, id+120+1))
  1770. }
  1771. func TestUintPk(t *testing.T) {
  1772. name := "go"
  1773. u := &UintPk{
  1774. ID: 8,
  1775. Name: name,
  1776. }
  1777. created, pk, err := dORM.ReadOrCreate(u, "ID")
  1778. throwFail(t, err)
  1779. throwFail(t, AssertIs(created, true))
  1780. throwFail(t, AssertIs(u.Name, name))
  1781. nu := &UintPk{ID: 8}
  1782. created, pk, err = dORM.ReadOrCreate(nu, "ID")
  1783. throwFail(t, err)
  1784. throwFail(t, AssertIs(created, false))
  1785. throwFail(t, AssertIs(nu.ID, u.ID))
  1786. throwFail(t, AssertIs(pk, u.ID))
  1787. throwFail(t, AssertIs(nu.Name, name))
  1788. dORM.Delete(u)
  1789. }
  1790. func TestPtrPk(t *testing.T) {
  1791. parent := &IntegerPk{ID: 10, Value: "10"}
  1792. id, _ := dORM.Insert(parent)
  1793. if !IsMysql {
  1794. // MySql does not support last_insert_id in this case: see #2382
  1795. throwFail(t, AssertIs(id, 10))
  1796. }
  1797. ptr := PtrPk{ID: parent, Positive: true}
  1798. num, err := dORM.InsertMulti(2, []PtrPk{ptr})
  1799. throwFail(t, err)
  1800. throwFail(t, AssertIs(num, 1))
  1801. throwFail(t, AssertIs(ptr.ID, parent))
  1802. nptr := &PtrPk{ID: parent}
  1803. created, pk, err := dORM.ReadOrCreate(nptr, "ID")
  1804. throwFail(t, err)
  1805. throwFail(t, AssertIs(created, false))
  1806. throwFail(t, AssertIs(pk, 10))
  1807. throwFail(t, AssertIs(nptr.ID, parent))
  1808. throwFail(t, AssertIs(nptr.Positive, true))
  1809. nptr = &PtrPk{Positive: true}
  1810. created, pk, err = dORM.ReadOrCreate(nptr, "Positive")
  1811. throwFail(t, err)
  1812. throwFail(t, AssertIs(created, false))
  1813. throwFail(t, AssertIs(pk, 10))
  1814. throwFail(t, AssertIs(nptr.ID, parent))
  1815. nptr.Positive = false
  1816. num, err = dORM.Update(nptr)
  1817. throwFail(t, err)
  1818. throwFail(t, AssertIs(num, 1))
  1819. throwFail(t, AssertIs(nptr.ID, parent))
  1820. throwFail(t, AssertIs(nptr.Positive, false))
  1821. num, err = dORM.Delete(nptr)
  1822. throwFail(t, err)
  1823. throwFail(t, AssertIs(num, 1))
  1824. }
  1825. func TestSnake(t *testing.T) {
  1826. cases := map[string]string{
  1827. "i": "i",
  1828. "I": "i",
  1829. "iD": "i_d",
  1830. "ID": "i_d",
  1831. "NO": "n_o",
  1832. "NOO": "n_o_o",
  1833. "NOOooOOoo": "n_o_ooo_o_ooo",
  1834. "OrderNO": "order_n_o",
  1835. "tagName": "tag_name",
  1836. "tag_Name": "tag__name",
  1837. "tag_name": "tag_name",
  1838. "_tag_name": "_tag_name",
  1839. "tag_666name": "tag_666name",
  1840. "tag_666Name": "tag_666_name",
  1841. }
  1842. for name, want := range cases {
  1843. got := snakeString(name)
  1844. throwFail(t, AssertIs(got, want))
  1845. }
  1846. }
  1847. func TestIgnoreCaseTag(t *testing.T) {
  1848. type testTagModel struct {
  1849. ID int `orm:"pk"`
  1850. NOO string `orm:"column(n)"`
  1851. Name01 string `orm:"NULL"`
  1852. Name02 string `orm:"COLUMN(Name)"`
  1853. Name03 string `orm:"Column(name)"`
  1854. }
  1855. modelCache.clean()
  1856. RegisterModel(&testTagModel{})
  1857. info, ok := modelCache.get("test_tag_model")
  1858. throwFail(t, AssertIs(ok, true))
  1859. throwFail(t, AssertNot(info, nil))
  1860. if t == nil {
  1861. return
  1862. }
  1863. throwFail(t, AssertIs(info.fields.GetByName("NOO").column, "n"))
  1864. throwFail(t, AssertIs(info.fields.GetByName("Name01").null, true))
  1865. throwFail(t, AssertIs(info.fields.GetByName("Name02").column, "Name"))
  1866. throwFail(t, AssertIs(info.fields.GetByName("Name03").column, "name"))
  1867. }
  1868. func TestInsertOrUpdate(t *testing.T) {
  1869. RegisterModel(new(User))
  1870. user := User{UserName: "unique_username133", Status: 1, Password: "o"}
  1871. user1 := User{UserName: "unique_username133", Status: 2, Password: "o"}
  1872. user2 := User{UserName: "unique_username133", Status: 3, Password: "oo"}
  1873. dORM.Insert(&user)
  1874. test := User{UserName: "unique_username133"}
  1875. fmt.Println(dORM.Driver().Name())
  1876. if dORM.Driver().Name() == "sqlite3" {
  1877. fmt.Println("sqlite3 is nonsupport")
  1878. return
  1879. }
  1880. //test1
  1881. _, err := dORM.InsertOrUpdate(&user1, "user_name")
  1882. if err != nil {
  1883. fmt.Println(err)
  1884. if err.Error() == "postgres version must 9.5 or higher" || err.Error() == "`sqlite3` nonsupport InsertOrUpdate in beego" {
  1885. } else {
  1886. throwFailNow(t, err)
  1887. }
  1888. } else {
  1889. dORM.Read(&test, "user_name")
  1890. throwFailNow(t, AssertIs(user1.Status, test.Status))
  1891. }
  1892. //test2
  1893. _, err = dORM.InsertOrUpdate(&user2, "user_name")
  1894. if err != nil {
  1895. fmt.Println(err)
  1896. if err.Error() == "postgres version must 9.5 or higher" || err.Error() == "`sqlite3` nonsupport InsertOrUpdate in beego" {
  1897. } else {
  1898. throwFailNow(t, err)
  1899. }
  1900. } else {
  1901. dORM.Read(&test, "user_name")
  1902. throwFailNow(t, AssertIs(user2.Status, test.Status))
  1903. throwFailNow(t, AssertIs(user2.Password, strings.TrimSpace(test.Password)))
  1904. }
  1905. //test3 +
  1906. _, err = dORM.InsertOrUpdate(&user2, "user_name", "status=status+1")
  1907. if err != nil {
  1908. fmt.Println(err)
  1909. if err.Error() == "postgres version must 9.5 or higher" || err.Error() == "`sqlite3` nonsupport InsertOrUpdate in beego" {
  1910. } else {
  1911. throwFailNow(t, err)
  1912. }
  1913. } else {
  1914. dORM.Read(&test, "user_name")
  1915. throwFailNow(t, AssertIs(user2.Status+1, test.Status))
  1916. }
  1917. //test4 -
  1918. _, err = dORM.InsertOrUpdate(&user2, "user_name", "status=status-1")
  1919. if err != nil {
  1920. fmt.Println(err)
  1921. if err.Error() == "postgres version must 9.5 or higher" || err.Error() == "`sqlite3` nonsupport InsertOrUpdate in beego" {
  1922. } else {
  1923. throwFailNow(t, err)
  1924. }
  1925. } else {
  1926. dORM.Read(&test, "user_name")
  1927. throwFailNow(t, AssertIs((user2.Status+1)-1, test.Status))
  1928. }
  1929. //test5 *
  1930. _, err = dORM.InsertOrUpdate(&user2, "user_name", "status=status*3")
  1931. if err != nil {
  1932. fmt.Println(err)
  1933. if err.Error() == "postgres version must 9.5 or higher" || err.Error() == "`sqlite3` nonsupport InsertOrUpdate in beego" {
  1934. } else {
  1935. throwFailNow(t, err)
  1936. }
  1937. } else {
  1938. dORM.Read(&test, "user_name")
  1939. throwFailNow(t, AssertIs(((user2.Status+1)-1)*3, test.Status))
  1940. }
  1941. //test6 /
  1942. _, err = dORM.InsertOrUpdate(&user2, "user_name", "Status=Status/3")
  1943. if err != nil {
  1944. fmt.Println(err)
  1945. if err.Error() == "postgres version must 9.5 or higher" || err.Error() == "`sqlite3` nonsupport InsertOrUpdate in beego" {
  1946. } else {
  1947. throwFailNow(t, err)
  1948. }
  1949. } else {
  1950. dORM.Read(&test, "user_name")
  1951. throwFailNow(t, AssertIs((((user2.Status+1)-1)*3)/3, test.Status))
  1952. }
  1953. }