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.

98 lines
2.1 KiB

10 months ago
  1. package schema
  2. import (
  3. "encoding/json"
  4. "github.com/golang/protobuf/proto"
  5. . "github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
  6. "github.com/stretchr/testify/assert"
  7. "testing"
  8. )
  9. func TestEnumScalarType(t *testing.T) {
  10. tests := []struct {
  11. name string
  12. enum ScalarType
  13. expected int32
  14. }{
  15. {"Boolean", ScalarType_BOOLEAN, 0},
  16. {"Integer", ScalarType_INTEGER, 1},
  17. {"Long", ScalarType_LONG, 3},
  18. {"Float", ScalarType_FLOAT, 4},
  19. {"Double", ScalarType_DOUBLE, 5},
  20. {"Bytes", ScalarType_BYTES, 6},
  21. {"String", ScalarType_STRING, 7},
  22. }
  23. for _, tt := range tests {
  24. t.Run(tt.name, func(t *testing.T) {
  25. assert.Equal(t, tt.expected, int32(tt.enum))
  26. })
  27. }
  28. }
  29. func TestMapType(t *testing.T) {
  30. mt := &MapType{
  31. Key: "key",
  32. Value: &Type{Kind: &Type_ScalarType{ScalarType: ScalarType_BOOLEAN}},
  33. }
  34. assert.NotNil(t, mt)
  35. }
  36. func TestField(t *testing.T) {
  37. field := &Field{
  38. Name: "field_name",
  39. Type: &Type{Kind: &Type_ScalarType{ScalarType: ScalarType_INTEGER}},
  40. Index: 1,
  41. IsOptional: true,
  42. IsRepeated: false,
  43. }
  44. assert.NotNil(t, field)
  45. }
  46. func TestRecordType(t *testing.T) {
  47. subRecord := &RecordType{
  48. Fields: []*Field{
  49. {
  50. Name: "field_1",
  51. Type: &Type{Kind: &Type_ScalarType{ScalarType: ScalarType_INTEGER}},
  52. Index: 1,
  53. IsOptional: true,
  54. IsRepeated: false,
  55. },
  56. {
  57. Name: "field_2",
  58. Type: &Type{Kind: &Type_ScalarType{ScalarType: ScalarType_STRING}},
  59. Index: 2,
  60. IsOptional: true,
  61. IsRepeated: false,
  62. },
  63. },
  64. }
  65. record := &RecordType{
  66. Fields: []*Field{
  67. {
  68. Name: "field_key",
  69. Type: &Type{Kind: &Type_ScalarType{ScalarType: ScalarType_INTEGER}},
  70. Index: 1,
  71. IsOptional: true,
  72. IsRepeated: false,
  73. },
  74. {
  75. Name: "field_record",
  76. Type: &Type{Kind: &Type_RecordType{RecordType: subRecord}},
  77. Index: 2,
  78. IsOptional: true,
  79. IsRepeated: false,
  80. },
  81. },
  82. }
  83. // serialize record to protobuf text marshalling
  84. text := proto.MarshalTextString(record)
  85. println(text)
  86. bytes, _ := json.Marshal(record)
  87. println(string(bytes))
  88. assert.NotNil(t, record)
  89. }