github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/graphql/graphqlizer/fields_test.go (about)

     1  package graphqlizer_test
     2  
     3  import (
     4  	"regexp"
     5  	"testing"
     6  
     7  	"github.com/kyma-incubator/compass/components/director/pkg/graphql/graphqlizer"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestGqlFieldsProvider_Page(t *testing.T) {
    12  	t.Run("Success", func(t *testing.T) {
    13  		fp := &graphqlizer.GqlFieldsProvider{}
    14  		expected := "data {\n\t\tproperty\n\t}\n\tpageInfo {startCursor\n\t\tendCursor\n\t\thasNextPage}\n\ttotalCount\n\t"
    15  		actual := fp.Page("property")
    16  		assert.Equal(t, expected, actual)
    17  	})
    18  }
    19  
    20  func TestGqlFieldsProvider_OmitCombinedFieldsForApplication(t *testing.T) {
    21  	type testCase struct {
    22  		name               string
    23  		fp                 *graphqlizer.GqlFieldsProvider
    24  		omit               []string
    25  		expectedProperties map[string]int
    26  	}
    27  	tests := []testCase{
    28  		{
    29  			name: "with omitted top level complex fields",
    30  			fp:   &graphqlizer.GqlFieldsProvider{},
    31  			omit: []string{"bundles", "webhooks"},
    32  			expectedProperties: map[string]int{
    33  				"id":                  2,
    34  				"name":                1,
    35  				"integrationSystemID": 1,
    36  				"status":              1,
    37  				"auths":               1,
    38  				"bundles":             0,
    39  				"instanceAuths":       0,
    40  				"webhooks":            0,
    41  				"apiDefinitions":      0,
    42  				"eventDefinitions":    0,
    43  				"documents":           0,
    44  				"fetchRequest":        0,
    45  			},
    46  		},
    47  		{
    48  			name: "with multiple omitted 'fetchRequest' fields",
    49  			fp:   &graphqlizer.GqlFieldsProvider{},
    50  			omit: []string{"bundles.apiDefinitions.spec.fetchRequest", "bundles.eventDefinitions.spec.fetchRequest", "bundles.documents.fetchRequest"},
    51  			expectedProperties: map[string]int{
    52  				"id":                  10,
    53  				"name":                4,
    54  				"integrationSystemID": 1,
    55  				"status":              2,
    56  				"auths":               1,
    57  				"bundles":             1,
    58  				"instanceAuths":       1,
    59  				"webhooks":            1,
    60  				"apiDefinitions":      1,
    61  				"eventDefinitions":    1,
    62  				"documents":           1,
    63  				"fetchRequest":        0,
    64  			},
    65  		},
    66  		{
    67  			name: "with certain field omitted only in some nested complex fields",
    68  			fp:   &graphqlizer.GqlFieldsProvider{},
    69  			omit: []string{"bundles.apiDefinitions.spec.fetchRequest", "bundles.eventDefinitions.spec.fetchRequest"},
    70  			expectedProperties: map[string]int{
    71  				"id":                  10,
    72  				"name":                4,
    73  				"integrationSystemID": 1,
    74  				"status":              3,
    75  				"auths":               1,
    76  				"bundles":             1,
    77  				"instanceAuths":       1,
    78  				"webhooks":            1,
    79  				"apiDefinitions":      1,
    80  				"eventDefinitions":    1,
    81  				"documents":           1,
    82  				"fetchRequest":        1,
    83  			},
    84  		},
    85  		{
    86  			name: "with omitted nested fields",
    87  			fp:   &graphqlizer.GqlFieldsProvider{},
    88  			omit: []string{"auths", "webhooks", "status", "bundles.instanceAuths", "bundles.documents", "bundles.apiDefinitions.spec.fetchRequest", "bundles.eventDefinitions.spec.fetchRequest"},
    89  			expectedProperties: map[string]int{
    90  				"id":                  6,
    91  				"name":                4,
    92  				"integrationSystemID": 1,
    93  				"status":              0,
    94  				"auths":               0,
    95  				"bundles":             1,
    96  				"instanceAuths":       0,
    97  				"webhooks":            0,
    98  				"apiDefinitions":      1,
    99  				"eventDefinitions":    1,
   100  				"documents":           0,
   101  				"fetchRequest":        0,
   102  			},
   103  		},
   104  	}
   105  	for _, tt := range tests {
   106  		t.Run(tt.name, func(t *testing.T) {
   107  			actual := tt.fp.OmitForApplication(tt.omit)
   108  			for expectedProp, expectedCount := range tt.expectedProperties {
   109  				fieldRegex := regexp.MustCompile(`\b` + expectedProp + `\b`)
   110  
   111  				matches := fieldRegex.FindAllStringIndex(actual, -1)
   112  				actualCount := len(matches)
   113  
   114  				assert.Equal(t, expectedCount, actualCount, expectedProp)
   115  			}
   116  		})
   117  	}
   118  }
   119  
   120  func TestGqlFieldsProvider_OmitSeparatelyFieldsForApplication(t *testing.T) {
   121  	type testCase struct {
   122  		name               string
   123  		fp                 *graphqlizer.GqlFieldsProvider
   124  		omit               []string
   125  		expectedProperties map[string]int
   126  	}
   127  	tests := []testCase{
   128  		{
   129  			name: "with no omitted fields on 'applications' level",
   130  			fp:   &graphqlizer.GqlFieldsProvider{},
   131  			omit: []string{},
   132  			expectedProperties: map[string]int{
   133  				"id":           10,
   134  				"name":         4,
   135  				"fetchRequest": 3,
   136  			},
   137  		},
   138  		{
   139  			name: "with omitted simple field on 'applications' level",
   140  			fp:   &graphqlizer.GqlFieldsProvider{},
   141  			omit: []string{"integrationSystemID"},
   142  			expectedProperties: map[string]int{
   143  				"id":                  10,
   144  				"name":                4,
   145  				"integrationSystemID": 0,
   146  				"bundles":             1,
   147  				"apiDefinitions":      1,
   148  				"fetchRequest":        3,
   149  			},
   150  		},
   151  		{
   152  			name: "with omitted complex field 'webhooks' on 'applications' level",
   153  			fp:   &graphqlizer.GqlFieldsProvider{},
   154  			omit: []string{"webhooks"},
   155  			expectedProperties: map[string]int{
   156  				"id":                  9,
   157  				"name":                4,
   158  				"integrationSystemID": 1,
   159  				"bundles":             1,
   160  				"webhooks":            0,
   161  				"apiDefinitions":      1,
   162  				"fetchRequest":        3,
   163  			},
   164  		},
   165  		{
   166  			name: "with omitted simple field on 'webhooks' level",
   167  			fp:   &graphqlizer.GqlFieldsProvider{},
   168  			omit: []string{"webhooks.id"},
   169  			expectedProperties: map[string]int{
   170  				"id":                  9,
   171  				"name":                4,
   172  				"integrationSystemID": 1,
   173  				"bundles":             1,
   174  				"webhooks":            1,
   175  				"apiDefinitions":      1,
   176  				"fetchRequest":        3,
   177  			},
   178  		},
   179  		{
   180  			name: "with omitted complex field 'bundles' on 'applications' level",
   181  			fp:   &graphqlizer.GqlFieldsProvider{},
   182  			omit: []string{"bundles"},
   183  			expectedProperties: map[string]int{
   184  				"id":                  3,
   185  				"name":                1,
   186  				"integrationSystemID": 1,
   187  				"bundles":             0,
   188  				"webhooks":            1,
   189  				"apiDefinitions":      0,
   190  				"fetchRequest":        0,
   191  			},
   192  		},
   193  		{
   194  			name: "with omitted simple field on 'bundles' level",
   195  			fp:   &graphqlizer.GqlFieldsProvider{},
   196  			omit: []string{"bundles.id"},
   197  			expectedProperties: map[string]int{
   198  				"id":                  9,
   199  				"name":                4,
   200  				"integrationSystemID": 1,
   201  				"bundles":             1,
   202  				"webhooks":            1,
   203  				"apiDefinitions":      1,
   204  				"fetchRequest":        3,
   205  			},
   206  		},
   207  		{
   208  			name: "with omitted simple field on 'instanceAuth' level",
   209  			fp:   &graphqlizer.GqlFieldsProvider{},
   210  			omit: []string{"bundles.instanceAuths.id"},
   211  			expectedProperties: map[string]int{
   212  				"id":                  9,
   213  				"name":                4,
   214  				"integrationSystemID": 1,
   215  				"bundles":             1,
   216  				"webhooks":            1,
   217  				"apiDefinitions":      1,
   218  				"fetchRequest":        3,
   219  				"reason":              1,
   220  			},
   221  		},
   222  		{
   223  			name: "with omitted simple field on 'instanceAuth.status' level",
   224  			fp:   &graphqlizer.GqlFieldsProvider{},
   225  			omit: []string{"bundles.instanceAuths.status.reason"},
   226  			expectedProperties: map[string]int{
   227  				"id":                  10,
   228  				"name":                4,
   229  				"integrationSystemID": 1,
   230  				"bundles":             1,
   231  				"webhooks":            1,
   232  				"apiDefinitions":      1,
   233  				"fetchRequest":        3,
   234  				"reason":              0,
   235  			},
   236  		},
   237  		{
   238  			name: "with omitted simple field on 'apiDefinitions' level",
   239  			fp:   &graphqlizer.GqlFieldsProvider{},
   240  			omit: []string{"bundles.apiDefinitions.id"},
   241  			expectedProperties: map[string]int{
   242  				"id":                  9,
   243  				"name":                4,
   244  				"integrationSystemID": 1,
   245  				"bundles":             1,
   246  				"webhooks":            1,
   247  				"apiDefinitions":      1,
   248  				"fetchRequest":        3,
   249  				"filter":              3,
   250  			},
   251  		},
   252  		{
   253  			name: "with omitted 'fetchRequest' field on 'apiDefinitions.spec' level",
   254  			fp:   &graphqlizer.GqlFieldsProvider{},
   255  			omit: []string{"bundles.apiDefinitions.spec.fetchRequest"},
   256  			expectedProperties: map[string]int{
   257  				"id":                  10,
   258  				"name":                4,
   259  				"integrationSystemID": 1,
   260  				"bundles":             1,
   261  				"webhooks":            1,
   262  				"apiDefinitions":      1,
   263  				"fetchRequest":        2,
   264  				"filter":              2,
   265  			},
   266  		},
   267  		{
   268  			name: "with omitted simple field on 'apiDefinitions.spec.fetchRequest' level",
   269  			fp:   &graphqlizer.GqlFieldsProvider{},
   270  			omit: []string{"bundles.apiDefinitions.spec.fetchRequest.filter"},
   271  			expectedProperties: map[string]int{
   272  				"id":                  10,
   273  				"name":                4,
   274  				"integrationSystemID": 1,
   275  				"bundles":             1,
   276  				"webhooks":            1,
   277  				"apiDefinitions":      1,
   278  				"fetchRequest":        3,
   279  				"filter":              2,
   280  				"forRemoval":          2,
   281  			},
   282  		},
   283  		{
   284  			name: "with omitted simple field on 'apiDefinitions.version' level",
   285  			fp:   &graphqlizer.GqlFieldsProvider{},
   286  			omit: []string{"bundles.apiDefinitions.version.forRemoval"},
   287  			expectedProperties: map[string]int{
   288  				"id":                  10,
   289  				"name":                4,
   290  				"integrationSystemID": 1,
   291  				"bundles":             1,
   292  				"webhooks":            1,
   293  				"apiDefinitions":      1,
   294  				"fetchRequest":        3,
   295  				"forRemoval":          1,
   296  			},
   297  		},
   298  		{
   299  			name: "with omitted simple field on 'eventDefinitions' level",
   300  			fp:   &graphqlizer.GqlFieldsProvider{},
   301  			omit: []string{"bundles.eventDefinitions.id"},
   302  			expectedProperties: map[string]int{
   303  				"id":                  9,
   304  				"name":                4,
   305  				"integrationSystemID": 1,
   306  				"bundles":             1,
   307  				"webhooks":            1,
   308  				"eventDefinitions":    1,
   309  				"fetchRequest":        3,
   310  				"filter":              3,
   311  			},
   312  		},
   313  		{
   314  			name: "with omitted 'fetchRequest' field on 'eventDefinitions.spec' level",
   315  			fp:   &graphqlizer.GqlFieldsProvider{},
   316  			omit: []string{"bundles.eventDefinitions.spec.fetchRequest"},
   317  			expectedProperties: map[string]int{
   318  				"id":                  10,
   319  				"name":                4,
   320  				"integrationSystemID": 1,
   321  				"bundles":             1,
   322  				"webhooks":            1,
   323  				"eventDefinitions":    1,
   324  				"fetchRequest":        2,
   325  				"filter":              2,
   326  			},
   327  		},
   328  		{
   329  			name: "with omitted simple field on 'eventDefinitions.spec.fetchRequest' level",
   330  			fp:   &graphqlizer.GqlFieldsProvider{},
   331  			omit: []string{"bundles.eventDefinitions.spec.fetchRequest.filter"},
   332  			expectedProperties: map[string]int{
   333  				"id":                  10,
   334  				"name":                4,
   335  				"integrationSystemID": 1,
   336  				"bundles":             1,
   337  				"webhooks":            1,
   338  				"eventDefinitions":    1,
   339  				"fetchRequest":        3,
   340  				"filter":              2,
   341  				"forRemoval":          2,
   342  			},
   343  		},
   344  		{
   345  			name: "with omitted simple field on 'eventDefinitions.version' level",
   346  			fp:   &graphqlizer.GqlFieldsProvider{},
   347  			omit: []string{"bundles.eventDefinitions.version.forRemoval"},
   348  			expectedProperties: map[string]int{
   349  				"id":                  10,
   350  				"name":                4,
   351  				"integrationSystemID": 1,
   352  				"bundles":             1,
   353  				"webhooks":            1,
   354  				"eventDefinitions":    1,
   355  				"fetchRequest":        3,
   356  				"forRemoval":          1,
   357  			},
   358  		},
   359  		{
   360  			name: "with omitted simple field on 'documents' level",
   361  			fp:   &graphqlizer.GqlFieldsProvider{},
   362  			omit: []string{"bundles.documents.id"},
   363  			expectedProperties: map[string]int{
   364  				"id":                  9,
   365  				"name":                4,
   366  				"integrationSystemID": 1,
   367  				"bundles":             1,
   368  				"webhooks":            1,
   369  				"documents":           1,
   370  				"fetchRequest":        3,
   371  				"filter":              3,
   372  			},
   373  		},
   374  		{
   375  			name: "with omitted 'fetchRequest' field on 'documents' level",
   376  			fp:   &graphqlizer.GqlFieldsProvider{},
   377  			omit: []string{"bundles.documents.fetchRequest"},
   378  			expectedProperties: map[string]int{
   379  				"id":                  10,
   380  				"name":                4,
   381  				"integrationSystemID": 1,
   382  				"bundles":             1,
   383  				"webhooks":            1,
   384  				"documents":           1,
   385  				"fetchRequest":        2,
   386  				"filter":              2,
   387  			},
   388  		},
   389  		{
   390  			name: "with omitted simple field on 'documents.fetchRequest' level",
   391  			fp:   &graphqlizer.GqlFieldsProvider{},
   392  			omit: []string{"bundles.documents.fetchRequest.filter"},
   393  			expectedProperties: map[string]int{
   394  				"id":                  10,
   395  				"name":                4,
   396  				"integrationSystemID": 1,
   397  				"bundles":             1,
   398  				"webhooks":            1,
   399  				"documents":           1,
   400  				"fetchRequest":        3,
   401  				"filter":              2,
   402  				"forRemoval":          2,
   403  			},
   404  		},
   405  		{
   406  			name: "with omitted non-existing fields",
   407  			fp:   &graphqlizer.GqlFieldsProvider{},
   408  			omit: []string{"bundles.nonExisting", "idTypo", "bundlesTypo.id", "bundles.apiDefinitions.idTypo"},
   409  			expectedProperties: map[string]int{
   410  				"id":               10,
   411  				"name":             4,
   412  				"bundles":          1,
   413  				"fetchRequest":     3,
   414  				"apiDefinitions":   1,
   415  				"eventDefinitions": 1,
   416  				"documents":        1,
   417  			},
   418  		},
   419  	}
   420  	for _, tt := range tests {
   421  		t.Run(tt.name, func(t *testing.T) {
   422  			actual := tt.fp.OmitForApplication(tt.omit)
   423  			for expectedProp, expectedCount := range tt.expectedProperties {
   424  				fieldRegex := regexp.MustCompile(`\b` + expectedProp + `\b`)
   425  
   426  				matches := fieldRegex.FindAllStringIndex(actual, -1)
   427  				actualCount := len(matches)
   428  
   429  				assert.Equal(t, expectedCount, actualCount, expectedProp)
   430  			}
   431  		})
   432  	}
   433  }