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

     1  package version_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/kyma-incubator/compass/components/director/internal/repo/testdb"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/kyma-incubator/compass/components/director/internal/domain/version"
    11  	"github.com/kyma-incubator/compass/components/director/internal/model"
    12  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestConverter_ToGraphQL(t *testing.T) {
    17  	// GIVEN
    18  	testCases := []struct {
    19  		Name     string
    20  		Input    *model.Version
    21  		Expected *graphql.Version
    22  	}{
    23  		{
    24  			Name:     "All properties given",
    25  			Input:    fixModelVersion("foo", true, "bar", false),
    26  			Expected: fixGQLVersion("foo", true, "bar", false),
    27  		},
    28  		{
    29  			Name:     "Empty",
    30  			Input:    &model.Version{},
    31  			Expected: &graphql.Version{},
    32  		},
    33  		{
    34  			Name:     "Nil",
    35  			Input:    nil,
    36  			Expected: nil,
    37  		},
    38  	}
    39  
    40  	for _, testCase := range testCases {
    41  		t.Run(testCase.Name, func(t *testing.T) {
    42  			converter := version.NewConverter()
    43  
    44  			// WHEN
    45  			res := converter.ToGraphQL(testCase.Input)
    46  
    47  			// then
    48  			assert.Equal(t, testCase.Expected, res)
    49  		})
    50  	}
    51  }
    52  
    53  func TestConverter_InputFromGraphQL(t *testing.T) {
    54  	// GIVEN
    55  	testCases := []struct {
    56  		Name     string
    57  		Input    *graphql.VersionInput
    58  		Expected *model.VersionInput
    59  	}{
    60  		{
    61  			Name:     "All properties given",
    62  			Input:    fixGQLVersionInput("foo", true, "bar", false),
    63  			Expected: fixModelVersionInput("foo", true, "bar", false),
    64  		},
    65  		{
    66  			Name:     "Empty",
    67  			Input:    &graphql.VersionInput{},
    68  			Expected: &model.VersionInput{},
    69  		},
    70  		{
    71  			Name:     "Nil",
    72  			Input:    nil,
    73  			Expected: nil,
    74  		},
    75  	}
    76  
    77  	for _, testCase := range testCases {
    78  		t.Run(testCase.Name, func(t *testing.T) {
    79  			converter := version.NewConverter()
    80  
    81  			// WHEN
    82  			res := converter.InputFromGraphQL(testCase.Input)
    83  
    84  			// then
    85  			assert.Equal(t, testCase.Expected, res)
    86  		})
    87  	}
    88  }
    89  
    90  func TestConverter_FromEntity(t *testing.T) {
    91  	t.Run("success all nullable properties filled", func(t *testing.T) {
    92  		// GIVEN
    93  		versionEntity := *fixVersionEntity("v1.2", true, "v1.1", false)
    94  		versionConv := version.NewConverter()
    95  		// WHEN
    96  		versionModel := versionConv.FromEntity(versionEntity)
    97  		// THEN
    98  		require.NotNil(t, versionModel)
    99  		assertVersion(t, versionEntity, *versionModel)
   100  	})
   101  
   102  	t.Run("success all nullable properties empty", func(t *testing.T) {
   103  		// GIVEN
   104  		versionEntity := version.Version{}
   105  		versionConv := version.NewConverter()
   106  		// WHEN
   107  		versionModel := versionConv.FromEntity(versionEntity)
   108  		// THEN
   109  		require.Nil(t, versionModel)
   110  	})
   111  }
   112  func TestConverter_ToEntity(t *testing.T) {
   113  	t.Run("success all nullable properties filled", func(t *testing.T) {
   114  		versionModel := *fixModelVersion("v1.2", true, "v1.1", false)
   115  		versionConv := version.NewConverter()
   116  		// WHEN
   117  		versionEntity := versionConv.ToEntity(versionModel)
   118  		// THEN
   119  		assertVersion(t, versionEntity, versionModel)
   120  	})
   121  
   122  	t.Run("success all nullable properties empty", func(t *testing.T) {
   123  		versionModel := model.Version{}
   124  		versionConv := version.NewConverter()
   125  		// WHEN
   126  		versionEntity := versionConv.ToEntity(versionModel)
   127  		// THEN
   128  		assertVersion(t, versionEntity, versionModel)
   129  	})
   130  }
   131  
   132  func assertVersion(t *testing.T, entity version.Version, model model.Version) {
   133  	var value *string
   134  	if model.Value != "" {
   135  		value = &model.Value
   136  	}
   137  	testdb.AssertSQLNullStringEqualTo(t, entity.Value, value)
   138  	testdb.AssertSQLNullStringEqualTo(t, entity.DeprecatedSince, model.DeprecatedSince)
   139  	testdb.AssertSQLNullBool(t, entity.Deprecated, model.Deprecated)
   140  	testdb.AssertSQLNullBool(t, entity.ForRemoval, model.ForRemoval)
   141  }