github.com/chrusty/openapi2proto@v0.0.0-20171127225041-f5804f48ccdb/proto_test.go (about)

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