|
| 1 | +package schema |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/apache/arrow-go/v18/arrow" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestResource_Validate(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + resource *Resource |
| 15 | + valueSetter func(resource *Resource) error |
| 16 | + err error |
| 17 | + }{ |
| 18 | + { |
| 19 | + name: "valid resource without primary keys or primary key components", |
| 20 | + resource: NewResourceData(&Table{Name: "test", Columns: ColumnList{{Name: "col1", Type: arrow.BinaryTypes.String}}}, nil, nil), |
| 21 | + err: nil, |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "valid resource with primary keys", |
| 25 | + resource: NewResourceData(&Table{Name: "test", Columns: ColumnList{{Name: "col1", Type: arrow.BinaryTypes.String, PrimaryKey: true}}}, nil, nil), |
| 26 | + err: nil, |
| 27 | + valueSetter: func(resource *Resource) error { |
| 28 | + return resource.Set("col1", "test") |
| 29 | + }, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "valid resource with primary key components", |
| 33 | + resource: NewResourceData(&Table{Name: "test", Columns: ColumnList{{Name: "col1", Type: arrow.BinaryTypes.String, PrimaryKeyComponent: true}}}, nil, nil), |
| 34 | + err: nil, |
| 35 | + valueSetter: func(resource *Resource) error { |
| 36 | + return resource.Set("col1", "test") |
| 37 | + }, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "invalid resource with primary keys", |
| 41 | + resource: NewResourceData(&Table{Name: "test", Columns: ColumnList{{Name: "col1", Type: arrow.BinaryTypes.String, PrimaryKey: true}}}, nil, nil), |
| 42 | + err: errors.New(`missing primary key on columns: [col1]`), |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "invalid resource with primary key components", |
| 46 | + resource: NewResourceData(&Table{Name: "test", Columns: ColumnList{{Name: "col1", Type: arrow.BinaryTypes.String, PrimaryKeyComponent: true}}}, nil, nil), |
| 47 | + err: errors.New(`missing primary key component on columns: [col1]`), |
| 48 | + }, |
| 49 | + } |
| 50 | + for _, tt := range tests { |
| 51 | + t.Run(tt.name, func(t *testing.T) { |
| 52 | + if tt.valueSetter != nil { |
| 53 | + require.NoError(t, tt.valueSetter(tt.resource)) |
| 54 | + } |
| 55 | + validationError := tt.resource.Validate() |
| 56 | + if tt.err == nil { |
| 57 | + require.NoError(t, validationError) |
| 58 | + } else { |
| 59 | + require.ErrorContains(t, validationError, tt.err.Error()) |
| 60 | + } |
| 61 | + }) |
| 62 | + } |
| 63 | +} |
0 commit comments