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.

86 lines
1.7KB

  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. "testing"
  7. "xorm.io/core"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. type IDGonicMapper struct {
  11. ID int64
  12. }
  13. func TestGonicMapperID(t *testing.T) {
  14. assert.NoError(t, prepareEngine())
  15. oldMapper := testEngine.GetColumnMapper()
  16. testEngine.UnMapType(rValue(new(IDGonicMapper)).Type())
  17. testEngine.SetMapper(core.LintGonicMapper)
  18. defer func() {
  19. testEngine.UnMapType(rValue(new(IDGonicMapper)).Type())
  20. testEngine.SetMapper(oldMapper)
  21. }()
  22. err := testEngine.CreateTables(new(IDGonicMapper))
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. tables, err := testEngine.DBMetas()
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. for _, tb := range tables {
  31. if tb.Name == "id_gonic_mapper" {
  32. if len(tb.PKColumns()) != 1 || tb.PKColumns()[0].Name != "id" {
  33. t.Fatal(tb)
  34. }
  35. return
  36. }
  37. }
  38. t.Fatal("not table id_gonic_mapper")
  39. }
  40. type IDSameMapper struct {
  41. ID int64
  42. }
  43. func TestSameMapperID(t *testing.T) {
  44. assert.NoError(t, prepareEngine())
  45. oldMapper := testEngine.GetColumnMapper()
  46. testEngine.UnMapType(rValue(new(IDSameMapper)).Type())
  47. testEngine.SetMapper(core.SameMapper{})
  48. defer func() {
  49. testEngine.UnMapType(rValue(new(IDSameMapper)).Type())
  50. testEngine.SetMapper(oldMapper)
  51. }()
  52. err := testEngine.CreateTables(new(IDSameMapper))
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. tables, err := testEngine.DBMetas()
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. for _, tb := range tables {
  61. if tb.Name == "IDSameMapper" {
  62. if len(tb.PKColumns()) != 1 || tb.PKColumns()[0].Name != "ID" {
  63. t.Fatal(tb)
  64. }
  65. return
  66. }
  67. }
  68. t.Fatal("not table IDSameMapper")
  69. }