THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

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.

1198 lines
22KB

  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. "testing"
  8. "time"
  9. "xorm.io/core"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. type IntId struct {
  13. Id int `xorm:"pk autoincr"`
  14. Name string
  15. }
  16. type Int16Id struct {
  17. Id int16 `xorm:"pk autoincr"`
  18. Name string
  19. }
  20. type Int32Id struct {
  21. Id int32 `xorm:"pk autoincr"`
  22. Name string
  23. }
  24. type UintId struct {
  25. Id uint `xorm:"pk autoincr"`
  26. Name string
  27. }
  28. type Uint16Id struct {
  29. Id uint16 `xorm:"pk autoincr"`
  30. Name string
  31. }
  32. type Uint32Id struct {
  33. Id uint32 `xorm:"pk autoincr"`
  34. Name string
  35. }
  36. type Uint64Id struct {
  37. Id uint64 `xorm:"pk autoincr"`
  38. Name string
  39. }
  40. type StringPK struct {
  41. Id string `xorm:"pk notnull"`
  42. Name string
  43. }
  44. type ID int64
  45. type MyIntPK struct {
  46. ID ID `xorm:"pk autoincr"`
  47. Name string
  48. }
  49. type StrID string
  50. type MyStringPK struct {
  51. ID StrID `xorm:"pk notnull"`
  52. Name string
  53. }
  54. func TestIntId(t *testing.T) {
  55. assert.NoError(t, prepareEngine())
  56. err := testEngine.DropTables(&IntId{})
  57. if err != nil {
  58. t.Error(err)
  59. panic(err)
  60. }
  61. err = testEngine.CreateTables(&IntId{})
  62. if err != nil {
  63. t.Error(err)
  64. panic(err)
  65. }
  66. cnt, err := testEngine.Insert(&IntId{Name: "test"})
  67. if err != nil {
  68. t.Error(err)
  69. panic(err)
  70. }
  71. if cnt != 1 {
  72. err = errors.New("insert count should be one")
  73. t.Error(err)
  74. panic(err)
  75. }
  76. bean := new(IntId)
  77. has, err := testEngine.Get(bean)
  78. if err != nil {
  79. t.Error(err)
  80. panic(err)
  81. }
  82. if !has {
  83. err = errors.New("get count should be one")
  84. t.Error(err)
  85. panic(err)
  86. }
  87. beans := make([]IntId, 0)
  88. err = testEngine.Find(&beans)
  89. if err != nil {
  90. t.Error(err)
  91. panic(err)
  92. }
  93. if len(beans) != 1 {
  94. err = errors.New("get count should be one")
  95. t.Error(err)
  96. panic(err)
  97. }
  98. beans2 := make(map[int]IntId)
  99. err = testEngine.Find(&beans2)
  100. if err != nil {
  101. t.Error(err)
  102. panic(err)
  103. }
  104. if len(beans2) != 1 {
  105. err = errors.New("get count should be one")
  106. t.Error(err)
  107. panic(err)
  108. }
  109. cnt, err = testEngine.ID(bean.Id).Delete(&IntId{})
  110. if err != nil {
  111. t.Error(err)
  112. panic(err)
  113. }
  114. if cnt != 1 {
  115. err = errors.New("insert count should be one")
  116. t.Error(err)
  117. panic(err)
  118. }
  119. }
  120. func TestInt16Id(t *testing.T) {
  121. assert.NoError(t, prepareEngine())
  122. err := testEngine.DropTables(&Int16Id{})
  123. if err != nil {
  124. t.Error(err)
  125. panic(err)
  126. }
  127. err = testEngine.CreateTables(&Int16Id{})
  128. if err != nil {
  129. t.Error(err)
  130. panic(err)
  131. }
  132. cnt, err := testEngine.Insert(&Int16Id{Name: "test"})
  133. if err != nil {
  134. t.Error(err)
  135. panic(err)
  136. }
  137. if cnt != 1 {
  138. err = errors.New("insert count should be one")
  139. t.Error(err)
  140. panic(err)
  141. }
  142. bean := new(Int16Id)
  143. has, err := testEngine.Get(bean)
  144. if err != nil {
  145. t.Error(err)
  146. panic(err)
  147. }
  148. if !has {
  149. err = errors.New("get count should be one")
  150. t.Error(err)
  151. panic(err)
  152. }
  153. beans := make([]Int16Id, 0)
  154. err = testEngine.Find(&beans)
  155. if err != nil {
  156. t.Error(err)
  157. panic(err)
  158. }
  159. if len(beans) != 1 {
  160. err = errors.New("get count should be one")
  161. t.Error(err)
  162. panic(err)
  163. }
  164. beans2 := make(map[int16]Int16Id, 0)
  165. err = testEngine.Find(&beans2)
  166. if err != nil {
  167. t.Error(err)
  168. panic(err)
  169. }
  170. if len(beans2) != 1 {
  171. err = errors.New("get count should be one")
  172. t.Error(err)
  173. panic(err)
  174. }
  175. cnt, err = testEngine.ID(bean.Id).Delete(&Int16Id{})
  176. if err != nil {
  177. t.Error(err)
  178. panic(err)
  179. }
  180. if cnt != 1 {
  181. err = errors.New("insert count should be one")
  182. t.Error(err)
  183. panic(err)
  184. }
  185. }
  186. func TestInt32Id(t *testing.T) {
  187. assert.NoError(t, prepareEngine())
  188. err := testEngine.DropTables(&Int32Id{})
  189. if err != nil {
  190. t.Error(err)
  191. panic(err)
  192. }
  193. err = testEngine.CreateTables(&Int32Id{})
  194. if err != nil {
  195. t.Error(err)
  196. panic(err)
  197. }
  198. cnt, err := testEngine.Insert(&Int32Id{Name: "test"})
  199. if err != nil {
  200. t.Error(err)
  201. panic(err)
  202. }
  203. if cnt != 1 {
  204. err = errors.New("insert count should be one")
  205. t.Error(err)
  206. panic(err)
  207. }
  208. bean := new(Int32Id)
  209. has, err := testEngine.Get(bean)
  210. if err != nil {
  211. t.Error(err)
  212. panic(err)
  213. }
  214. if !has {
  215. err = errors.New("get count should be one")
  216. t.Error(err)
  217. panic(err)
  218. }
  219. beans := make([]Int32Id, 0)
  220. err = testEngine.Find(&beans)
  221. if err != nil {
  222. t.Error(err)
  223. panic(err)
  224. }
  225. if len(beans) != 1 {
  226. err = errors.New("get count should be one")
  227. t.Error(err)
  228. panic(err)
  229. }
  230. beans2 := make(map[int32]Int32Id, 0)
  231. err = testEngine.Find(&beans2)
  232. if err != nil {
  233. t.Error(err)
  234. panic(err)
  235. }
  236. if len(beans2) != 1 {
  237. err = errors.New("get count should be one")
  238. t.Error(err)
  239. panic(err)
  240. }
  241. cnt, err = testEngine.ID(bean.Id).Delete(&Int32Id{})
  242. if err != nil {
  243. t.Error(err)
  244. panic(err)
  245. }
  246. if cnt != 1 {
  247. err = errors.New("insert count should be one")
  248. t.Error(err)
  249. panic(err)
  250. }
  251. }
  252. func TestUintId(t *testing.T) {
  253. assert.NoError(t, prepareEngine())
  254. err := testEngine.DropTables(&UintId{})
  255. if err != nil {
  256. t.Error(err)
  257. panic(err)
  258. }
  259. err = testEngine.CreateTables(&UintId{})
  260. if err != nil {
  261. t.Error(err)
  262. panic(err)
  263. }
  264. cnt, err := testEngine.Insert(&UintId{Name: "test"})
  265. if err != nil {
  266. t.Error(err)
  267. panic(err)
  268. }
  269. if cnt != 1 {
  270. err = errors.New("insert count should be one")
  271. t.Error(err)
  272. panic(err)
  273. }
  274. var inserts = []UintId{
  275. {Name: "test1"},
  276. {Name: "test2"},
  277. }
  278. cnt, err = testEngine.Insert(&inserts)
  279. if err != nil {
  280. t.Error(err)
  281. panic(err)
  282. }
  283. if cnt != 2 {
  284. err = errors.New("insert count should be two")
  285. t.Error(err)
  286. panic(err)
  287. }
  288. bean := new(UintId)
  289. has, err := testEngine.Get(bean)
  290. if err != nil {
  291. t.Error(err)
  292. panic(err)
  293. }
  294. if !has {
  295. err = errors.New("get count should be one")
  296. t.Error(err)
  297. panic(err)
  298. }
  299. beans := make([]UintId, 0)
  300. err = testEngine.Find(&beans)
  301. if err != nil {
  302. t.Error(err)
  303. panic(err)
  304. }
  305. if len(beans) != 3 {
  306. err = errors.New("get count should be three")
  307. t.Error(err)
  308. panic(err)
  309. }
  310. beans2 := make(map[uint]UintId, 0)
  311. err = testEngine.Find(&beans2)
  312. if err != nil {
  313. t.Error(err)
  314. panic(err)
  315. }
  316. if len(beans2) != 3 {
  317. err = errors.New("get count should be three")
  318. t.Error(err)
  319. panic(err)
  320. }
  321. cnt, err = testEngine.ID(bean.Id).Delete(&UintId{})
  322. if err != nil {
  323. t.Error(err)
  324. panic(err)
  325. }
  326. if cnt != 1 {
  327. err = errors.New("insert count should be one")
  328. t.Error(err)
  329. panic(err)
  330. }
  331. }
  332. func TestUint16Id(t *testing.T) {
  333. assert.NoError(t, prepareEngine())
  334. err := testEngine.DropTables(&Uint16Id{})
  335. if err != nil {
  336. t.Error(err)
  337. panic(err)
  338. }
  339. err = testEngine.CreateTables(&Uint16Id{})
  340. if err != nil {
  341. t.Error(err)
  342. panic(err)
  343. }
  344. cnt, err := testEngine.Insert(&Uint16Id{Name: "test"})
  345. if err != nil {
  346. t.Error(err)
  347. panic(err)
  348. }
  349. if cnt != 1 {
  350. err = errors.New("insert count should be one")
  351. t.Error(err)
  352. panic(err)
  353. }
  354. bean := new(Uint16Id)
  355. has, err := testEngine.Get(bean)
  356. if err != nil {
  357. t.Error(err)
  358. panic(err)
  359. }
  360. if !has {
  361. err = errors.New("get count should be one")
  362. t.Error(err)
  363. panic(err)
  364. }
  365. beans := make([]Uint16Id, 0)
  366. err = testEngine.Find(&beans)
  367. if err != nil {
  368. t.Error(err)
  369. panic(err)
  370. }
  371. if len(beans) != 1 {
  372. err = errors.New("get count should be one")
  373. t.Error(err)
  374. panic(err)
  375. }
  376. beans2 := make(map[uint16]Uint16Id, 0)
  377. err = testEngine.Find(&beans2)
  378. if err != nil {
  379. t.Error(err)
  380. panic(err)
  381. }
  382. if len(beans2) != 1 {
  383. err = errors.New("get count should be one")
  384. t.Error(err)
  385. panic(err)
  386. }
  387. cnt, err = testEngine.ID(bean.Id).Delete(&Uint16Id{})
  388. if err != nil {
  389. t.Error(err)
  390. panic(err)
  391. }
  392. if cnt != 1 {
  393. err = errors.New("insert count should be one")
  394. t.Error(err)
  395. panic(err)
  396. }
  397. }
  398. func TestUint32Id(t *testing.T) {
  399. assert.NoError(t, prepareEngine())
  400. err := testEngine.DropTables(&Uint32Id{})
  401. if err != nil {
  402. t.Error(err)
  403. panic(err)
  404. }
  405. err = testEngine.CreateTables(&Uint32Id{})
  406. if err != nil {
  407. t.Error(err)
  408. panic(err)
  409. }
  410. cnt, err := testEngine.Insert(&Uint32Id{Name: "test"})
  411. if err != nil {
  412. t.Error(err)
  413. panic(err)
  414. }
  415. if cnt != 1 {
  416. err = errors.New("insert count should be one")
  417. t.Error(err)
  418. panic(err)
  419. }
  420. bean := new(Uint32Id)
  421. has, err := testEngine.Get(bean)
  422. if err != nil {
  423. t.Error(err)
  424. panic(err)
  425. }
  426. if !has {
  427. err = errors.New("get count should be one")
  428. t.Error(err)
  429. panic(err)
  430. }
  431. beans := make([]Uint32Id, 0)
  432. err = testEngine.Find(&beans)
  433. if err != nil {
  434. t.Error(err)
  435. panic(err)
  436. }
  437. if len(beans) != 1 {
  438. err = errors.New("get count should be one")
  439. t.Error(err)
  440. panic(err)
  441. }
  442. beans2 := make(map[uint32]Uint32Id, 0)
  443. err = testEngine.Find(&beans2)
  444. if err != nil {
  445. t.Error(err)
  446. panic(err)
  447. }
  448. if len(beans2) != 1 {
  449. err = errors.New("get count should be one")
  450. t.Error(err)
  451. panic(err)
  452. }
  453. cnt, err = testEngine.ID(bean.Id).Delete(&Uint32Id{})
  454. if err != nil {
  455. t.Error(err)
  456. panic(err)
  457. }
  458. if cnt != 1 {
  459. err = errors.New("insert count should be one")
  460. t.Error(err)
  461. panic(err)
  462. }
  463. }
  464. func TestUint64Id(t *testing.T) {
  465. assert.NoError(t, prepareEngine())
  466. err := testEngine.DropTables(&Uint64Id{})
  467. if err != nil {
  468. t.Error(err)
  469. panic(err)
  470. }
  471. err = testEngine.CreateTables(&Uint64Id{})
  472. if err != nil {
  473. t.Error(err)
  474. panic(err)
  475. }
  476. idbean := &Uint64Id{Name: "test"}
  477. cnt, err := testEngine.Insert(idbean)
  478. if err != nil {
  479. t.Error(err)
  480. panic(err)
  481. }
  482. if cnt != 1 {
  483. err = errors.New("insert count should be one")
  484. t.Error(err)
  485. panic(err)
  486. }
  487. bean := new(Uint64Id)
  488. has, err := testEngine.Get(bean)
  489. if err != nil {
  490. t.Error(err)
  491. panic(err)
  492. }
  493. if !has {
  494. err = errors.New("get count should be one")
  495. t.Error(err)
  496. panic(err)
  497. }
  498. if bean.Id != idbean.Id {
  499. panic(errors.New("should be equal"))
  500. }
  501. beans := make([]Uint64Id, 0)
  502. err = testEngine.Find(&beans)
  503. if err != nil {
  504. t.Error(err)
  505. panic(err)
  506. }
  507. if len(beans) != 1 {
  508. err = errors.New("get count should be one")
  509. t.Error(err)
  510. panic(err)
  511. }
  512. if *bean != beans[0] {
  513. panic(errors.New("should be equal"))
  514. }
  515. beans2 := make(map[uint64]Uint64Id, 0)
  516. err = testEngine.Find(&beans2)
  517. if err != nil {
  518. t.Error(err)
  519. panic(err)
  520. }
  521. if len(beans2) != 1 {
  522. err = errors.New("get count should be one")
  523. t.Error(err)
  524. panic(err)
  525. }
  526. if *bean != beans2[bean.Id] {
  527. panic(errors.New("should be equal"))
  528. }
  529. cnt, err = testEngine.ID(bean.Id).Delete(&Uint64Id{})
  530. if err != nil {
  531. t.Error(err)
  532. panic(err)
  533. }
  534. if cnt != 1 {
  535. err = errors.New("insert count should be one")
  536. t.Error(err)
  537. panic(err)
  538. }
  539. }
  540. func TestStringPK(t *testing.T) {
  541. assert.NoError(t, prepareEngine())
  542. err := testEngine.DropTables(&StringPK{})
  543. if err != nil {
  544. t.Error(err)
  545. panic(err)
  546. }
  547. err = testEngine.CreateTables(&StringPK{})
  548. if err != nil {
  549. t.Error(err)
  550. panic(err)
  551. }
  552. cnt, err := testEngine.Insert(&StringPK{Id: "1-1-2", Name: "test"})
  553. if err != nil {
  554. t.Error(err)
  555. panic(err)
  556. }
  557. if cnt != 1 {
  558. err = errors.New("insert count should be one")
  559. t.Error(err)
  560. panic(err)
  561. }
  562. bean := new(StringPK)
  563. has, err := testEngine.Get(bean)
  564. if err != nil {
  565. t.Error(err)
  566. panic(err)
  567. }
  568. if !has {
  569. err = errors.New("get count should be one")
  570. t.Error(err)
  571. panic(err)
  572. }
  573. beans := make([]StringPK, 0)
  574. err = testEngine.Find(&beans)
  575. if err != nil {
  576. t.Error(err)
  577. panic(err)
  578. }
  579. if len(beans) != 1 {
  580. err = errors.New("get count should be one")
  581. t.Error(err)
  582. panic(err)
  583. }
  584. beans2 := make(map[string]StringPK)
  585. err = testEngine.Find(&beans2)
  586. if err != nil {
  587. t.Error(err)
  588. panic(err)
  589. }
  590. if len(beans2) != 1 {
  591. err = errors.New("get count should be one")
  592. t.Error(err)
  593. panic(err)
  594. }
  595. cnt, err = testEngine.ID(bean.Id).Delete(&StringPK{})
  596. if err != nil {
  597. t.Error(err)
  598. panic(err)
  599. }
  600. if cnt != 1 {
  601. err = errors.New("insert count should be one")
  602. t.Error(err)
  603. panic(err)
  604. }
  605. }
  606. type CompositeKey struct {
  607. Id1 int64 `xorm:"id1 pk"`
  608. Id2 int64 `xorm:"id2 pk"`
  609. UpdateStr string
  610. }
  611. func TestCompositeKey(t *testing.T) {
  612. assert.NoError(t, prepareEngine())
  613. err := testEngine.DropTables(&CompositeKey{})
  614. if err != nil {
  615. t.Error(err)
  616. panic(err)
  617. }
  618. err = testEngine.CreateTables(&CompositeKey{})
  619. if err != nil {
  620. t.Error(err)
  621. panic(err)
  622. }
  623. cnt, err := testEngine.Insert(&CompositeKey{11, 22, ""})
  624. if err != nil {
  625. t.Error(err)
  626. } else if cnt != 1 {
  627. t.Error(errors.New("failed to insert CompositeKey{11, 22}"))
  628. }
  629. cnt, err = testEngine.Insert(&CompositeKey{11, 22, ""})
  630. if err == nil || cnt == 1 {
  631. t.Error(errors.New("inserted CompositeKey{11, 22}"))
  632. }
  633. var compositeKeyVal CompositeKey
  634. has, err := testEngine.ID(core.PK{11, 22}).Get(&compositeKeyVal)
  635. if err != nil {
  636. t.Error(err)
  637. } else if !has {
  638. t.Error(errors.New("can't get CompositeKey{11, 22}"))
  639. }
  640. var compositeKeyVal2 CompositeKey
  641. // test passing PK ptr, this test seem failed withCache
  642. has, err = testEngine.ID(&core.PK{11, 22}).Get(&compositeKeyVal2)
  643. if err != nil {
  644. t.Error(err)
  645. } else if !has {
  646. t.Error(errors.New("can't get CompositeKey{11, 22}"))
  647. }
  648. if compositeKeyVal != compositeKeyVal2 {
  649. t.Error(errors.New("should be equal"))
  650. }
  651. var cps = make([]CompositeKey, 0)
  652. err = testEngine.Find(&cps)
  653. if err != nil {
  654. t.Error(err)
  655. }
  656. if len(cps) != 1 {
  657. t.Error(errors.New("should has one record"))
  658. }
  659. if cps[0] != compositeKeyVal {
  660. t.Error(errors.New("should be equal"))
  661. }
  662. cnt, err = testEngine.Insert(&CompositeKey{22, 22, ""})
  663. if err != nil {
  664. t.Error(err)
  665. } else if cnt != 1 {
  666. t.Error(errors.New("failed to insert CompositeKey{22, 22}"))
  667. }
  668. cps = make([]CompositeKey, 0)
  669. err = testEngine.Find(&cps)
  670. assert.NoError(t, err)
  671. assert.EqualValues(t, 2, len(cps), "should has two record")
  672. assert.EqualValues(t, compositeKeyVal, cps[0], "should be equeal")
  673. compositeKeyVal = CompositeKey{UpdateStr: "test1"}
  674. cnt, err = testEngine.ID(core.PK{11, 22}).Update(&compositeKeyVal)
  675. if err != nil {
  676. t.Error(err)
  677. } else if cnt != 1 {
  678. t.Error(errors.New("can't update CompositeKey{11, 22}"))
  679. }
  680. cnt, err = testEngine.ID(core.PK{11, 22}).Delete(&CompositeKey{})
  681. if err != nil {
  682. t.Error(err)
  683. } else if cnt != 1 {
  684. t.Error(errors.New("can't delete CompositeKey{11, 22}"))
  685. }
  686. }
  687. func TestCompositeKey2(t *testing.T) {
  688. assert.NoError(t, prepareEngine())
  689. type User struct {
  690. UserId string `xorm:"varchar(19) not null pk"`
  691. NickName string `xorm:"varchar(19) not null"`
  692. GameId uint32 `xorm:"integer pk"`
  693. Score int32 `xorm:"integer"`
  694. }
  695. err := testEngine.DropTables(&User{})
  696. if err != nil {
  697. t.Error(err)
  698. panic(err)
  699. }
  700. err = testEngine.CreateTables(&User{})
  701. if err != nil {
  702. t.Error(err)
  703. panic(err)
  704. }
  705. cnt, err := testEngine.Insert(&User{"11", "nick", 22, 5})
  706. if err != nil {
  707. t.Error(err)
  708. } else if cnt != 1 {
  709. t.Error(errors.New("failed to insert User{11, 22}"))
  710. }
  711. cnt, err = testEngine.Insert(&User{"11", "nick", 22, 6})
  712. if err == nil || cnt == 1 {
  713. t.Error(errors.New("inserted User{11, 22}"))
  714. }
  715. var user User
  716. has, err := testEngine.ID(core.PK{"11", 22}).Get(&user)
  717. if err != nil {
  718. t.Error(err)
  719. } else if !has {
  720. t.Error(errors.New("can't get User{11, 22}"))
  721. }
  722. // test passing PK ptr, this test seem failed withCache
  723. has, err = testEngine.ID(&core.PK{"11", 22}).Get(&user)
  724. if err != nil {
  725. t.Error(err)
  726. } else if !has {
  727. t.Error(errors.New("can't get User{11, 22}"))
  728. }
  729. user = User{NickName: "test1"}
  730. cnt, err = testEngine.ID(core.PK{"11", 22}).Update(&user)
  731. if err != nil {
  732. t.Error(err)
  733. } else if cnt != 1 {
  734. t.Error(errors.New("can't update User{11, 22}"))
  735. }
  736. cnt, err = testEngine.ID(core.PK{"11", 22}).Delete(&User{})
  737. if err != nil {
  738. t.Error(err)
  739. } else if cnt != 1 {
  740. t.Error(errors.New("can't delete CompositeKey{11, 22}"))
  741. }
  742. }
  743. type MyString string
  744. type UserPK2 struct {
  745. UserId MyString `xorm:"varchar(19) not null pk"`
  746. NickName string `xorm:"varchar(19) not null"`
  747. GameId uint32 `xorm:"integer pk"`
  748. Score int32 `xorm:"integer"`
  749. }
  750. func TestCompositeKey3(t *testing.T) {
  751. assert.NoError(t, prepareEngine())
  752. err := testEngine.DropTables(&UserPK2{})
  753. if err != nil {
  754. t.Error(err)
  755. panic(err)
  756. }
  757. err = testEngine.CreateTables(&UserPK2{})
  758. if err != nil {
  759. t.Error(err)
  760. panic(err)
  761. }
  762. cnt, err := testEngine.Insert(&UserPK2{"11", "nick", 22, 5})
  763. if err != nil {
  764. t.Error(err)
  765. } else if cnt != 1 {
  766. t.Error(errors.New("failed to insert User{11, 22}"))
  767. }
  768. cnt, err = testEngine.Insert(&UserPK2{"11", "nick", 22, 6})
  769. if err == nil || cnt == 1 {
  770. t.Error(errors.New("inserted User{11, 22}"))
  771. }
  772. var user UserPK2
  773. has, err := testEngine.ID(core.PK{"11", 22}).Get(&user)
  774. if err != nil {
  775. t.Error(err)
  776. } else if !has {
  777. t.Error(errors.New("can't get User{11, 22}"))
  778. }
  779. // test passing PK ptr, this test seem failed withCache
  780. has, err = testEngine.ID(&core.PK{"11", 22}).Get(&user)
  781. if err != nil {
  782. t.Error(err)
  783. } else if !has {
  784. t.Error(errors.New("can't get User{11, 22}"))
  785. }
  786. user = UserPK2{NickName: "test1"}
  787. cnt, err = testEngine.ID(core.PK{"11", 22}).Update(&user)
  788. if err != nil {
  789. t.Error(err)
  790. } else if cnt != 1 {
  791. t.Error(errors.New("can't update User{11, 22}"))
  792. }
  793. cnt, err = testEngine.ID(core.PK{"11", 22}).Delete(&UserPK2{})
  794. if err != nil {
  795. t.Error(err)
  796. } else if cnt != 1 {
  797. t.Error(errors.New("can't delete CompositeKey{11, 22}"))
  798. }
  799. }
  800. func TestMyIntId(t *testing.T) {
  801. assert.NoError(t, prepareEngine())
  802. err := testEngine.DropTables(&MyIntPK{})
  803. if err != nil {
  804. t.Error(err)
  805. panic(err)
  806. }
  807. err = testEngine.CreateTables(&MyIntPK{})
  808. if err != nil {
  809. t.Error(err)
  810. panic(err)
  811. }
  812. idbean := &MyIntPK{Name: "test"}
  813. cnt, err := testEngine.Insert(idbean)
  814. if err != nil {
  815. t.Error(err)
  816. panic(err)
  817. }
  818. if cnt != 1 {
  819. err = errors.New("insert count should be one")
  820. t.Error(err)
  821. panic(err)
  822. }
  823. bean := new(MyIntPK)
  824. has, err := testEngine.Get(bean)
  825. if err != nil {
  826. t.Error(err)
  827. panic(err)
  828. }
  829. if !has {
  830. err = errors.New("get count should be one")
  831. t.Error(err)
  832. panic(err)
  833. }
  834. if bean.ID != idbean.ID {
  835. panic(errors.New("should be equal"))
  836. }
  837. var beans []MyIntPK
  838. err = testEngine.Find(&beans)
  839. if err != nil {
  840. t.Error(err)
  841. panic(err)
  842. }
  843. if len(beans) != 1 {
  844. err = errors.New("get count should be one")
  845. t.Error(err)
  846. panic(err)
  847. }
  848. if *bean != beans[0] {
  849. panic(errors.New("should be equal"))
  850. }
  851. beans2 := make(map[ID]MyIntPK, 0)
  852. err = testEngine.Find(&beans2)
  853. if err != nil {
  854. t.Error(err)
  855. panic(err)
  856. }
  857. if len(beans2) != 1 {
  858. err = errors.New("get count should be one")
  859. t.Error(err)
  860. panic(err)
  861. }
  862. if *bean != beans2[bean.ID] {
  863. panic(errors.New("should be equal"))
  864. }
  865. cnt, err = testEngine.ID(bean.ID).Delete(&MyIntPK{})
  866. if err != nil {
  867. t.Error(err)
  868. panic(err)
  869. }
  870. if cnt != 1 {
  871. err = errors.New("insert count should be one")
  872. t.Error(err)
  873. panic(err)
  874. }
  875. }
  876. func TestMyStringId(t *testing.T) {
  877. assert.NoError(t, prepareEngine())
  878. err := testEngine.DropTables(&MyStringPK{})
  879. if err != nil {
  880. t.Error(err)
  881. panic(err)
  882. }
  883. err = testEngine.CreateTables(&MyStringPK{})
  884. if err != nil {
  885. t.Error(err)
  886. panic(err)
  887. }
  888. idbean := &MyStringPK{ID: "1111", Name: "test"}
  889. cnt, err := testEngine.Insert(idbean)
  890. if err != nil {
  891. t.Error(err)
  892. panic(err)
  893. }
  894. if cnt != 1 {
  895. err = errors.New("insert count should be one")
  896. t.Error(err)
  897. panic(err)
  898. }
  899. bean := new(MyStringPK)
  900. has, err := testEngine.Get(bean)
  901. if err != nil {
  902. t.Error(err)
  903. panic(err)
  904. }
  905. if !has {
  906. err = errors.New("get count should be one")
  907. t.Error(err)
  908. panic(err)
  909. }
  910. if bean.ID != idbean.ID {
  911. panic(errors.New("should be equal"))
  912. }
  913. var beans []MyStringPK
  914. err = testEngine.Find(&beans)
  915. if err != nil {
  916. t.Error(err)
  917. panic(err)
  918. }
  919. if len(beans) != 1 {
  920. err = errors.New("get count should be one")
  921. t.Error(err)
  922. panic(err)
  923. }
  924. if *bean != beans[0] {
  925. panic(errors.New("should be equal"))
  926. }
  927. beans2 := make(map[StrID]MyStringPK, 0)
  928. err = testEngine.Find(&beans2)
  929. if err != nil {
  930. t.Error(err)
  931. panic(err)
  932. }
  933. if len(beans2) != 1 {
  934. err = errors.New("get count should be one")
  935. t.Error(err)
  936. panic(err)
  937. }
  938. if *bean != beans2[bean.ID] {
  939. panic(errors.New("should be equal"))
  940. }
  941. cnt, err = testEngine.ID(bean.ID).Delete(&MyStringPK{})
  942. if err != nil {
  943. t.Error(err)
  944. panic(err)
  945. }
  946. if cnt != 1 {
  947. err = errors.New("insert count should be one")
  948. t.Error(err)
  949. panic(err)
  950. }
  951. }
  952. func TestSingleAutoIncrColumn(t *testing.T) {
  953. type Account struct {
  954. Id int64 `xorm:"pk autoincr"`
  955. }
  956. assert.NoError(t, prepareEngine())
  957. assertSync(t, new(Account))
  958. _, err := testEngine.Insert(&Account{})
  959. assert.NoError(t, err)
  960. }
  961. func TestCompositePK(t *testing.T) {
  962. type TaskSolution struct {
  963. UID string `xorm:"notnull pk UUID 'uid'"`
  964. TID string `xorm:"notnull pk UUID 'tid'"`
  965. Created time.Time `xorm:"created"`
  966. Updated time.Time `xorm:"updated"`
  967. }
  968. assert.NoError(t, prepareEngine())
  969. tables1, err := testEngine.DBMetas()
  970. assert.NoError(t, err)
  971. assertSync(t, new(TaskSolution))
  972. assert.NoError(t, testEngine.Sync2(new(TaskSolution)))
  973. tables2, err := testEngine.DBMetas()
  974. assert.NoError(t, err)
  975. assert.EqualValues(t, 1+len(tables1), len(tables2))
  976. var table *core.Table
  977. for _, t := range tables2 {
  978. if t.Name == testEngine.GetTableMapper().Obj2Table("TaskSolution") {
  979. table = t
  980. break
  981. }
  982. }
  983. assert.NotEqual(t, nil, table)
  984. pkCols := table.PKColumns()
  985. assert.EqualValues(t, 2, len(pkCols))
  986. assert.EqualValues(t, "uid", pkCols[0].Name)
  987. assert.EqualValues(t, "tid", pkCols[1].Name)
  988. }
  989. func TestNoPKIdQueryUpdate(t *testing.T) {
  990. type NoPKTable struct {
  991. Username string
  992. }
  993. assert.NoError(t, prepareEngine())
  994. assertSync(t, new(NoPKTable))
  995. cnt, err := testEngine.Insert(&NoPKTable{
  996. Username: "test",
  997. })
  998. assert.NoError(t, err)
  999. assert.EqualValues(t, 1, cnt)
  1000. var res NoPKTable
  1001. has, err := testEngine.ID("test").Get(&res)
  1002. assert.Error(t, err)
  1003. assert.False(t, has)
  1004. cnt, err = testEngine.ID("test").Update(&NoPKTable{
  1005. Username: "test1",
  1006. })
  1007. assert.Error(t, err)
  1008. assert.EqualValues(t, 0, cnt)
  1009. type UnvalidPKTable struct {
  1010. ID int `xorm:"id"`
  1011. Username string
  1012. }
  1013. assertSync(t, new(UnvalidPKTable))
  1014. cnt, err = testEngine.Insert(&UnvalidPKTable{
  1015. ID: 1,
  1016. Username: "test",
  1017. })
  1018. assert.NoError(t, err)
  1019. assert.EqualValues(t, 1, cnt)
  1020. var res2 UnvalidPKTable
  1021. has, err = testEngine.ID(1).Get(&res2)
  1022. assert.Error(t, err)
  1023. assert.False(t, has)
  1024. cnt, err = testEngine.ID(1).Update(&UnvalidPKTable{
  1025. Username: "test1",
  1026. })
  1027. assert.Error(t, err)
  1028. assert.EqualValues(t, 0, cnt)
  1029. }