|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/cloudquery/codegen/jsonschema" |
| 8 | +) |
| 9 | + |
| 10 | +func TestSpecValidate(t *testing.T) { |
| 11 | + testCases := []struct { |
| 12 | + name string |
| 13 | + spec Spec |
| 14 | + err bool |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "empty", |
| 18 | + spec: Spec{}, |
| 19 | + }, |
| 20 | + { |
| 21 | + name: "valid", |
| 22 | + spec: Spec{ |
| 23 | + ProjectID: "project_id", |
| 24 | + ServiceAccountJSON: "{}", |
| 25 | + OrderDirection: "asc", |
| 26 | + MaxBatchSize: 1, |
| 27 | + }, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "invalid order direction", |
| 31 | + spec: Spec{ |
| 32 | + ProjectID: "project_id", |
| 33 | + ServiceAccountJSON: "{}", |
| 34 | + OrderDirection: "invalid", |
| 35 | + MaxBatchSize: 1, |
| 36 | + }, |
| 37 | + err: true, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "invalid max batch size", |
| 41 | + spec: Spec{ |
| 42 | + ProjectID: "project_id", |
| 43 | + ServiceAccountJSON: "{}", |
| 44 | + OrderDirection: "asc", |
| 45 | + MaxBatchSize: -1, |
| 46 | + }, |
| 47 | + err: true, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "base64 service account", |
| 51 | + spec: Spec{ |
| 52 | + ProjectID: "project_id", |
| 53 | + ServiceAccountJSON: base64.StdEncoding.EncodeToString([]byte("{}")), |
| 54 | + OrderDirection: "asc", |
| 55 | + MaxBatchSize: 1, |
| 56 | + UseBase64: true, |
| 57 | + }, |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + for _, tc := range testCases { |
| 62 | + t.Run(tc.name, func(t *testing.T) { |
| 63 | + err := tc.spec.Validate() |
| 64 | + if tc.err && err == nil { |
| 65 | + t.Errorf("expected error but got none") |
| 66 | + } |
| 67 | + if !tc.err && err != nil { |
| 68 | + t.Errorf("expected no error but got %v", err) |
| 69 | + } |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestJSONSchema(t *testing.T) { |
| 75 | + jsonschema.TestJSONSchema(t, JSONSchema, []jsonschema.TestCase{ |
| 76 | + { |
| 77 | + Name: "empty spec", |
| 78 | + Spec: `{}`, |
| 79 | + }, |
| 80 | + { |
| 81 | + Name: "valid spec", |
| 82 | + Spec: `{"project_id": "project_id","service_account_json": "{}","order_direction": "asc","max_batch_size": 1}`, |
| 83 | + }, |
| 84 | + { |
| 85 | + Name: "invalid order direction", |
| 86 | + Spec: `{"project_id": "project_id","service_account_json": "{}","order_direction": "invalid","max_batch_size": 1}`, |
| 87 | + Err: true, |
| 88 | + }, |
| 89 | + { |
| 90 | + Name: "invalid max batch size", |
| 91 | + Spec: `{"project_id": "project_id","service_account_json": "{}","order_direction": "asc","max_batch_size": -1}`, |
| 92 | + Err: true, |
| 93 | + }, |
| 94 | + { |
| 95 | + Name: "base64 service account", |
| 96 | + Spec: `{"project_id": "project_id","service_account_json": "` + base64.StdEncoding.EncodeToString([]byte("{}")) + `","order_direction": "asc","max_batch_size": 1,"use_base64": true}`, |
| 97 | + }, |
| 98 | + }) |
| 99 | +} |
0 commit comments