...

Source file src/github.com/redhat-developer/odo/pkg/service/crd_builder_test.go

Documentation: github.com/redhat-developer/odo/pkg/service

     1  package service
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/go-openapi/spec"
     8  	"github.com/google/go-cmp/cmp"
     9  )
    10  
    11  func TestBuildCRDFromParams(t *testing.T) {
    12  	tests := []struct {
    13  		name    string
    14  		crd     *spec.Schema
    15  		params  map[string]string
    16  		want    map[string]interface{}
    17  		wantErr bool
    18  	}{
    19  		{
    20  			name: "params ok without crd",
    21  			params: map[string]string{
    22  				"u":     "1",
    23  				"a.b.c": "2",
    24  				"a.b.d": "3",
    25  				"a.B":   "4",
    26  			},
    27  			want: map[string]interface{}{
    28  				"u": int64(1),
    29  				"a": map[string]interface{}{
    30  					"b": map[string]interface{}{
    31  						"c": int64(2),
    32  						"d": int64(3),
    33  					},
    34  					"B": int64(4),
    35  				},
    36  			},
    37  			wantErr: false,
    38  		},
    39  		{
    40  			name: "typed params without crd",
    41  			params: map[string]string{
    42  				"a.bool":   "true",
    43  				"a.string": "foobar",
    44  				"a.float":  "1.234",
    45  			},
    46  			want: map[string]interface{}{
    47  				"a": map[string]interface{}{
    48  					"bool":   true,
    49  					"string": "foobar",
    50  					"float":  1.234,
    51  				},
    52  			},
    53  			wantErr: false,
    54  		},
    55  		{
    56  			name: "typed params with crd",
    57  			params: map[string]string{
    58  				"a.bool":    "true",
    59  				"a.string1": "foobar",
    60  				"a.string2": "true",
    61  				"a.string3": "1.234",
    62  				"a.string4": "11",
    63  				"a.float":   "1.234",
    64  				"a.int":     "11",
    65  			},
    66  			crd: &spec.Schema{
    67  				SchemaProps: spec.SchemaProps{
    68  					Type: spec.StringOrArray{
    69  						"object",
    70  					},
    71  					Properties: map[string]spec.Schema{
    72  						"a": {
    73  							SchemaProps: spec.SchemaProps{
    74  								Type: spec.StringOrArray{
    75  									"object",
    76  								},
    77  								Properties: map[string]spec.Schema{
    78  									"bool": {
    79  										SchemaProps: spec.SchemaProps{
    80  											Type: spec.StringOrArray{
    81  												"boolean",
    82  											},
    83  										},
    84  									},
    85  									"int": {
    86  										SchemaProps: spec.SchemaProps{
    87  											Type: spec.StringOrArray{
    88  												"integer",
    89  											},
    90  										},
    91  									},
    92  									"float": {
    93  										SchemaProps: spec.SchemaProps{
    94  											Type: spec.StringOrArray{
    95  												"number",
    96  											},
    97  										},
    98  									},
    99  									"string1": {
   100  										SchemaProps: spec.SchemaProps{
   101  											Type: spec.StringOrArray{
   102  												"string",
   103  											},
   104  										},
   105  									},
   106  									"string2": {
   107  										SchemaProps: spec.SchemaProps{
   108  											Type: spec.StringOrArray{
   109  												"string",
   110  											},
   111  										},
   112  									},
   113  									"string3": {
   114  										SchemaProps: spec.SchemaProps{
   115  											Type: spec.StringOrArray{
   116  												"string",
   117  											},
   118  										},
   119  									},
   120  									"string4": {
   121  										SchemaProps: spec.SchemaProps{
   122  											Type: spec.StringOrArray{
   123  												"string",
   124  											},
   125  										},
   126  									},
   127  								},
   128  							},
   129  						},
   130  					},
   131  				},
   132  			},
   133  			want: map[string]interface{}{
   134  				"a": map[string]interface{}{
   135  					"bool":    true,
   136  					"string1": "foobar",
   137  					"string2": "true",
   138  					"string3": "1.234",
   139  					"string4": "11",
   140  					"float":   1.234,
   141  					"int":     int64(11),
   142  				},
   143  			},
   144  			wantErr: false,
   145  		},
   146  		{
   147  			name: "params error map defined before value",
   148  			params: map[string]string{
   149  				"u":     "1",
   150  				"a.b.c": "2",
   151  				"a.b":   "3",
   152  				"a.B":   "4",
   153  			},
   154  			want:    nil,
   155  			wantErr: true,
   156  		},
   157  		{
   158  			name: "params error value defined before map",
   159  			params: map[string]string{
   160  				"u":     "1",
   161  				"a.b":   "2",
   162  				"a.b.c": "3",
   163  				"a.B":   "4",
   164  			},
   165  			want:    nil,
   166  			wantErr: true,
   167  		},
   168  	}
   169  
   170  	for _, tt := range tests {
   171  		t.Run(tt.name, func(t *testing.T) {
   172  			got, gotErr := BuildCRDFromParams(tt.params, tt.crd, "a group", "a version", "a kind")
   173  			if gotErr != nil != tt.wantErr {
   174  				t.Errorf("got err: %v, expected err: %v\n", gotErr != nil, tt.wantErr)
   175  			}
   176  			if gotErr == nil {
   177  				if diff := cmp.Diff(tt.want, got["spec"]); diff != "" {
   178  					t.Errorf("BuildCRDFromParams() mismatch (-want +got):\n%s", diff)
   179  					jsonGot, _ := json.Marshal(got["spec"])
   180  					jsonWant, _ := json.Marshal(tt.want)
   181  					t.Errorf("\ngot:  %+v\n\nwant: %v\n", string(jsonGot), string(jsonWant))
   182  				}
   183  			}
   184  		})
   185  	}
   186  }
   187  

View as plain text