github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/document/converter_test.go (about)

     1  package document_test
     2  
     3  import (
     4  	"database/sql"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/kyma-incubator/compass/components/director/internal/repo"
     9  
    10  	"github.com/kyma-incubator/compass/components/director/pkg/str"
    11  
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/kyma-incubator/compass/components/director/internal/domain/document"
    15  	"github.com/kyma-incubator/compass/components/director/internal/domain/document/automock"
    16  	"github.com/kyma-incubator/compass/components/director/internal/model"
    17  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
    18  	"github.com/stretchr/testify/assert"
    19  )
    20  
    21  func TestConverter_ToGraphQL(t *testing.T) {
    22  	// GIVEN
    23  	testCases := []struct {
    24  		Name     string
    25  		Input    *model.Document
    26  		Expected *graphql.Document
    27  	}{
    28  		{
    29  			Name:     "All properties given",
    30  			Input:    fixModelDocumentForApp("1", "foo"),
    31  			Expected: fixGQLDocument("1", "foo"),
    32  		},
    33  		{
    34  			Name:     "Empty",
    35  			Input:    &model.Document{BaseEntity: &model.BaseEntity{}},
    36  			Expected: &graphql.Document{BaseEntity: &graphql.BaseEntity{}},
    37  		},
    38  		{
    39  			Name:     "Nil",
    40  			Input:    nil,
    41  			Expected: nil,
    42  		},
    43  	}
    44  
    45  	for _, testCase := range testCases {
    46  		t.Run(testCase.Name, func(t *testing.T) {
    47  			frConv := &automock.FetchRequestConverter{}
    48  			converter := document.NewConverter(frConv)
    49  
    50  			// WHEN
    51  			res := converter.ToGraphQL(testCase.Input)
    52  
    53  			// then
    54  			assert.Equal(t, testCase.Expected, res)
    55  			frConv.AssertExpectations(t)
    56  		})
    57  	}
    58  }
    59  
    60  func TestConverter_MultipleToGraphQL(t *testing.T) {
    61  	// GIVEN
    62  	input := []*model.Document{
    63  		fixModelDocumentForApp("1", "foo"),
    64  		fixModelDocumentForApp("2", "bar"),
    65  		{BaseEntity: &model.BaseEntity{}},
    66  		nil,
    67  	}
    68  	expected := []*graphql.Document{
    69  		fixGQLDocument("1", "foo"),
    70  		fixGQLDocument("2", "bar"),
    71  		{BaseEntity: &graphql.BaseEntity{}},
    72  	}
    73  	frConv := &automock.FetchRequestConverter{}
    74  	converter := document.NewConverter(frConv)
    75  
    76  	// WHEN
    77  	res := converter.MultipleToGraphQL(input)
    78  
    79  	// then
    80  	assert.Equal(t, expected, res)
    81  	frConv.AssertExpectations(t)
    82  }
    83  
    84  func TestConverter_InputFromGraphQL(t *testing.T) {
    85  	// GIVEN
    86  	testCases := []struct {
    87  		Name     string
    88  		Input    *graphql.DocumentInput
    89  		Expected *model.DocumentInput
    90  	}{
    91  		{
    92  			Name:     "All properties given",
    93  			Input:    fixGQLDocumentInput("foo"),
    94  			Expected: fixModelDocumentInput("foo"),
    95  		},
    96  		{
    97  			Name:     "Empty",
    98  			Input:    &graphql.DocumentInput{},
    99  			Expected: &model.DocumentInput{},
   100  		},
   101  		{
   102  			Name:     "Nil",
   103  			Input:    nil,
   104  			Expected: nil,
   105  		},
   106  	}
   107  
   108  	for _, testCase := range testCases {
   109  		t.Run(testCase.Name, func(t *testing.T) {
   110  			frConv := &automock.FetchRequestConverter{}
   111  			if testCase.Input != nil {
   112  				frConv.On("InputFromGraphQL", testCase.Input.FetchRequest).Return(testCase.Expected.FetchRequest, nil)
   113  			}
   114  			converter := document.NewConverter(frConv)
   115  
   116  			// WHEN
   117  			res, err := converter.InputFromGraphQL(testCase.Input)
   118  
   119  			// then
   120  			assert.NoError(t, err)
   121  			assert.Equal(t, testCase.Expected, res)
   122  			frConv.AssertExpectations(t)
   123  		})
   124  	}
   125  }
   126  
   127  func TestConverter_MultipleInputFromGraphQL(t *testing.T) {
   128  	// GIVEN
   129  	input := []*graphql.DocumentInput{
   130  		fixGQLDocumentInput("foo"),
   131  		fixGQLDocumentInput("bar"),
   132  		{},
   133  		nil,
   134  	}
   135  	expected := []*model.DocumentInput{
   136  		fixModelDocumentInput("foo"),
   137  		fixModelDocumentInput("bar"),
   138  		{},
   139  	}
   140  	frConv := &automock.FetchRequestConverter{}
   141  	frConv.On("InputFromGraphQL", input[0].FetchRequest).Return(expected[0].FetchRequest, nil)
   142  	frConv.On("InputFromGraphQL", (*graphql.FetchRequestInput)(nil)).Return(nil, nil)
   143  	converter := document.NewConverter(frConv)
   144  
   145  	// WHEN
   146  	res, err := converter.MultipleInputFromGraphQL(input)
   147  
   148  	// then
   149  	assert.NoError(t, err)
   150  	assert.Equal(t, expected, res)
   151  	frConv.AssertExpectations(t)
   152  }
   153  
   154  func TestToEntity(t *testing.T) {
   155  	// GIVEN
   156  	sut := document.NewConverter(nil)
   157  
   158  	modelWithRequiredFields := model.Document{
   159  		BundleID:    "givenBundleID",
   160  		AppID:       str.Ptr("givenAppID"),
   161  		Title:       "givenTitle",
   162  		Description: "givenDescription",
   163  		DisplayName: "givenDisplayName",
   164  		Format:      "givenFormat",
   165  		BaseEntity: &model.BaseEntity{
   166  			ID:        "givenID",
   167  			Ready:     true,
   168  			CreatedAt: &fixedTimestamp,
   169  			UpdatedAt: &time.Time{},
   170  			DeletedAt: &time.Time{},
   171  			Error:     nil,
   172  		},
   173  	}
   174  
   175  	t.Run("only required fields", func(t *testing.T) {
   176  		givenModel := modelWithRequiredFields
   177  		// WHEN
   178  		actual, err := sut.ToEntity(&givenModel)
   179  		// THEN
   180  		require.NoError(t, err)
   181  		assert.Equal(t, &document.Entity{
   182  			BndlID:      givenModel.BundleID,
   183  			AppID:       repo.NewNullableString(givenModel.AppID),
   184  			Title:       givenModel.Title,
   185  			Description: givenModel.Description,
   186  			DisplayName: givenModel.DisplayName,
   187  			Format:      string(givenModel.Format),
   188  			BaseEntity: &repo.BaseEntity{
   189  				ID:        givenModel.ID,
   190  				Ready:     givenModel.Ready,
   191  				CreatedAt: givenModel.CreatedAt,
   192  				UpdatedAt: givenModel.UpdatedAt,
   193  				DeletedAt: givenModel.DeletedAt,
   194  				Error:     repo.NewNullableString(givenModel.Error),
   195  			},
   196  		}, actual)
   197  	})
   198  
   199  	t.Run("all fields", func(t *testing.T) {
   200  		givenModel := modelWithRequiredFields
   201  		givenModel.Data = str.Ptr("givenData")
   202  		givenModel.Kind = str.Ptr("givenKind")
   203  		// WHEN
   204  		actual, err := sut.ToEntity(&givenModel)
   205  		// THEN
   206  		require.NoError(t, err)
   207  		assert.Equal(t, sql.NullString{Valid: true, String: "givenData"}, actual.Data)
   208  		assert.Equal(t, sql.NullString{Valid: true, String: "givenKind"}, actual.Kind)
   209  	})
   210  }
   211  
   212  func TestFromEntity(t *testing.T) {
   213  	// GIVEN
   214  	sut := document.NewConverter(nil)
   215  	entityWithRequiredFields := document.Entity{
   216  		BndlID:      "givenBundleID",
   217  		AppID:       repo.NewValidNullableString("givenAppID"),
   218  		Title:       "givenTitle",
   219  		DisplayName: "givenDisplayName",
   220  		Description: "givenDescription",
   221  		Format:      "MARKDOWN",
   222  		BaseEntity: &repo.BaseEntity{
   223  			ID: "givenID",
   224  		},
   225  	}
   226  
   227  	t.Run("only required fields", func(t *testing.T) {
   228  		givenEntity := entityWithRequiredFields
   229  		// WHEN
   230  		actualModel, err := sut.FromEntity(&givenEntity)
   231  		// THEN
   232  		require.NoError(t, err)
   233  		assert.Equal(t, &model.Document{
   234  			BundleID:    "givenBundleID",
   235  			AppID:       str.Ptr("givenAppID"),
   236  			Title:       "givenTitle",
   237  			DisplayName: "givenDisplayName",
   238  			Description: "givenDescription",
   239  			Format:      model.DocumentFormatMarkdown,
   240  			BaseEntity: &model.BaseEntity{
   241  				ID: "givenID",
   242  			},
   243  		}, actualModel)
   244  	})
   245  
   246  	t.Run("all fields", func(t *testing.T) {
   247  		givenEntity := entityWithRequiredFields
   248  		givenEntity.Data = sql.NullString{
   249  			Valid:  true,
   250  			String: "givenData",
   251  		}
   252  		givenEntity.Kind = sql.NullString{
   253  			Valid:  true,
   254  			String: "givenKind",
   255  		}
   256  
   257  		// WHEN
   258  		actualModel, err := sut.FromEntity(&givenEntity)
   259  		// THEN
   260  		require.NoError(t, err)
   261  		assert.Equal(t, str.Ptr("givenData"), actualModel.Data)
   262  		assert.Equal(t, str.Ptr("givenKind"), actualModel.Kind)
   263  	})
   264  }