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.

115 lines
2.2 KiB

9 months ago
9 months ago
  1. package schema
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
  4. "github.com/stretchr/testify/assert"
  5. "testing"
  6. )
  7. func TestStructToSchema(t *testing.T) {
  8. type args struct {
  9. instance any
  10. }
  11. tests := []struct {
  12. name string
  13. args args
  14. want *schema_pb.RecordType
  15. }{
  16. {
  17. name: "scalar type",
  18. args: args{
  19. instance: 1,
  20. },
  21. want: nil,
  22. },
  23. {
  24. name: "simple struct type",
  25. args: args{
  26. instance: struct {
  27. Field1 int
  28. Field2 string
  29. }{},
  30. },
  31. want: NewRecordTypeBuilder().
  32. SetField("Field1", TypeInteger).
  33. SetField("Field2", TypeString).
  34. Build(),
  35. },
  36. {
  37. name: "simple list",
  38. args: args{
  39. instance: struct {
  40. Field1 []int
  41. Field2 string
  42. }{},
  43. },
  44. want: NewRecordTypeBuilder().
  45. SetField("Field1", ListOf(TypeInteger)).
  46. SetField("Field2", TypeString).
  47. Build(),
  48. },
  49. {
  50. name: "simple []byte",
  51. args: args{
  52. instance: struct {
  53. Field2 []byte
  54. }{},
  55. },
  56. want: NewRecordTypeBuilder().
  57. SetField("Field2", TypeBytes).
  58. Build(),
  59. },
  60. {
  61. name: "nested simpe structs",
  62. args: args{
  63. instance: struct {
  64. Field1 int
  65. Field2 struct {
  66. Field3 string
  67. Field4 int
  68. }
  69. }{},
  70. },
  71. want: NewRecordTypeBuilder().
  72. SetField("Field1", TypeInteger).
  73. SetRecordField("Field2", NewRecordTypeBuilder().
  74. SetField("Field3", TypeString).
  75. SetField("Field4", TypeInteger),
  76. ).
  77. Build(),
  78. },
  79. {
  80. name: "nested struct type",
  81. args: args{
  82. instance: struct {
  83. Field1 int
  84. Field2 struct {
  85. Field3 string
  86. Field4 []int
  87. Field5 struct {
  88. Field6 string
  89. Field7 []byte
  90. }
  91. }
  92. }{},
  93. },
  94. want: NewRecordTypeBuilder().
  95. SetField("Field1", TypeInteger).
  96. SetRecordField("Field2", NewRecordTypeBuilder().
  97. SetField("Field3", TypeString).
  98. SetField("Field4", ListOf(TypeInteger)).
  99. SetRecordField("Field5", NewRecordTypeBuilder().
  100. SetField("Field6", TypeString).
  101. SetField("Field7", TypeBytes),
  102. ),
  103. ).
  104. Build(),
  105. },
  106. }
  107. for _, tt := range tests {
  108. t.Run(tt.name, func(t *testing.T) {
  109. assert.Equalf(t, tt.want, StructToSchema(tt.args.instance), "StructToSchema(%v)", tt.args.instance)
  110. })
  111. }
  112. }