github.com/cbroglie/openapi2proto@v0.0.0-20171004221549-76b8501da882/proto_test.go (about) 1 package openapi2proto 2 3 import ( 4 "encoding/json" 5 "io/ioutil" 6 "os" 7 "path" 8 "testing" 9 10 "gopkg.in/yaml.v2" 11 ) 12 13 func TestRefType(t *testing.T) { 14 tests := []struct { 15 tName string 16 ref string 17 defs map[string]*Items 18 19 want string 20 wantPkg string 21 }{ 22 { 23 "Simple ref", 24 25 "#/definitions/Name", 26 map[string]*Items{ 27 "Name": &Items{ 28 Type: "object", 29 }, 30 }, 31 "Name", 32 "", 33 }, 34 { 35 "URL nested ref", 36 37 "http://something.com/commons/name.json#/definitions/Name", 38 nil, 39 "commons.name.Name", 40 "commons/name.proto", 41 }, 42 { 43 "URL no ref", 44 45 "http://something.com/commons/name.json", 46 nil, 47 "commons.Name", 48 "commons/name.proto", 49 }, 50 { 51 "relative no ref", 52 53 "commons/names/Name.json", 54 nil, 55 "commons.names.Name", 56 "commons/names/name.proto", 57 }, 58 { 59 "relative nested ref", 60 61 "commons/names/Name.json#/definitions/Name", 62 nil, 63 "commons.names.name.Name", 64 "commons/names/name.proto", 65 }, 66 { 67 "relative nested ref", 68 69 "something.json#/definitions/RelativeRef", 70 nil, 71 "something.RelativeRef", 72 "something.proto", 73 }, 74 75 { 76 "relative nested ref", 77 78 "names.json#/definitions/Name", 79 nil, 80 "names.Name", 81 "names.proto", 82 }, 83 84 { 85 "relative ref, back one dir", 86 87 "../commons/names/Name.json", 88 nil, 89 "commons.names.Name", 90 "commons/names/name.proto", 91 }, 92 { 93 "relative nested ref, back two dir", 94 95 "../../commons/names/Name.json#/definitions/Name", 96 nil, 97 "commons.names.name.Name", 98 "commons/names/name.proto", 99 }, 100 } 101 102 for _, test := range tests { 103 t.Run(test.tName, func(t *testing.T) { 104 got, gotPkg := refType(test.ref, test.defs) 105 if got != test.want { 106 t.Errorf("[%s] expected %q got %q", test.tName, test.want, got) 107 } 108 109 if gotPkg != test.wantPkg { 110 t.Errorf("[%s] expected package %q got %q", test.tName, test.wantPkg, gotPkg) 111 } 112 }) 113 } 114 } 115 116 func TestGenerateProto(t *testing.T) { 117 tests := []struct { 118 yaml bool 119 options bool 120 givenFixturePath string 121 122 wantProto string 123 }{ 124 { 125 true, 126 false, 127 "fixtures/cats.yaml", 128 129 "fixtures/cats.proto", 130 }, 131 { 132 false, 133 false, 134 "fixtures/semantic_api.json", 135 136 "fixtures/semantic_api.proto", 137 }, 138 { 139 false, 140 false, 141 "fixtures/most_popular.json", 142 143 "fixtures/most_popular.proto", 144 }, 145 { 146 true, 147 false, 148 "fixtures/spec.yaml", 149 150 "fixtures/spec.proto", 151 }, 152 { 153 false, 154 false, 155 "fixtures/spec.json", 156 157 "fixtures/spec.proto", 158 }, 159 { 160 false, 161 true, 162 "fixtures/semantic_api.json", 163 164 "fixtures/semantic_api-options.proto", 165 }, 166 { 167 false, 168 true, 169 "fixtures/most_popular.json", 170 171 "fixtures/most_popular-options.proto", 172 }, 173 { 174 true, 175 true, 176 "fixtures/spec.yaml", 177 178 "fixtures/spec-options.proto", 179 }, 180 { 181 false, 182 true, 183 "fixtures/spec.json", 184 185 "fixtures/spec-options.proto", 186 }, 187 { 188 false, 189 false, 190 "fixtures/includes_query.json", 191 192 "fixtures/includes_query.proto", 193 }, 194 { 195 false, 196 false, 197 "fixtures/lowercase_def.json", 198 199 "fixtures/lowercase_def.proto", 200 }, 201 { 202 true, 203 false, 204 "fixtures/petstore/swagger.yaml", 205 206 "fixtures/petstore/swagger.proto", 207 }, 208 } 209 210 origin, _ := os.Getwd() 211 for _, test := range tests { 212 t.Run(test.givenFixturePath, func(t *testing.T) { 213 os.Chdir(origin) 214 testSpec, err := ioutil.ReadFile(test.givenFixturePath) 215 if err != nil { 216 t.Fatal("unable to open test fixture: ", err) 217 } 218 219 os.Chdir(path.Dir(test.givenFixturePath)) 220 var testAPI APIDefinition 221 if test.yaml { 222 err = yaml.Unmarshal(testSpec, &testAPI) 223 if err != nil { 224 t.Fatalf("unable to unmarshal text fixture into APIDefinition: %s - %s ", 225 test.givenFixturePath, err) 226 } 227 } else { 228 err = json.Unmarshal(testSpec, &testAPI) 229 if err != nil { 230 t.Fatal("unable to unmarshal text fixture into APIDefinition: %s - %s", 231 test.givenFixturePath, err) 232 } 233 234 } 235 236 protoResult, err := GenerateProto(&testAPI, test.options) 237 if err != nil { 238 t.Fatal("unable to generate protobuf from APIDefinition: ", err) 239 } 240 241 os.Chdir(origin) 242 want, err := ioutil.ReadFile(test.wantProto) 243 if err != nil { 244 t.Fatal("unable to open test fixture: ", err) 245 } 246 247 if string(want) != string(protoResult) { 248 t.Errorf("testYaml (%s) expected:\n%s\nGOT:\n%s", 249 test.givenFixturePath, want, protoResult) 250 } 251 }) 252 } 253 }