schema_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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_BOOL, 0},
  16. {"Integer", ScalarType_INT32, 1},
  17. {"Long", ScalarType_INT64, 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 TestField(t *testing.T) {
  30. field := &Field{
  31. Name: "field_name",
  32. Type: &Type{Kind: &Type_ScalarType{ScalarType: ScalarType_INT32}},
  33. FieldIndex: 1,
  34. IsRepeated: false,
  35. }
  36. assert.NotNil(t, field)
  37. }
  38. func TestRecordType(t *testing.T) {
  39. subRecord := &RecordType{
  40. Fields: []*Field{
  41. {
  42. Name: "field_1",
  43. Type: &Type{Kind: &Type_ScalarType{ScalarType: ScalarType_INT32}},
  44. FieldIndex: 1,
  45. IsRepeated: false,
  46. },
  47. {
  48. Name: "field_2",
  49. Type: &Type{Kind: &Type_ScalarType{ScalarType: ScalarType_STRING}},
  50. FieldIndex: 2,
  51. IsRepeated: false,
  52. },
  53. },
  54. }
  55. record := &RecordType{
  56. Fields: []*Field{
  57. {
  58. Name: "field_key",
  59. Type: &Type{Kind: &Type_ScalarType{ScalarType: ScalarType_INT32}},
  60. FieldIndex: 1,
  61. IsRepeated: false,
  62. },
  63. {
  64. Name: "field_record",
  65. Type: &Type{Kind: &Type_RecordType{RecordType: subRecord}},
  66. FieldIndex: 2,
  67. IsRepeated: false,
  68. },
  69. },
  70. }
  71. // serialize record to protobuf text marshalling
  72. text := proto.MarshalTextString(record)
  73. println(text)
  74. bytes, _ := json.Marshal(record)
  75. println(string(bytes))
  76. assert.NotNil(t, record)
  77. }