Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3,mssql,oracle https://xorm.io
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

804 lines
18KB

  1. // Copyright 2017 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. import (
  6. "errors"
  7. "fmt"
  8. "testing"
  9. "time"
  10. "xorm.io/core"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestJoinLimit(t *testing.T) {
  14. assert.NoError(t, prepareEngine())
  15. type Salary struct {
  16. Id int64
  17. Lid int64
  18. }
  19. type CheckList struct {
  20. Id int64
  21. Eid int64
  22. }
  23. type Empsetting struct {
  24. Id int64
  25. Name string
  26. }
  27. assert.NoError(t, testEngine.Sync2(new(Salary), new(CheckList), new(Empsetting)))
  28. var emp Empsetting
  29. cnt, err := testEngine.Insert(&emp)
  30. assert.NoError(t, err)
  31. assert.EqualValues(t, 1, cnt)
  32. var checklist = CheckList{
  33. Eid: emp.Id,
  34. }
  35. cnt, err = testEngine.Insert(&checklist)
  36. assert.NoError(t, err)
  37. assert.EqualValues(t, 1, cnt)
  38. var salary = Salary{
  39. Lid: checklist.Id,
  40. }
  41. cnt, err = testEngine.Insert(&salary)
  42. assert.NoError(t, err)
  43. assert.EqualValues(t, 1, cnt)
  44. var salaries []Salary
  45. err = testEngine.Table("salary").
  46. Join("INNER", "check_list", "check_list.id = salary.lid").
  47. Join("LEFT", "empsetting", "empsetting.id = check_list.eid").
  48. Limit(10, 0).
  49. Find(&salaries)
  50. assert.NoError(t, err)
  51. }
  52. func assertSync(t *testing.T, beans ...interface{}) {
  53. for _, bean := range beans {
  54. assert.NoError(t, testEngine.DropTables(bean))
  55. assert.NoError(t, testEngine.Sync2(bean))
  56. }
  57. }
  58. func TestWhere(t *testing.T) {
  59. assert.NoError(t, prepareEngine())
  60. assertSync(t, new(Userinfo))
  61. users := make([]Userinfo, 0)
  62. err := testEngine.Where("(id) > ?", 2).Find(&users)
  63. if err != nil {
  64. t.Error(err)
  65. panic(err)
  66. }
  67. fmt.Println(users)
  68. err = testEngine.Where("(id) > ?", 2).And("(id) < ?", 10).Find(&users)
  69. if err != nil {
  70. t.Error(err)
  71. panic(err)
  72. }
  73. fmt.Println(users)
  74. }
  75. func TestFind(t *testing.T) {
  76. assert.NoError(t, prepareEngine())
  77. assertSync(t, new(Userinfo))
  78. users := make([]Userinfo, 0)
  79. err := testEngine.Find(&users)
  80. assert.NoError(t, err)
  81. for _, user := range users {
  82. fmt.Println(user)
  83. }
  84. users2 := make([]Userinfo, 0)
  85. var tbName = testEngine.Quote(testEngine.TableName(new(Userinfo), true))
  86. err = testEngine.SQL("select * from " + tbName).Find(&users2)
  87. assert.NoError(t, err)
  88. }
  89. func TestFind2(t *testing.T) {
  90. assert.NoError(t, prepareEngine())
  91. users := make([]*Userinfo, 0)
  92. assertSync(t, new(Userinfo))
  93. err := testEngine.Find(&users)
  94. assert.NoError(t, err)
  95. for _, user := range users {
  96. fmt.Println(user)
  97. }
  98. }
  99. type Team struct {
  100. Id int64
  101. }
  102. type TeamUser struct {
  103. OrgId int64
  104. Uid int64
  105. TeamId int64
  106. }
  107. func (TeamUser) TableName() string {
  108. return "team_user"
  109. }
  110. func TestFind3(t *testing.T) {
  111. var teamUser = new(TeamUser)
  112. assert.NoError(t, prepareEngine())
  113. err := testEngine.Sync2(new(Team), teamUser)
  114. assert.NoError(t, err)
  115. var teams []Team
  116. err = testEngine.Cols("`team`.id").
  117. Where("`team_user`.org_id=?", 1).
  118. And("`team_user`.uid=?", 2).
  119. Join("INNER", "`team_user`", "`team_user`.team_id=`team`.id").
  120. Find(&teams)
  121. assert.NoError(t, err)
  122. teams = make([]Team, 0)
  123. err = testEngine.Cols("`team`.id").
  124. Where("`team_user`.org_id=?", 1).
  125. And("`team_user`.uid=?", 2).
  126. Join("INNER", teamUser, "`team_user`.team_id=`team`.id").
  127. Find(&teams)
  128. assert.NoError(t, err)
  129. teams = make([]Team, 0)
  130. err = testEngine.Cols("`team`.id").
  131. Where("`team_user`.org_id=?", 1).
  132. And("`team_user`.uid=?", 2).
  133. Join("INNER", []interface{}{teamUser}, "`team_user`.team_id=`team`.id").
  134. Find(&teams)
  135. assert.NoError(t, err)
  136. teams = make([]Team, 0)
  137. err = testEngine.Cols("`team`.id").
  138. Where("`tu`.org_id=?", 1).
  139. And("`tu`.uid=?", 2).
  140. Join("INNER", []string{"team_user", "tu"}, "`tu`.team_id=`team`.id").
  141. Find(&teams)
  142. assert.NoError(t, err)
  143. teams = make([]Team, 0)
  144. err = testEngine.Cols("`team`.id").
  145. Where("`tu`.org_id=?", 1).
  146. And("`tu`.uid=?", 2).
  147. Join("INNER", []interface{}{"team_user", "tu"}, "`tu`.team_id=`team`.id").
  148. Find(&teams)
  149. assert.NoError(t, err)
  150. teams = make([]Team, 0)
  151. err = testEngine.Cols("`team`.id").
  152. Where("`tu`.org_id=?", 1).
  153. And("`tu`.uid=?", 2).
  154. Join("INNER", []interface{}{teamUser, "tu"}, "`tu`.team_id=`team`.id").
  155. Find(&teams)
  156. assert.NoError(t, err)
  157. }
  158. func TestFindMap(t *testing.T) {
  159. assert.NoError(t, prepareEngine())
  160. assertSync(t, new(Userinfo))
  161. users := make(map[int64]Userinfo)
  162. err := testEngine.Find(&users)
  163. if err != nil {
  164. t.Error(err)
  165. panic(err)
  166. }
  167. for _, user := range users {
  168. fmt.Println(user)
  169. }
  170. }
  171. func TestFindMap2(t *testing.T) {
  172. assert.NoError(t, prepareEngine())
  173. assertSync(t, new(Userinfo))
  174. users := make(map[int64]*Userinfo)
  175. err := testEngine.Find(&users)
  176. if err != nil {
  177. t.Error(err)
  178. panic(err)
  179. }
  180. for id, user := range users {
  181. fmt.Println(id, user)
  182. }
  183. }
  184. func TestDistinct(t *testing.T) {
  185. assert.NoError(t, prepareEngine())
  186. assertSync(t, new(Userinfo))
  187. _, err := testEngine.Insert(&Userinfo{
  188. Username: "lunny",
  189. })
  190. assert.NoError(t, err)
  191. users := make([]Userinfo, 0)
  192. departname := testEngine.GetTableMapper().Obj2Table("Departname")
  193. err = testEngine.Distinct(departname).Find(&users)
  194. assert.NoError(t, err)
  195. assert.EqualValues(t, 1, len(users))
  196. fmt.Println(users)
  197. type Depart struct {
  198. Departname string
  199. }
  200. users2 := make([]Depart, 0)
  201. err = testEngine.Distinct(departname).Table(new(Userinfo)).Find(&users2)
  202. assert.NoError(t, err)
  203. if len(users2) != 1 {
  204. fmt.Println(len(users2))
  205. t.Error(err)
  206. panic(errors.New("should be one record"))
  207. }
  208. fmt.Println(users2)
  209. }
  210. func TestOrder(t *testing.T) {
  211. assert.NoError(t, prepareEngine())
  212. assertSync(t, new(Userinfo))
  213. users := make([]Userinfo, 0)
  214. err := testEngine.OrderBy("id desc").Find(&users)
  215. assert.NoError(t, err)
  216. fmt.Println(users)
  217. users2 := make([]Userinfo, 0)
  218. err = testEngine.Asc("id", "username").Desc("height").Find(&users2)
  219. assert.NoError(t, err)
  220. fmt.Println(users2)
  221. }
  222. func TestGroupBy(t *testing.T) {
  223. assert.NoError(t, prepareEngine())
  224. assertSync(t, new(Userinfo))
  225. users := make([]Userinfo, 0)
  226. err := testEngine.GroupBy("id, username").Find(&users)
  227. assert.NoError(t, err)
  228. }
  229. func TestHaving(t *testing.T) {
  230. assert.NoError(t, prepareEngine())
  231. assertSync(t, new(Userinfo))
  232. users := make([]Userinfo, 0)
  233. err := testEngine.GroupBy("username").Having("username='xlw'").Find(&users)
  234. assert.NoError(t, err)
  235. fmt.Println(users)
  236. /*users = make([]Userinfo, 0)
  237. err = testEngine.Cols("id, username").GroupBy("username").Having("username='xlw'").Find(&users)
  238. if err != nil {
  239. t.Error(err)
  240. panic(err)
  241. }
  242. fmt.Println(users)*/
  243. }
  244. func TestOrderSameMapper(t *testing.T) {
  245. assert.NoError(t, prepareEngine())
  246. testEngine.UnMapType(rValue(new(Userinfo)).Type())
  247. mapper := testEngine.GetTableMapper()
  248. testEngine.SetMapper(core.SameMapper{})
  249. defer func() {
  250. testEngine.UnMapType(rValue(new(Userinfo)).Type())
  251. testEngine.SetMapper(mapper)
  252. }()
  253. assertSync(t, new(Userinfo))
  254. users := make([]Userinfo, 0)
  255. err := testEngine.OrderBy("(id) desc").Find(&users)
  256. assert.NoError(t, err)
  257. fmt.Println(users)
  258. users2 := make([]Userinfo, 0)
  259. err = testEngine.Asc("(id)", "Username").Desc("Height").Find(&users2)
  260. assert.NoError(t, err)
  261. fmt.Println(users2)
  262. }
  263. func TestHavingSameMapper(t *testing.T) {
  264. assert.NoError(t, prepareEngine())
  265. testEngine.UnMapType(rValue(new(Userinfo)).Type())
  266. mapper := testEngine.GetTableMapper()
  267. testEngine.SetMapper(core.SameMapper{})
  268. defer func() {
  269. testEngine.UnMapType(rValue(new(Userinfo)).Type())
  270. testEngine.SetMapper(mapper)
  271. }()
  272. assertSync(t, new(Userinfo))
  273. users := make([]Userinfo, 0)
  274. err := testEngine.GroupBy("`Username`").Having("`Username`='xlw'").Find(&users)
  275. if err != nil {
  276. t.Fatal(err)
  277. }
  278. fmt.Println(users)
  279. }
  280. func TestFindInts(t *testing.T) {
  281. assert.NoError(t, prepareEngine())
  282. assertSync(t, new(Userinfo))
  283. userinfo := testEngine.GetTableMapper().Obj2Table("Userinfo")
  284. var idsInt64 []int64
  285. err := testEngine.Table(userinfo).Cols("id").Desc("id").Find(&idsInt64)
  286. if err != nil {
  287. t.Fatal(err)
  288. }
  289. fmt.Println(idsInt64)
  290. var idsInt32 []int32
  291. err = testEngine.Table(userinfo).Cols("id").Desc("id").Find(&idsInt32)
  292. if err != nil {
  293. t.Fatal(err)
  294. }
  295. fmt.Println(idsInt32)
  296. var idsInt []int
  297. err = testEngine.Table(userinfo).Cols("id").Desc("id").Find(&idsInt)
  298. if err != nil {
  299. t.Fatal(err)
  300. }
  301. fmt.Println(idsInt)
  302. var idsUint []uint
  303. err = testEngine.Table(userinfo).Cols("id").Desc("id").Find(&idsUint)
  304. if err != nil {
  305. t.Fatal(err)
  306. }
  307. fmt.Println(idsUint)
  308. type MyInt int
  309. var idsMyInt []MyInt
  310. err = testEngine.Table(userinfo).Cols("id").Desc("id").Find(&idsMyInt)
  311. if err != nil {
  312. t.Fatal(err)
  313. }
  314. fmt.Println(idsMyInt)
  315. }
  316. func TestFindStrings(t *testing.T) {
  317. assert.NoError(t, prepareEngine())
  318. assertSync(t, new(Userinfo))
  319. userinfo := testEngine.GetTableMapper().Obj2Table("Userinfo")
  320. username := testEngine.GetColumnMapper().Obj2Table("Username")
  321. var idsString []string
  322. err := testEngine.Table(userinfo).Cols(username).Desc("id").Find(&idsString)
  323. if err != nil {
  324. t.Fatal(err)
  325. }
  326. fmt.Println(idsString)
  327. }
  328. func TestFindMyString(t *testing.T) {
  329. assert.NoError(t, prepareEngine())
  330. assertSync(t, new(Userinfo))
  331. userinfo := testEngine.GetTableMapper().Obj2Table("Userinfo")
  332. username := testEngine.GetColumnMapper().Obj2Table("Username")
  333. var idsMyString []MyString
  334. err := testEngine.Table(userinfo).Cols(username).Desc("id").Find(&idsMyString)
  335. if err != nil {
  336. t.Fatal(err)
  337. }
  338. fmt.Println(idsMyString)
  339. }
  340. func TestFindInterface(t *testing.T) {
  341. assert.NoError(t, prepareEngine())
  342. assertSync(t, new(Userinfo))
  343. userinfo := testEngine.GetTableMapper().Obj2Table("Userinfo")
  344. username := testEngine.GetColumnMapper().Obj2Table("Username")
  345. var idsInterface []interface{}
  346. err := testEngine.Table(userinfo).Cols(username).Desc("id").Find(&idsInterface)
  347. if err != nil {
  348. t.Fatal(err)
  349. }
  350. fmt.Println(idsInterface)
  351. }
  352. func TestFindSliceBytes(t *testing.T) {
  353. assert.NoError(t, prepareEngine())
  354. assertSync(t, new(Userinfo))
  355. userinfo := testEngine.GetTableMapper().Obj2Table("Userinfo")
  356. var ids [][][]byte
  357. err := testEngine.Table(userinfo).Desc("id").Find(&ids)
  358. if err != nil {
  359. t.Fatal(err)
  360. }
  361. for _, record := range ids {
  362. fmt.Println(record)
  363. }
  364. }
  365. func TestFindSlicePtrString(t *testing.T) {
  366. assert.NoError(t, prepareEngine())
  367. assertSync(t, new(Userinfo))
  368. userinfo := testEngine.GetTableMapper().Obj2Table("Userinfo")
  369. var ids [][]*string
  370. err := testEngine.Table(userinfo).Desc("id").Find(&ids)
  371. if err != nil {
  372. t.Fatal(err)
  373. }
  374. for _, record := range ids {
  375. fmt.Println(record)
  376. }
  377. }
  378. func TestFindMapBytes(t *testing.T) {
  379. assert.NoError(t, prepareEngine())
  380. assertSync(t, new(Userinfo))
  381. userinfo := testEngine.GetTableMapper().Obj2Table("Userinfo")
  382. var ids []map[string][]byte
  383. err := testEngine.Table(userinfo).Desc("id").Find(&ids)
  384. if err != nil {
  385. t.Fatal(err)
  386. }
  387. for _, record := range ids {
  388. fmt.Println(record)
  389. }
  390. }
  391. func TestFindMapPtrString(t *testing.T) {
  392. assert.NoError(t, prepareEngine())
  393. assertSync(t, new(Userinfo))
  394. userinfo := testEngine.GetTableMapper().Obj2Table("Userinfo")
  395. var ids []map[string]*string
  396. err := testEngine.Table(userinfo).Desc("id").Find(&ids)
  397. assert.NoError(t, err)
  398. for _, record := range ids {
  399. fmt.Println(record)
  400. }
  401. }
  402. func TestFindBit(t *testing.T) {
  403. type FindBitStruct struct {
  404. Id int64
  405. Msg bool `xorm:"bit"`
  406. }
  407. assert.NoError(t, prepareEngine())
  408. assertSync(t, new(FindBitStruct))
  409. cnt, err := testEngine.Insert([]FindBitStruct{
  410. {
  411. Msg: false,
  412. },
  413. {
  414. Msg: true,
  415. },
  416. })
  417. assert.NoError(t, err)
  418. assert.EqualValues(t, 2, cnt)
  419. var results = make([]FindBitStruct, 0, 2)
  420. err = testEngine.Find(&results)
  421. assert.NoError(t, err)
  422. assert.EqualValues(t, 2, len(results))
  423. }
  424. func TestFindMark(t *testing.T) {
  425. type Mark struct {
  426. Mark1 string `xorm:"VARCHAR(1)"`
  427. Mark2 string `xorm:"VARCHAR(1)"`
  428. MarkA string `xorm:"VARCHAR(1)"`
  429. }
  430. assert.NoError(t, prepareEngine())
  431. assertSync(t, new(Mark))
  432. cnt, err := testEngine.Insert([]Mark{
  433. {
  434. Mark1: "1",
  435. Mark2: "2",
  436. MarkA: "A",
  437. },
  438. {
  439. Mark1: "1",
  440. Mark2: "2",
  441. MarkA: "A",
  442. },
  443. })
  444. assert.NoError(t, err)
  445. assert.EqualValues(t, 2, cnt)
  446. var results = make([]Mark, 0, 2)
  447. err = testEngine.Find(&results)
  448. assert.NoError(t, err)
  449. assert.EqualValues(t, 2, len(results))
  450. }
  451. func TestFindAndCountOneFunc(t *testing.T) {
  452. type FindAndCountStruct struct {
  453. Id int64
  454. Content string
  455. Msg bool `xorm:"bit"`
  456. }
  457. assert.NoError(t, prepareEngine())
  458. assertSync(t, new(FindAndCountStruct))
  459. cnt, err := testEngine.Insert([]FindAndCountStruct{
  460. {
  461. Content: "111",
  462. Msg: false,
  463. },
  464. {
  465. Content: "222",
  466. Msg: true,
  467. },
  468. })
  469. assert.NoError(t, err)
  470. assert.EqualValues(t, 2, cnt)
  471. var results = make([]FindAndCountStruct, 0, 2)
  472. cnt, err = testEngine.FindAndCount(&results)
  473. assert.NoError(t, err)
  474. assert.EqualValues(t, 2, len(results))
  475. assert.EqualValues(t, 2, cnt)
  476. results = make([]FindAndCountStruct, 0, 1)
  477. cnt, err = testEngine.Where("msg = ?", true).FindAndCount(&results)
  478. assert.NoError(t, err)
  479. assert.EqualValues(t, 1, len(results))
  480. assert.EqualValues(t, 1, cnt)
  481. results = make([]FindAndCountStruct, 0, 1)
  482. cnt, err = testEngine.Where("msg = ?", true).Limit(1).FindAndCount(&results)
  483. assert.NoError(t, err)
  484. assert.EqualValues(t, 1, len(results))
  485. assert.EqualValues(t, 1, cnt)
  486. results = make([]FindAndCountStruct, 0, 1)
  487. cnt, err = testEngine.Where("msg = ?", true).Select("id, content, msg").
  488. Limit(1).FindAndCount(&results)
  489. assert.NoError(t, err)
  490. assert.EqualValues(t, 1, len(results))
  491. assert.EqualValues(t, 1, cnt)
  492. results = make([]FindAndCountStruct, 0, 1)
  493. cnt, err = testEngine.Where("msg = ?", true).Desc("id").
  494. Limit(1).FindAndCount(&results)
  495. assert.NoError(t, err)
  496. assert.EqualValues(t, 1, len(results))
  497. assert.EqualValues(t, 1, cnt)
  498. }
  499. type FindMapDevice struct {
  500. Deviceid string `xorm:"pk"`
  501. Status int
  502. }
  503. func (device *FindMapDevice) TableName() string {
  504. return "devices"
  505. }
  506. func TestFindMapStringId(t *testing.T) {
  507. assert.NoError(t, prepareEngine())
  508. assertSync(t, new(FindMapDevice))
  509. cnt, err := testEngine.Insert(&FindMapDevice{
  510. Deviceid: "1",
  511. Status: 1,
  512. })
  513. assert.NoError(t, err)
  514. assert.EqualValues(t, 1, cnt)
  515. deviceIDs := []string{"1"}
  516. deviceMaps := make(map[string]*FindMapDevice, len(deviceIDs))
  517. err = testEngine.
  518. Where("status = ?", 1).
  519. In("deviceid", deviceIDs).
  520. Find(&deviceMaps)
  521. assert.NoError(t, err)
  522. deviceMaps2 := make(map[string]FindMapDevice, len(deviceIDs))
  523. err = testEngine.
  524. Where("status = ?", 1).
  525. In("deviceid", deviceIDs).
  526. Find(&deviceMaps2)
  527. assert.NoError(t, err)
  528. devices := make([]*FindMapDevice, 0, len(deviceIDs))
  529. err = testEngine.Find(&devices)
  530. assert.NoError(t, err)
  531. devices2 := make([]FindMapDevice, 0, len(deviceIDs))
  532. err = testEngine.Find(&devices2)
  533. assert.NoError(t, err)
  534. var device FindMapDevice
  535. has, err := testEngine.Get(&device)
  536. assert.NoError(t, err)
  537. assert.True(t, has)
  538. has, err = testEngine.Exist(&FindMapDevice{})
  539. assert.NoError(t, err)
  540. assert.True(t, has)
  541. cnt, err = testEngine.Count(new(FindMapDevice))
  542. assert.NoError(t, err)
  543. assert.EqualValues(t, 1, cnt)
  544. cnt, err = testEngine.ID("1").Update(&FindMapDevice{
  545. Status: 2,
  546. })
  547. assert.NoError(t, err)
  548. assert.EqualValues(t, 1, cnt)
  549. sum, err := testEngine.SumInt(new(FindMapDevice), "status")
  550. assert.NoError(t, err)
  551. assert.EqualValues(t, 2, sum)
  552. cnt, err = testEngine.ID("1").Delete(new(FindMapDevice))
  553. assert.NoError(t, err)
  554. assert.EqualValues(t, 1, cnt)
  555. }
  556. func TestFindExtends(t *testing.T) {
  557. type FindExtendsB struct {
  558. ID int64
  559. }
  560. type FindExtendsA struct {
  561. FindExtendsB `xorm:"extends"`
  562. }
  563. assert.NoError(t, prepareEngine())
  564. assertSync(t, new(FindExtendsA))
  565. cnt, err := testEngine.Insert(&FindExtendsA{
  566. FindExtendsB: FindExtendsB{},
  567. })
  568. assert.NoError(t, err)
  569. assert.EqualValues(t, 1, cnt)
  570. cnt, err = testEngine.Insert(&FindExtendsA{
  571. FindExtendsB: FindExtendsB{},
  572. })
  573. assert.NoError(t, err)
  574. assert.EqualValues(t, 1, cnt)
  575. var results []FindExtendsA
  576. err = testEngine.Find(&results)
  577. assert.NoError(t, err)
  578. assert.EqualValues(t, 2, len(results))
  579. }
  580. func TestFindExtends3(t *testing.T) {
  581. type FindExtendsCC struct {
  582. ID int64
  583. Name string
  584. }
  585. type FindExtendsBB struct {
  586. FindExtendsCC `xorm:"extends"`
  587. }
  588. type FindExtendsAA struct {
  589. FindExtendsBB `xorm:"extends"`
  590. }
  591. assert.NoError(t, prepareEngine())
  592. assertSync(t, new(FindExtendsAA))
  593. cnt, err := testEngine.Insert(&FindExtendsAA{
  594. FindExtendsBB: FindExtendsBB{
  595. FindExtendsCC: FindExtendsCC{
  596. Name: "cc1",
  597. },
  598. },
  599. })
  600. assert.NoError(t, err)
  601. assert.EqualValues(t, 1, cnt)
  602. cnt, err = testEngine.Insert(&FindExtendsAA{
  603. FindExtendsBB: FindExtendsBB{
  604. FindExtendsCC: FindExtendsCC{
  605. Name: "cc2",
  606. },
  607. },
  608. })
  609. assert.NoError(t, err)
  610. assert.EqualValues(t, 1, cnt)
  611. var results []FindExtendsAA
  612. err = testEngine.Find(&results)
  613. assert.NoError(t, err)
  614. assert.EqualValues(t, 2, len(results))
  615. }
  616. func TestFindCacheLimit(t *testing.T) {
  617. type InviteCode struct {
  618. ID int64 `xorm:"pk autoincr 'id'"`
  619. Code string `xorm:"unique"`
  620. Created time.Time `xorm:"created"`
  621. }
  622. assert.NoError(t, prepareEngine())
  623. assertSync(t, new(InviteCode))
  624. cnt, err := testEngine.Insert(&InviteCode{
  625. Code: "123456",
  626. })
  627. assert.NoError(t, err)
  628. assert.EqualValues(t, 1, cnt)
  629. cnt, err = testEngine.Insert(&InviteCode{
  630. Code: "234567",
  631. })
  632. assert.NoError(t, err)
  633. assert.EqualValues(t, 1, cnt)
  634. for i := 0; i < 8; i++ {
  635. var beans []InviteCode
  636. err = testEngine.Limit(1, 0).Find(&beans)
  637. assert.NoError(t, err)
  638. assert.EqualValues(t, 1, len(beans))
  639. }
  640. for i := 0; i < 8; i++ {
  641. var beans2 []*InviteCode
  642. err = testEngine.Limit(1, 0).Find(&beans2)
  643. assert.NoError(t, err)
  644. assert.EqualValues(t, 1, len(beans2))
  645. }
  646. }
  647. func TestFindJoin(t *testing.T) {
  648. type SceneItem struct {
  649. Type int
  650. DeviceId int64
  651. }
  652. type DeviceUserPrivrels struct {
  653. UserId int64
  654. DeviceId int64
  655. }
  656. assert.NoError(t, prepareEngine())
  657. assertSync(t, new(SceneItem), new(DeviceUserPrivrels))
  658. var scenes []SceneItem
  659. err := testEngine.Join("LEFT OUTER", "device_user_privrels", "device_user_privrels.device_id=scene_item.device_id").
  660. Where("scene_item.type=?", 3).Or("device_user_privrels.user_id=?", 339).Find(&scenes)
  661. assert.NoError(t, err)
  662. scenes = make([]SceneItem, 0)
  663. err = testEngine.Join("LEFT OUTER", new(DeviceUserPrivrels), "device_user_privrels.device_id=scene_item.device_id").
  664. Where("scene_item.type=?", 3).Or("device_user_privrels.user_id=?", 339).Find(&scenes)
  665. assert.NoError(t, err)
  666. }