|
15 | 15 | package parser |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "path" |
| 19 | + "path/filepath" |
18 | 20 | "testing" |
19 | 21 |
|
20 | 22 | "github.com/google/go-cmp/cmp" |
21 | 23 | "github.com/google/go-cmp/cmp/cmpopts" |
22 | 24 | "github.com/googleapis/librarian/internal/sidekick/internal/api" |
| 25 | + "github.com/googleapis/librarian/internal/sidekick/internal/config" |
23 | 26 | ) |
24 | 27 |
|
| 28 | +var ( |
| 29 | + testdataDir, _ = filepath.Abs("../../testdata") |
| 30 | +) |
| 31 | + |
| 32 | +func TestCreateModelOpenAPI(t *testing.T) { |
| 33 | + cfg := &config.Config{ |
| 34 | + General: config.GeneralConfig{ |
| 35 | + SpecificationFormat: "openapi", |
| 36 | + ServiceConfig: path.Join(testdataDir, "googleapis/google/cloud/secretmanager/v1/secretmanager_v1.yaml"), |
| 37 | + SpecificationSource: path.Join(testdataDir, "openapi/secretmanager_openapi_v1.json"), |
| 38 | + }, |
| 39 | + } |
| 40 | + model, err := CreateModel(cfg) |
| 41 | + if err != nil { |
| 42 | + t.Fatal(err) |
| 43 | + } |
| 44 | + _, ok := model.State.ServiceByID[".google.cloud.secretmanager.v1.SecretManagerService"] |
| 45 | + if !ok { |
| 46 | + t.Errorf("missing service (.google.cloud.secretmanager.v1.SecretManagerService) in ServiceByID index") |
| 47 | + return |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestCreateModelProtobuf(t *testing.T) { |
| 52 | + requireProtoc(t) |
| 53 | + cfg := &config.Config{ |
| 54 | + General: config.GeneralConfig{ |
| 55 | + SpecificationFormat: "protobuf", |
| 56 | + ServiceConfig: "google/cloud/secretmanager/v1/secretmanager_v1.yaml", |
| 57 | + SpecificationSource: "google/cloud/secretmanager/v1", |
| 58 | + }, |
| 59 | + Source: map[string]string{ |
| 60 | + "googleapis-root": path.Join(testdataDir, "googleapis"), |
| 61 | + }, |
| 62 | + } |
| 63 | + model, err := CreateModel(cfg) |
| 64 | + if err != nil { |
| 65 | + t.Fatal(err) |
| 66 | + } |
| 67 | + _, ok := model.State.ServiceByID[".google.cloud.secretmanager.v1.SecretManagerService"] |
| 68 | + if !ok { |
| 69 | + t.Errorf("missing service (.google.cloud.secretmanager.v1.SecretManagerService) in ServiceByID index") |
| 70 | + return |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestCreateModelOverrides(t *testing.T) { |
| 75 | + requireProtoc(t) |
| 76 | + cfg := &config.Config{ |
| 77 | + General: config.GeneralConfig{ |
| 78 | + SpecificationFormat: "protobuf", |
| 79 | + ServiceConfig: "google/cloud/secretmanager/v1/secretmanager_v1.yaml", |
| 80 | + SpecificationSource: "google/cloud/secretmanager/v1", |
| 81 | + }, |
| 82 | + Source: map[string]string{ |
| 83 | + "googleapis-root": path.Join(testdataDir, "googleapis"), |
| 84 | + "name-override": "Name Override", |
| 85 | + "title-override": "Title Override", |
| 86 | + "description-override": "Description Override", |
| 87 | + }, |
| 88 | + } |
| 89 | + model, err := CreateModel(cfg) |
| 90 | + if err != nil { |
| 91 | + t.Fatal(err) |
| 92 | + } |
| 93 | + type TestCase struct { |
| 94 | + got string |
| 95 | + want string |
| 96 | + } |
| 97 | + testCases := []TestCase{ |
| 98 | + {model.Name, "Name Override"}, |
| 99 | + {model.Title, "Title Override"}, |
| 100 | + {model.Description, "Description Override"}, |
| 101 | + } |
| 102 | + for _, c := range testCases { |
| 103 | + if c.got != c.want { |
| 104 | + t.Errorf("mimatched override got=%q, want=%q", c.got, c.want) |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestCreateModelNone(t *testing.T) { |
| 110 | + requireProtoc(t) |
| 111 | + cfg := &config.Config{ |
| 112 | + General: config.GeneralConfig{ |
| 113 | + SpecificationFormat: "none", |
| 114 | + ServiceConfig: "google/cloud/secretmanager/v1/secretmanager_v1.yaml", |
| 115 | + SpecificationSource: "none", |
| 116 | + }, |
| 117 | + Source: map[string]string{ |
| 118 | + "googleapis-root": path.Join(testdataDir, "googleapis"), |
| 119 | + "name-override": "Name Override", |
| 120 | + "title-override": "Title Override", |
| 121 | + "description-override": "Description Override", |
| 122 | + }, |
| 123 | + } |
| 124 | + model, err := CreateModel(cfg) |
| 125 | + if err != nil { |
| 126 | + t.Fatal(err) |
| 127 | + } |
| 128 | + if model != nil { |
| 129 | + t.Errorf("expected `nil` model with source format == none") |
| 130 | + } |
| 131 | +} |
| 132 | + |
25 | 133 | func checkMessage(t *testing.T, got *api.Message, want *api.Message) { |
26 | 134 | t.Helper() |
27 | 135 | // Checking Parent, Messages, Fields, and OneOfs requires special handling. |
|
0 commit comments