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.

85 lines
1.9 KiB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
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 TestField(t *testing.T) {
  30. field := &Field{
  31. Name: "field_name",
  32. Type: &Type{Kind: &Type_ScalarType{ScalarType: ScalarType_INTEGER}},
  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_INTEGER}},
  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_INTEGER}},
  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. }